Skip to content

Commit

Permalink
[CONJ-168] Rewritten batch insert can fail if the first value isn't a…
Browse files Browse the repository at this point in the history
… parameter
  • Loading branch information
rusher committed Jul 21, 2015
1 parent 7b629cd commit 58b62cc
Show file tree
Hide file tree
Showing 5 changed files with 467 additions and 406 deletions.
5 changes: 3 additions & 2 deletions src/main/java/org/mariadb/jdbc/MySQLPreparedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ public MySQLPreparedStatement(MySQLConnection connection,
if(log.isDebugEnabled()) {
log.debug("Creating prepared statement for " + sql);
}
isInsertRewriteable(sql);
dQuery = new MySQLParameterizedQuery(Utils.nativeSQL(sql, connection.noBackslashEscapes),
connection.noBackslashEscapes);
connection.noBackslashEscapes, isRewriteable?firstRewrite.length():-1);
parametersCleared = true;
}

Expand Down Expand Up @@ -194,7 +195,7 @@ public void addBatch(final String sql) throws SQLException {

private void checkBatchFields() {
if (batchQueries == null) {
batchQueries = new ArrayList<Query>();
batchQueries = new ArrayList<>();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/mariadb/jdbc/MySQLStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ public int getResultSetType() throws SQLException {
*/
public void addBatch(final String sql) throws SQLException {
if (batchQueries == null) {
batchQueries = new ArrayList<Query>();
batchQueries = new ArrayList<>();
}
isInsertRewriteable(sql);
batchQueries.add(new MySQLQuery(sql));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,18 @@ public class MySQLParameterizedQuery implements ParameterizedQuery {
private int paramCount;
private String query;
private byte[][] queryPartsArray;
private byte[] rewriteFirstPart = null;

public MySQLParameterizedQuery(String query, boolean noBackslashEscapes) {
public MySQLParameterizedQuery(String query, boolean noBackslashEscapes, int rewriteOffset) {
this.query = query;
List<String> queryParts = createQueryParts(query, noBackslashEscapes);
if (rewriteOffset != -1) {
try {
rewriteFirstPart = queryParts.get(0).substring(rewriteOffset + 1).getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("UTF-8 not supported", e);
}
}
queryPartsArray = new byte[queryParts.size()][];
for(int i=0;i < queryParts.size(); i++) {
try {
Expand All @@ -95,6 +103,7 @@ public MySQLParameterizedQuery cloneQuery() {
q.paramCount = paramCount;
q.query = query;
q.queryPartsArray = queryPartsArray;
q.rewriteFirstPart = rewriteFirstPart;
return q;
}

Expand Down Expand Up @@ -138,7 +147,8 @@ public void writeToRewritablePart(final OutputStream os, int rewriteOffset) thro
if(queryPartsArray.length == 0) {
throw new AssertionError("Invalid query, queryParts was empty");
}
os.write(",(".getBytes());
os.write((",(").getBytes());
os.write(rewriteFirstPart);
for(int i = 1; i<queryPartsArray.length; i++) {
parameters[i-1].writeTo(os);
if(queryPartsArray[i].length != 0)
Expand Down
Loading

0 comments on commit 58b62cc

Please sign in to comment.