Skip to content

Commit

Permalink
Merge pull request #2949 from liquibase/fix-quote-parsing
Browse files Browse the repository at this point in the history
Improved parsing of single-quoted strings
  • Loading branch information
nvoxland committed Jun 22, 2022
2 parents ad4fe6c + dbf9bc2 commit 341e7a6
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 36 deletions.
11 changes: 9 additions & 2 deletions liquibase-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,16 @@

<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<groupId>org.javacc.plugin</groupId>
<artifactId>javacc-maven-plugin</artifactId>
<version>2.6</version>
<version>3.0.3</version>
<dependencies>
<dependency>
<groupId>net.java.dev.javacc</groupId>
<artifactId>javacc</artifactId>
<version>7.0.11</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>javacc</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ TOKEN:
| < S_IDENTIFIER: ( <LETTER> | <UNICODE_LETTERS> )+ ( <DIGIT> | <LETTER> | <UNICODE_LETTERS> | <SPECIAL_CHARS> )* >
| < #LETTER: ["a"-"z", "A"-"Z", "_", "$"] >
| < #SPECIAL_CHARS: "$" | "_" | "#" | "@" >
| < S_CHAR_LITERAL: "'" (~["'"]|"\\'")* "'" ("'" (~["'"]|"\\'")* "'")*>
| < S_CHAR_LITERAL: (["U","E","N","R","B"]|"RB"|"_utf8")? (("'" ( <ESC> | ~["'", "\\", "\n", "\r"] )* "'") | ("'" ("''" | ~["'"])* "'")) >
| < S_QUOTED_IDENTIFIER: "\"" (~["\n","\r","\""])+ "\"" | ("`" (~["\n","\r","`"])+ "`") | ( "[" ~["0"-"9","]"] (~["\n","\r","]"])* "]" ) >
| <EMPTY_QUOTE: "\"" "\"">
| < #ESC: "\\" ["n","t","b","r","f","\\","'","\""] >

/*
Built list from http://stackoverflow.com/a/37668315/45756
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class SqlParserTest extends Specification {
"a ~ b" | ["a", "~", "b"]
"a > b" | ["a", ">", "b"]
"a <> b" | ["a", "<", ">", "b"]
"a != '\\\\' here" | ["a", "!", "=", "'\\\\'", "here"]

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class StringUtilTest extends Specification {
that Arrays.asList(StringUtil.processMultiLineSQL(rawString, stripComments, splitStatements, endDelimiter)), Matchers.contains(expected.toArray())

where:
stripComments | splitStatements | endDelimiter | rawString | expected
stripComments | splitStatements | endDelimiter | rawString | expected
true | true | null | "/**\nSome comments go here\n**/\ncreate table sqlfilerollback (id int);\n\n/**\nSome morecomments go here\n**/\ncreate table sqlfilerollback2 (id int);" | ["create table sqlfilerollback (id int)", "create table sqlfilerollback2 (id int)"]
true | true | null | "/*\nThis is a test comment of MS-SQL script\n*/\n\nSelect * from Test;\nUpdate Test set field = 1" | ["Select * from Test", "Update Test set field = 1"]
true | true | null | "some sql/*Some text\nmore text*/more sql" | ["some sqlmore sql"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class SimpleSqlGrammarTest extends Specification {
@Unroll
def test() {
when:
def grammar = new SimpleSqlGrammar(new ByteArrayInputStream(input.getBytes()))
def tokenManager = new SimpleSqlGrammarTokenManager(new SimpleCharStream(new StringReader(input)));
def grammar = new SimpleSqlGrammar(tokenManager)

def tokens = new ArrayList<String>()
Token token
Expand All @@ -20,17 +21,27 @@ class SimpleSqlGrammarTest extends Specification {
tokens == expected

where:
input | expected
"" | []
"sql goes here" | ["sql", " ", "goes", " ", "here"]
" odd spacing stuff " | [" ", "odd", " ", "spacing", " ", "stuff", " "]
"create table test" | ["create", " ", "table", " ", "test"]
"create table catalog.schema.test" | ["create", " ", "table", " ", "catalog.schema.test"]
"create table [test]" | ["create", " ", "table", " ", "[test]"]
"create table \"test\"" | ["create", " ", "table", " ", "\"test\""]
"create table /* comment here */ test" | ["create", " ", "table", " ", "/* comment here */", " ", "test"]
"insert 'a string'" | ["insert", " ", "'a string'"]
"invalid ' sql" | ["invalid", " ", "'", " ", "sql"]
"utf8-〠@chars works" | ["utf8", "-", "〠@chars", " ", "works"]
input | expected
"" | []
"sql goes here" | ["sql", " ", "goes", " ", "here"]
" odd spacing stuff " | [" ", "odd", " ", "spacing", " ", "stuff", " "]
"create table test" | ["create", " ", "table", " ", "test"]
"create table catalog.schema.test" | ["create", " ", "table", " ", "catalog.schema.test"]
"create table [test]" | ["create", " ", "table", " ", "[test]"]
"create table \"test\"" | ["create", " ", "table", " ", "\"test\""]
"create table /* comment here */ test" | ["create", " ", "table", " ", "/* comment here */", " ", "test"]
"insert 'a string'" | ["insert", " ", "'a string'"]
"escaped quotes ' '' '" | ["escaped", " ", "quotes", " ", "' '' '"]
"escaped quotes ''''" | ["escaped", " ", "quotes", " ", "''''"]
"mysql escaped quotes ' \\' '" | ["mysql", " ", "escaped", " ", "quotes", " ", "' \\' '"]
"mysql escaped quotes '\\''" | ["mysql", " ", "escaped", " ", "quotes", " ", "'\\''"]
"invalid ' sql" | ["invalid", " ", "'", " ", "sql"]
"'invalid' ' sql" | ["'invalid'", " ", "'", " ", "sql"]
"utf8-〠@chars works" | ["utf8", "-", "〠@chars", " ", "works"]
"single '\\' works" | ["single", " ", "'\\'", " ", "works"]
"double '\\\\' works" | ["double", " ", "'\\\\'", " ", "works"]
"unquoted \\\\ works" | ["unquoted", " ", "\\", "\\", " ", "works"]
"'one quote' then 'two quote' then 'three quote' more" | ["'one quote'", " ", "then", " ", "'two quote'", " ", "then", " ", "'three quote'", " ", "more"]
"'\\\\' then quotes '')" | ["'\\\\'", " ", "then", " ", "quotes", " ", "''", ")"]
}
}
19 changes: 0 additions & 19 deletions liquibase-extension-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,6 @@
</resources>

<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>javacc-maven-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>javacc</id>
<goals>
<goal>javacc</goal>
</goals>
<configuration>
<javaUnicodeEscape>false</javaUnicodeEscape>
<unicodeInput>true</unicodeInput>
</configuration>
</execution>
</executions>
</plugin>


<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
Expand Down

0 comments on commit 341e7a6

Please sign in to comment.