Skip to content

Commit

Permalink
[CONJ-1213] sql command ending with semicolon and trailing space are …
Browse files Browse the repository at this point in the history
…not using bulk
  • Loading branch information
rusher committed Nov 15, 2024
1 parent b7f38ea commit 8c8200f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/main/java/org/mariadb/jdbc/util/ClientParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,21 @@ public static ClientParser parameterParts(String queryString, boolean noBackslas
}
lastChar = car;
}

// multi contains ";" not finishing statement.
boolean isMulti = multiQueryIdx != -1 && multiQueryIdx < queryLength - 1;
if (isMulti) {
// ensure there is not only empty
boolean hasAdditionalPart = false;
for (int i = multiQueryIdx + 1; i < queryLength; i++) {
byte car = query[i];
if (car != (byte) ' ' && car != (byte) '\n' && car != (byte) '\r' && car != (byte) '\t') {
hasAdditionalPart = true;
break;
}
}
isMulti = hasAdditionalPart;
}
return new ClientParser(
queryString, query, paramPositions, isInsert, isInsertDupplicate, isMulti);
}
Expand Down
38 changes: 36 additions & 2 deletions src/test/java/org/mariadb/jdbc/unit/util/ClientParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@

import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mariadb.jdbc.util.ClientParser;

@SuppressWarnings("ConstantConditions")
public class ClientParserTest {

private void parse(
String sql, String[] expected, String[] expectedNoBackSlash, boolean isInsertDuplicate) {
String sql,
String[] expected,
String[] expectedNoBackSlash,
boolean isInsertDuplicate,
boolean isMulti) {
ClientParser parser = ClientParser.parameterParts(sql, false);
assertEquals(expected.length, parser.getParamCount() + 1, displayErr(parser, expected));

Expand Down Expand Up @@ -41,6 +47,7 @@ private void parse(
new String(parser.getQuery(), pos, paramPos - pos));

assertEquals(isInsertDuplicate, parser.isInsertDuplicate());
assertEquals(isMulti, parser.isMultiQuery());
}

private String displayErr(ClientParser parser, String[] exp) {
Expand Down Expand Up @@ -70,34 +77,61 @@ public void ClientParser() {
"SELECT '\\\\test' /*test* #/ ;`*/",
new String[] {"SELECT '\\\\test' /*test* #/ ;`*/"},
new String[] {"SELECT '\\\\test' /*test* #/ ;`*/"},
false,
false);
parse(
"DO '\\\"', \"\\'\"",
new String[] {"DO '\\\"', \"\\'\""},
new String[] {"DO '\\\"', \"\\'\""},
false,
false);
parse(
"INSERT INTO TABLE(id,val) VALUES (1,2)",
new String[] {"INSERT INTO TABLE(id,val) VALUES (1,2)"},
new String[] {"INSERT INTO TABLE(id,val) VALUES (1,2)"},
false,
false);
parse(
"INSERT INTO TABLE(id,val) VALUES (1,2) ON DUPLICATE KEY UPDATE",
new String[] {"INSERT INTO TABLE(id,val) VALUES (1,2) ON DUPLICATE KEY UPDATE"},
new String[] {"INSERT INTO TABLE(id,val) VALUES (1,2) ON DUPLICATE KEY UPDATE"},
true);
true,
false);
parse(
"INSERT INTO TABLE(id,val) VALUES (1,2) ON DUPLICATE",
new String[] {"INSERT INTO TABLE(id,val) VALUES (1,2) ON DUPLICATE"},
new String[] {"INSERT INTO TABLE(id,val) VALUES (1,2) ON DUPLICATE"},
false,
false);
parse(
"INSERT INTO TABLE(id,val) VALUES (1,2) ONDUPLICATE",
new String[] {"INSERT INTO TABLE(id,val) VALUES (1,2) ONDUPLICATE"},
new String[] {"INSERT INTO TABLE(id,val) VALUES (1,2) ONDUPLICATE"},
false,
false);
}

@ParameterizedTest()
@ValueSource(
strings = {
"DO 1; SELECT 1",
"DO 1;\nDO 2",
})
public void MultiQueryParser(String sql) {
assertTrue(ClientParser.parameterParts(sql, false).isMultiQuery());
}

@ParameterizedTest()
@ValueSource(
strings = {
"DO 1; ",
"DO 1; \n ",
"DO 1;"
})
public void NonMultiQueryParser(String sql) {
assertFalse(ClientParser.parameterParts(sql, false).isMultiQuery());
}

@Test
public void ClientParserInsertFlag() {
assertFalse(ClientParser.parameterParts("WRONG INSERT_COMMAND", true).isInsert());
Expand Down

0 comments on commit 8c8200f

Please sign in to comment.