PageRenderTime 32ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 1ms

/EQEmuJSM/mysql-connector-java-5.1.13/src/testsuite/regression/CallableStatementRegressionTest.java

http://cubbers-eqemu-utils.googlecode.com/
Java | 1778 lines | 1210 code | 275 blank | 293 comment | 125 complexity | 5b3326aca423f5b19d77b1ef88e754d0 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0

Large files files are truncated, but you can click here to view the full file

  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.regression;
  22. import java.io.ByteArrayInputStream;
  23. import java.io.ByteArrayOutputStream;
  24. import java.io.InputStream;
  25. import java.lang.reflect.InvocationTargetException;
  26. import java.lang.reflect.Method;
  27. import java.sql.CallableStatement;
  28. import java.sql.Connection;
  29. import java.sql.PreparedStatement;
  30. import java.sql.ResultSetMetaData;
  31. import java.sql.SQLException;
  32. import java.sql.Types;
  33. import java.util.Properties;
  34. import testsuite.BaseTestCase;
  35. import com.mysql.jdbc.DatabaseMetaData;
  36. import com.mysql.jdbc.NonRegisteringDriver;
  37. import com.mysql.jdbc.SQLError;
  38. /**
  39. * Tests fixes for bugs in CallableStatement code.
  40. *
  41. * @version $Id: CallableStatementRegressionTest.java,v 1.1.2.6 2004/12/09
  42. * 15:57:26 mmatthew Exp $
  43. */
  44. public class CallableStatementRegressionTest extends BaseTestCase {
  45. public CallableStatementRegressionTest(String name) {
  46. super(name);
  47. }
  48. /**
  49. * Runs all test cases in this test suite
  50. *
  51. * @param args
  52. * ignored
  53. */
  54. public static void main(String[] args) {
  55. junit.textui.TestRunner.run(CallableStatementRegressionTest.class);
  56. }
  57. /**
  58. * Tests fix for BUG#3539 getProcedures() does not return any procedures in
  59. * result set
  60. *
  61. * @throws Exception
  62. * if an error occurs.
  63. */
  64. public void testBug3539() throws Exception {
  65. if (!serverSupportsStoredProcedures()) {
  66. return;
  67. }
  68. try {
  69. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testBug3539");
  70. this.stmt.executeUpdate("CREATE PROCEDURE testBug3539()\n"
  71. + "BEGIN\n" + "SELECT 1;" + "end\n");
  72. this.rs = this.conn.getMetaData().getProcedures(null, null,
  73. "testBug3539");
  74. assertTrue(this.rs.next());
  75. assertTrue("testBug3539".equals(this.rs.getString(3)));
  76. } finally {
  77. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testBug3539");
  78. }
  79. }
  80. /**
  81. * Tests fix for BUG#3540 getProcedureColumns doesn't work with wildcards
  82. * for procedure name
  83. *
  84. * @throws Exception
  85. * if an error occurs.
  86. */
  87. public void testBug3540() throws Exception {
  88. if (!serverSupportsStoredProcedures()) {
  89. return;
  90. }
  91. try {
  92. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testBug3540");
  93. this.stmt
  94. .executeUpdate("CREATE PROCEDURE testBug3540(x int, out y int)\n"
  95. + "BEGIN\n" + "SELECT 1;" + "end\n");
  96. this.rs = this.conn.getMetaData().getProcedureColumns(null, null,
  97. "testBug3540%", "%");
  98. assertTrue(this.rs.next());
  99. assertTrue("testBug3540".equals(this.rs.getString(3)));
  100. assertTrue("x".equals(this.rs.getString(4)));
  101. assertTrue(this.rs.next());
  102. assertTrue("testBug3540".equals(this.rs.getString(3)));
  103. assertTrue("y".equals(this.rs.getString(4)));
  104. assertTrue(!this.rs.next());
  105. } finally {
  106. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testBug3540");
  107. }
  108. }
  109. /**
  110. * Tests fix for BUG#7026 - DBMD.getProcedures() doesn't respect catalog
  111. * parameter
  112. *
  113. * @throws Exception
  114. * if the test fails.
  115. */
  116. public void testBug7026() throws Exception {
  117. if (!serverSupportsStoredProcedures()) {
  118. return;
  119. }
  120. try {
  121. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testBug7026");
  122. this.stmt
  123. .executeUpdate("CREATE PROCEDURE testBug7026(x int, out y int)\n"
  124. + "BEGIN\n" + "SELECT 1;" + "end\n");
  125. //
  126. // Should be found this time.
  127. //
  128. this.rs = this.conn.getMetaData().getProcedures(
  129. this.conn.getCatalog(), null, "testBug7026");
  130. assertTrue(this.rs.next());
  131. assertTrue("testBug7026".equals(this.rs.getString(3)));
  132. assertTrue(!this.rs.next());
  133. //
  134. // This time, shouldn't be found, because not associated with
  135. // this (bogus) catalog
  136. //
  137. this.rs = this.conn.getMetaData().getProcedures("abfgerfg", null,
  138. "testBug7026");
  139. assertTrue(!this.rs.next());
  140. //
  141. // Should be found this time as well, as we haven't
  142. // specified a catalog.
  143. //
  144. this.rs = this.conn.getMetaData().getProcedures(null, null,
  145. "testBug7026");
  146. assertTrue(this.rs.next());
  147. assertTrue("testBug7026".equals(this.rs.getString(3)));
  148. assertTrue(!this.rs.next());
  149. } finally {
  150. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testBug7026");
  151. }
  152. }
  153. /**
  154. * Tests fix for BUG#9319 -- Stored procedures with same name in different
  155. * databases confuse the driver when it tries to determine parameter
  156. * counts/types.
  157. *
  158. * @throws Exception
  159. * if the test fails
  160. */
  161. public void testBug9319() throws Exception {
  162. if (!serverSupportsStoredProcedures()) {
  163. return;
  164. }
  165. boolean doASelect = true; // SELECT currently causes the server to
  166. // hang on the
  167. // last execution of this testcase, filed as BUG#9405
  168. if (isAdminConnectionConfigured()) {
  169. Connection db2Connection = null;
  170. Connection db1Connection = null;
  171. try {
  172. db2Connection = getAdminConnection();
  173. db1Connection = getAdminConnection();
  174. db2Connection.createStatement().executeUpdate(
  175. "CREATE DATABASE IF NOT EXISTS db_9319_2");
  176. db2Connection.setCatalog("db_9319_2");
  177. db2Connection.createStatement().executeUpdate(
  178. "DROP PROCEDURE IF EXISTS COMPROVAR_USUARI");
  179. db2Connection.createStatement().executeUpdate(
  180. "CREATE PROCEDURE COMPROVAR_USUARI(IN p_CodiUsuari VARCHAR(10),"
  181. + "\nIN p_contrasenya VARCHAR(10),"
  182. + "\nOUT p_userId INTEGER,"
  183. + "\nOUT p_userName VARCHAR(30),"
  184. + "\nOUT p_administrador VARCHAR(1),"
  185. + "\nOUT p_idioma VARCHAR(2))"
  186. + "\nBEGIN"
  187. + (doASelect ? "\nselect 2;"
  188. : "\nSELECT 2 INTO p_administrador;")
  189. + "\nEND");
  190. db1Connection.createStatement().executeUpdate(
  191. "CREATE DATABASE IF NOT EXISTS db_9319_1");
  192. db1Connection.setCatalog("db_9319_1");
  193. db1Connection.createStatement().executeUpdate(
  194. "DROP PROCEDURE IF EXISTS COMPROVAR_USUARI");
  195. db1Connection.createStatement().executeUpdate(
  196. "CREATE PROCEDURE COMPROVAR_USUARI(IN p_CodiUsuari VARCHAR(10),"
  197. + "\nIN p_contrasenya VARCHAR(10),"
  198. + "\nOUT p_userId INTEGER,"
  199. + "\nOUT p_userName VARCHAR(30),"
  200. + "\nOUT p_administrador VARCHAR(1))"
  201. + "\nBEGIN"
  202. + (doASelect ? "\nselect 1;"
  203. : "\nSELECT 1 INTO p_administrador;")
  204. + "\nEND");
  205. CallableStatement cstmt = db2Connection
  206. .prepareCall("{ call COMPROVAR_USUARI(?, ?, ?, ?, ?, ?) }");
  207. cstmt.setString(1, "abc");
  208. cstmt.setString(2, "def");
  209. cstmt.registerOutParameter(3, java.sql.Types.INTEGER);
  210. cstmt.registerOutParameter(4, java.sql.Types.VARCHAR);
  211. cstmt.registerOutParameter(5, java.sql.Types.VARCHAR);
  212. cstmt.registerOutParameter(6, java.sql.Types.VARCHAR);
  213. cstmt.execute();
  214. if (doASelect) {
  215. this.rs = cstmt.getResultSet();
  216. assertTrue(this.rs.next());
  217. assertEquals(2, this.rs.getInt(1));
  218. } else {
  219. assertEquals(2, cstmt.getInt(5));
  220. }
  221. cstmt = db1Connection
  222. .prepareCall("{ call COMPROVAR_USUARI(?, ?, ?, ?, ?, ?) }");
  223. cstmt.setString(1, "abc");
  224. cstmt.setString(2, "def");
  225. cstmt.registerOutParameter(3, java.sql.Types.INTEGER);
  226. cstmt.registerOutParameter(4, java.sql.Types.VARCHAR);
  227. cstmt.registerOutParameter(5, java.sql.Types.VARCHAR);
  228. try {
  229. cstmt.registerOutParameter(6, java.sql.Types.VARCHAR);
  230. fail("Should've thrown an exception");
  231. } catch (SQLException sqlEx) {
  232. assertEquals(SQLError.SQL_STATE_ILLEGAL_ARGUMENT, sqlEx
  233. .getSQLState());
  234. }
  235. cstmt = db1Connection
  236. .prepareCall("{ call COMPROVAR_USUARI(?, ?, ?, ?, ?) }");
  237. cstmt.setString(1, "abc");
  238. cstmt.setString(2, "def");
  239. cstmt.registerOutParameter(3, java.sql.Types.INTEGER);
  240. cstmt.registerOutParameter(4, java.sql.Types.VARCHAR);
  241. cstmt.registerOutParameter(5, java.sql.Types.VARCHAR);
  242. cstmt.execute();
  243. if (doASelect) {
  244. this.rs = cstmt.getResultSet();
  245. assertTrue(this.rs.next());
  246. assertEquals(1, this.rs.getInt(1));
  247. } else {
  248. assertEquals(1, cstmt.getInt(5));
  249. }
  250. String quoteChar = db2Connection.getMetaData()
  251. .getIdentifierQuoteString();
  252. cstmt = db2Connection.prepareCall("{ call " + quoteChar
  253. + db1Connection.getCatalog() + quoteChar + "."
  254. + quoteChar + "COMPROVAR_USUARI" + quoteChar
  255. + "(?, ?, ?, ?, ?) }");
  256. cstmt.setString(1, "abc");
  257. cstmt.setString(2, "def");
  258. cstmt.registerOutParameter(3, java.sql.Types.INTEGER);
  259. cstmt.registerOutParameter(4, java.sql.Types.VARCHAR);
  260. cstmt.registerOutParameter(5, java.sql.Types.VARCHAR);
  261. cstmt.execute();
  262. if (doASelect) {
  263. this.rs = cstmt.getResultSet();
  264. assertTrue(this.rs.next());
  265. assertEquals(1, this.rs.getInt(1));
  266. } else {
  267. assertEquals(1, cstmt.getInt(5));
  268. }
  269. } finally {
  270. if (db2Connection != null) {
  271. db2Connection.createStatement().executeUpdate(
  272. "DROP PROCEDURE IF EXISTS COMPROVAR_USUARI");
  273. db2Connection.createStatement().executeUpdate(
  274. "DROP DATABASE IF EXISTS db_9319_2");
  275. }
  276. if (db1Connection != null) {
  277. db1Connection.createStatement().executeUpdate(
  278. "DROP PROCEDURE IF EXISTS COMPROVAR_USUARI");
  279. db1Connection.createStatement().executeUpdate(
  280. "DROP DATABASE IF EXISTS db_9319_1");
  281. }
  282. }
  283. }
  284. }
  285. /*
  286. * public void testBug9319() throws Exception { boolean doASelect = false; //
  287. * SELECT currently causes the server to hang on the // last execution of
  288. * this testcase, filed as BUG#9405
  289. *
  290. * if (versionMeetsMinimum(5, 0, 2)) { if (isAdminConnectionConfigured()) {
  291. * Connection db2Connection = null; Connection db1Connection = null;
  292. *
  293. * try { db2Connection = getAdminConnection();
  294. *
  295. * db2Connection.createStatement().executeUpdate( "CREATE DATABASE IF NOT
  296. * EXISTS db_9319"); db2Connection.setCatalog("db_9319");
  297. *
  298. * db2Connection.createStatement().executeUpdate( "DROP PROCEDURE IF EXISTS
  299. * COMPROVAR_USUARI");
  300. *
  301. * db2Connection.createStatement().executeUpdate( "CREATE PROCEDURE
  302. * COMPROVAR_USUARI(IN p_CodiUsuari VARCHAR(10)," + "\nIN p_contrasenya
  303. * VARCHAR(10)," + "\nOUT p_userId INTEGER," + "\nOUT p_userName
  304. * VARCHAR(30)," + "\nOUT p_administrador VARCHAR(1)," + "\nOUT p_idioma
  305. * VARCHAR(2))" + "\nBEGIN" + (doASelect ? "\nselect 2;" : "\nSELECT 2 INTO
  306. * p_administrador;" ) + "\nEND");
  307. *
  308. * this.stmt .executeUpdate("DROP PROCEDURE IF EXISTS COMPROVAR_USUARI");
  309. * this.stmt .executeUpdate("CREATE PROCEDURE COMPROVAR_USUARI(IN
  310. * p_CodiUsuari VARCHAR(10)," + "\nIN p_contrasenya VARCHAR(10)," + "\nOUT
  311. * p_userId INTEGER," + "\nOUT p_userName VARCHAR(30)," + "\nOUT
  312. * p_administrador VARCHAR(1))" + "\nBEGIN" + (doASelect ? "\nselect 1;" :
  313. * "\nSELECT 1 INTO p_administrador;" ) + "\nEND");
  314. *
  315. * CallableStatement cstmt = db2Connection .prepareCall("{ call
  316. * COMPROVAR_USUARI(?, ?, ?, ?, ?, ?) }"); cstmt.setString(1, "abc");
  317. * cstmt.setString(2, "def"); cstmt.registerOutParameter(3,
  318. * java.sql.Types.INTEGER); cstmt.registerOutParameter(4,
  319. * java.sql.Types.VARCHAR); cstmt.registerOutParameter(5,
  320. * java.sql.Types.VARCHAR);
  321. *
  322. * cstmt.registerOutParameter(6, java.sql.Types.VARCHAR);
  323. *
  324. * cstmt.execute();
  325. *
  326. * if (doASelect) { this.rs = cstmt.getResultSet();
  327. * assertTrue(this.rs.next()); assertEquals(2, this.rs.getInt(1)); } else {
  328. * assertEquals(2, cstmt.getInt(5)); }
  329. *
  330. * cstmt = this.conn .prepareCall("{ call COMPROVAR_USUARI(?, ?, ?, ?, ?, ?)
  331. * }"); cstmt.setString(1, "abc"); cstmt.setString(2, "def");
  332. * cstmt.registerOutParameter(3, java.sql.Types.INTEGER);
  333. * cstmt.registerOutParameter(4, java.sql.Types.VARCHAR);
  334. * cstmt.registerOutParameter(5, java.sql.Types.VARCHAR);
  335. *
  336. * try { cstmt.registerOutParameter(6, java.sql.Types.VARCHAR);
  337. * fail("Should've thrown an exception"); } catch (SQLException sqlEx) {
  338. * assertEquals(SQLError.SQL_STATE_ILLEGAL_ARGUMENT, sqlEx .getSQLState()); }
  339. *
  340. * cstmt = this.conn .prepareCall("{ call COMPROVAR_USUARI(?, ?, ?, ?, ?)
  341. * }"); cstmt.setString(1, "abc"); cstmt.setString(2, "def");
  342. * cstmt.registerOutParameter(3, java.sql.Types.INTEGER);
  343. * cstmt.registerOutParameter(4, java.sql.Types.VARCHAR);
  344. * cstmt.registerOutParameter(5, java.sql.Types.VARCHAR);
  345. *
  346. * cstmt.execute();
  347. *
  348. * if (doASelect) { this.rs = cstmt.getResultSet();
  349. * assertTrue(this.rs.next()); assertEquals(1, this.rs.getInt(1)); } else {
  350. * assertEquals(1, cstmt.getInt(5)); }
  351. *
  352. * String quoteChar =
  353. * db2Connection.getMetaData().getIdentifierQuoteString();
  354. *
  355. * cstmt = db2Connection .prepareCall("{ call " + quoteChar +
  356. * this.conn.getCatalog() + quoteChar + "." + quoteChar + "COMPROVAR_USUARI" +
  357. * quoteChar + "(?, ?, ?, ?, ?) }"); cstmt.setString(1, "abc");
  358. * cstmt.setString(2, "def"); cstmt.registerOutParameter(3,
  359. * java.sql.Types.INTEGER); cstmt.registerOutParameter(4,
  360. * java.sql.Types.VARCHAR); cstmt.registerOutParameter(5,
  361. * java.sql.Types.VARCHAR);
  362. *
  363. * cstmt.execute();
  364. *
  365. * if (doASelect) { this.rs = cstmt.getResultSet();
  366. * assertTrue(this.rs.next()); assertEquals(1, this.rs.getInt(1)); } else {
  367. * assertEquals(1, cstmt.getInt(5)); } } finally { if (db2Connection !=
  368. * null) { db2Connection.createStatement().executeUpdate( "DROP PROCEDURE IF
  369. * EXISTS COMPROVAR_USUARI"); //
  370. * db2Connection.createStatement().executeUpdate( // "DROP DATABASE IF
  371. * EXISTS db_9319"); }
  372. *
  373. * this.stmt .executeUpdate("DROP PROCEDURE IF EXISTS COMPROVAR_USUARI"); } } } }
  374. */
  375. /**
  376. * Tests fix for BUG#9682 - Stored procedures with DECIMAL parameters with
  377. * storage specifications that contained "," in them would fail.
  378. *
  379. * @throws Exception
  380. * if the test fails.
  381. */
  382. public void testBug9682() throws Exception {
  383. if (!serverSupportsStoredProcedures()) {
  384. return;
  385. }
  386. CallableStatement cStmt = null;
  387. try {
  388. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testBug9682");
  389. this.stmt
  390. .executeUpdate("CREATE PROCEDURE testBug9682(decimalParam DECIMAL(18,0))"
  391. + "\nBEGIN" + "\n SELECT 1;" + "\nEND");
  392. cStmt = this.conn.prepareCall("Call testBug9682(?)");
  393. cStmt.setDouble(1, 18.0);
  394. cStmt.execute();
  395. } finally {
  396. if (cStmt != null) {
  397. cStmt.close();
  398. }
  399. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testBug9682");
  400. }
  401. }
  402. /**
  403. * Tests fix forBUG#10310 - Driver doesn't support {?=CALL(...)} for calling
  404. * stored functions. This involved adding support for function retrieval to
  405. * DatabaseMetaData.getProcedures() and getProcedureColumns() as well.
  406. *
  407. * @throws Exception
  408. * if the test fails.
  409. */
  410. public void testBug10310() throws Exception {
  411. if (!serverSupportsStoredProcedures()) {
  412. return;
  413. }
  414. CallableStatement cStmt = null;
  415. try {
  416. this.stmt.executeUpdate("DROP FUNCTION IF EXISTS testBug10310");
  417. this.stmt
  418. .executeUpdate("CREATE FUNCTION testBug10310(a float, b bigint, c int) RETURNS INT NO SQL"
  419. + "\nBEGIN" + "\nRETURN a;" + "\nEND");
  420. cStmt = this.conn.prepareCall("{? = CALL testBug10310(?,?,?)}");
  421. cStmt.registerOutParameter(1, Types.INTEGER);
  422. cStmt.setFloat(2, 2);
  423. cStmt.setInt(3, 1);
  424. cStmt.setInt(4, 1);
  425. if (!isRunningOnJdk131()) {
  426. assertEquals(4, cStmt.getParameterMetaData()
  427. .getParameterCount());
  428. assertEquals(Types.INTEGER, cStmt.getParameterMetaData()
  429. .getParameterType(1));
  430. }
  431. java.sql.DatabaseMetaData dbmd = this.conn.getMetaData();
  432. this.rs = ((com.mysql.jdbc.DatabaseMetaData)dbmd).getFunctionColumns(this.conn.getCatalog(), null, "testBug10310", "%");
  433. ResultSetMetaData rsmd = this.rs.getMetaData();
  434. assertEquals(17, rsmd.getColumnCount());
  435. assertEquals("FUNCTION_CAT", rsmd.getColumnName(1));
  436. assertEquals("FUNCTION_SCHEM", rsmd.getColumnName(2));
  437. assertEquals("FUNCTION_NAME", rsmd.getColumnName(3));
  438. assertEquals("COLUMN_NAME", rsmd.getColumnName(4));
  439. assertEquals("COLUMN_TYPE", rsmd.getColumnName(5));
  440. assertEquals("DATA_TYPE", rsmd.getColumnName(6));
  441. assertEquals("TYPE_NAME", rsmd.getColumnName(7));
  442. assertEquals("PRECISION", rsmd.getColumnName(8));
  443. assertEquals("LENGTH", rsmd.getColumnName(9));
  444. assertEquals("SCALE", rsmd.getColumnName(10));
  445. assertEquals("RADIX", rsmd.getColumnName(11));
  446. assertEquals("NULLABLE", rsmd.getColumnName(12));
  447. assertEquals("REMARKS", rsmd.getColumnName(13));
  448. assertEquals("CHAR_OCTET_LENGTH", rsmd.getColumnName(14));
  449. assertEquals("ORDINAL_POSITION", rsmd.getColumnName(15));
  450. assertEquals("IS_NULLABLE", rsmd.getColumnName(16));
  451. assertEquals("SPECIFIC_NAME", rsmd.getColumnName(17));
  452. this.rs.close();
  453. assertFalse(cStmt.execute());
  454. assertEquals(2f, cStmt.getInt(1), .001);
  455. assertEquals("java.lang.Integer", cStmt.getObject(1).getClass()
  456. .getName());
  457. assertEquals(-1, cStmt.executeUpdate());
  458. assertEquals(2f, cStmt.getInt(1), .001);
  459. assertEquals("java.lang.Integer", cStmt.getObject(1).getClass()
  460. .getName());
  461. if (!isRunningOnJdk131()) {
  462. cStmt.setFloat("a", 4);
  463. cStmt.setInt("b", 1);
  464. cStmt.setInt("c", 1);
  465. assertFalse(cStmt.execute());
  466. assertEquals(4f, cStmt.getInt(1), .001);
  467. assertEquals("java.lang.Integer", cStmt.getObject(1).getClass()
  468. .getName());
  469. assertEquals(-1, cStmt.executeUpdate());
  470. assertEquals(4f, cStmt.getInt(1), .001);
  471. assertEquals("java.lang.Integer", cStmt.getObject(1).getClass()
  472. .getName());
  473. }
  474. // Check metadata while we're at it
  475. this.rs = dbmd.getProcedures(this.conn.getCatalog(), null,
  476. "testBug10310");
  477. this.rs.next();
  478. assertEquals("testBug10310", this.rs.getString("PROCEDURE_NAME"));
  479. assertEquals(DatabaseMetaData.procedureReturnsResult, this.rs
  480. .getShort("PROCEDURE_TYPE"));
  481. cStmt.setNull(2, Types.FLOAT);
  482. cStmt.setInt(3, 1);
  483. cStmt.setInt(4, 1);
  484. assertFalse(cStmt.execute());
  485. assertEquals(0f, cStmt.getInt(1), .001);
  486. assertEquals(true, cStmt.wasNull());
  487. assertEquals(null, cStmt.getObject(1));
  488. assertEquals(true, cStmt.wasNull());
  489. assertEquals(-1, cStmt.executeUpdate());
  490. assertEquals(0f, cStmt.getInt(1), .001);
  491. assertEquals(true, cStmt.wasNull());
  492. assertEquals(null, cStmt.getObject(1));
  493. assertEquals(true, cStmt.wasNull());
  494. // Check with literals, not all parameters filled!
  495. cStmt = this.conn.prepareCall("{? = CALL testBug10310(4,5,?)}");
  496. cStmt.registerOutParameter(1, Types.INTEGER);
  497. cStmt.setInt(2, 1);
  498. assertFalse(cStmt.execute());
  499. assertEquals(4f, cStmt.getInt(1), .001);
  500. assertEquals("java.lang.Integer", cStmt.getObject(1).getClass()
  501. .getName());
  502. assertEquals(-1, cStmt.executeUpdate());
  503. assertEquals(4f, cStmt.getInt(1), .001);
  504. assertEquals("java.lang.Integer", cStmt.getObject(1).getClass()
  505. .getName());
  506. if (!isRunningOnJdk131()) {
  507. assertEquals(2, cStmt.getParameterMetaData()
  508. .getParameterCount());
  509. assertEquals(Types.INTEGER, cStmt.getParameterMetaData()
  510. .getParameterType(1));
  511. assertEquals(Types.INTEGER, cStmt.getParameterMetaData()
  512. .getParameterType(2));
  513. }
  514. } finally {
  515. if (this.rs != null) {
  516. this.rs.close();
  517. this.rs = null;
  518. }
  519. if (cStmt != null) {
  520. cStmt.close();
  521. }
  522. this.stmt.executeUpdate("DROP FUNCTION IF EXISTS testBug10310");
  523. }
  524. }
  525. /**
  526. * Tests fix for Bug#12417 - stored procedure catalog name is case-sensitive
  527. * on Windows (this is actually a server bug, but we have a workaround in
  528. * place for it now).
  529. *
  530. * @throws Exception
  531. * if the test fails.
  532. */
  533. public void testBug12417() throws Exception {
  534. if (serverSupportsStoredProcedures() && isServerRunningOnWindows()) {
  535. Connection ucCatalogConn = null;
  536. try {
  537. this.stmt
  538. .executeUpdate("DROP PROCEDURE IF EXISTS testBug12417");
  539. this.stmt.executeUpdate("CREATE PROCEDURE testBug12417()\n"
  540. + "BEGIN\n" + "SELECT 1;" + "end\n");
  541. ucCatalogConn = getConnectionWithProps((Properties) null);
  542. ucCatalogConn.setCatalog(this.conn.getCatalog().toUpperCase());
  543. ucCatalogConn.prepareCall("{call testBug12417()}");
  544. } finally {
  545. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testBug3539");
  546. if (ucCatalogConn != null) {
  547. ucCatalogConn.close();
  548. }
  549. }
  550. }
  551. }
  552. public void testBug15121() throws Exception {
  553. if (false /* needs to be fixed on server */) {
  554. if (versionMeetsMinimum(5, 0)) {
  555. this.stmt
  556. .executeUpdate("DROP PROCEDURE IF EXISTS p_testBug15121");
  557. this.stmt.executeUpdate("CREATE PROCEDURE p_testBug15121()\n"
  558. + "BEGIN\n" + "SELECT * from idonotexist;\n" + "END");
  559. Properties props = new Properties();
  560. props.setProperty(NonRegisteringDriver.DBNAME_PROPERTY_KEY, "");
  561. Connection noDbConn = null;
  562. try {
  563. noDbConn = getConnectionWithProps(props);
  564. StringBuffer queryBuf = new StringBuffer("{call ");
  565. String quotedId = this.conn.getMetaData()
  566. .getIdentifierQuoteString();
  567. queryBuf.append(quotedId);
  568. queryBuf.append(this.conn.getCatalog());
  569. queryBuf.append(quotedId);
  570. queryBuf.append(".p_testBug15121()}");
  571. noDbConn.prepareCall(queryBuf.toString()).execute();
  572. } finally {
  573. if (noDbConn != null) {
  574. noDbConn.close();
  575. }
  576. }
  577. }
  578. }
  579. }
  580. /**
  581. * Tests fix for BUG#15464 - INOUT parameter does not store IN value.
  582. *
  583. * @throws Exception
  584. * if the test fails
  585. */
  586. public void testBug15464() throws Exception {
  587. if (!serverSupportsStoredProcedures()) {
  588. return;
  589. }
  590. CallableStatement storedProc = null;
  591. try {
  592. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testInOutParam");
  593. this.stmt
  594. .executeUpdate("create procedure testInOutParam(IN p1 VARCHAR(255), INOUT p2 INT)\n"
  595. + "begin\n"
  596. + " DECLARE z INT;\n"
  597. + "SET z = p2 + 1;\n"
  598. + "SET p2 = z;\n"
  599. + "SELECT p1;\n"
  600. + "SELECT CONCAT('zyxw', p1);\n"
  601. + "end\n");
  602. storedProc = this.conn.prepareCall("{call testInOutParam(?, ?)}");
  603. storedProc.setString(1, "abcd");
  604. storedProc.setInt(2, 4);
  605. storedProc.registerOutParameter(2, Types.INTEGER);
  606. storedProc.execute();
  607. assertEquals(5, storedProc.getInt(2));
  608. } finally {
  609. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testInOutParam");
  610. }
  611. }
  612. /**
  613. * Tests fix for BUG#17898 - registerOutParameter not working when some
  614. * parameters pre-populated. Still waiting for feedback from JDBC experts
  615. * group to determine what correct parameter count from getMetaData() should
  616. * be, however.
  617. *
  618. * @throws Exception
  619. * if the test fails
  620. */
  621. public void testBug17898() throws Exception {
  622. if (!serverSupportsStoredProcedures()) {
  623. return;
  624. }
  625. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testBug17898");
  626. this.stmt
  627. .executeUpdate("CREATE PROCEDURE testBug17898(param1 VARCHAR(50), OUT param2 INT)\nBEGIN\nDECLARE rtn INT;\nSELECT 1 INTO rtn;\nSET param2=rtn;\nEND");
  628. CallableStatement cstmt = this.conn
  629. .prepareCall("{CALL testBug17898('foo', ?)}");
  630. cstmt.registerOutParameter(1, Types.INTEGER);
  631. cstmt.execute();
  632. assertEquals(1, cstmt.getInt(1));
  633. if (!isRunningOnJdk131()) {
  634. cstmt.clearParameters();
  635. cstmt.registerOutParameter("param2", Types.INTEGER);
  636. cstmt.execute();
  637. assertEquals(1, cstmt.getInt(1));
  638. }
  639. }
  640. /**
  641. * Tests fix for BUG#21462 - JDBC (and ODBC) specifications allow
  642. * no-parenthesis CALL statements for procedures with no arguments, MySQL
  643. * server does not.
  644. *
  645. * @throws Exception
  646. * if the test fails.
  647. */
  648. public void testBug21462() throws Exception {
  649. if (!serverSupportsStoredProcedures()) {
  650. return;
  651. }
  652. CallableStatement cstmt = null;
  653. try {
  654. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testBug21462");
  655. this.stmt
  656. .executeUpdate("CREATE PROCEDURE testBug21462() BEGIN SELECT 1; END");
  657. cstmt = this.conn.prepareCall("{CALL testBug21462}");
  658. cstmt.execute();
  659. } finally {
  660. if (cstmt != null) {
  661. cstmt.close();
  662. }
  663. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testBug21462");
  664. }
  665. }
  666. /**
  667. * Tests fix for BUG#22024 - Newlines causing whitespace to span confuse
  668. * procedure parser when getting parameter metadata for stored procedures.
  669. *
  670. * @throws Exception
  671. * if the test fails
  672. */
  673. public void testBug22024() throws Exception {
  674. if (!serverSupportsStoredProcedures()) {
  675. return;
  676. }
  677. CallableStatement cstmt = null;
  678. try {
  679. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testBug22024");
  680. this.stmt
  681. .executeUpdate("CREATE PROCEDURE testBug22024(\r\n)\r\n BEGIN SELECT 1; END");
  682. cstmt = this.conn.prepareCall("{CALL testBug22024()}");
  683. cstmt.execute();
  684. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testBug22024");
  685. this.stmt
  686. .executeUpdate("CREATE PROCEDURE testBug22024(\r\na INT)\r\n BEGIN SELECT 1; END");
  687. cstmt = this.conn.prepareCall("{CALL testBug22024(?)}");
  688. cstmt.setInt(1, 1);
  689. cstmt.execute();
  690. } finally {
  691. if (cstmt != null) {
  692. cstmt.close();
  693. }
  694. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testBug22024");
  695. }
  696. }
  697. /**
  698. * Tests workaround for server crash when calling stored procedures via a
  699. * server-side prepared statement (driver now detects prepare(stored
  700. * procedure) and substitutes client-side prepared statement).
  701. *
  702. * @throws Exception
  703. * if the test fails
  704. */
  705. public void testBug22297() throws Exception {
  706. if (!serverSupportsStoredProcedures()) {
  707. return;
  708. }
  709. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testBug22297");
  710. createTable("tblTestBug2297_1", "("
  711. + "id varchar(20) NOT NULL default '',"
  712. + "Income double(19,2) default NULL)");
  713. createTable("tblTestBug2297_2", "("
  714. + "id varchar(20) NOT NULL default '',"
  715. + "CreatedOn datetime default NULL)");
  716. this.stmt
  717. .executeUpdate("CREATE PROCEDURE testBug22297(pcaseid INT)"
  718. + "BEGIN"
  719. + "\nSET @sql = \"DROP TEMPORARY TABLE IF EXISTS tmpOrders\";"
  720. + " PREPARE stmt FROM @sql;"
  721. + " EXECUTE stmt;"
  722. + " DEALLOCATE PREPARE stmt;"
  723. + "\nSET @sql = \"CREATE TEMPORARY TABLE tmpOrders SELECT id, 100 AS Income FROM tblTestBug2297_1 GROUP BY id\";"
  724. + " PREPARE stmt FROM @sql;"
  725. + " EXECUTE stmt;"
  726. + " DEALLOCATE PREPARE stmt;"
  727. + "\n SELECT id, Income FROM (SELECT e.id AS id ,COALESCE(prof.Income,0) AS Income"
  728. + "\n FROM tblTestBug2297_2 e LEFT JOIN tmpOrders prof ON e.id = prof.id"
  729. + "\n WHERE e.CreatedOn > '2006-08-01') AS Final ORDER BY id;"
  730. + "\nEND");
  731. this.stmt
  732. .executeUpdate("INSERT INTO tblTestBug2297_1 (`id`,`Income`) VALUES "
  733. + "('a',4094.00),"
  734. + "('b',500.00),"
  735. + "('c',3462.17),"
  736. + " ('d',500.00)," + " ('e',600.00)");
  737. this.stmt
  738. .executeUpdate("INSERT INTO tblTestBug2297_2 (`id`,`CreatedOn`) VALUES "
  739. + "('d','2006-08-31 00:00:00'),"
  740. + "('e','2006-08-31 00:00:00'),"
  741. + "('b','2006-08-31 00:00:00'),"
  742. + "('c','2006-08-31 00:00:00'),"
  743. + "('a','2006-08-31 00:00:00')");
  744. try {
  745. this.pstmt = this.conn.prepareStatement("{CALL testBug22297(?)}");
  746. this.pstmt.setInt(1, 1);
  747. this.rs = this.pstmt.executeQuery();
  748. String[] ids = new String[] { "a", "b", "c", "d", "e" };
  749. int pos = 0;
  750. while (this.rs.next()) {
  751. assertEquals(ids[pos++], rs.getString(1));
  752. assertEquals(100, rs.getInt(2));
  753. }
  754. assertTrue(this.pstmt.getClass().getName().indexOf("Server") == -1);
  755. } finally {
  756. closeMemberJDBCResources();
  757. }
  758. }
  759. public void testHugeNumberOfParameters() throws Exception {
  760. if (!serverSupportsStoredProcedures()) {
  761. return;
  762. }
  763. this.stmt
  764. .executeUpdate("DROP PROCEDURE IF EXISTS testHugeNumberOfParameters");
  765. StringBuffer procDef = new StringBuffer(
  766. "CREATE PROCEDURE testHugeNumberOfParameters(");
  767. for (int i = 0; i < 274; i++) {
  768. if (i != 0) {
  769. procDef.append(",");
  770. }
  771. procDef.append(" OUT param_" + i + " VARCHAR(32)");
  772. }
  773. procDef.append(")\nBEGIN\nSELECT 1;\nEND");
  774. this.stmt.executeUpdate(procDef.toString());
  775. CallableStatement cStmt = null;
  776. try {
  777. cStmt = this.conn
  778. .prepareCall("{call testHugeNumberOfParameters(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,"
  779. +
  780. "?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,"
  781. + "?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,"
  782. + "?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,"
  783. + "?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,"
  784. + "?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,"
  785. + "?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
  786. cStmt.registerOutParameter(274, Types.VARCHAR);
  787. cStmt.execute();
  788. } finally {
  789. if (cStmt != null) {
  790. cStmt.close();
  791. }
  792. }
  793. }
  794. public void testPrepareOfMultiRs() throws Exception {
  795. if (!serverSupportsStoredProcedures()) {
  796. return;
  797. }
  798. this.stmt.executeUpdate("Drop procedure if exists p");
  799. this.stmt
  800. .executeUpdate("create procedure p () begin select 1; select 2; end;");
  801. PreparedStatement ps = null;
  802. try {
  803. ps = this.conn.prepareStatement("call p()");
  804. ps.execute();
  805. this.rs = ps.getResultSet();
  806. assertTrue(this.rs.next());
  807. assertEquals(1, this.rs.getInt(1));
  808. assertTrue(ps.getMoreResults());
  809. this.rs = ps.getResultSet();
  810. assertTrue(this.rs.next());
  811. assertEquals(2, this.rs.getInt(1));
  812. assertTrue(!ps.getMoreResults());
  813. } finally {
  814. if (this.rs != null) {
  815. this.rs.close();
  816. this.rs = null;
  817. }
  818. if (ps != null) {
  819. ps.close();
  820. }
  821. }
  822. }
  823. /**
  824. * Tests fix for BUG#25379 - INOUT parameters in CallableStatements get
  825. * doubly-escaped.
  826. *
  827. * @throws Exception
  828. * if the test fails.
  829. */
  830. public void testBug25379() throws Exception {
  831. if (!serverSupportsStoredProcedures()) {
  832. return;
  833. }
  834. createTable("testBug25379", "(col char(40))");
  835. try {
  836. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS sp_testBug25379");
  837. this.stmt
  838. .executeUpdate("CREATE PROCEDURE sp_testBug25379 (INOUT invalue char(255))"
  839. + "\nBEGIN"
  840. + "\ninsert into testBug25379(col) values(invalue);"
  841. + "\nEND");
  842. CallableStatement cstmt = this.conn
  843. .prepareCall("{call sp_testBug25379(?)}");
  844. cstmt.setString(1, "'john'");
  845. cstmt.executeUpdate();
  846. assertEquals("'john'", cstmt.getString(1));
  847. assertEquals("'john'", getSingleValue("testBug25379", "col", "")
  848. .toString());
  849. } finally {
  850. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS sp_testBug25379");
  851. }
  852. }
  853. /**
  854. * Tests fix for BUG#25715 - CallableStatements with OUT/INOUT parameters
  855. * that are "binary" have extra 7 bytes (which happens to be the _binary
  856. * introducer!)
  857. *
  858. * @throws Exception
  859. * if the test fails.
  860. */
  861. public void testBug25715() throws Exception {
  862. if (!serverSupportsStoredProcedures()) {
  863. return; // no stored procs
  864. }
  865. if (isRunningOnJdk131()) {
  866. return; // no such method to test
  867. }
  868. createProcedure("spbug25715", "(INOUT mblob MEDIUMBLOB)" + "BEGIN"
  869. + " SELECT 1 FROM DUAL WHERE 1=0;" + "\nEND");
  870. CallableStatement cstmt = null;
  871. try {
  872. cstmt = this.conn.prepareCall("{call spbug25715(?)}");
  873. byte[] buf = new byte[65];
  874. for (int i = 0; i < 65; i++)
  875. buf[i] = 1;
  876. int il = buf.length;
  877. int[] typesToTest = new int[] { Types.BIT, Types.BINARY,
  878. Types.BLOB, Types.JAVA_OBJECT, Types.LONGVARBINARY,
  879. Types.VARBINARY };
  880. for (int i = 0; i < typesToTest.length; i++) {
  881. cstmt.setBinaryStream("mblob", new ByteArrayInputStream(buf),
  882. buf.length);
  883. cstmt.registerOutParameter("mblob", typesToTest[i]);
  884. cstmt.executeUpdate();
  885. InputStream is = cstmt.getBlob("mblob").getBinaryStream();
  886. ByteArrayOutputStream bOut = new ByteArrayOutputStream();
  887. int bytesRead = 0;
  888. byte[] readBuf = new byte[256];
  889. while ((bytesRead = is.read(readBuf)) != -1) {
  890. bOut.write(readBuf, 0, bytesRead);
  891. }
  892. byte[] fromSelectBuf = bOut.toByteArray();
  893. int ol = fromSelectBuf.length;
  894. assertEquals(il, ol);
  895. }
  896. cstmt.close();
  897. } finally {
  898. closeMemberJDBCResources();
  899. if (cstmt != null) {
  900. cstmt.close();
  901. }
  902. }
  903. }
  904. protected boolean serverSupportsStoredProcedures() throws SQLException {
  905. return versionMeetsMinimum(5, 0);
  906. }
  907. public void testBug26143() throws Exception {
  908. if (!serverSupportsStoredProcedures()) {
  909. return; // no stored procedure support
  910. }
  911. this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testBug26143");
  912. this.stmt
  913. .executeUpdate("CREATE DEFINER=CURRENT_USER PROCEDURE testBug26143(I INT) COMMENT 'abcdefg'"
  914. + "\nBEGIN\n" + "SELECT I * 10;" + "\nEND");
  915. this.conn.prepareCall("{call testBug26143(?)").close();
  916. }
  917. /**
  918. * Tests fix for BUG#26959 - comments confuse procedure parser.
  919. *
  920. * @throws Exception
  921. * if the test fails
  922. */
  923. public void testBug26959() throws Exception {
  924. if (!serverSupportsStoredProcedures()) {
  925. return;
  926. }
  927. createProcedure(
  928. "testBug26959",
  929. "(_ACTION varchar(20),"
  930. + "\n`/*dumb-identifier-1*/` int,"
  931. + "\n`#dumb-identifier-2` int,"
  932. + "\n`--dumb-identifier-3` int,"
  933. + "\n_CLIENT_ID int, -- ABC"
  934. + "\n_LOGIN_ID int, # DEF"
  935. + "\n_WHERE varchar(2000),"
  936. + "\n_SORT varchar(2000),"
  937. + "\n out _SQL varchar(/* inline right here - oh my gosh! */ 8000),"
  938. + "\n _SONG_ID int,"
  939. + "\n _NOTES varchar(2000),"
  940. + "\n out _RESULT varchar(10)"
  941. + "\n /*"
  942. + "\n , -- Generic result parameter"
  943. + "\n out _PERIOD_ID int, -- Returns the period_id. Useful when using @PREDEFLINK to return which is the last period"
  944. + "\n _SONGS_LIST varchar(8000),"
  945. + "\n _COMPOSERID int,"
  946. + "\n _PUBLISHERID int,"
  947. + "\n _PREDEFLINK int -- If the user is accessing through a predefined link: 0=none 1=last period"
  948. + "\n */) BEGIN SELECT 1; END");
  949. createProcedure(
  950. "testBug26959_1",
  951. "(`/*id*/` /* before type 1 */ varchar(20),"
  952. + "/* after type 1 */ OUT result2 DECIMAL(/*size1*/10,/*size2*/2) /* p2 */)"
  953. + "BEGIN SELECT action, result; END");
  954. try {
  955. this.conn.prepareCall(
  956. "{call testBug26959(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}")
  957. .close();
  958. this.rs = this.conn.getMetaData().getProcedureColumns(
  959. this.conn.getCatalog(), null, "testBug26959", "%");
  960. String[] parameterNames = new String[] { "_ACTION",
  961. "/*dumb-identifier-1*/", "#dumb-identifier-2",
  962. "--dumb-identifier-3", "_CLIENT_ID", "_LOGIN_ID", "_WHERE",
  963. "_SORT", "_SQL", "_SONG_ID", "_NOTES", "_RESULT" };
  964. int[] parameterTypes = new int[] { Types.VARCHAR, Types.INTEGER,
  965. Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.INTEGER,
  966. Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.INTEGER,
  967. Types.VARCHAR, Types.VARCHAR };
  968. int[] direction = new int[] { DatabaseMetaData.procedureColumnIn,
  969. DatabaseMetaData.procedureColumnIn,
  970. DatabaseMetaData.procedureColumnIn,
  971. DatabaseMetaData.procedureColumnIn,
  972. DatabaseMetaData.procedureColumnIn,
  973. DatabaseMetaData.procedureColumnIn,
  974. DatabaseMetaData.procedureColumnIn,
  975. DatabaseMetaData.procedureColumnIn,
  976. DatabaseMetaData.procedureColumnOut,
  977. DatabaseMetaData.procedureColumnIn,
  978. DatabaseMetaData.procedureColumnIn,
  979. DatabaseMetaData.procedureColumnOut };
  980. int[] precision = new int[] { 20, 10, 10, 10, 10, 10, 2000, 2000,
  981. 8000, 10, 2000, 10 };
  982. int index = 0;
  983. while (this.rs.next()) {
  984. assertEquals(parameterNames[index], this.rs
  985. .getString("COLUMN_NAME"));
  986. assertEquals(parameterTypes[index], this.rs.getInt("DATA_TYPE"));
  987. assertEquals(precision[index], this.rs.getInt("PRECISION"));
  988. assertEquals(direction[index], this.rs.getInt("COLUMN_TYPE"));
  989. index++;
  990. }
  991. this.rs.close();
  992. index = 0;
  993. parameterNames = new String[] { "/*id*/", "result2" };
  994. parameterTypes = new int[] { Types.VARCHAR, Types.DECIMAL };
  995. precision = new int[] { 20, 10 };
  996. direction = new int[] { DatabaseMetaData.procedureColumnIn,
  997. DatabaseMetaData.procedureColumnOut };
  998. int[] scale = new int[] { 0, 2 };
  999. this.conn.prepareCall("{call testBug26959_1(?, ?)}").close();
  1000. this.rs = this.conn.getMetaData().getProcedureColumns(
  1001. this.conn.getCatalog(), null, "testBug26959_1", "%");
  1002. while (this.rs.next()) {
  1003. assertEquals(parameterNames[index], this.rs
  1004. .getString("COLUMN_NAME"));
  1005. assertEquals(parameterTypes[index], this.rs.getInt("DATA_TYPE"));
  1006. assertEquals(precision[index], this.rs.getInt("PRECISION"));
  1007. assertEquals(scale[index], this.rs.getInt("SCALE"));
  1008. assertEquals(direction[index], this.rs.getInt("COLUMN_TYPE"));
  1009. index++;
  1010. }
  1011. } finally {
  1012. closeMemberJDBCResources();
  1013. }
  1014. }
  1015. /**
  1016. * Tests fix for BUG#27400 - CALL [comment] some_proc() doesn't work
  1017. */
  1018. public void testBug27400() throws Exception {
  1019. if (!serverSupportsStoredProcedures()) {
  1020. return; // SPs not supported
  1021. }
  1022. createProcedure("testBug27400",
  1023. "(a INT, b VARCHAR(32)) BEGIN SELECT 1; END");
  1024. CallableStatement cStmt = null;
  1025. try {
  1026. cStmt = this.conn
  1027. .prepareCall("{CALL /* SOME COMMENT */ testBug27400( /* does this work too? */ ?, ?)} # and a commented ? here too");
  1028. assertTrue(cStmt.toString().indexOf("/*") != -1); // we don't want
  1029. // to strip the
  1030. // comments
  1031. cStmt.setInt(1, 1);
  1032. cStmt.setString(2, "bleh");
  1033. cStmt.execute();
  1034. } finally {
  1035. if (cStmt != null) {
  1036. cStmt.close();
  1037. }
  1038. }
  1039. }
  1040. /**
  1041. * Tests fix for BUG#28689 - CallableStatement.executeBatch() doesn't work
  1042. * when connection property "noAccessToProcedureBodies" has been set to
  1043. * "true".
  1044. *
  1045. * The fix involves changing the behavior of "noAccessToProcedureBodies", in
  1046. * that the driver will now report all paramters as "IN" paramters but allow
  1047. * callers to call registerOutParameter() on them.
  1048. *
  1049. * @throws Exception
  1050. */
  1051. public void testBug28689() throws Exception {
  1052. if (!versionMeetsMinimum(5, 0)) {
  1053. return; // no stored procedures
  1054. }
  1055. createTable("testBug28689", "(" +
  1056. "`id` int(11) NOT NULL auto_increment,"
  1057. + "`usuario` varchar(255) default NULL,"
  1058. + "PRIMARY KEY (`id`)" + ")");
  1059. this.stmt
  1060. .executeUpdate("INSERT INTO testBug28689 (usuario) VALUES ('AAAAAA')");
  1061. createProcedure(
  1062. "sp_testBug28689",
  1063. "(tid INT)"
  1064. + "\nBEGIN"
  1065. + "\nUPDATE testBug28689 SET usuario = 'BBBBBB' WHERE id = tid;"
  1066. + "\nEND");
  1067. Connection noProcedureBodiesConn = getConnectionWithProps("noAccessToProcedureBodies=true");
  1068. CallableStatement cStmt = null;
  1069. try {
  1070. cStmt = noProcedureBodiesConn
  1071. .prepareCall("{CALL sp_testBug28689(?)}");
  1072. cStmt.setInt(1, 1);
  1073. cStmt.addBatch();
  1074. cStmt.executeBatch();
  1075. assertEquals("BBBBBB", getSingleIndexedValueWithQuery(
  1076. noProcedureBodiesConn, 1,
  1077. "SELECT `usuario` FROM testBug28689 WHERE id=1"));
  1078. } finally {
  1079. if (cStmt != null) {
  1080. cStmt.close();
  1081. }
  1082. if (noProcedureBodiesConn != null) {
  1083. noProcedureBodiesConn.close();
  1084. }
  1085. }
  1086. }
  1087. /**
  1088. * Tests fix for Bug#31823 - CallableStatement.setNull() on a stored
  1089. * function would throw an ArrayIndexOutOfBounds when setting the last
  1090. * parameter to null when calling setNull().
  1091. *
  1092. * @throws Exception
  1093. */
  1094. public void testBug31823() throws Exception {
  1095. if (!versionMeetsMinimum(5, 0)) {
  1096. return; // no stored functions
  1097. }
  1098. createTable("testBug31823",
  1099. "(value_1 BIGINT PRIMARY KEY,value_2 VARCHAR(20))");
  1100. createFunction(
  1101. "f_testBug31823",
  1102. "(value_1_v BIGINT,value_2_v VARCHAR(20)) RETURNS BIGINT "
  1103. + "DETERMINISTIC MODIFIES SQL DATA BEGIN INSERT INTO testBug31823 VALUES (value_1_v,value_2_v); "
  1104. + "RETURN value_1_v; END;");
  1105. // Prepare the function call
  1106. CallableStatement callable = null;
  1107. try {
  1108. callable = this.conn.prepareCall("{? = call f_testBug31823(?,?)}");
  1109. callable.registerOutParameter(1, Types.BIGINT);
  1110. // Add row with non-null value
  1111. callable.setLong(2, 1);
  1112. callable.setString(3, "Non-null value");
  1113. callable.executeUpdate();
  1114. assertEquals(1, callable.getLong(1));
  1115. // Add row with null value
  1116. callable.setLong(2, 2);
  1117. callable.setNull(3, Types.VARCHAR);
  1118. callable.executeUpdate();
  1119. assertEquals(2, callable.getLong(1));
  1120. Method[] setters = CallableStatement.class.getMethods();
  1121. for (int i = 0; i < setters.length; i++) {
  1122. if (setters[i].getName().startsWith("set")) {
  1123. Class[] args = setters[i].getParameterTypes();
  1124. if (args.length == 2 && args[0].equals(Integer.TYPE)) {
  1125. if (!args[1].isPrimitive()) {
  1126. try {
  1127. setters[i].invoke(callable, new Object[] {
  1128. new Integer(2), null });
  1129. } catch (InvocationTargetException ive) {
  1130. if (!(ive.getCause() instanceof com.mysql.jdbc.NotImplemented ||
  1131. ive.getCause().getClass().getName().equals("java.sql.SQLFeatureNotSupportedException"))) {
  1132. throw ive;
  1133. }
  1134. }
  1135. } else {
  1136. if (args[1].getName().equals("boolean")) {
  1137. try {
  1138. setters[i].invoke(callable, new Object[] {
  1139. new Integer(2), Boolean.FALSE });
  1140. } catch (InvocationTargetException ive) {
  1141. if (!(ive.getCause() instanceof com.mysql.jdbc.NotImplemented ||
  1142. ive.getCause().getClass().getName().equals("java.sql.SQLFeatureNotSupportedException"))) {
  1143. throw ive;
  1144. }
  1145. }
  1146. }
  1147. if (args[1].getName().equals("byte")) {
  1148. try {
  1149. setters[i].invoke(callable,
  1150. new Object[] { new Integer(2),
  1151. new Byte((byte) 0) });
  1152. } catch (InvocationTargetException ive) {
  1153. if (!(ive.getCause() instanceof com.mysql.jdbc.NotImplemented ||
  1154. ive.getCause().getClass().getName().equals("java.sql.SQLFeatureNotSupportedException"))) {
  1155. throw ive;
  1156. }
  1157. }
  1158. }
  1159. if (args[1].getName().equals("double")) {
  1160. try {
  1161. setters[i].invoke(callable, new Object[] {
  1162. new Integer(2), new Double(0) });
  1163. } catch (InvocationTargetException ive) {
  1164. if (!(ive.getCause() instanceof com.mysql.jdbc.NotImplemented ||
  1165. ive.getCause().getClass().getName().equals("java.sql.SQLFeatureNotSupportedException"))) {
  1166. throw ive;
  1167. }
  1168. }
  1169. }
  1170. if (args[1].getName().equals("float")) {
  1171. try {
  1172. setters[i].invoke(callable, new Object[] {
  1173. new Integer(2), new Float(0) });
  1174. } catch (InvocationTargetException ive) {
  1175. if (!(ive.getCause() instanceof com.mysql.jdbc.NotImplemented ||
  1176. ive.getCause().getClass().getName().equals("java.sql.SQLFeatureNotSupportedException"))) {
  1177. throw ive;
  1178. }
  1179. }
  1180. }
  1181. if (args[1].getName().equals("int")) {
  1182. try {
  1183. setters[i].invoke(callable, new Object[] {
  1184. new Integer(2), new Integer(0) });
  1185. } catch (InvocationTargetException ive) {
  1186. if (!(ive.getCause() instanceof com.mysql.jdbc.NotImplemented ||
  1187. ive.getCause().getClass().getName().equals("java.sql.SQLFeatureNotSupportedException"))) {
  1188. throw ive;
  1189. }
  1190. }
  1191. }
  1192. if (args[1].getName().equals("long")) {
  1193. try {
  1194. setters[i].invoke(callable, new Object[] {
  1195. new Integer(2), new Long(0) });
  1196. } catch (InvocationTargetException ive) {
  1197. if (!(ive.getCause() instanceof com.mysql.jdbc.NotImplemented ||
  1198. ive.getCause().getClass().getName().equals("java.sql.SQLFeatureNotSupportedException"))) {
  1199. throw ive;
  1200. }
  1201. }
  1202. }
  1203. if (args[1].getName().equals("short")) {
  1204. try {
  1205. setters[i].invoke(callable, new Object[] {
  1206. new Integer(2),
  1207. new Short((short) 0) });
  1208. } catch (InvocationTargetException ive) {
  1209. if (!(ive.getCause() instanceof com.mysql.jdbc.NotImplemented ||
  1210. ive.getCause().getClass().getName().equals("java.sql.SQLFeatureNotSupportedException"))) {
  1211. throw ive;
  1212. }
  1213. }
  1214. }
  1215. }
  1216. }
  1217. }
  1218. }
  1219. } finally {
  1220. if (callable != null) {
  1221. callable.close();
  1222. }
  1223. }
  1224. }
  1225. /**
  1226. * Tests fix for Bug#32246 - When unpacking rows directly, we don't hand off
  1227. * error message packets to the internal method which decodes them
  1228. * correctly, so no exception is rasied, and the driver than hangs trying to
  1229. * read rows that aren't there...
  1230. *
  1231. * @throws Exception
  1232. * if the test fails
  1233. */
  1234. public void testBug32246() throws Exception {
  1235. if (!versionMeetsMinimum(5, 0)) {
  1236. return;
  1237. }
  1238. doBug32246(this.conn);
  1239. dropTable("test_table_2");
  1240. dropTable("test_table_1");
  1241. doBug32246(getConnectionWithProps("useDirectRowUnpack=false"));
  1242. }
  1243. private void doBug32246(Connection aConn) throws SQLException {
  1244. createTable("test_table_1",
  1245. "(value_1 BIGINT PRIMARY KEY) ENGINE=InnoDB");
  1246. this.stmt.executeUpdate("INSERT INTO test_table_1 VALUES (1)");
  1247. createTable("test_table_2",
  1248. "(value_2 BIGINT PRIMARY KEY) ENGINE=InnoDB");
  1249. this.stmt.executeUpdate("DROP FUNCTION IF EXISTS test_function");
  1250. createFunction("test_function", "() RETURNS BIGINT "
  1251. + "DETERMINISTIC MODIFIES SQL DATA BEGIN "
  1252. + "DECLARE max_value BIGINT; "
  1253. + "SELECT MAX(value_1) INTO max_value FROM test_table_2; "
  1254. + "RETURN max_value; END;");
  1255. CallableStatement callable = null;
  1256. try {
  1257. callable = aConn.prepareCall("{? = call test_function()}");
  1258. callable.registerOutParameter(1, Types.BIGINT);
  1259. try {
  1260. callable.executeUpdate();
  1261. fail("impossible; we should never get here.");
  1262. } catch (SQLException sqlEx) {
  1263. assertEquals("42S22", sqlEx.getSQLState());
  1264. }
  1265. createTable("test_table_1",
  1266. "(value_1 BIGINT PRIMARY KEY) ENGINE=InnoDB");
  1267. this.stmt.executeUpdate("INSERT INTO test_table_1 VALUES (1)");
  1268. createTable(
  1269. "test_table_2",
  1270. "(value_2 BIGINT PRIMARY KEY, "
  1271. + " FOREIGN KEY (value_2) REFERENCES test_table_1 (value_1) ON DELETE CASCADE) ENGINE=InnoDB");
  1272. createFunction(
  1273. "test_function",
  1274. "(value BIGINT) RETURNS BIGINT "
  1275. + "DETERMINISTIC MODIFIES SQL DATA BEGIN "
  1276. + "INSERT INTO test_table_2 VALUES (value); RETURN value; END;");
  1277. callable = aConn.prepareCall("{? = call test_function(?)}");
  1278. callable.registerOutParameter(1, Types.BIGINT);
  1279. callable.setLong(2, 1);
  1280. callable.executeUpdate();
  1281. callable.setLong(2, 2);
  1282. try {
  1283. callable.executeUpdate();
  1284. fail("impossible; we should never get here.");
  1285. } catch (SQLException sqlEx) {
  1286. assertEquals("23000", sqlEx.getSQLState());
  1287. }
  1288. } finally {
  1289. if (callable != null) {
  1290. callable.close();
  1291. }
  1292. }
  1293. }
  1294. public void testBitSp() throws Exception {
  1295. if (!versionMeetsMinimum(5, 0)) {
  1296. return;
  1297. }
  1298. createTable("`Bit_Tab`", "(" + " `MAX_VAL` tinyint(1) default NULL,"
  1299. + " `MIN_VAL` tinyint(1) default NULL,"
  1300. + " `NULL_VAL` tinyint(1) default NULL)");
  1301. createProcedure(
  1302. "Bit_Proc",
  1303. "(out MAX_PARAM TINYINT, out MIN_PARAM TINYINT, out NULL_PARAM TINYINT)"
  1304. + "begin select MAX_VAL, MIN_VAL, NULL_VAL into MAX_PARAM, MIN_PARAM, NULL_PARAM from Bit_Tab; end");
  1305. Boolean minBooleanVal;
  1306. Boolean oRetVal;
  1307. String Min_Val_Query = "SELECT MIN_VAL from Bit_Tab";
  1308. String sMaxBooleanVal = "1";
  1309. // sMaxBooleanVal = "true";
  1310. Boolean bool = Boolean.valueOf("true");
  1311. String Min_Insert = "insert into Bit_Tab values(1,0,null)";
  1312. // System.out.println("Value to insert=" + extractVal(Min_Insert,1));
  1313. CallableStatement cstmt;
  1314. stmt.executeUpdate("delete from Bit_Tab");
  1315. stmt.executeUpdate(Min_Insert);
  1316. cstmt = conn.prepareCall("{call Bit_Proc(?,?,?)}");
  1317. System.out.println("register the output parameters");
  1318. cstmt.registerOutParameter(1, java.sql.Types.BIT);
  1319. cstmt.registerOutParameter(2, java.sql.Types.BIT);
  1320. cstmt.registerOutParameter(3, java.sql.Types.BIT);
  1321. System.out.println("execute the procedure");
  1322. cstmt.executeUpdate();
  1323. System.out.println("invoke getBoolean method");
  1324. boolean bRetVal = cstmt.getBoolean(2);
  1325. oRetVal = new Boolean(bRetVal);
  1326. minBooleanVal = new Boolean("false");
  1327. rs = stmt.executeQuery(Min_Val_Query);
  1328. if (oRetVal.equals(minBooleanVal))
  1329. System.out.println("getBoolean returns the Minimum value ");
  1330. else {
  1331. System.out
  1332. .println("getBoolean() did not return the Minimum value, getBoolean Failed!");
  1333. }
  1334. }
  1335. public void testNotReallyCallableStatement() throws Exception {
  1336. if (!versionMeetsMinimum(5, 0)) {
  1337. return;
  1338. }
  1339. CallableStatement cstmt = null;
  1340. try {
  1341. this.stmt.executeUpdate("DROP TABLE IF EXISTS testNotReallyCallableStatement");
  1342. cstmt = this.conn.prepareCall("CREATE TABLE testNotReallyCallableStatement(field1 INT)");
  1343. } finally {
  1344. this.stmt.executeUpdate("DROP TABLE IF EXISTS testNotReallyCallableStatement");
  1345. if (cstmt != null) {
  1346. cstmt.close();
  1347. }
  1348. }
  1349. }
  1350. public void testBug35199() throws Exception {
  1351. if (!versionMeetsMinimum(5, 0)) {
  1352. return

Large files files are truncated, but you can click here to view the full file