PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/EQEmuJSM/mysql-connector-java-5.1.13/src/testsuite/simple/CallableStatementTest.java

http://cubbers-eqemu-utils.googlecode.com/
Java | 584 lines | 371 code | 125 blank | 88 comment | 39 complexity | 1ad2034947b4c623a347ef14af747f91 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0
  1. /*
  2. Copyright 2002-2007 MySQL AB, 2008 Sun Microsystems
  3. All rights reserved. Use is subject to license terms.
  4. The MySQL Connector/J is licensed under the terms of the GPL,
  5. like most MySQL Connectors. There are special exceptions to the
  6. terms and conditions of the GPL as it is applied to this software,
  7. see the FLOSS License Exception available on mysql.com.
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation; version 2 of the
  11. License.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  19. 02110-1301 USA
  20. */
  21. package testsuite.simple;
  22. import java.sql.CallableStatement;
  23. import java.sql.Connection;
  24. import java.sql.ParameterMetaData;
  25. import java.sql.ResultSet;
  26. import java.sql.ResultSetMetaData;
  27. import java.sql.SQLException;
  28. import java.sql.Types;
  29. import java.util.Properties;
  30. import testsuite.BaseTestCase;
  31. import com.mysql.jdbc.SQLError;
  32. import com.mysql.jdbc.log.StandardLogger;
  33. /**
  34. * Tests callable statement functionality.
  35. *
  36. * @author Mark Matthews
  37. * @version $Id: CallableStatementTest.java,v 1.1.2.1 2005/05/13 18:58:37
  38. * mmatthews Exp $
  39. */
  40. public class CallableStatementTest extends BaseTestCase {
  41. /**
  42. * DOCUMENT ME!
  43. *
  44. * @param name
  45. */
  46. public CallableStatementTest(String name) {
  47. super(name);
  48. }
  49. /**
  50. * Tests functioning of inout parameters
  51. *
  52. * @throws Exception
  53. * if the test fails
  54. */
  55. public void testInOutParams() throws Exception {
  56. if (versionMeetsMinimum(5, 0)) {
  57. CallableStatement storedProc = null;
  58. try {
  59. this.stmt
  60. .executeUpdate("DROP PROCEDURE IF EXISTS testInOutParam");
  61. this.stmt
  62. .executeUpdate("create procedure testInOutParam(IN p1 VARCHAR(255), INOUT p2 INT)\n"
  63. + "begin\n"
  64. + " DECLARE z INT;\n"
  65. + "SET z = p2 + 1;\n"
  66. + "SET p2 = z;\n"
  67. + "SELECT p1;\n"
  68. + "SELECT CONCAT('zyxw', p1);\n"
  69. + "end\n");
  70. storedProc = this.conn.prepareCall("{call testInOutParam(?, ?)}");
  71. storedProc.setString(1, "abcd");
  72. storedProc.setInt(2, 4);
  73. storedProc.registerOutParameter(2, Types.INTEGER);
  74. storedProc.execute();
  75. assertEquals(5, storedProc.getInt(2));
  76. } finally {
  77. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testInOutParam");
  78. }
  79. }
  80. }
  81. public void testBatch() throws Exception {
  82. if (versionMeetsMinimum(5, 0)) {
  83. Connection batchedConn = null;
  84. try {
  85. createTable("testBatchTable", "(field1 INT)");
  86. createProcedure("testBatch", "(IN foo VARCHAR(15))\n"
  87. + "begin\n"
  88. + "INSERT INTO testBatchTable VALUES (foo);\n"
  89. + "end\n");
  90. executeBatchedStoredProc(this.conn);
  91. batchedConn = getConnectionWithProps("rewriteBatchedStatements=true,profileSQL=true");
  92. StringBuffer outBuf = new StringBuffer();
  93. StandardLogger.bufferedLog = outBuf;
  94. executeBatchedStoredProc(batchedConn);
  95. String[] log = outBuf.toString().split(";");
  96. assertTrue(log.length > 20);
  97. } finally {
  98. StandardLogger.bufferedLog = null;
  99. closeMemberJDBCResources();
  100. if (batchedConn != null) {
  101. batchedConn.close();
  102. }
  103. }
  104. }
  105. }
  106. private void executeBatchedStoredProc(Connection c) throws Exception {
  107. this.stmt.executeUpdate("TRUNCATE TABLE testBatchTable");
  108. CallableStatement storedProc = c.prepareCall("{call testBatch(?)}");
  109. try {
  110. int numBatches = 300;
  111. for (int i = 0; i < numBatches; i++) {
  112. storedProc.setInt(1, i + 1);
  113. storedProc.addBatch();
  114. }
  115. int[] counts = storedProc.executeBatch();
  116. assertEquals(numBatches, counts.length);
  117. for (int i = 0; i < numBatches; i++) {
  118. assertEquals(1, counts[i]);
  119. }
  120. this.rs = this.stmt.executeQuery("SELECT field1 FROM testBatchTable ORDER BY field1 ASC");
  121. for (int i = 0; i < numBatches; i++) {
  122. assertTrue(this.rs.next());
  123. assertEquals(i + 1, this.rs.getInt(1));
  124. }
  125. } finally {
  126. closeMemberJDBCResources();
  127. if (storedProc != null) {
  128. storedProc.close();
  129. }
  130. }
  131. }
  132. /**
  133. * Tests functioning of output parameters.
  134. *
  135. * @throws Exception
  136. * if the test fails.
  137. */
  138. public void testOutParams() throws Exception {
  139. if (versionMeetsMinimum(5, 0)) {
  140. CallableStatement storedProc = null;
  141. try {
  142. this.stmt
  143. .executeUpdate("DROP PROCEDURE IF EXISTS testOutParam");
  144. this.stmt
  145. .executeUpdate("CREATE PROCEDURE testOutParam(x int, out y int)\n"
  146. + "begin\n"
  147. + "declare z int;\n"
  148. + "set z = x+1, y = z;\n" + "end\n");
  149. storedProc = this.conn.prepareCall("{call testOutParam(?, ?)}");
  150. storedProc.setInt(1, 5);
  151. storedProc.registerOutParameter(2, Types.INTEGER);
  152. storedProc.execute();
  153. System.out.println(storedProc);
  154. int indexedOutParamToTest = storedProc.getInt(2);
  155. if (!isRunningOnJdk131()) {
  156. int namedOutParamToTest = storedProc.getInt("y");
  157. assertTrue("Named and indexed parameter are not the same",
  158. indexedOutParamToTest == namedOutParamToTest);
  159. assertTrue("Output value not returned correctly",
  160. indexedOutParamToTest == 6);
  161. // Start over, using named parameters, this time
  162. storedProc.clearParameters();
  163. storedProc.setInt("x", 32);
  164. storedProc.registerOutParameter("y", Types.INTEGER);
  165. storedProc.execute();
  166. indexedOutParamToTest = storedProc.getInt(2);
  167. namedOutParamToTest = storedProc.getInt("y");
  168. assertTrue("Named and indexed parameter are not the same",
  169. indexedOutParamToTest == namedOutParamToTest);
  170. assertTrue("Output value not returned correctly",
  171. indexedOutParamToTest == 33);
  172. try {
  173. storedProc.registerOutParameter("x", Types.INTEGER);
  174. assertTrue(
  175. "Should not be able to register an out parameter on a non-out parameter",
  176. true);
  177. } catch (SQLException sqlEx) {
  178. if (!SQLError.SQL_STATE_ILLEGAL_ARGUMENT.equals(sqlEx
  179. .getSQLState())) {
  180. throw sqlEx;
  181. }
  182. }
  183. try {
  184. storedProc.getInt("x");
  185. assertTrue(
  186. "Should not be able to retreive an out parameter on a non-out parameter",
  187. true);
  188. } catch (SQLException sqlEx) {
  189. if (!SQLError.SQL_STATE_COLUMN_NOT_FOUND.equals(sqlEx
  190. .getSQLState())) {
  191. throw sqlEx;
  192. }
  193. }
  194. }
  195. try {
  196. storedProc.registerOutParameter(1, Types.INTEGER);
  197. assertTrue(
  198. "Should not be able to register an out parameter on a non-out parameter",
  199. true);
  200. } catch (SQLException sqlEx) {
  201. if (!SQLError.SQL_STATE_ILLEGAL_ARGUMENT.equals(sqlEx
  202. .getSQLState())) {
  203. throw sqlEx;
  204. }
  205. }
  206. } finally {
  207. this.stmt.executeUpdate("DROP PROCEDURE testOutParam");
  208. }
  209. }
  210. }
  211. /**
  212. * Tests functioning of output parameters.
  213. *
  214. * @throws Exception
  215. * if the test fails.
  216. */
  217. public void testResultSet() throws Exception {
  218. if (versionMeetsMinimum(5, 0)) {
  219. CallableStatement storedProc = null;
  220. try {
  221. this.stmt
  222. .executeUpdate("DROP TABLE IF EXISTS testSpResultTbl1");
  223. this.stmt
  224. .executeUpdate("DROP TABLE IF EXISTS testSpResultTbl2");
  225. this.stmt
  226. .executeUpdate("CREATE TABLE testSpResultTbl1 (field1 INT)");
  227. this.stmt
  228. .executeUpdate("INSERT INTO testSpResultTbl1 VALUES (1), (2)");
  229. this.stmt
  230. .executeUpdate("CREATE TABLE testSpResultTbl2 (field2 varchar(255))");
  231. this.stmt
  232. .executeUpdate("INSERT INTO testSpResultTbl2 VALUES ('abc'), ('def')");
  233. this.stmt
  234. .executeUpdate("DROP PROCEDURE IF EXISTS testSpResult");
  235. this.stmt
  236. .executeUpdate("CREATE PROCEDURE testSpResult()\n"
  237. + "BEGIN\n"
  238. + "SELECT field2 FROM testSpResultTbl2 WHERE field2='abc';\n"
  239. + "UPDATE testSpResultTbl1 SET field1=2;\n"
  240. + "SELECT field2 FROM testSpResultTbl2 WHERE field2='def';\n"
  241. + "end\n");
  242. storedProc = this.conn.prepareCall("{call testSpResult()}");
  243. storedProc.execute();
  244. this.rs = storedProc.getResultSet();
  245. ResultSetMetaData rsmd = this.rs.getMetaData();
  246. assertTrue(rsmd.getColumnCount() == 1);
  247. assertTrue("field2".equals(rsmd.getColumnName(1)));
  248. assertTrue(rsmd.getColumnType(1) == Types.VARCHAR);
  249. assertTrue(this.rs.next());
  250. assertTrue("abc".equals(this.rs.getString(1)));
  251. // TODO: This does not yet work in MySQL 5.0
  252. // assertTrue(!storedProc.getMoreResults());
  253. // assertTrue(storedProc.getUpdateCount() == 2);
  254. assertTrue(storedProc.getMoreResults());
  255. ResultSet nextResultSet = storedProc.getResultSet();
  256. rsmd = nextResultSet.getMetaData();
  257. assertTrue(rsmd.getColumnCount() == 1);
  258. assertTrue("field2".equals(rsmd.getColumnName(1)));
  259. assertTrue(rsmd.getColumnType(1) == Types.VARCHAR);
  260. assertTrue(nextResultSet.next());
  261. assertTrue("def".equals(nextResultSet.getString(1)));
  262. nextResultSet.close();
  263. this.rs.close();
  264. storedProc.execute();
  265. } finally {
  266. this.stmt
  267. .executeUpdate("DROP PROCEDURE IF EXISTS testSpResult");
  268. this.stmt
  269. .executeUpdate("DROP TABLE IF EXISTS testSpResultTbl1");
  270. this.stmt
  271. .executeUpdate("DROP TABLE IF EXISTS testSpResultTbl2");
  272. }
  273. }
  274. }
  275. /**
  276. * Tests parsing of stored procedures
  277. *
  278. * @throws Exception
  279. * if an error occurs.
  280. */
  281. public void testSPParse() throws Exception {
  282. if (versionMeetsMinimum(5, 0)) {
  283. CallableStatement storedProc = null;
  284. try {
  285. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testSpParse");
  286. this.stmt
  287. .executeUpdate("CREATE PROCEDURE testSpParse(IN FOO VARCHAR(15))\n"
  288. + "BEGIN\n" + "SELECT 1;\n" + "end\n");
  289. storedProc = this.conn.prepareCall("{call testSpParse()}");
  290. } finally {
  291. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testSpParse");
  292. }
  293. }
  294. }
  295. /**
  296. * Tests parsing/execution of stored procedures with no parameters...
  297. *
  298. * @throws Exception
  299. * if an error occurs.
  300. */
  301. public void testSPNoParams() throws Exception {
  302. if (versionMeetsMinimum(5, 0)) {
  303. CallableStatement storedProc = null;
  304. try {
  305. this.stmt
  306. .executeUpdate("DROP PROCEDURE IF EXISTS testSPNoParams");
  307. this.stmt.executeUpdate("CREATE PROCEDURE testSPNoParams()\n"
  308. + "BEGIN\n" + "SELECT 1;\n" + "end\n");
  309. storedProc = this.conn.prepareCall("{call testSPNoParams()}");
  310. storedProc.execute();
  311. } finally {
  312. this.stmt
  313. .executeUpdate("DROP PROCEDURE IF EXISTS testSPNoParams");
  314. }
  315. }
  316. }
  317. /**
  318. * Tests parsing of stored procedures
  319. *
  320. * @throws Exception
  321. * if an error occurs.
  322. */
  323. public void testSPCache() throws Exception {
  324. if (isRunningOnJdk131()) {
  325. return; // no support for LRUCache
  326. }
  327. if (versionMeetsMinimum(5, 0)) {
  328. CallableStatement storedProc = null;
  329. try {
  330. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testSpParse");
  331. this.stmt
  332. .executeUpdate("CREATE PROCEDURE testSpParse(IN FOO VARCHAR(15))\n"
  333. + "BEGIN\n" + "SELECT 1;\n" + "end\n");
  334. int numIterations = 10;
  335. long startTime = System.currentTimeMillis();
  336. for (int i = 0; i < numIterations; i++) {
  337. storedProc = this.conn.prepareCall("{call testSpParse(?)}");
  338. storedProc.close();
  339. }
  340. long elapsedTime = System.currentTimeMillis() - startTime;
  341. System.out.println("Standard parsing/execution: " + elapsedTime
  342. + " ms");
  343. storedProc = this.conn.prepareCall("{call testSpParse(?)}");
  344. storedProc.setString(1, "abc");
  345. this.rs = storedProc.executeQuery();
  346. assertTrue(this.rs.next());
  347. assertTrue(this.rs.getInt(1) == 1);
  348. Properties props = new Properties();
  349. props.setProperty("cacheCallableStmts", "true");
  350. Connection cachedSpConn = getConnectionWithProps(props);
  351. startTime = System.currentTimeMillis();
  352. for (int i = 0; i < numIterations; i++) {
  353. storedProc = cachedSpConn
  354. .prepareCall("{call testSpParse(?)}");
  355. storedProc.close();
  356. }
  357. elapsedTime = System.currentTimeMillis() - startTime;
  358. System.out
  359. .println("Cached parse stage: " + elapsedTime + " ms");
  360. storedProc = cachedSpConn.prepareCall("{call testSpParse(?)}");
  361. storedProc.setString(1, "abc");
  362. this.rs = storedProc.executeQuery();
  363. assertTrue(this.rs.next());
  364. assertTrue(this.rs.getInt(1) == 1);
  365. } finally {
  366. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testSpParse");
  367. }
  368. }
  369. }
  370. public void testOutParamsNoBodies() throws Exception {
  371. if (versionMeetsMinimum(5, 0)) {
  372. CallableStatement storedProc = null;
  373. Properties props = new Properties();
  374. props.setProperty("noAccessToProcedureBodies", "true");
  375. Connection spConn = getConnectionWithProps(props);
  376. try {
  377. this.stmt
  378. .executeUpdate("DROP PROCEDURE IF EXISTS testOutParam");
  379. this.stmt
  380. .executeUpdate("CREATE PROCEDURE testOutParam(x int, out y int)\n"
  381. + "begin\n"
  382. + "declare z int;\n"
  383. + "set z = x+1, y = z;\n" + "end\n");
  384. storedProc = spConn.prepareCall("{call testOutParam(?, ?)}");
  385. storedProc.setInt(1, 5);
  386. storedProc.registerOutParameter(2, Types.INTEGER);
  387. storedProc.execute();
  388. int indexedOutParamToTest = storedProc.getInt(2);
  389. assertTrue("Output value not returned correctly",
  390. indexedOutParamToTest == 6);
  391. storedProc.clearParameters();
  392. storedProc.setInt(1, 32);
  393. storedProc.registerOutParameter(2, Types.INTEGER);
  394. storedProc.execute();
  395. indexedOutParamToTest = storedProc.getInt(2);
  396. assertTrue("Output value not returned correctly",
  397. indexedOutParamToTest == 33);
  398. } finally {
  399. this.stmt.executeUpdate("DROP PROCEDURE testOutParam");
  400. }
  401. }
  402. }
  403. /**
  404. * Runs all test cases in this test suite
  405. *
  406. * @param args
  407. */
  408. public static void main(String[] args) {
  409. junit.textui.TestRunner.run(CallableStatementTest.class);
  410. }
  411. /** Tests the new parameter parser that doesn't require "BEGIN" or "\n" at end
  412. * of parameter declaration
  413. * @throws Exception
  414. */
  415. public void testParameterParser() throws Exception {
  416. if (!versionMeetsMinimum(5, 0)) {
  417. return;
  418. }
  419. CallableStatement cstmt = null;
  420. try {
  421. createTable("t1",
  422. "(id char(16) not null default '', data int not null)");
  423. createTable("t2", "(s char(16), i int, d double)");
  424. createProcedure("foo42",
  425. "() insert into test.t1 values ('foo', 42);");
  426. this.conn.prepareCall("{CALL foo42()}");
  427. this.conn.prepareCall("{CALL foo42}");
  428. createProcedure("bar",
  429. "(x char(16), y int, z DECIMAL(10)) insert into test.t1 values (x, y);");
  430. cstmt = this.conn.prepareCall("{CALL bar(?, ?, ?)}");
  431. if (!isRunningOnJdk131()) {
  432. ParameterMetaData md = cstmt.getParameterMetaData();
  433. assertEquals(3, md.getParameterCount());
  434. assertEquals(Types.CHAR, md.getParameterType(1));
  435. assertEquals(Types.INTEGER, md.getParameterType(2));
  436. assertEquals(Types.DECIMAL, md.getParameterType(3));
  437. }
  438. createProcedure("p", "() label1: WHILE @a=0 DO SET @a=1; END WHILE");
  439. this.conn.prepareCall("{CALL p()}");
  440. createFunction("f", "() RETURNS INT NO SQL return 1; ");
  441. cstmt = this.conn.prepareCall("{? = CALL f()}");
  442. if (!isRunningOnJdk131()) {
  443. ParameterMetaData md = cstmt.getParameterMetaData();
  444. assertEquals(Types.INTEGER, md.getParameterType(1));
  445. }
  446. } finally {
  447. if (cstmt != null) {
  448. cstmt.close();
  449. }
  450. }
  451. }
  452. }