Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[CONJ-270] correction when using prepareStatement without parameters …
…and option rewriteBatchedStatements to true
  • Loading branch information
rusher committed Apr 4, 2016
1 parent 651af72 commit 968f92b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -39,7 +39,7 @@ Development snapshot are available on sonatype nexus repository
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>1.5.0-SNAPSHOT</version>
<version>1.4.1-SNAPSHOT</version>
</dependency>
</dependencies>
```
Expand Down
Expand Up @@ -439,6 +439,7 @@ private List<String> createRewritableParts(String queryString, boolean noBacksla
boolean isFirstChar = true;
boolean isInsert = false;
boolean semicolon = false;
boolean hasValueParam = false;

char[] query = queryString.toCharArray();

Expand Down Expand Up @@ -518,6 +519,9 @@ private List<String> createRewritableParts(String queryString, boolean noBacksla
case '?':
if (state == LexState.Normal) {
isParam = true;
if (valueIndex > -1) {
hasValueParam = true;
}
if (isAfterValue) {
//having parameters after the last ")" of value is not rewritable
isRewriteable = false;
Expand Down Expand Up @@ -582,6 +586,10 @@ private List<String> createRewritableParts(String queryString, boolean noBacksla
if (state == LexState.Normal) {
isInParenthesis--;
if (isInParenthesis == 0 && valueIndex != -1 && !addPartAfterValue) {
if (!hasValueParam) {
//if no parameters, don't add pre part value
addPartPreValue = true;
}
//after the values data
isAfterValue = true;
sb.append(car);
Expand Down Expand Up @@ -637,4 +645,5 @@ private List<String> createRewritableParts(String queryString, boolean noBacksla
}
return partList;
}

}
18 changes: 18 additions & 0 deletions src/test/java/org/mariadb/jdbc/PreparedStatementTest.java
Expand Up @@ -33,6 +33,7 @@ public static void initClass() throws SQLException {
createTable("test_insert_select","`field1` varchar(20)");
createTable("test_decimal_insert", "`field1` decimal(10, 7)");
createTable("PreparedStatementTest1", "id int not null primary key auto_increment, test longblob");
createTable("PreparedStatementTest2", "my_col varchar(20)");
}

@Test
Expand Down Expand Up @@ -223,4 +224,21 @@ public void testRewriteMultiPacket() throws SQLException {
}
assertEquals(10, counter);
}


/**
* CONJ-273: permit client PrepareParameter without parameters.
* @throws Throwable
*/
@Test
public void clientPrepareStatementWithoutParameter() throws Throwable {
try (Connection connection = setConnection("&rewriteBatchedStatements=true")) {
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO PreparedStatementTest2 (my_col) VALUES ('my_val')");
preparedStatement.execute();

PreparedStatement preparedStatementMulti = connection.prepareStatement(
"INSERT INTO PreparedStatementTest2 (my_col) VALUES ('my_val1'),('my_val2')");
preparedStatementMulti.execute();
}
}
}

0 comments on commit 968f92b

Please sign in to comment.