Skip to content

Commit

Permalink
[CONJ-22] finalize server preparedStatement implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Sep 15, 2015
1 parent 9f3eb20 commit 0f78c03
Show file tree
Hide file tree
Showing 8 changed files with 402 additions and 375 deletions.
516 changes: 259 additions & 257 deletions src/main/java/org/mariadb/jdbc/MySQLResultSet.java

Large diffs are not rendered by default.

35 changes: 18 additions & 17 deletions src/main/java/org/mariadb/jdbc/MySQLServerPreparedStatement.java
Expand Up @@ -191,28 +191,29 @@ public ResultSetMetaData getMetaData() throws SQLException {


/**
* Submits a batch of commands to the database for execution and if all commands execute successfully, returns an
* <p>Submits a batch of commands to the database for execution and if all commands execute successfully, returns an
* array of update counts. The <code>int</code> elements of the array that is returned are ordered to correspond to
* the commands in the batch, which are ordered according to the order in which they were added to the batch. The
* elements in the array returned by the method <code>executeBatch</code> may be one of the following: <OL> <LI>A
* number greater than or equal to zero -- indicates that the command was processed successfully and is an update
* count giving the number of rows in the database that were affected by the command's execution <LI>A value of
* <code>SUCCESS_NO_INFO</code> -- indicates that the command was processed successfully but that the number of rows
* elements in the array returned by the method <code>executeBatch</code> may be one of the following:</p>
* <ol>
* <li>A number greater than or equal to zero -- indicates that the command was processed successfully and is an update
* count giving the number of rows in the database that were affected by the command's execution
* <li>A value of <code>SUCCESS_NO_INFO</code> -- indicates that the command was processed successfully but that the number of rows
* affected is unknown
* <p/>
*
* If one of the commands in a batch update fails to execute properly, this method throws a
* <code>BatchUpdateException</code>, and a JDBC driver may or may not continue to process the remaining commands in
* the batch. However, the driver's behavior must be consistent with a particular DBMS, either always continuing to
* process commands or never continuing to process commands. If the driver continues processing after a failure,
* the array returned by the method <code>BatchUpdateException.getUpdateCounts</code> will contain as many elements
* as there are commands in the batch, and at least one of the elements will be the following:
* <p/>
*
* <LI>A value of <code>EXECUTE_FAILED</code> -- indicates that the command failed to execute successfully and
* occurs only if a driver continues to process commands after a command fails </OL>
* <p/>
* occurs only if a driver continues to process commands after a command fails </ol>
*
* The possible implementations and return values have been modified in the Java 2 SDK, Standard Edition, version
* 1.3 to accommodate the option of continuing to proccess commands in a batch update after a
* <code>BatchUpdateException</code> obejct has been thrown.
* <code>BatchUpdateException</code> object has been thrown.
*
* @return an array of update counts containing one element for each command in the batch. The elements of the
* array are ordered according to the order in which commands were added to the batch.
Expand Down Expand Up @@ -390,14 +391,14 @@ public boolean execute() throws SQLException {


/**
* Releases this <code>Statement</code> object's database and JDBC resources immediately instead of waiting for this
* <p>Releases this <code>Statement</code> object's database and JDBC resources immediately instead of waiting for this
* to happen when it is automatically closed. It is generally good practice to release resources as soon as you are
* finished with them to avoid tying up database resources.
* <p/>
* Calling the method <code>close</code> on a <code>Statement</code> object that is already closed has no effect.
* <p/>
* <B>Note:</B>When a <code>Statement</code> object is closed, its current <code>ResultSet</code> object, if one
* exists, is also closed.
* finished with them to avoid tying up database resources.</p>
*
* <p>Calling the method <code>close</code> on a <code>Statement</code> object that is already closed has no effect.</p>
*
* <p><B>Note:</B>When a <code>Statement</code> object is closed, its current <code>ResultSet</code> object, if one
* exists, is also closed.</p>
*
* @throws java.sql.SQLException if a database access error occurs
*/
Expand Down
Expand Up @@ -33,11 +33,13 @@ private StreamingSelectResult(MySQLColumnInformation[] info, MySQLProtocol proto
}

/**
* @param packet the result set packet from the server
*
* @param packet the result set packet from the server
* @param packetFetcher packetfetcher
* @param protocol the current connection protocol class
* @param protocol the current connection protocol class
* @param binaryProtocol is the mysql protocol binary
* @return a StreamingQueryResult
* @throws IOException when something goes wrong while reading/writing from the server
* @throws IOException when something goes wrong while reading/writing from the server
* @throws QueryException if there is an actual active result on the current connection
*/
public static StreamingSelectResult createStreamingSelectResult(
Expand Down
35 changes: 22 additions & 13 deletions src/test/java/org/mariadb/jdbc/DriverTest.java
Expand Up @@ -166,12 +166,19 @@ public void updateTest() throws SQLException {

@Test
public void ralfTest() throws SQLException {
connection.prepareStatement(
"select serverrequ0_.id as id75_, serverrequ0_.date_time as date2_75_, serverrequ0_.name as name75_, serverrequ0_.server_request_type as server4_75_, serverrequ0_.response_code_storage_tag as response5_75_, serverrequ0_.storage_tag as storage6_75_, serverrequ0_.timeout as "+
" timeout75_, serverrequ0_.url as url75_ from ussd_server_request_tag serverrequ0_ where serverrequ0_.name='ß' limit ?");


Statement stmt = connection.createStatement();
stmt.execute("DROP TABLE IF EXISTS t2");
stmt.execute("CREATE TABLE t2 (id int not null primary key auto_increment, test varchar(10))");
for (int i=0;i<10;i++) stmt.execute("INSERT INTO t2 (test) VALUES ('aßa"+i+"')");
PreparedStatement ps = connection.prepareStatement("SELECT * FROM t2 where test like'%ß%' limit ?");
ps.setInt(1,5);
ps.addBatch();
ResultSet rs = ps.executeQuery();
int result = 0;
while (rs.next()) result++;
assertEquals(result, 5);
}

@Test
public void autoIncTest() throws SQLException {
String query = "CREATE TABLE t2 (id int not null primary key auto_increment, test varchar(10))";
Expand Down Expand Up @@ -802,11 +809,11 @@ public void testPreparedStatementsWithComments() throws SQLException {
public void testPreparedStatementsWithQuotes() throws SQLException {
connection.createStatement().execute("drop table if exists quotesPreparedStatements");
connection.createStatement().execute(
"create table quotesPreparedStatements (id int not null primary key auto_increment, a varchar(10))");
"create table quotesPreparedStatements (id int not null primary key auto_increment, a varchar(10) , b varchar(10))");

String query = "INSERT INTO quotesPreparedStatements (a) VALUES ('hellooo?') # ?";
String query = "INSERT INTO quotesPreparedStatements (a,b) VALUES ('hellooo?', ?) # ?";
PreparedStatement pstmt = connection.prepareStatement(query);

pstmt.setString(1,"ff");
pstmt.execute();
}

Expand Down Expand Up @@ -979,10 +986,15 @@ public void getUrlFailTest2() throws SQLException {
URL url = rs.getURL("url");
assertNotNull(url);
}

@Test
public void setNull() throws SQLException {
PreparedStatement ps = connection.prepareStatement("insert blabla (?)");
ps.setString(1,null);
Statement stmt = connection.createStatement();
stmt.executeUpdate("drop table if exists blabla");
stmt.executeUpdate("CREATE TABLE blabla (valsue varchar(20))");

PreparedStatement ps = connection.prepareStatement("insert blabla VALUE (?)");
ps.setString(1, null);
}

@Test
Expand All @@ -1003,9 +1015,6 @@ public void testBug501452() throws SQLException {
ps.addBatch();

ps.executeBatch();

connection.commit();

}

@Test
Expand Down
31 changes: 17 additions & 14 deletions src/test/java/org/mariadb/jdbc/MultiTest.java
Expand Up @@ -289,7 +289,9 @@ public void rewriteBatchedStatementsSemicolon() throws SQLException {
tmpConnection = openNewConnection(connURI, props);
tmpConnection.createStatement().execute("TRUNCATE t3");

PreparedStatement sqlInsert = tmpConnection.prepareStatement("INSERT INTO t3 (message) VALUES (?)");
int currentInsert = retrieveSessionVariableFromServer(tmpConnection, "Com_insert");

PreparedStatement sqlInsert = tmpConnection.prepareStatement("/*CLIENT*/ INSERT INTO t3 (message) VALUES (?)");
sqlInsert.setString(1, "aa");
sqlInsert.addBatch();
sqlInsert.setString(1, "b;b");
Expand All @@ -308,10 +310,12 @@ public void rewriteBatchedStatementsSemicolon() throws SQLException {
Assert.assertEquals(1, updateCounts[i]);
}

tmpConnection.commit();
verifyInsertCount(tmpConnection, 1);
assertEquals(1, retrieveSessionVariableFromServer(tmpConnection, "Com_insert") - currentInsert);

currentInsert = retrieveSessionVariableFromServer(tmpConnection, "Com_insert");

// Test for multiple statements which isn't allowed. rewrite shouldn't work
sqlInsert = tmpConnection.prepareStatement("INSERT INTO t3 (message) VALUES (?); INSERT INTO t3 (message) VALUES ('multiple')");
sqlInsert = tmpConnection.prepareStatement("/*CLIENT*/ INSERT INTO t3 (message) VALUES (?); INSERT INTO t3 (message) VALUES ('multiple')");
sqlInsert.setString(1, "aa");
sqlInsert.addBatch();
sqlInsert.setString(1, "b;b");
Expand All @@ -322,8 +326,9 @@ public void rewriteBatchedStatementsSemicolon() throws SQLException {
Assert.assertEquals(2, updateCounts.length);
Assert.assertEquals(1, updateCounts[0]);
Assert.assertEquals(1, updateCounts[1]);
verifyInsertCount(tmpConnection, 5);
tmpConnection.commit();

assertEquals(4, retrieveSessionVariableFromServer(tmpConnection, "Com_insert") - currentInsert);

} finally {
log.debug("rewriteBatchedStatementsSemicolon end");
if (tmpConnection != null) tmpConnection.close();
Expand Down Expand Up @@ -386,6 +391,7 @@ public void rewriteBatchedStatementsUpdateTest() throws SQLException {
}
}


/**
* CONJ-152: rewriteBatchedStatements and multiple executeBatch check
* @throws SQLException
Expand Down Expand Up @@ -413,9 +419,8 @@ public void testMultipleExecuteBatch() throws SQLException {
preparedStatement.setInt(2, 3);
preparedStatement.addBatch();

preparedStatement.executeBatch();
int[] updateCounts = preparedStatement.executeBatch();
assertEquals(0, updateCounts.length);
assertEquals(2, updateCounts.length);

preparedStatement.setString(1, "executebatch3");
preparedStatement.setInt(2, 1);
Expand Down Expand Up @@ -485,10 +490,10 @@ public void updateCountTest() throws SQLException {

//Insert in prepare statement, cannot know the number og each one
Assert.assertEquals(4, insertCounts.length);
Assert.assertEquals(-2, insertCounts[0]);
Assert.assertEquals(-2, insertCounts[1]);
Assert.assertEquals(-2, insertCounts[2]);
Assert.assertEquals(-2, insertCounts[3]);
Assert.assertEquals(1, insertCounts[0]);
Assert.assertEquals(0, insertCounts[1]);
Assert.assertEquals(1, insertCounts[2]);
Assert.assertEquals(1, insertCounts[3]);


PreparedStatement sqlUpdate = tmpConnection.prepareStatement("UPDATE t4 SET test = ? WHERE test = ?");
Expand Down Expand Up @@ -538,7 +543,6 @@ public void testInsertWithLeadingConstantValue() throws Exception {
insertStmt.setString(2, "b2");
insertStmt.addBatch();
insertStmt.executeBatch();
tmpConnection.commit();
} finally {
if (tmpConnection != null) tmpConnection.close();
}
Expand All @@ -564,7 +568,6 @@ public void testInsertWithoutFirstContent() throws Exception {
insertStmt.setString(2, "b2");
insertStmt.addBatch();
insertStmt.executeBatch();
tmpConnection.commit();
} finally {
if (tmpConnection != null) tmpConnection.close();
}
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/mariadb/jdbc/MySQLDriverTest.java
Expand Up @@ -532,6 +532,7 @@ public void preparedStatementParameterReuse() throws Exception {
@Test
public void UpdateCachedRowSet() throws Exception {
Statement st = connection.createStatement();
connection.setAutoCommit(false);
st.execute("DROP TABLE IF EXISTS updatable");
st.execute("CREATE TABLE updatable(i int primary key, a varchar(10))");
st.execute("INSERT INTO updatable values(1,'a')");
Expand Down

0 comments on commit 0f78c03

Please sign in to comment.