Skip to content
Permalink
Browse files
fix: IndexOutOfBounds on prepared multistatement with insert values
Re-initialize "isValuesFound and isCurrentReWriteCompatible" variables so multi-statement
prepared sql like INSERT INTO inttable(a) VALUES (?);... could be parsed properly.

Previously it identified the second statement as multi-values since isValuesFound was not reset

closes #1289
  • Loading branch information
vlsi committed Aug 15, 2018
1 parent 9534e9c commit c2885dd0cfc793f81e5dd3ed2300bb32476eb14a
@@ -176,6 +176,8 @@
bindPositions.clear();
}
nativeSql.setLength(0);
isValuesFound = false;
isCurrentReWriteCompatible = false;
valuesBraceOpenPosition = -1;
valuesBraceClosePosition = -1;
valuesBraceCloseFound = false;
@@ -256,6 +256,25 @@ public void testTrailingSpaces() throws SQLException {
pstmt.close();
}

@Test
public void testBinds() throws SQLException {
// braces around (42) are required to puzzle the parser
String query = "INSERT INTO inttable(a) VALUES (?);SELECT (42)";
PreparedStatement ps = con.prepareStatement(query);
ps.setInt(1, 100500);
ps.execute();
ResultSet rs = ps.getResultSet();
Assert.assertNull("insert produces no results ==> getResultSet should be null", rs);
Assert.assertTrue("There are two statements => getMoreResults should be true", ps.getMoreResults());
rs = ps.getResultSet();
Assert.assertNotNull("select produces results ==> getResultSet should be not null", rs);
Assert.assertTrue("select produces 1 row ==> rs.next should be true", rs.next());
Assert.assertEquals("second result of query " + query, 42, rs.getInt(1));

TestUtil.closeQuietly(rs);
TestUtil.closeQuietly(ps);
}

@Test
public void testSetNull() throws SQLException {
// valid: fully qualified type to setNull()
@@ -126,6 +126,20 @@ public void testDelete() throws SQLException {
assertEquals("This is a delete command", SqlCommandType.DELETE, query.command.getType());
}

@Test
public void testMultiQueryWithBind() throws SQLException {
// braces around (42) are required to puzzle the parser
String sql = "INSERT INTO inttable(a) VALUES (?);SELECT (42)";
List<NativeQuery> queries = Parser.parseJdbcSql(sql, true, true, true,true);
NativeQuery query = queries.get(0);
assertEquals("query(0) of " + sql,
"INSERT: INSERT INTO inttable(a) VALUES ($1)",
query.command.getType() + ": " + query.nativeSql);
query = queries.get(1);
assertEquals("query(1) of " + sql,
"SELECT: SELECT (42)",
query.command.getType() + ": " + query.nativeSql);
}

@Test
public void testMove() throws SQLException {

0 comments on commit c2885dd

Please sign in to comment.