Skip to content

Commit

Permalink
0002187: auto create tables doesn't support comment characters in quo…
Browse files Browse the repository at this point in the history
…ted column and table names
  • Loading branch information
chenson42 committed Feb 11, 2015
1 parent 73111ca commit 0927b7f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
38 changes: 16 additions & 22 deletions symmetric-db/src/main/java/org/jumpmind/db/sql/SqlScriptReader.java
Expand Up @@ -127,30 +127,11 @@ protected String prepareForExecute(StringBuilder sql) {
}

protected String trimComments(String line) {
int inLiteralStart = -1;
int inLiteralEnd = -1;
char[] content = line.toCharArray();
for (int i = 0; i < line.length(); i++) {
if (inLiteralStart == -1 && content[i] == '\'') {
inLiteralStart = i;
for (int j = inLiteralStart + 1; j < line.length(); j++) {
if (content[j] == '\'') {
if (j + 1 < content.length && content[j + 1] == '\'') {
j++;
} else {
inLiteralEnd = j + 1;
break;
}
}
}
}

if (inLiteralEnd == i) {
inLiteralEnd = -1;
inLiteralStart = -1;
}

if (inLiteralStart == -1) {
if (!betweenOccurences('\'', i, content)
&& !betweenOccurences('"', i, content)
&& !betweenOccurences('`', i, content)) {
for (char c : COMMENT_CHARS) {
if (c == content[i]) {
if (i + 1 < content.length && content[i + 1] == c) {
Expand All @@ -166,6 +147,19 @@ protected String trimComments(String line) {
}
return line;
}

protected boolean betweenOccurences(char q, int i, char[] content) {
boolean startFound = false;
for (int j = 0; j < content.length; j++) {
if (content[j] == q)
if (j < i) {
startFound = !startFound;
} else if (j > i) {
return startFound;
}
}
return false;
}

protected boolean checkStatementEnds(String s) {
return s.trim().endsWith("" + delimiter);
Expand Down
Expand Up @@ -41,6 +41,7 @@ public void testReadScript() throws Exception {
assertEquals("update something set oops=';' where whoops='test'", reader.readSqlStatement());
assertEquals("update test set one = '''', two='\\\\##--''' where one is null", reader.readSqlStatement());
assertEquals("update test\n set one = '1', two = '2'\nwhere one = 'one'", reader.readSqlStatement());
assertEquals("create table \"TE--ST\" (\"ID##2\" VARCHAR(100))", reader.readSqlStatement());
assertNull(reader.readSqlStatement());
reader.close();
}
Expand Down
3 changes: 2 additions & 1 deletion symmetric-db/src/test/resources/test-script-1.sql
Expand Up @@ -37,4 +37,5 @@ update test set one = '''', two='\\##--''' where one is null; // comment
update test
set one = '1', two = '2'
where one = 'one';
## last comment
## last comment
create table "TE--ST" ("ID##2" VARCHAR(100));

0 comments on commit 0927b7f

Please sign in to comment.