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