0001: /*
0002: Copyright (C) 2002-2007 MySQL AB
0003:
0004: This program is free software; you can redistribute it and/or modify
0005: it under the terms of version 2 of the GNU General Public License as
0006: published by the Free Software Foundation.
0007:
0008: There are special exceptions to the terms and conditions of the GPL
0009: as it is applied to this software. View the full text of the
0010: exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
0011: software distribution.
0012:
0013: This program is distributed in the hope that it will be useful,
0014: but WITHOUT ANY WARRANTY; without even the implied warranty of
0015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0016: GNU General Public License for more details.
0017:
0018: You should have received a copy of the GNU General Public License
0019: along with this program; if not, write to the Free Software
0020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0021:
0022:
0023:
0024: */
0025: package testsuite.simple;
0026:
0027: import java.io.ByteArrayInputStream;
0028: import java.io.ByteArrayOutputStream;
0029: import java.io.CharArrayReader;
0030: import java.io.InputStream;
0031: import java.io.Reader;
0032: import java.io.StringReader;
0033: import java.math.BigDecimal;
0034: import java.rmi.server.UID;
0035: import java.sql.BatchUpdateException;
0036: import java.sql.CallableStatement;
0037: import java.sql.Connection;
0038: import java.sql.Date;
0039: import java.sql.PreparedStatement;
0040: import java.sql.ResultSet;
0041: import java.sql.SQLException;
0042: import java.sql.Statement;
0043: import java.sql.Time;
0044: import java.sql.Timestamp;
0045: import java.sql.Types;
0046: import java.text.SimpleDateFormat;
0047: import java.util.Locale;
0048: import java.util.Properties;
0049:
0050: import testsuite.BaseTestCase;
0051:
0052: import com.mysql.jdbc.NotImplemented;
0053: import com.mysql.jdbc.ParameterBindings;
0054: import com.mysql.jdbc.SQLError;
0055: import com.mysql.jdbc.StatementImpl;
0056: import com.mysql.jdbc.StringUtils;
0057:
0058: /**
0059: * DOCUMENT ME!
0060: *
0061: * @author Mark Matthews
0062: * @version $Id: StatementsTest.java 4494 2005-10-31 22:30:34 -0600 (Mon, 31 Oct
0063: * 2005) mmatthews $
0064: */
0065: public class StatementsTest extends BaseTestCase {
0066: private static final int MAX_COLUMN_LENGTH = 255;
0067:
0068: private static final int MAX_COLUMNS_TO_TEST = 40;
0069:
0070: private static final int MIN_COLUMN_LENGTH = 10;
0071:
0072: private static final int STEP = 8;
0073:
0074: /**
0075: * Runs all test cases in this test suite
0076: *
0077: * @param args
0078: */
0079: public static void main(String[] args) {
0080: junit.textui.TestRunner.run(StatementsTest.class);
0081: }
0082:
0083: /**
0084: * Creates a new StatementsTest object.
0085: *
0086: * @param name
0087: * DOCUMENT ME!
0088: */
0089: public StatementsTest(String name) {
0090: super (name);
0091: }
0092:
0093: /**
0094: * DOCUMENT ME!
0095: *
0096: * @throws Exception
0097: * DOCUMENT ME!
0098: */
0099: public void setUp() throws Exception {
0100: super .setUp();
0101:
0102: this .stmt.executeUpdate("DROP TABLE IF EXISTS statement_test");
0103:
0104: this .stmt
0105: .executeUpdate("DROP TABLE IF EXISTS statement_batch_test");
0106:
0107: this .stmt
0108: .executeUpdate("CREATE TABLE statement_test (id int not null primary key auto_increment, strdata1 varchar(255) not null, strdata2 varchar(255))");
0109:
0110: this .stmt
0111: .executeUpdate("CREATE TABLE statement_batch_test "
0112: + "(id int not null primary key auto_increment, "
0113: + "strdata1 varchar(255) not null, strdata2 varchar(255), "
0114: + "UNIQUE INDEX (strdata1))");
0115:
0116: for (int i = 6; i < MAX_COLUMNS_TO_TEST; i += STEP) {
0117: this .stmt
0118: .executeUpdate("DROP TABLE IF EXISTS statement_col_test_"
0119: + i);
0120:
0121: StringBuffer insertBuf = new StringBuffer(
0122: "INSERT INTO statement_col_test_");
0123: StringBuffer stmtBuf = new StringBuffer(
0124: "CREATE TABLE IF NOT EXISTS statement_col_test_");
0125: stmtBuf.append(i);
0126: insertBuf.append(i);
0127: stmtBuf.append(" (");
0128: insertBuf.append(" VALUES (");
0129:
0130: boolean firstTime = true;
0131:
0132: for (int j = 0; j < i; j++) {
0133: if (!firstTime) {
0134: stmtBuf.append(",");
0135: insertBuf.append(",");
0136: } else {
0137: firstTime = false;
0138: }
0139:
0140: stmtBuf.append("col_");
0141: stmtBuf.append(j);
0142: stmtBuf.append(" VARCHAR(");
0143: stmtBuf.append(MAX_COLUMN_LENGTH);
0144: stmtBuf.append(")");
0145: insertBuf.append("'");
0146:
0147: int numChars = 16;
0148:
0149: for (int k = 0; k < numChars; k++) {
0150: insertBuf.append("A");
0151: }
0152:
0153: insertBuf.append("'");
0154: }
0155:
0156: stmtBuf.append(")");
0157: insertBuf.append(")");
0158: this .stmt.executeUpdate(stmtBuf.toString());
0159: this .stmt.executeUpdate(insertBuf.toString());
0160: }
0161:
0162: // explicitly set the catalog to exercise code in execute(),
0163: // executeQuery() and
0164: // executeUpdate()
0165: // FIXME: Only works on Windows!
0166: // this.conn.setCatalog(this.conn.getCatalog().toUpperCase());
0167: }
0168:
0169: /**
0170: * DOCUMENT ME!
0171: *
0172: * @throws Exception
0173: * DOCUMENT ME!
0174: */
0175: public void tearDown() throws Exception {
0176: this .stmt.executeUpdate("DROP TABLE statement_test");
0177:
0178: for (int i = 0; i < MAX_COLUMNS_TO_TEST; i += STEP) {
0179: StringBuffer stmtBuf = new StringBuffer(
0180: "DROP TABLE IF EXISTS statement_col_test_");
0181: stmtBuf.append(i);
0182: this .stmt.executeUpdate(stmtBuf.toString());
0183: }
0184:
0185: try {
0186: this .stmt.executeUpdate("DROP TABLE statement_batch_test");
0187: } catch (SQLException sqlEx) {
0188: ;
0189: }
0190:
0191: super .tearDown();
0192: }
0193:
0194: /**
0195: * DOCUMENT ME!
0196: *
0197: * @throws SQLException
0198: * DOCUMENT ME!
0199: */
0200: public void testAccessorsAndMutators() throws SQLException {
0201: assertTrue(
0202: "Connection can not be null, and must be same connection",
0203: this .stmt.getConnection() == this .conn);
0204:
0205: // Set max rows, to exercise code in execute(), executeQuery() and
0206: // executeUpdate()
0207: Statement accessorStmt = null;
0208:
0209: try {
0210: accessorStmt = this .conn.createStatement();
0211: accessorStmt.setMaxRows(1);
0212: accessorStmt.setMaxRows(0); // FIXME, test that this actually
0213: // affects rows returned
0214: accessorStmt.setMaxFieldSize(255);
0215: assertTrue("Max field size should match what was set",
0216: accessorStmt.getMaxFieldSize() == 255);
0217:
0218: try {
0219: accessorStmt.setMaxFieldSize(Integer.MAX_VALUE);
0220: fail("Should not be able to set max field size > max_packet_size");
0221: } catch (SQLException sqlEx) {
0222: ;
0223: }
0224:
0225: accessorStmt.setCursorName("undef");
0226: accessorStmt.setEscapeProcessing(true);
0227: accessorStmt
0228: .setFetchDirection(java.sql.ResultSet.FETCH_FORWARD);
0229:
0230: int fetchDirection = accessorStmt.getFetchDirection();
0231: assertTrue("Set fetch direction != get fetch direction",
0232: fetchDirection == java.sql.ResultSet.FETCH_FORWARD);
0233:
0234: try {
0235: accessorStmt.setFetchDirection(Integer.MAX_VALUE);
0236: fail("Should not be able to set fetch direction to invalid value");
0237: } catch (SQLException sqlEx) {
0238: ;
0239: }
0240:
0241: try {
0242: accessorStmt.setMaxRows(50000000 + 10);
0243: fail("Should not be able to set max rows > 50000000");
0244: } catch (SQLException sqlEx) {
0245: ;
0246: }
0247:
0248: try {
0249: accessorStmt.setMaxRows(Integer.MIN_VALUE);
0250: fail("Should not be able to set max rows < 0");
0251: } catch (SQLException sqlEx) {
0252: ;
0253: }
0254:
0255: int fetchSize = this .stmt.getFetchSize();
0256:
0257: try {
0258: accessorStmt.setMaxRows(4);
0259: accessorStmt.setFetchSize(Integer.MAX_VALUE);
0260: fail("Should not be able to set FetchSize > max rows");
0261: } catch (SQLException sqlEx) {
0262: ;
0263: }
0264:
0265: try {
0266: accessorStmt.setFetchSize(-2);
0267: fail("Should not be able to set FetchSize < 0");
0268: } catch (SQLException sqlEx) {
0269: ;
0270: }
0271:
0272: assertTrue(
0273: "Fetch size before invalid setFetchSize() calls should match fetch size now",
0274: fetchSize == this .stmt.getFetchSize());
0275: } finally {
0276: if (accessorStmt != null) {
0277: try {
0278: accessorStmt.close();
0279: } catch (SQLException sqlEx) {
0280: ;
0281: }
0282:
0283: accessorStmt = null;
0284: }
0285: }
0286: }
0287:
0288: /**
0289: * DOCUMENT ME!
0290: *
0291: * @throws SQLException
0292: * DOCUMENT ME!
0293: */
0294: public void testAutoIncrement() throws SQLException {
0295: if (!isRunningOnJdk131()) {
0296: try {
0297: this .stmt = this .conn.createStatement(
0298: java.sql.ResultSet.TYPE_FORWARD_ONLY,
0299: java.sql.ResultSet.CONCUR_READ_ONLY);
0300: this .stmt.setFetchSize(Integer.MIN_VALUE);
0301: this .stmt
0302: .executeUpdate("INSERT INTO statement_test (strdata1) values ('blah')");
0303:
0304: int autoIncKeyFromApi = -1;
0305: this .rs = this .stmt.getGeneratedKeys();
0306:
0307: if (this .rs.next()) {
0308: autoIncKeyFromApi = this .rs.getInt(1);
0309: } else {
0310: fail("Failed to retrieve AUTO_INCREMENT using Statement.getGeneratedKeys()");
0311: }
0312:
0313: this .rs.close();
0314:
0315: int autoIncKeyFromFunc = -1;
0316: this .rs = this .stmt
0317: .executeQuery("SELECT LAST_INSERT_ID()");
0318:
0319: if (this .rs.next()) {
0320: autoIncKeyFromFunc = this .rs.getInt(1);
0321: } else {
0322: fail("Failed to retrieve AUTO_INCREMENT using LAST_INSERT_ID()");
0323: }
0324:
0325: if ((autoIncKeyFromApi != -1)
0326: && (autoIncKeyFromFunc != -1)) {
0327: assertTrue(
0328: "Key retrieved from API ("
0329: + autoIncKeyFromApi
0330: + ") does not match key retrieved from LAST_INSERT_ID() "
0331: + autoIncKeyFromFunc + ") function",
0332: autoIncKeyFromApi == autoIncKeyFromFunc);
0333: } else {
0334: fail("AutoIncrement keys were '0'");
0335: }
0336: } finally {
0337: if (this .rs != null) {
0338: try {
0339: this .rs.close();
0340: } catch (Exception ex) { /* ignore */
0341: ;
0342: }
0343: }
0344:
0345: this .rs = null;
0346: }
0347: }
0348: }
0349:
0350: /**
0351: * Tests all variants of numerical types (signed/unsigned) for correct
0352: * operation when used as return values from a prepared statement.
0353: *
0354: * @throws Exception
0355: */
0356: public void testBinaryResultSetNumericTypes() throws Exception {
0357: /*
0358: * TINYINT 1 -128 127 SMALLINT 2 -32768 32767 MEDIUMINT 3 -8388608
0359: * 8388607 INT 4 -2147483648 2147483647 BIGINT 8 -9223372036854775808
0360: * 9223372036854775807
0361: */
0362:
0363: String unsignedMinimum = "0";
0364:
0365: String tiMinimum = "-128";
0366: String tiMaximum = "127";
0367: String utiMaximum = "255";
0368:
0369: String siMinimum = "-32768";
0370: String siMaximum = "32767";
0371: String usiMaximum = "65535";
0372:
0373: String miMinimum = "-8388608";
0374: String miMaximum = "8388607";
0375: String umiMaximum = "16777215";
0376:
0377: String iMinimum = "-2147483648";
0378: String iMaximum = "2147483647";
0379: String uiMaximum = "4294967295";
0380:
0381: String biMinimum = "-9223372036854775808";
0382: String biMaximum = "9223372036854775807";
0383: String ubiMaximum = "18446744073709551615";
0384:
0385: try {
0386: this .stmt
0387: .executeUpdate("DROP TABLE IF EXISTS testBinaryResultSetNumericTypes");
0388: this .stmt
0389: .executeUpdate("CREATE TABLE testBinaryResultSetNumericTypes(rowOrder TINYINT, ti TINYINT,"
0390: + "uti TINYINT UNSIGNED, si SMALLINT,"
0391: + "usi SMALLINT UNSIGNED, mi MEDIUMINT,"
0392: + "umi MEDIUMINT UNSIGNED, i INT, ui INT UNSIGNED,"
0393: + "bi BIGINT, ubi BIGINT UNSIGNED)");
0394: PreparedStatement inserter = this .conn
0395: .prepareStatement("INSERT INTO testBinaryResultSetNumericTypes VALUES (?,?,?,?,?,?,?,?,?,?,?)");
0396: inserter.setInt(1, 0);
0397: inserter.setString(2, tiMinimum);
0398: inserter.setString(3, unsignedMinimum);
0399: inserter.setString(4, siMinimum);
0400: inserter.setString(5, unsignedMinimum);
0401: inserter.setString(6, miMinimum);
0402: inserter.setString(7, unsignedMinimum);
0403: inserter.setString(8, iMinimum);
0404: inserter.setString(9, unsignedMinimum);
0405: inserter.setString(10, biMinimum);
0406: inserter.setString(11, unsignedMinimum);
0407: inserter.executeUpdate();
0408:
0409: inserter.setInt(1, 1);
0410: inserter.setString(2, tiMaximum);
0411: inserter.setString(3, utiMaximum);
0412: inserter.setString(4, siMaximum);
0413: inserter.setString(5, usiMaximum);
0414: inserter.setString(6, miMaximum);
0415: inserter.setString(7, umiMaximum);
0416: inserter.setString(8, iMaximum);
0417: inserter.setString(9, uiMaximum);
0418: inserter.setString(10, biMaximum);
0419: inserter.setString(11, ubiMaximum);
0420: inserter.executeUpdate();
0421:
0422: PreparedStatement selector = this .conn
0423: .prepareStatement("SELECT * FROM testBinaryResultSetNumericTypes ORDER by rowOrder ASC");
0424: this .rs = selector.executeQuery();
0425:
0426: assertTrue(this .rs.next());
0427:
0428: assertTrue(this .rs.getString(2).equals(tiMinimum));
0429: assertTrue(this .rs.getString(3).equals(unsignedMinimum));
0430: assertTrue(this .rs.getString(4).equals(siMinimum));
0431: assertTrue(this .rs.getString(5).equals(unsignedMinimum));
0432: assertTrue(this .rs.getString(6).equals(miMinimum));
0433: assertTrue(this .rs.getString(7).equals(unsignedMinimum));
0434: assertTrue(this .rs.getString(8).equals(iMinimum));
0435: assertTrue(this .rs.getString(9).equals(unsignedMinimum));
0436: assertTrue(this .rs.getString(10).equals(biMinimum));
0437: assertTrue(this .rs.getString(11).equals(unsignedMinimum));
0438:
0439: assertTrue(this .rs.next());
0440:
0441: assertTrue(this .rs.getString(2) + " != " + tiMaximum,
0442: this .rs.getString(2).equals(tiMaximum));
0443: assertTrue(this .rs.getString(3) + " != " + utiMaximum,
0444: this .rs.getString(3).equals(utiMaximum));
0445: assertTrue(this .rs.getString(4) + " != " + siMaximum,
0446: this .rs.getString(4).equals(siMaximum));
0447: assertTrue(this .rs.getString(5) + " != " + usiMaximum,
0448: this .rs.getString(5).equals(usiMaximum));
0449: assertTrue(this .rs.getString(6) + " != " + miMaximum,
0450: this .rs.getString(6).equals(miMaximum));
0451: assertTrue(this .rs.getString(7) + " != " + umiMaximum,
0452: this .rs.getString(7).equals(umiMaximum));
0453: assertTrue(this .rs.getString(8) + " != " + iMaximum,
0454: this .rs.getString(8).equals(iMaximum));
0455: assertTrue(this .rs.getString(9) + " != " + uiMaximum,
0456: this .rs.getString(9).equals(uiMaximum));
0457: assertTrue(this .rs.getString(10) + " != " + biMaximum,
0458: this .rs.getString(10).equals(biMaximum));
0459: assertTrue(this .rs.getString(11) + " != " + ubiMaximum,
0460: this .rs.getString(11).equals(ubiMaximum));
0461:
0462: assertTrue(!this .rs.next());
0463: } finally {
0464: this .stmt
0465: .executeUpdate("DROP TABLE IF EXISTS testBinaryResultSetNumericTypes");
0466: }
0467: }
0468:
0469: /**
0470: * Tests stored procedure functionality
0471: *
0472: * @throws Exception
0473: * if an error occurs.
0474: */
0475: public void testCallableStatement() throws Exception {
0476: if (versionMeetsMinimum(5, 0)) {
0477: CallableStatement cStmt = null;
0478: String stringVal = "abcdefg";
0479: int intVal = 42;
0480:
0481: try {
0482: try {
0483: this .stmt
0484: .executeUpdate("DROP PROCEDURE testCallStmt");
0485: } catch (SQLException sqlEx) {
0486: if (sqlEx.getMessage().indexOf("does not exist") == -1) {
0487: throw sqlEx;
0488: }
0489: }
0490:
0491: this .stmt
0492: .executeUpdate("DROP TABLE IF EXISTS callStmtTbl");
0493: this .stmt
0494: .executeUpdate("CREATE TABLE callStmtTbl (x CHAR(16), y INT)");
0495:
0496: this .stmt
0497: .executeUpdate("CREATE PROCEDURE testCallStmt(n INT, x CHAR(16), y INT)"
0498: + " WHILE n DO"
0499: + " SET n = n - 1;"
0500: + " INSERT INTO callStmtTbl VALUES (x, y);"
0501: + " END WHILE;");
0502:
0503: int rowsToCheck = 15;
0504:
0505: cStmt = this .conn
0506: .prepareCall("{call testCallStmt(?,?,?)}");
0507: cStmt.setInt(1, rowsToCheck);
0508: cStmt.setString(2, stringVal);
0509: cStmt.setInt(3, intVal);
0510: cStmt.execute();
0511:
0512: this .rs = this .stmt
0513: .executeQuery("SELECT x,y FROM callStmtTbl");
0514:
0515: int numRows = 0;
0516:
0517: while (this .rs.next()) {
0518: assertTrue(this .rs.getString(1).equals(stringVal)
0519: && (this .rs.getInt(2) == intVal));
0520:
0521: numRows++;
0522: }
0523:
0524: this .rs.close();
0525: this .rs = null;
0526:
0527: cStmt.close();
0528: cStmt = null;
0529:
0530: System.out.println(rowsToCheck + " rows returned");
0531:
0532: assertTrue(numRows == rowsToCheck);
0533: } finally {
0534: try {
0535: this .stmt
0536: .executeUpdate("DROP PROCEDURE testCallStmt");
0537: } catch (SQLException sqlEx) {
0538: if (sqlEx.getMessage().indexOf("does not exist") == -1) {
0539: throw sqlEx;
0540: }
0541: }
0542:
0543: this .stmt
0544: .executeUpdate("DROP TABLE IF EXISTS callStmtTbl");
0545:
0546: if (cStmt != null) {
0547: cStmt.close();
0548: }
0549: }
0550: }
0551: }
0552:
0553: public void testCancelStatement() throws Exception {
0554:
0555: if (versionMeetsMinimum(5, 0)) {
0556: Connection cancelConn = null;
0557:
0558: try {
0559: cancelConn = getConnectionWithProps((String) null);
0560: final Statement cancelStmt = cancelConn
0561: .createStatement();
0562:
0563: cancelStmt.setQueryTimeout(1);
0564:
0565: long begin = System.currentTimeMillis();
0566:
0567: try {
0568: cancelStmt.execute("SELECT SLEEP(30)");
0569: } catch (SQLException sqlEx) {
0570: assertTrue("Probably wasn't actually cancelled",
0571: System.currentTimeMillis() - begin < 30000);
0572: }
0573:
0574: for (int i = 0; i < 1000; i++) {
0575: try {
0576: cancelStmt.executeQuery("SELECT 1");
0577: } catch (SQLException timedOutEx) {
0578: break;
0579: }
0580: }
0581:
0582: // Make sure we can still use the connection...
0583:
0584: cancelStmt.setQueryTimeout(0);
0585: this .rs = cancelStmt.executeQuery("SELECT 1");
0586:
0587: assertTrue(this .rs.next());
0588: assertEquals(1, this .rs.getInt(1));
0589:
0590: cancelStmt.setQueryTimeout(0);
0591:
0592: new Thread() {
0593:
0594: public void run() {
0595: try {
0596: try {
0597: sleep(5000);
0598: } catch (InterruptedException iEx) {
0599: // ignore
0600: }
0601:
0602: cancelStmt.cancel();
0603: } catch (SQLException sqlEx) {
0604: throw new RuntimeException(sqlEx.toString());
0605: }
0606: }
0607:
0608: }.start();
0609:
0610: begin = System.currentTimeMillis();
0611:
0612: try {
0613: cancelStmt.execute("SELECT SLEEP(30)");
0614: } catch (SQLException sqlEx) {
0615: assertTrue("Probably wasn't actually cancelled",
0616: System.currentTimeMillis() - begin < 30000);
0617: }
0618:
0619: for (int i = 0; i < 1000; i++) {
0620: try {
0621: cancelStmt.executeQuery("SELECT 1");
0622: } catch (SQLException timedOutEx) {
0623: break;
0624: }
0625: }
0626:
0627: // Make sure we can still use the connection...
0628:
0629: this .rs = cancelStmt.executeQuery("SELECT 1");
0630:
0631: assertTrue(this .rs.next());
0632: assertEquals(1, this .rs.getInt(1));
0633:
0634: final PreparedStatement cancelPstmt = cancelConn
0635: .prepareStatement("SELECT SLEEP(30)");
0636:
0637: cancelPstmt.setQueryTimeout(1);
0638:
0639: begin = System.currentTimeMillis();
0640:
0641: try {
0642: cancelPstmt.execute();
0643: } catch (SQLException sqlEx) {
0644: assertTrue("Probably wasn't actually cancelled",
0645: System.currentTimeMillis() - begin < 30000);
0646: }
0647:
0648: for (int i = 0; i < 1000; i++) {
0649: try {
0650: cancelPstmt.executeQuery("SELECT 1");
0651: } catch (SQLException timedOutEx) {
0652: break;
0653: }
0654: }
0655:
0656: // Make sure we can still use the connection...
0657:
0658: this .rs = cancelStmt.executeQuery("SELECT 1");
0659:
0660: assertTrue(this .rs.next());
0661: assertEquals(1, this .rs.getInt(1));
0662:
0663: cancelPstmt.setQueryTimeout(0);
0664:
0665: new Thread() {
0666:
0667: public void run() {
0668: try {
0669: try {
0670: sleep(5000);
0671: } catch (InterruptedException iEx) {
0672: // ignore
0673: }
0674:
0675: cancelPstmt.cancel();
0676: } catch (SQLException sqlEx) {
0677: throw new RuntimeException(sqlEx.toString());
0678: }
0679: }
0680:
0681: }.start();
0682:
0683: begin = System.currentTimeMillis();
0684:
0685: try {
0686: cancelPstmt.execute();
0687: } catch (SQLException sqlEx) {
0688: assertTrue("Probably wasn't actually cancelled",
0689: System.currentTimeMillis() - begin < 30000);
0690: }
0691:
0692: for (int i = 0; i < 1000; i++) {
0693: try {
0694: cancelPstmt.executeQuery("SELECT 1");
0695: } catch (SQLException timedOutEx) {
0696: break;
0697: }
0698: }
0699:
0700: // Make sure we can still use the connection...
0701:
0702: this .rs = cancelStmt.executeQuery("SELECT 1");
0703:
0704: assertTrue(this .rs.next());
0705: assertEquals(1, this .rs.getInt(1));
0706:
0707: final PreparedStatement cancelClientPstmt = ((com.mysql.jdbc.Connection) cancelConn)
0708: .clientPrepareStatement("SELECT SLEEP(30)");
0709:
0710: cancelClientPstmt.setQueryTimeout(1);
0711:
0712: begin = System.currentTimeMillis();
0713:
0714: try {
0715: cancelClientPstmt.execute();
0716: } catch (SQLException sqlEx) {
0717: assertTrue("Probably wasn't actually cancelled",
0718: System.currentTimeMillis() - begin < 30000);
0719: }
0720:
0721: for (int i = 0; i < 1000; i++) {
0722: try {
0723: cancelStmt.executeQuery("SELECT 1");
0724: } catch (SQLException timedOutEx) {
0725: break;
0726: }
0727: }
0728:
0729: // Make sure we can still use the connection...
0730:
0731: this .rs = cancelStmt.executeQuery("SELECT 1");
0732:
0733: assertTrue(this .rs.next());
0734: assertEquals(1, this .rs.getInt(1));
0735:
0736: cancelClientPstmt.setQueryTimeout(0);
0737:
0738: new Thread() {
0739:
0740: public void run() {
0741: try {
0742: try {
0743: sleep(5000);
0744: } catch (InterruptedException iEx) {
0745: // ignore
0746: }
0747:
0748: cancelClientPstmt.cancel();
0749: } catch (SQLException sqlEx) {
0750: throw new RuntimeException(sqlEx.toString());
0751: }
0752: }
0753:
0754: }.start();
0755:
0756: begin = System.currentTimeMillis();
0757:
0758: try {
0759: cancelClientPstmt.execute();
0760: } catch (SQLException sqlEx) {
0761: assertTrue("Probably wasn't actually cancelled",
0762: System.currentTimeMillis() - begin < 30000);
0763: }
0764:
0765: for (int i = 0; i < 1000; i++) {
0766: try {
0767: cancelClientPstmt.executeQuery("SELECT 1");
0768: } catch (SQLException timedOutEx) {
0769: break;
0770: }
0771: }
0772:
0773: // Make sure we can still use the connection...
0774:
0775: this .rs = cancelStmt.executeQuery("SELECT 1");
0776:
0777: assertTrue(this .rs.next());
0778: assertEquals(1, this .rs.getInt(1));
0779: } finally {
0780: if (this .rs != null) {
0781: ResultSet toClose = this .rs;
0782: this .rs = null;
0783: toClose.close();
0784: }
0785:
0786: if (cancelConn != null) {
0787: cancelConn.close();
0788: }
0789: }
0790: }
0791: }
0792:
0793: /**
0794: * DOCUMENT ME!
0795: *
0796: * @throws SQLException
0797: * DOCUMENT ME!
0798: */
0799: public void testClose() throws SQLException {
0800: Statement closeStmt = null;
0801: boolean exceptionAfterClosed = false;
0802:
0803: try {
0804: closeStmt = this .conn.createStatement();
0805: closeStmt.close();
0806:
0807: try {
0808: closeStmt.executeQuery("SELECT 1");
0809: } catch (SQLException sqlEx) {
0810: exceptionAfterClosed = true;
0811: }
0812: } finally {
0813: if (closeStmt != null) {
0814: try {
0815: closeStmt.close();
0816: } catch (SQLException sqlEx) {
0817: /* ignore */
0818: }
0819: }
0820:
0821: closeStmt = null;
0822: }
0823:
0824: assertTrue(
0825: "Operations not allowed on Statement after .close() is called!",
0826: exceptionAfterClosed);
0827: }
0828:
0829: public void testEnableStreamingResults() throws Exception {
0830: Statement streamStmt = this .conn.createStatement();
0831: ((com.mysql.jdbc.Statement) streamStmt)
0832: .enableStreamingResults();
0833: assertEquals(streamStmt.getFetchSize(), Integer.MIN_VALUE);
0834: assertEquals(streamStmt.getResultSetType(),
0835: ResultSet.TYPE_FORWARD_ONLY);
0836: }
0837:
0838: public void testHoldingResultSetsOverClose() throws Exception {
0839: Properties props = new Properties();
0840: props.setProperty("holdResultsOpenOverStatementClose", "true");
0841:
0842: Connection conn2 = getConnectionWithProps(props);
0843:
0844: Statement stmt2 = null;
0845: PreparedStatement pstmt2 = null;
0846:
0847: try {
0848: stmt2 = conn2.createStatement();
0849:
0850: this .rs = stmt2.executeQuery("SELECT 1");
0851: this .rs.next();
0852: this .rs.getInt(1);
0853: stmt2.close();
0854: this .rs.getInt(1);
0855:
0856: stmt2 = conn2.createStatement();
0857: stmt2.execute("SELECT 1");
0858: this .rs = stmt2.getResultSet();
0859: this .rs.next();
0860: this .rs.getInt(1);
0861: stmt2.execute("SELECT 2");
0862: this .rs.getInt(1);
0863:
0864: pstmt2 = conn2.prepareStatement("SELECT 1");
0865: this .rs = pstmt2.executeQuery();
0866: this .rs.next();
0867: this .rs.getInt(1);
0868: pstmt2.close();
0869: this .rs.getInt(1);
0870:
0871: pstmt2 = conn2.prepareStatement("SELECT 1");
0872: this .rs = pstmt2.executeQuery();
0873: this .rs.next();
0874: this .rs.getInt(1);
0875: pstmt2.executeQuery();
0876: this .rs.getInt(1);
0877: pstmt2.execute();
0878: this .rs.getInt(1);
0879:
0880: pstmt2 = ((com.mysql.jdbc.Connection) conn2)
0881: .clientPrepareStatement("SELECT 1");
0882: this .rs = pstmt2.executeQuery();
0883: this .rs.next();
0884: this .rs.getInt(1);
0885: pstmt2.close();
0886: this .rs.getInt(1);
0887:
0888: pstmt2 = ((com.mysql.jdbc.Connection) conn2)
0889: .clientPrepareStatement("SELECT 1");
0890: this .rs = pstmt2.executeQuery();
0891: this .rs.next();
0892: this .rs.getInt(1);
0893: pstmt2.executeQuery();
0894: this .rs.getInt(1);
0895: pstmt2.execute();
0896: this .rs.getInt(1);
0897:
0898: stmt2 = conn2.createStatement();
0899: this .rs = stmt2.executeQuery("SELECT 1");
0900: this .rs.next();
0901: this .rs.getInt(1);
0902: stmt2.executeQuery("SELECT 2");
0903: this .rs.getInt(1);
0904: this .rs = stmt2.executeQuery("SELECT 1");
0905: this .rs.next();
0906: this .rs.getInt(1);
0907: stmt2.executeUpdate("SET @var=1");
0908: this .rs.getInt(1);
0909: stmt2.execute("SET @var=2");
0910: this .rs.getInt(1);
0911: } finally {
0912: if (stmt2 != null) {
0913: stmt2.close();
0914: }
0915: }
0916: }
0917:
0918: /**
0919: * DOCUMENT ME!
0920: *
0921: * @throws SQLException
0922: * DOCUMENT ME!
0923: */
0924: public void testInsert() throws SQLException {
0925: try {
0926: boolean autoCommit = this .conn.getAutoCommit();
0927:
0928: // Test running a query for an update. It should fail.
0929: try {
0930: this .conn.setAutoCommit(false);
0931: this .stmt.executeUpdate("SELECT * FROM statement_test");
0932: } catch (SQLException sqlEx) {
0933: assertTrue("Exception thrown for unknown reason", sqlEx
0934: .getSQLState().equalsIgnoreCase("01S03"));
0935: } finally {
0936: this .conn.setAutoCommit(autoCommit);
0937: }
0938:
0939: // Test running a update for an query. It should fail.
0940: try {
0941: this .conn.setAutoCommit(false);
0942: this .stmt
0943: .executeQuery("UPDATE statement_test SET strdata1='blah' WHERE 1=0");
0944: } catch (SQLException sqlEx) {
0945: assertTrue("Exception thrown for unknown reason", sqlEx
0946: .getSQLState().equalsIgnoreCase(
0947: SQLError.SQL_STATE_ILLEGAL_ARGUMENT));
0948: } finally {
0949: this .conn.setAutoCommit(autoCommit);
0950: }
0951:
0952: for (int i = 0; i < 10; i++) {
0953: int updateCount = this .stmt
0954: .executeUpdate("INSERT INTO statement_test (strdata1,strdata2) values ('abcdefg', 'poi')");
0955: assertTrue("Update count must be '1', was '"
0956: + updateCount + "'", (updateCount == 1));
0957: }
0958:
0959: if (!isRunningOnJdk131()) {
0960: int insertIdFromGeneratedKeys = Integer.MIN_VALUE;
0961:
0962: this .stmt
0963: .executeUpdate("INSERT INTO statement_test (strdata1, strdata2) values ('a', 'a'), ('b', 'b'), ('c', 'c')");
0964: this .rs = this .stmt.getGeneratedKeys();
0965:
0966: if (this .rs.next()) {
0967: insertIdFromGeneratedKeys = this .rs.getInt(1);
0968: }
0969:
0970: this .rs.close();
0971: this .rs = this .stmt
0972: .executeQuery("SELECT LAST_INSERT_ID()");
0973:
0974: int insertIdFromServer = Integer.MIN_VALUE;
0975:
0976: if (this .rs.next()) {
0977: insertIdFromServer = this .rs.getInt(1);
0978: }
0979:
0980: assertEquals(insertIdFromGeneratedKeys,
0981: insertIdFromServer);
0982: }
0983: } finally {
0984: if (this .rs != null) {
0985: try {
0986: this .rs.close();
0987: } catch (Exception ex) { /* ignore */
0988: ;
0989: }
0990: }
0991:
0992: this .rs = null;
0993: }
0994: }
0995:
0996: /**
0997: * Tests multiple statement support
0998: *
0999: * @throws Exception
1000: * DOCUMENT ME!
1001: */
1002: public void testMultiStatements() throws Exception {
1003: if (versionMeetsMinimum(4, 1)) {
1004: Connection multiStmtConn = null;
1005: Statement multiStmt = null;
1006:
1007: try {
1008: Properties props = new Properties();
1009: props.setProperty("allowMultiQueries", "true");
1010:
1011: multiStmtConn = getConnectionWithProps(props);
1012:
1013: multiStmt = multiStmtConn.createStatement();
1014:
1015: multiStmt
1016: .executeUpdate("DROP TABLE IF EXISTS testMultiStatements");
1017: multiStmt
1018: .executeUpdate("CREATE TABLE testMultiStatements (field1 VARCHAR(255), field2 INT, field3 DOUBLE)");
1019: multiStmt
1020: .executeUpdate("INSERT INTO testMultiStatements VALUES ('abcd', 1, 2)");
1021:
1022: multiStmt
1023: .execute("SELECT field1 FROM testMultiStatements WHERE field1='abcd';"
1024: + "UPDATE testMultiStatements SET field3=3;"
1025: + "SELECT field3 FROM testMultiStatements WHERE field3=3");
1026:
1027: this .rs = multiStmt.getResultSet();
1028:
1029: assertTrue(this .rs.next());
1030:
1031: assertTrue("abcd".equals(this .rs.getString(1)));
1032: this .rs.close();
1033:
1034: // Next should be an update count...
1035: assertTrue(!multiStmt.getMoreResults());
1036:
1037: assertTrue("Update count was "
1038: + multiStmt.getUpdateCount() + ", expected 1",
1039: multiStmt.getUpdateCount() == 1);
1040:
1041: assertTrue(multiStmt.getMoreResults());
1042:
1043: this .rs = multiStmt.getResultSet();
1044:
1045: assertTrue(this .rs.next());
1046:
1047: assertTrue(this .rs.getDouble(1) == 3);
1048:
1049: // End of multi results
1050: assertTrue(!multiStmt.getMoreResults());
1051: assertTrue(multiStmt.getUpdateCount() == -1);
1052: } finally {
1053: if (multiStmt != null) {
1054: multiStmt
1055: .executeUpdate("DROP TABLE IF EXISTS testMultiStatements");
1056:
1057: multiStmt.close();
1058: }
1059:
1060: if (multiStmtConn != null) {
1061: multiStmtConn.close();
1062: }
1063: }
1064: }
1065: }
1066:
1067: /**
1068: * Tests that NULLs and '' work correctly.
1069: *
1070: * @throws SQLException
1071: * if an error occurs
1072: */
1073: public void testNulls() throws SQLException {
1074: try {
1075: this .stmt.executeUpdate("DROP TABLE IF EXISTS nullTest");
1076: this .stmt
1077: .executeUpdate("CREATE TABLE IF NOT EXISTS nullTest (field_1 CHAR(20), rowOrder INT)");
1078: this .stmt
1079: .executeUpdate("INSERT INTO nullTest VALUES (null, 1), ('', 2)");
1080:
1081: this .rs = this .stmt
1082: .executeQuery("SELECT field_1 FROM nullTest ORDER BY rowOrder");
1083:
1084: this .rs.next();
1085:
1086: assertTrue("NULL field not returned as NULL", (this .rs
1087: .getString("field_1") == null)
1088: && this .rs.wasNull());
1089:
1090: this .rs.next();
1091:
1092: assertTrue("Empty field not returned as \"\"", this .rs
1093: .getString("field_1").equals("")
1094: && !this .rs.wasNull());
1095:
1096: this .rs.close();
1097: } finally {
1098: if (this .rs != null) {
1099: try {
1100: this .rs.close();
1101: } catch (Exception ex) {
1102: // ignore
1103: }
1104: }
1105:
1106: this .stmt.executeUpdate("DROP TABLE IF EXISTS nullTest");
1107: }
1108: }
1109:
1110: public void testParsedConversionWarning() throws Exception {
1111: if (versionMeetsMinimum(4, 1)) {
1112: try {
1113: Properties props = new Properties();
1114: props.setProperty("useUsageAdvisor", "true");
1115: Connection warnConn = getConnectionWithProps(props);
1116:
1117: this .stmt
1118: .executeUpdate("DROP TABLE IF EXISTS testParsedConversionWarning");
1119: this .stmt
1120: .executeUpdate("CREATE TABLE testParsedConversionWarning(field1 VARCHAR(255))");
1121: this .stmt
1122: .executeUpdate("INSERT INTO testParsedConversionWarning VALUES ('1.0')");
1123:
1124: PreparedStatement badStmt = warnConn
1125: .prepareStatement("SELECT field1 FROM testParsedConversionWarning");
1126:
1127: this .rs = badStmt.executeQuery();
1128: assertTrue(this .rs.next());
1129: this .rs.getFloat(1);
1130: } finally {
1131: this .stmt
1132: .executeUpdate("DROP TABLE IF EXISTS testParsedConversionWarning");
1133: }
1134: }
1135: }
1136:
1137: /**
1138: * DOCUMENT ME!
1139: *
1140: * @throws SQLException
1141: * DOCUMENT ME!
1142: */
1143: public void testPreparedStatement() throws SQLException {
1144: this .stmt
1145: .executeUpdate("INSERT INTO statement_test (id, strdata1,strdata2) values (999,'abcdefg', 'poi')");
1146: this .pstmt = this .conn
1147: .prepareStatement("UPDATE statement_test SET strdata1=?, strdata2=? where id=999");
1148: this .pstmt.setString(1, "iop");
1149: this .pstmt.setString(2, "higjklmn");
1150:
1151: // pstmt.setInt(3, 999);
1152: int updateCount = this .pstmt.executeUpdate();
1153: assertTrue("Update count must be '1', was '" + updateCount
1154: + "'", (updateCount == 1));
1155:
1156: this .pstmt.clearParameters();
1157:
1158: this .pstmt.close();
1159:
1160: this .rs = this .stmt
1161: .executeQuery("SELECT id, strdata1, strdata2 FROM statement_test");
1162:
1163: assertTrue(this .rs.next());
1164: assertTrue(this .rs.getInt(1) == 999);
1165: assertTrue("Expected 'iop', received '" + this .rs.getString(2)
1166: + "'", "iop".equals(this .rs.getString(2)));
1167: assertTrue("Expected 'higjklmn', received '"
1168: + this .rs.getString(3) + "'", "higjklmn".equals(this .rs
1169: .getString(3)));
1170: }
1171:
1172: /**
1173: * DOCUMENT ME!
1174: *
1175: * @throws SQLException
1176: * DOCUMENT ME!
1177: */
1178: public void testPreparedStatementBatch() throws SQLException {
1179: this .pstmt = this .conn
1180: .prepareStatement("INSERT INTO "
1181: + "statement_batch_test (strdata1, strdata2) VALUES (?,?)");
1182:
1183: for (int i = 0; i < 1000; i++) {
1184: this .pstmt.setString(1, "batch_" + i);
1185: this .pstmt.setString(2, "batch_" + i);
1186: this .pstmt.addBatch();
1187: }
1188:
1189: int[] updateCounts = this .pstmt.executeBatch();
1190:
1191: for (int i = 0; i < updateCounts.length; i++) {
1192: assertTrue("Update count must be '1', was '"
1193: + updateCounts[i] + "'", (updateCounts[i] == 1));
1194: }
1195: }
1196:
1197: public void testRowFetch() throws Exception {
1198: if (versionMeetsMinimum(5, 0, 5)) {
1199: createTable("testRowFetch", "(field1 int)");
1200:
1201: this .stmt
1202: .executeUpdate("INSERT INTO testRowFetch VALUES (1)");
1203:
1204: Connection fetchConn = null;
1205:
1206: Properties props = new Properties();
1207: props.setProperty("useCursorFetch", "true");
1208:
1209: try {
1210: fetchConn = getConnectionWithProps(props);
1211:
1212: PreparedStatement fetchStmt = fetchConn
1213: .prepareStatement("SELECT field1 FROM testRowFetch WHERE field1=1");
1214: fetchStmt.setFetchSize(10);
1215: this .rs = fetchStmt.executeQuery();
1216: assertTrue(this .rs.next());
1217:
1218: this .stmt
1219: .executeUpdate("INSERT INTO testRowFetch VALUES (2), (3)");
1220:
1221: fetchStmt = fetchConn
1222: .prepareStatement("SELECT field1 FROM testRowFetch ORDER BY field1");
1223: fetchStmt.setFetchSize(1);
1224: this .rs = fetchStmt.executeQuery();
1225:
1226: assertTrue(this .rs.next());
1227: assertEquals(1, this .rs.getInt(1));
1228: assertTrue(this .rs.next());
1229: assertEquals(2, this .rs.getInt(1));
1230: assertTrue(this .rs.next());
1231: assertEquals(3, this .rs.getInt(1));
1232: assertEquals(false, this .rs.next());
1233:
1234: fetchStmt.executeQuery();
1235: } finally {
1236: if (fetchConn != null) {
1237: fetchConn.close();
1238: }
1239: }
1240:
1241: }
1242: }
1243:
1244: /**
1245: * DOCUMENT ME!
1246: *
1247: * @throws SQLException
1248: * DOCUMENT ME!
1249: */
1250: public void testSelectColumns() throws SQLException {
1251: for (int i = 6; i < MAX_COLUMNS_TO_TEST; i += STEP) {
1252: long start = System.currentTimeMillis();
1253: this .rs = this .stmt
1254: .executeQuery("SELECT * from statement_col_test_"
1255: + i);
1256:
1257: if (this .rs.next()) {
1258: ;
1259: }
1260:
1261: long end = System.currentTimeMillis();
1262: System.out.println(i + " columns = " + (end - start)
1263: + " ms");
1264: }
1265: }
1266:
1267: /**
1268: * Tests for PreparedStatement.setObject()
1269: *
1270: * @throws Exception
1271: */
1272: public void testSetObject() throws Exception {
1273: Properties props = new Properties();
1274: props.put("noDatetimeStringSync", "true"); // value=true for #5
1275: Connection conn1 = getConnectionWithProps(props);
1276: Statement stmt1 = conn1.createStatement();
1277: stmt1.executeUpdate("DROP TABLE IF EXISTS t1");
1278: stmt1.executeUpdate("CREATE TABLE t1 (" + "c1 DECIMAL," // instance of
1279: // String
1280: + "c2 VARCHAR(255)," // instance of String
1281: + "c3 BLOB," // instance of byte[]
1282: + "c4 DATE," // instance of java.util.Date
1283: + "c5 TIMESTAMP," // instance of String
1284: + "c6 TIME," // instance of String
1285: + "c7 TIME)"); // instance of java.sql.Timestamp
1286:
1287: this .pstmt = conn1
1288: .prepareStatement("INSERT INTO t1 VALUES (?, ?, ?, ?, ?, ?, ?)");
1289:
1290: long currentTime = System.currentTimeMillis();
1291:
1292: this .pstmt.setObject(1, "1000", Types.DECIMAL);
1293: this .pstmt.setObject(2, "2000", Types.VARCHAR);
1294: this .pstmt.setObject(3, new byte[] { 0 }, Types.BLOB);
1295: this .pstmt.setObject(4, new java.util.Date(currentTime),
1296: Types.DATE);
1297: this .pstmt.setObject(5, "2000-01-01 23-59-59", Types.TIMESTAMP);
1298: this .pstmt.setObject(6, "11:22:33", Types.TIME);
1299: this .pstmt.setObject(7, new java.sql.Timestamp(currentTime),
1300: Types.TIME);
1301: this .pstmt.execute();
1302: this .rs = stmt1.executeQuery("SELECT * FROM t1");
1303: this .rs.next();
1304:
1305: assertEquals("1000", this .rs.getString(1));
1306: assertEquals("2000", this .rs.getString(2));
1307: assertEquals(1, ((byte[]) this .rs.getObject(3)).length);
1308: assertEquals(0, ((byte[]) this .rs.getObject(3))[0]);
1309: assertEquals(new java.sql.Date(currentTime).toString(), this .rs
1310: .getDate(4).toString());
1311:
1312: if (versionMeetsMinimum(4, 1)) {
1313: assertEquals("2000-01-01 23:59:59", this .rs.getString(5));
1314: } else {
1315: assertEquals("20000101235959", this .rs.getString(5));
1316: }
1317:
1318: assertEquals("11:22:33", this .rs.getString(6));
1319: assertEquals(new java.sql.Time(currentTime).toString(), this .rs
1320: .getString(7));
1321: }
1322:
1323: public void testStatementRewriteBatch() throws Exception {
1324: for (int j = 0; j < 2; j++) {
1325: Properties props = new Properties();
1326:
1327: if (j == 0) {
1328: props.setProperty("useServerPrepStmts", "true");
1329: }
1330:
1331: props.setProperty("rewriteBatchedStatements", "true");
1332: Connection multiConn = getConnectionWithProps(props);
1333: createTable("testStatementRewriteBatch",
1334: "(pk_field INT PRIMARY KEY NOT NULL AUTO_INCREMENT, field1 INT)");
1335: Statement multiStmt = multiConn.createStatement();
1336: multiStmt
1337: .addBatch("INSERT INTO testStatementRewriteBatch(field1) VALUES (1)");
1338: multiStmt
1339: .addBatch("INSERT INTO testStatementRewriteBatch(field1) VALUES (2)");
1340: multiStmt
1341: .addBatch("INSERT INTO testStatementRewriteBatch(field1) VALUES (3)");
1342: multiStmt
1343: .addBatch("INSERT INTO testStatementRewriteBatch(field1) VALUES (4)");
1344: multiStmt
1345: .addBatch("UPDATE testStatementRewriteBatch SET field1=5 WHERE field1=1");
1346: multiStmt
1347: .addBatch("UPDATE testStatementRewriteBatch SET field1=6 WHERE field1=2 OR field1=3");
1348:
1349: int[] counts = multiStmt.executeBatch();
1350:
1351: if (!isRunningOnJdk131()) {
1352: ResultSet genKeys = multiStmt.getGeneratedKeys();
1353:
1354: for (int i = 1; i < 5; i++) {
1355: genKeys.next();
1356: assertEquals(i, genKeys.getInt(1));
1357: }
1358: }
1359:
1360: assertEquals(counts.length, 6);
1361: assertEquals(counts[0], 1);
1362: assertEquals(counts[1], 1);
1363: assertEquals(counts[2], 1);
1364: assertEquals(counts[3], 1);
1365: assertEquals(counts[4], 1);
1366: assertEquals(counts[5], 2);
1367:
1368: this .rs = multiStmt
1369: .executeQuery("SELECT field1 FROM testStatementRewriteBatch ORDER BY field1");
1370: assertTrue(this .rs.next());
1371: assertEquals(this .rs.getInt(1), 4);
1372: assertTrue(this .rs.next());
1373: assertEquals(this .rs.getInt(1), 5);
1374: assertTrue(this .rs.next());
1375: assertEquals(this .rs.getInt(1), 6);
1376: assertTrue(this .rs.next());
1377: assertEquals(this .rs.getInt(1), 6);
1378:
1379: createTable("testStatementRewriteBatch",
1380: "(pk_field INT PRIMARY KEY NOT NULL AUTO_INCREMENT, field1 INT)");
1381: props.clear();
1382: props.setProperty("rewriteBatchedStatements", "true");
1383: props.setProperty("sessionVariables",
1384: "max_allowed_packet=1024");
1385: multiConn = getConnectionWithProps(props);
1386: multiStmt = multiConn.createStatement();
1387:
1388: for (int i = 0; i < 1000; i++) {
1389: multiStmt
1390: .addBatch("INSERT INTO testStatementRewriteBatch(field1) VALUES ("
1391: + i + ")");
1392: }
1393:
1394: multiStmt.executeBatch();
1395:
1396: if (!isRunningOnJdk131()) {
1397: ResultSet genKeys = multiStmt.getGeneratedKeys();
1398:
1399: for (int i = 1; i < 1000; i++) {
1400: genKeys.next();
1401: assertEquals(i, genKeys.getInt(1));
1402: }
1403: }
1404:
1405: createTable("testStatementRewriteBatch",
1406: "(pk_field INT PRIMARY KEY NOT NULL AUTO_INCREMENT, field1 INT)");
1407:
1408: props.clear();
1409: props.setProperty("useServerPrepStmts", j == 0 ? "true"
1410: : "false");
1411: props.setProperty("rewriteBatchedStatements", "true");
1412: multiConn = getConnectionWithProps(props);
1413:
1414: PreparedStatement pStmt = null;
1415:
1416: if (!isRunningOnJdk131()) {
1417: pStmt = multiConn
1418: .prepareStatement(
1419: "INSERT INTO testStatementRewriteBatch(field1) VALUES (?)",
1420: Statement.RETURN_GENERATED_KEYS);
1421:
1422: for (int i = 0; i < 1000; i++) {
1423: pStmt.setInt(1, i);
1424: pStmt.addBatch();
1425: }
1426:
1427: pStmt.executeBatch();
1428:
1429: ResultSet genKeys = pStmt.getGeneratedKeys();
1430:
1431: for (int i = 1; i < 1000; i++) {
1432: genKeys.next();
1433: assertEquals(i, genKeys.getInt(1));
1434: }
1435: }
1436:
1437: createTable("testStatementRewriteBatch",
1438: "(pk_field INT PRIMARY KEY NOT NULL AUTO_INCREMENT, field1 INT)");
1439: props.setProperty("useServerPrepStmts", j == 0 ? "true"
1440: : "false");
1441: props.setProperty("rewriteBatchedStatements", "true");
1442: props.setProperty("sessionVariables",
1443: "max_allowed_packet=1024");
1444: multiConn = getConnectionWithProps(props);
1445:
1446: if (!isRunningOnJdk131()) {
1447:
1448: pStmt = multiConn
1449: .prepareStatement(
1450: "INSERT INTO testStatementRewriteBatch(field1) VALUES (?)",
1451: Statement.RETURN_GENERATED_KEYS);
1452:
1453: for (int i = 0; i < 1000; i++) {
1454: pStmt.setInt(1, i);
1455: pStmt.addBatch();
1456: }
1457:
1458: pStmt.executeBatch();
1459:
1460: ResultSet genKeys = pStmt.getGeneratedKeys();
1461:
1462: for (int i = 1; i < 1000; i++) {
1463: genKeys.next();
1464: assertEquals(i, genKeys.getInt(1));
1465: }
1466: }
1467:
1468: Object[][] differentTypes = new Object[1000][14];
1469:
1470: createTable(
1471: "rewriteBatchTypes",
1472: "(internalOrder int, f1 tinyint null, "
1473: + "f2 smallint null, f3 int null, f4 bigint null, "
1474: + "f5 decimal(8, 2) null, f6 float null, f7 double null, "
1475: + "f8 varchar(255) null, f9 text null, f10 blob null, "
1476: + "f11 blob null, f12 datetime null, f13 time null, f14 date null)");
1477:
1478: for (int i = 0; i < 1000; i++) {
1479: differentTypes[i][0] = Math.random() < .5 ? null
1480: : new Byte((byte) (Math.random() * 127));
1481: differentTypes[i][1] = Math.random() < .5 ? null
1482: : new Short(
1483: (short) (Math.random() * Short.MAX_VALUE));
1484: differentTypes[i][2] = Math.random() < .5 ? null
1485: : new Integer(
1486: (int) (Math.random() * Integer.MAX_VALUE));
1487: differentTypes[i][3] = Math.random() < .5 ? null
1488: : new Long(
1489: (long) (Math.random() * Long.MAX_VALUE));
1490: differentTypes[i][4] = Math.random() < .5 ? null
1491: : new BigDecimal("19.95");
1492: differentTypes[i][5] = Math.random() < .5 ? null
1493: : new Float(3 + ((float) (Math.random())));
1494: differentTypes[i][6] = Math.random() < .5 ? null
1495: : new Double(3 + (Math.random()));
1496: differentTypes[i][7] = Math.random() < .5 ? null
1497: : randomString();
1498: differentTypes[i][8] = Math.random() < .5 ? null
1499: : randomString();
1500: differentTypes[i][9] = Math.random() < .5 ? null
1501: : randomString().getBytes();
1502: differentTypes[i][10] = Math.random() < .5 ? null
1503: : randomString().getBytes();
1504: differentTypes[i][11] = Math.random() < .5 ? null
1505: : new Timestamp(System.currentTimeMillis());
1506: differentTypes[i][12] = Math.random() < .5 ? null
1507: : new Time(System.currentTimeMillis());
1508: differentTypes[i][13] = Math.random() < .5 ? null
1509: : new Date(System.currentTimeMillis());
1510: }
1511:
1512: props.setProperty("useServerPrepStmts", j == 0 ? "true"
1513: : "false");
1514: props.setProperty("rewriteBatchedStatements", "true");
1515: props.setProperty("sessionVariables",
1516: "max_allowed_packet=1024");
1517: multiConn = getConnectionWithProps(props);
1518: pStmt = multiConn
1519: .prepareStatement("INSERT INTO rewriteBatchTypes(internalOrder,f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
1520:
1521: for (int i = 0; i < 1000; i++) {
1522: pStmt.setInt(1, i);
1523: for (int k = 0; k < 14; k++) {
1524: if (k == 8) {
1525: String asString = (String) differentTypes[i][k];
1526:
1527: if (asString == null) {
1528: pStmt.setObject(k + 2, null);
1529: } else {
1530: pStmt.setCharacterStream(k + 2,
1531: new StringReader(asString),
1532: asString.length());
1533: }
1534: } else if (k == 9) {
1535: byte[] asBytes = (byte[]) differentTypes[i][k];
1536:
1537: if (asBytes == null) {
1538: pStmt.setObject(k + 2, null);
1539: } else {
1540: pStmt.setBinaryStream(k + 2,
1541: new ByteArrayInputStream(asBytes),
1542: asBytes.length);
1543: }
1544: } else {
1545: pStmt.setObject(k + 2, differentTypes[i][k]);
1546: }
1547: }
1548: pStmt.addBatch();
1549: }
1550:
1551: pStmt.executeBatch();
1552:
1553: this .rs = this .stmt
1554: .executeQuery("SELECT f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14 FROM rewriteBatchTypes ORDER BY internalOrder");
1555:
1556: int idx = 0;
1557:
1558: // We need to format this ourselves, since we have to strip the nanos off of
1559: // TIMESTAMPs, so .equals() doesn't really work...
1560:
1561: SimpleDateFormat sdf = new SimpleDateFormat(
1562: "''yyyy-MM-dd HH:mm:ss''", Locale.US);
1563:
1564: while (this .rs.next()) {
1565: for (int k = 0; k < 14; k++) {
1566: if (differentTypes[idx][k] == null) {
1567: assertTrue("On row " + idx
1568: + " expected NULL, found "
1569: + this .rs.getObject(k + 1)
1570: + " in column " + (k + 1), this .rs
1571: .getObject(k + 1) == null);
1572: } else {
1573: String className = differentTypes[idx][k]
1574: .getClass().getName();
1575:
1576: if (className.equals("java.io.StringReader")) {
1577: StringReader reader = (StringReader) differentTypes[idx][k];
1578: StringBuffer buf = new StringBuffer();
1579:
1580: int c = 0;
1581:
1582: while ((c = reader.read()) != -1) {
1583: buf.append((char) c);
1584: }
1585:
1586: String asString = this .rs.getString(k + 1);
1587:
1588: assertEquals("On row " + idx + ", column "
1589: + (k + 1), buf.toString(), asString);
1590:
1591: } else if (differentTypes[idx][k] instanceof java.io.InputStream) {
1592: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
1593:
1594: int bytesRead = 0;
1595:
1596: byte[] buf = new byte[128];
1597: InputStream in = (InputStream) differentTypes[idx][k];
1598:
1599: while ((bytesRead = in.read(buf)) != -1) {
1600: bOut.write(buf, 0, bytesRead);
1601: }
1602:
1603: byte[] expected = bOut.toByteArray();
1604: byte[] actual = this .rs.getBytes(k + 1);
1605:
1606: assertEquals("On row " + idx + ", column "
1607: + (k + 1), StringUtils.dumpAsHex(
1608: expected, expected.length),
1609: StringUtils.dumpAsHex(actual,
1610: actual.length));
1611: } else if (differentTypes[idx][k] instanceof byte[]) {
1612: byte[] expected = (byte[]) differentTypes[idx][k];
1613: byte[] actual = this .rs.getBytes(k + 1);
1614: assertEquals("On row " + idx + ", column "
1615: + (k + 1), StringUtils.dumpAsHex(
1616: expected, expected.length),
1617: StringUtils.dumpAsHex(actual,
1618: actual.length));
1619: } else if (differentTypes[idx][k] instanceof Timestamp) {
1620: assertEquals("On row " + idx + ", column "
1621: + (k + 1), sdf
1622: .format(differentTypes[idx][k]),
1623: sdf
1624: .format(this .rs
1625: .getObject(k + 1)));
1626: } else if (differentTypes[idx][k] instanceof Double) {
1627: assertEquals("On row " + idx + ", column "
1628: + (k + 1),
1629: ((Double) differentTypes[idx][k])
1630: .doubleValue(), this .rs
1631: .getDouble(k + 1), .1);
1632: } else if (differentTypes[idx][k] instanceof Float) {
1633: assertEquals("On row " + idx + ", column "
1634: + (k + 1),
1635: ((Float) differentTypes[idx][k])
1636: .floatValue(), this .rs
1637: .getFloat(k + 1), .1);
1638: } else if (className.equals("java.lang.Byte")) {
1639: // special mapping in JDBC for ResultSet.getObject()
1640: assertEquals("On row " + idx + ", column "
1641: + (k + 1), new Integer(
1642: ((Byte) differentTypes[idx][k])
1643: .byteValue()), this .rs
1644: .getObject(k + 1));
1645: } else if (className.equals("java.lang.Short")) {
1646: // special mapping in JDBC for ResultSet.getObject()
1647: assertEquals("On row " + idx + ", column "
1648: + (k + 1), new Integer(
1649: ((Short) differentTypes[idx][k])
1650: .shortValue()), this .rs
1651: .getObject(k + 1));
1652: } else {
1653: assertEquals("On row "
1654: + idx
1655: + ", column "
1656: + (k + 1)
1657: + " ("
1658: + differentTypes[idx][k].getClass()
1659: + "/"
1660: + this .rs.getObject(k + 1)
1661: .getClass(),
1662: differentTypes[idx][k].toString(),
1663: this .rs.getObject(k + 1).toString());
1664: }
1665: }
1666: }
1667:
1668: idx++;
1669: }
1670: }
1671: }
1672:
1673: public void testBatchRewriteErrors() throws Exception {
1674: createTable("rewriteErrors",
1675: "(field1 int not null primary key)");
1676:
1677: Properties props = new Properties();
1678: Connection multiConn = null;
1679:
1680: for (int j = 0; j < 2; j++) {
1681: props.setProperty("useServerPrepStmts", "false");
1682:
1683: if (j == 1) {
1684: props.setProperty("continueBatchOnError", "false");
1685: }
1686:
1687: props.setProperty("sessionVariables",
1688: "max_allowed_packet=1024");
1689: props.setProperty("rewriteBatchedStatements", "true");
1690: multiConn = getConnectionWithProps(props);
1691: this .pstmt = multiConn
1692: .prepareStatement("INSERT INTO rewriteErrors VALUES (?)");
1693: Statement multiStmt = multiConn.createStatement();
1694:
1695: for (int i = 0; i < 4096; i++) {
1696: multiStmt.addBatch("INSERT INTO rewriteErrors VALUES ("
1697: + i + ")");
1698: this .pstmt.setInt(1, i);
1699: this .pstmt.addBatch();
1700: }
1701:
1702: multiStmt
1703: .addBatch("INSERT INTO rewriteErrors VALUES (2048)");
1704:
1705: this .pstmt.setInt(1, 2048);
1706: this .pstmt.addBatch();
1707:
1708: try {
1709: this .pstmt.executeBatch();
1710: } catch (BatchUpdateException bUpE) {
1711: int[] counts = bUpE.getUpdateCounts();
1712:
1713: for (int i = 4059; i < counts.length; i++) {
1714: assertEquals(counts[i], Statement.EXECUTE_FAILED);
1715: }
1716:
1717: assertEquals(4096, getRowCount("rewriteErrors"));
1718: }
1719:
1720: this .stmt.execute("TRUNCATE TABLE rewriteErrors");
1721:
1722: try {
1723: multiStmt.executeBatch();
1724: } catch (BatchUpdateException bUpE) {
1725: int[] counts = bUpE.getUpdateCounts();
1726:
1727: for (int i = 4094; i < counts.length; i++) {
1728: assertEquals(counts[i], Statement.EXECUTE_FAILED);
1729: }
1730:
1731: assertEquals(4096, getRowCount("rewriteErrors"));
1732: }
1733:
1734: if (versionMeetsMinimum(5, 0)) {
1735: this .stmt.execute("TRUNCATE TABLE rewriteErrors");
1736:
1737: createProcedure("sp_rewriteErrors",
1738: "(param1 INT)\nBEGIN\nINSERT INTO rewriteErrors VALUES (param1);\nEND");
1739:
1740: CallableStatement cStmt = multiConn
1741: .prepareCall("{ CALL sp_rewriteErrors(?)}");
1742:
1743: for (int i = 0; i < 4096; i++) {
1744: cStmt.setInt(1, i);
1745: cStmt.addBatch();
1746: }
1747:
1748: cStmt.setInt(1, 2048);
1749: cStmt.addBatch();
1750:
1751: try {
1752: cStmt.executeBatch();
1753: } catch (BatchUpdateException bUpE) {
1754: int[] counts = bUpE.getUpdateCounts();
1755:
1756: for (int i = 4093; i < counts.length; i++) {
1757: assertEquals(counts[i],
1758: Statement.EXECUTE_FAILED);
1759: }
1760:
1761: assertEquals(4096, getRowCount("rewriteErrors"));
1762: }
1763: }
1764: }
1765: }
1766:
1767: public void testStreamChange() throws Exception {
1768: createTable("testStreamChange",
1769: "(field1 varchar(32), field2 int, field3 TEXT, field4 BLOB)");
1770: this .pstmt = this .conn
1771: .prepareStatement("INSERT INTO testStreamChange VALUES (?, ?, ?, ?)");
1772:
1773: try {
1774: this .pstmt.setString(1, "A");
1775: this .pstmt.setInt(2, 1);
1776:
1777: char[] cArray = { 'A', 'B', 'C' };
1778: Reader r = new CharArrayReader(cArray);
1779: this .pstmt.setCharacterStream(3, r, cArray.length);
1780:
1781: byte[] bArray = { 'D', 'E', 'F' };
1782: ByteArrayInputStream bais = new ByteArrayInputStream(bArray);
1783: this .pstmt.setBinaryStream(4, bais, bArray.length);
1784:
1785: assertEquals(1, this .pstmt.executeUpdate());
1786:
1787: this .rs = this .stmt
1788: .executeQuery("SELECT field3, field4 from testStreamChange where field1='A'");
1789: this .rs.next();
1790: assertEquals("ABC", this .rs.getString(1));
1791: assertEquals("DEF", this .rs.getString(2));
1792:
1793: char[] ucArray = { 'C', 'E', 'S', 'U' };
1794: this .pstmt.setString(1, "CESU");
1795: this .pstmt.setInt(2, 3);
1796: Reader ucReader = new CharArrayReader(ucArray);
1797: this .pstmt.setCharacterStream(3, ucReader, ucArray.length);
1798: this .pstmt.setBinaryStream(4, null, 0);
1799: assertEquals(1, this .pstmt.executeUpdate());
1800:
1801: this .rs = this .stmt
1802: .executeQuery("SELECT field3, field4 from testStreamChange where field1='CESU'");
1803: this .rs.next();
1804: assertEquals("CESU", this .rs.getString(1));
1805: assertEquals(null, this .rs.getString(2));
1806: } finally {
1807: if (this .rs != null) {
1808: this .rs.close();
1809: this .rs = null;
1810: }
1811:
1812: if (this .pstmt != null) {
1813: this .pstmt.close();
1814: this .pstmt = null;
1815: }
1816: }
1817: }
1818:
1819: /**
1820: * DOCUMENT ME!
1821: *
1822: * @throws SQLException
1823: * DOCUMENT ME!
1824: */
1825: public void testStubbed() throws SQLException {
1826: if (!isRunningOnJdk131()) {
1827: try {
1828: this .stmt.getResultSetHoldability();
1829: } catch (NotImplemented notImplEx) {
1830: ;
1831: }
1832: }
1833: }
1834:
1835: public void testTruncationOnRead() throws Exception {
1836: this .rs = this .stmt.executeQuery("SELECT '" + Long.MAX_VALUE
1837: + "'");
1838: this .rs.next();
1839:
1840: try {
1841: this .rs.getByte(1);
1842: fail("Should've thrown an out-of-range exception");
1843: } catch (SQLException sqlEx) {
1844: assertTrue(SQLError.SQL_STATE_NUMERIC_VALUE_OUT_OF_RANGE
1845: .equals(sqlEx.getSQLState()));
1846: }
1847:
1848: try {
1849: this .rs.getShort(1);
1850: fail("Should've thrown an out-of-range exception");
1851: } catch (SQLException sqlEx) {
1852: assertTrue(SQLError.SQL_STATE_NUMERIC_VALUE_OUT_OF_RANGE
1853: .equals(sqlEx.getSQLState()));
1854: }
1855:
1856: try {
1857: this .rs.getInt(1);
1858: fail("Should've thrown an out-of-range exception");
1859: } catch (SQLException sqlEx) {
1860: assertTrue(SQLError.SQL_STATE_NUMERIC_VALUE_OUT_OF_RANGE
1861: .equals(sqlEx.getSQLState()));
1862: }
1863:
1864: this .rs = this .stmt.executeQuery("SELECT '" + Double.MAX_VALUE
1865: + "'");
1866:
1867: this .rs.next();
1868:
1869: try {
1870: this .rs.getByte(1);
1871: fail("Should've thrown an out-of-range exception");
1872: } catch (SQLException sqlEx) {
1873: assertTrue(SQLError.SQL_STATE_NUMERIC_VALUE_OUT_OF_RANGE
1874: .equals(sqlEx.getSQLState()));
1875: }
1876:
1877: try {
1878: this .rs.getShort(1);
1879: fail("Should've thrown an out-of-range exception");
1880: } catch (SQLException sqlEx) {
1881: assertTrue(SQLError.SQL_STATE_NUMERIC_VALUE_OUT_OF_RANGE
1882: .equals(sqlEx.getSQLState()));
1883: }
1884:
1885: try {
1886: this .rs.getInt(1);
1887: fail("Should've thrown an out-of-range exception");
1888: } catch (SQLException sqlEx) {
1889: assertTrue(SQLError.SQL_STATE_NUMERIC_VALUE_OUT_OF_RANGE
1890: .equals(sqlEx.getSQLState()));
1891: }
1892:
1893: try {
1894: this .rs.getLong(1);
1895: fail("Should've thrown an out-of-range exception");
1896: } catch (SQLException sqlEx) {
1897: assertTrue(SQLError.SQL_STATE_NUMERIC_VALUE_OUT_OF_RANGE
1898: .equals(sqlEx.getSQLState()));
1899: }
1900:
1901: try {
1902: this .rs.getLong(1);
1903: fail("Should've thrown an out-of-range exception");
1904: } catch (SQLException sqlEx) {
1905: assertTrue(SQLError.SQL_STATE_NUMERIC_VALUE_OUT_OF_RANGE
1906: .equals(sqlEx.getSQLState()));
1907: }
1908:
1909: PreparedStatement pStmt = null;
1910:
1911: System.out
1912: .println("Testing prepared statements with binary result sets now");
1913:
1914: try {
1915: this .stmt
1916: .executeUpdate("DROP TABLE IF EXISTS testTruncationOnRead");
1917: this .stmt
1918: .executeUpdate("CREATE TABLE testTruncationOnRead(intField INTEGER, bigintField BIGINT, doubleField DOUBLE)");
1919: this .stmt
1920: .executeUpdate("INSERT INTO testTruncationOnRead VALUES ("
1921: + Integer.MAX_VALUE
1922: + ", "
1923: + Long.MAX_VALUE
1924: + ", " + Double.MAX_VALUE + ")");
1925: this .stmt
1926: .executeUpdate("INSERT INTO testTruncationOnRead VALUES ("
1927: + Integer.MIN_VALUE
1928: + ", "
1929: + Long.MIN_VALUE
1930: + ", " + Double.MIN_VALUE + ")");
1931:
1932: pStmt = this .conn
1933: .prepareStatement("SELECT intField, bigintField, doubleField FROM testTruncationOnRead ORDER BY intField DESC");
1934: this .rs = pStmt.executeQuery();
1935:
1936: this .rs.next();
1937:
1938: try {
1939: this .rs.getByte(1);
1940: fail("Should've thrown an out-of-range exception");
1941: } catch (SQLException sqlEx) {
1942: assertTrue(SQLError.SQL_STATE_NUMERIC_VALUE_OUT_OF_RANGE
1943: .equals(sqlEx.getSQLState()));
1944: }
1945:
1946: try {
1947: this .rs.getInt(2);
1948: fail("Should've thrown an out-of-range exception");
1949: } catch (SQLException sqlEx) {
1950: assertTrue(SQLError.SQL_STATE_NUMERIC_VALUE_OUT_OF_RANGE
1951: .equals(sqlEx.getSQLState()));
1952: }
1953:
1954: try {
1955: this .rs.getLong(3);
1956: fail("Should've thrown an out-of-range exception");
1957: } catch (SQLException sqlEx) {
1958: assertTrue(SQLError.SQL_STATE_NUMERIC_VALUE_OUT_OF_RANGE
1959: .equals(sqlEx.getSQLState()));
1960: }
1961: } finally {
1962: this .stmt
1963: .executeUpdate("DROP TABLE IF EXISTS testTruncationOnRead");
1964: }
1965:
1966: }
1967:
1968: public void testStatementInterceptors() throws Exception {
1969: Connection interceptedConn = null;
1970:
1971: /*
1972: try {
1973: Properties props = new Properties();
1974: props.setProperty("statementInterceptors", "com.mysql.jdbc.interceptors.ResultSetScannerInterceptor");
1975: props.setProperty("resultSetScannerRegex", ".*");
1976: interceptedConn = getConnectionWithProps(props);
1977: this.rs = interceptedConn.createStatement().executeQuery("SELECT 'abc'");
1978: this.rs.next();
1979: this.rs.getString(1);
1980: } finally {
1981: closeMemberJDBCResources();
1982:
1983: if (interceptedConn != null) {
1984: interceptedConn.close();
1985: }
1986: }
1987: */
1988:
1989: try {
1990: Properties props = new Properties();
1991: props
1992: .setProperty("statementInterceptors",
1993: "com.mysql.jdbc.interceptors.ServerStatusDiffInterceptor");
1994:
1995: interceptedConn = getConnectionWithProps(props);
1996: this .rs = interceptedConn.createStatement().executeQuery(
1997: "SELECT 'abc'");
1998: } finally {
1999: closeMemberJDBCResources();
2000:
2001: if (interceptedConn != null) {
2002: interceptedConn.close();
2003: }
2004: }
2005: }
2006:
2007: public void testParameterBindings() throws Exception {
2008: // Need to check character set stuff, so need a new connection
2009: Connection utfConn = getConnectionWithProps("characterEncoding=utf-8");
2010:
2011: java.util.Date now = new java.util.Date();
2012:
2013: Object[] valuesToTest = new Object[] {
2014: new Byte(Byte.MIN_VALUE), new Short(Short.MIN_VALUE),
2015: new Integer(Integer.MIN_VALUE),
2016: new Long(Long.MIN_VALUE), new Double(Double.MIN_VALUE),
2017: "\u4E2D\u6587", new BigDecimal(Math.PI), null, // to test isNull
2018: now // to test serialization
2019: };
2020:
2021: StringBuffer statementText = new StringBuffer("SELECT ?");
2022:
2023: for (int i = 1; i < valuesToTest.length; i++) {
2024: statementText.append(",?");
2025: }
2026:
2027: this .pstmt = utfConn.prepareStatement(statementText.toString());
2028:
2029: for (int i = 0; i < valuesToTest.length; i++) {
2030: this .pstmt.setObject(i + 1, valuesToTest[i]);
2031: }
2032:
2033: ParameterBindings bindings = ((com.mysql.jdbc.PreparedStatement) this .pstmt)
2034: .getParameterBindings();
2035:
2036: for (int i = 0; i < valuesToTest.length; i++) {
2037: assertEquals(bindings.getObject(i + 1), valuesToTest[i]);
2038: }
2039: }
2040:
2041: public void testLocalInfileHooked() throws Exception {
2042: createTable("localInfileHooked",
2043: "(field1 int, field2 varchar(255))");
2044: String streamData = "1\tabcd\n2\tefgh\n3\tijkl";
2045: InputStream stream = new ByteArrayInputStream(streamData
2046: .getBytes());
2047: try {
2048: ((com.mysql.jdbc.Statement) this .stmt)
2049: .setLocalInfileInputStream(stream);
2050: this .stmt
2051: .execute("LOAD DATA LOCAL INFILE 'bogusFileName' INTO TABLE localInfileHooked");
2052: assertEquals(-1, stream.read());
2053: this .rs = this .stmt
2054: .executeQuery("SELECT field2 FROM localInfileHooked ORDER BY field1 ASC");
2055: this .rs.next();
2056: assertEquals("abcd", this .rs.getString(1));
2057: this .rs.next();
2058: assertEquals("efgh", this .rs.getString(1));
2059: this .rs.next();
2060: assertEquals("ijkl", this .rs.getString(1));
2061: } finally {
2062: ((com.mysql.jdbc.Statement) this.stmt)
2063: .setLocalInfileInputStream(null);
2064: closeMemberJDBCResources();
2065: }
2066: }
2067: }
|