Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[CONJ-283] MariaDbClientPreparedStatement : add new test for semicolo…
…n and temporary disable 5.5 (waiting for 5.5.49 released to permit Travis installation)
  • Loading branch information
rusher committed Apr 22, 2016
1 parent 3385a01 commit b65cfea
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 53 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Expand Up @@ -7,9 +7,6 @@ jdk:
- oraclejdk8
- oraclejdk7
env:
- MARIA_VERSION=5.5 MAX_ALLOWED_PACKET=8M INNODB_LOG_FILE_SIZE=80M
- MARIA_VERSION=5.5 MAX_ALLOWED_PACKET=20M INNODB_LOG_FILE_SIZE=200M
- MARIA_VERSION=5.5 MAX_ALLOWED_PACKET=40M INNODB_LOG_FILE_SIZE=400M
- MARIA_VERSION=10.0 MAX_ALLOWED_PACKET=8M INNODB_LOG_FILE_SIZE=80M
- MARIA_VERSION=10.0 MAX_ALLOWED_PACKET=20M INNODB_LOG_FILE_SIZE=200M
- MARIA_VERSION=10.0 MAX_ALLOWED_PACKET=40M INNODB_LOG_FILE_SIZE=400M
Expand Down
79 changes: 36 additions & 43 deletions src/main/java/org/mariadb/jdbc/MariaDbClientPreparedStatement.java
Expand Up @@ -398,25 +398,37 @@ public String toString() {
}

/**
* Create query part if query rewritable
* Separate query in a String list and set flag reWritablePrepare
* The parameters "?" (not in comments) emplacements are to be known.
*
* Rewritable query part :
* The only rewritten queries follow these notation:
* INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name [PARTITION (partition_list)] [(col,...)]
* {VALUES | VALUE} (...) [ ON DUPLICATE KEY UPDATE col=expr [, col=expr] ... ]
* With expr without parameter.
*
* INSERT ... SELECT will not be rewritten.
*
* String list :
*
* - pre value part
* - first assign part
* for each parameters :
* - part after parameter
* - after value part
* - After value and first parameter part
* - for each parameters :
* - part after parameter and before last parenthesis
* - Last query part
*
* example : INSERT INTO TABLE(col1,col2,col3,col4, col5) VALUES (9, ?, 5, ?, 8) ON DUPLICATE KEY UPDATE col2=col2+10
*
* - pre value part : INSERT INTO TABLE(col1,col2,col3,col4, col5) VALUES
* - first assign part : "(9 "
* - after value part : " (9 "
* - part after parameter 1: ", 5,"
* - ", 5,"
* - ",8)"
* - last part : ON DUPLICATE KEY UPDATE col2=col2+10
*
* With 2 series of parameters, this query will be rewritten like
* [INSERT INTO TABLE(col1,col2,col3,col4, col5) VALUES][ (9, param0_1, 5, param0_2, 8)][, (9, param1_1, 5, param1_2, 8)][ ON DUPLICATE
* KEY UPDATE col2=col2+10]
*
* @param queryString query String
* @param noBackslashEscapes must backslash be escaped.
* @return List of query part.
Expand Down Expand Up @@ -455,9 +467,7 @@ private List<String> createRewritableParts(String queryString, boolean noBacksla
char car = query[i];
switch (car) {
case '*':
if (state == LexState.Normal && lastChar == '/') {
state = LexState.SlashStarComment;
}
if (state == LexState.Normal && lastChar == '/') state = LexState.SlashStarComment;
break;
case '/':
if (state == LexState.SlashStarComment && lastChar == '*') {
Expand All @@ -468,21 +478,15 @@ private List<String> createRewritableParts(String queryString, boolean noBacksla
break;

case '#':
if (state == LexState.Normal) {
state = LexState.EOLComment;
}
if (state == LexState.Normal) state = LexState.EOLComment;
break;

case '-':
if (state == LexState.Normal && lastChar == '-') {
state = LexState.EOLComment;
}
if (state == LexState.Normal && lastChar == '-') state = LexState.EOLComment;
break;

case '\n':
if (state == LexState.EOLComment) {
state = LexState.Normal;
}
if (state == LexState.EOLComment) state = LexState.Normal;
break;

case '"':
Expand Down Expand Up @@ -511,9 +515,7 @@ private List<String> createRewritableParts(String queryString, boolean noBacksla
if (noBackslashEscapes) {
break;
}
if (state == LexState.String) {
state = LexState.Escape;
}
if (state == LexState.String) state = LexState.Escape;
break;

case '?':
Expand Down Expand Up @@ -591,9 +593,7 @@ private List<String> createRewritableParts(String queryString, boolean noBacksla
}
break;
case '(':
if (state == LexState.Normal) {
isInParenthesis++;
}
if (state == LexState.Normal) isInParenthesis++;
break;
case ')':
if (state == LexState.Normal) {
Expand All @@ -608,15 +608,11 @@ private List<String> createRewritableParts(String queryString, boolean noBacksla
break;
default:
if (state == LexState.Normal && isFirstChar && ((byte) car >= 40)) {
if (car == 'I' || car == 'i') {
isInsert = true;
}
if (car == 'I' || car == 'i') isInsert = true;
isFirstChar = false;
}
if (state == LexState.Normal && semicolon && ((byte) lastChar >= 40)) {
//multiple queries
reWritablePrepare = false;
}
//multiple queries
if (state == LexState.Normal && semicolon && ((byte) lastChar >= 40)) reWritablePrepare = false;
break;
}

Expand All @@ -627,22 +623,19 @@ private List<String> createRewritableParts(String queryString, boolean noBacksla
sb.append(car);
}
}

partList.add(0, (preValuePart1 == null) ? "" : preValuePart1);
if (!hasParam) {
//permit to have rewrite without parameter
partList.add(1, sb.toString());
sb.setLength(0);
} else {
partList.add(1, (preValuePart2 == null) ? "" : preValuePart2);
}
if (!isInsert) {
reWritablePrepare = false;
}
if (hasParam) {
//postValuePart is the value after the last parameter and parenthesis
//if no param, don't add to the list.
partList.add((postValuePart == null) ? "" : postValuePart);
}
} else partList.add(1, (preValuePart2 == null) ? "" : preValuePart2);

if (!isInsert) reWritablePrepare = false;

//postValuePart is the value after the last parameter and parenthesis
//if no param, don't add to the list.
if (hasParam) partList.add((postValuePart == null) ? "" : postValuePart);
partList.add(sb.toString());
return partList;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/mariadb/jdbc/MariaDbStatement.java
Expand Up @@ -86,7 +86,7 @@ public class MariaDbStatement implements Statement, Cloneable {
*/
protected MariaDbConnection connection;
protected Future<?> timerTaskFuture;
protected boolean isRewriteable = true;
private boolean isRewriteable = true;
protected int rewriteOffset = -1;
protected ResultSet batchResultSet = null;
protected volatile boolean closed = false;
Expand Down
Expand Up @@ -27,12 +27,13 @@ public void testRewritableWithConstantParameter() throws SQLException {
checkParsing("INSERT INTO TABLE(col1,col2,col3,col4, col5) VALUES (9, ?, 5, ?, 8) ON DUPLICATE KEY UPDATE col2=col2+10",
2, true,
new String[] {
"INSERT INTO TABLE(col1,col2,col3,col4, col5) VALUES",
" (9, ",
", 5, ",
", 8)",
" ON DUPLICATE KEY UPDATE col2=col2+10"});
"INSERT INTO TABLE(col1,col2,col3,col4, col5) VALUES",
" (9, ",
", 5, ",
", 8)",
" ON DUPLICATE KEY UPDATE col2=col2+10"});
}

@Test
public void testComment() throws SQLException {
checkParsing("/* insert Select INSERT INTO tt VALUES (?,?,?,?) */"
Expand Down Expand Up @@ -156,6 +157,28 @@ public void testSemiColon() throws SQLException {
"; INSERT INTO tt (tt) VALUES ('multiple')"});
}

@Test
public void testSemicolonRewritableIfAtEnd() throws SQLException {
checkParsing("INSERT INTO table (column1) VALUES (?); ",
1, true,
new String[] {
"INSERT INTO table (column1) VALUES",
" (",
")",
"; "});
}

@Test
public void testSemicolonNotRewritableIfNotAtEnd() throws SQLException {
checkParsing("INSERT INTO table (column1) VALUES (?); SELECT 1",
1, false,
new String[] {
"INSERT INTO table (column1) VALUES",
" (",
")",
"; SELECT 1"});
}

@Test
public void testError() throws SQLException {
checkParsing("INSERT INTO tt (tt) VALUES (?); INSERT INTO tt (tt) VALUES ('multiple')",
Expand Down
4 changes: 3 additions & 1 deletion src/test/resources/style.xml
Expand Up @@ -38,7 +38,9 @@
<property name="option" value="TEXT"/>
<property name="tokens" value="LITERAL_TRY, LITERAL_FINALLY, LITERAL_IF, LITERAL_ELSE, LITERAL_SWITCH"/>
</module>
<module name="NeedBraces"/>
<module name="NeedBraces">
<property name="allowSingleLineStatement" value="true"/>
</module>
<module name="LeftCurly">
<property name="maxLineLength" value="150"/>
</module>
Expand Down

0 comments on commit b65cfea

Please sign in to comment.