@@ -23,7 +23,12 @@ OPEN_PAREN = "("
23
23
CLOSE_PAREN = ")"
24
24
OPEN_COMMENT = "/*"
25
25
CLOSE_COMMENT = "*/"
26
- IDENTIFIER = ([:letter:] | "_" ) ([:letter:] | [ 0- 9] | [ _.] )*
26
+ UNQUOTED_IDENTIFIER = ([:letter:] | "_" ) ([:letter:] | [ 0- 9] | "_" )*
27
+ IDENTIFIER_PART = {UNQUOTED_IDENTIFIER} | {DOUBLE_QUOTED_STR} | {BACKTICK_QUOTED_STR}
28
+ // We are using {UNQUOTED_IDENTIFIER} instead of {IDENTIFIER_PART} here because DOUBLE_QUOTED_STR
29
+ // and BACKTICK_QUOTED_STR are handled separately. Depending on the context they appear in they will
30
+ // either be recorded as the identifier or replaced with ?.
31
+ IDENTIFIER = {UNQUOTED_IDENTIFIER} | ( {IDENTIFIER_PART} ( "." {IDENTIFIER_PART} )+)
27
32
BASIC_NUM = [ .+-] * [ 0- 9] ( [ 0- 9] | [ eE.+-] )*
28
33
HEX_NUM = "0x" ( [ a- f] | [ A- F] | [ 0- 9] )+
29
34
QUOTED_STR = "'" ( "''" | [^ '] )* "'"
@@ -69,12 +74,31 @@ WHITESPACE = [ \t\r\n]+
69
74
return builder. length() > LIMIT ;
70
75
}
71
76
77
+ private String removeQuotes(String identifierName, String quote) {
78
+ // remove quotes from the start and end of the identifier ("table" is transformed to table), if
79
+ // identifier contains quote anywhere else besides start and end leave it as is (quotes are not
80
+ // removed from "schema"."table")
81
+ if (identifierName. startsWith(quote) && identifierName. endsWith(quote)) {
82
+ String s = identifierName. substring(1 , identifierName. length() - 1 );
83
+ if (! s. contains(quote)) {
84
+ return s;
85
+ }
86
+ }
87
+ return identifierName;
88
+ }
89
+
72
90
/* * @return text matched by current token without enclosing double quotes or backticks */
73
91
private String readIdentifierName() {
74
92
String identifierName = yytext();
75
- if (identifierName != null && ((identifierName. startsWith(" \" " ) && identifierName. endsWith(" \" " ))
76
- || (identifierName. startsWith(" `" ) && identifierName. endsWith(" `" )))) {
77
- identifierName = identifierName. substring(1 , identifierName. length() - 1 );
93
+ if (identifierName != null ) {
94
+ String result = removeQuotes(identifierName, " \" " );
95
+ if (! result. equals(identifierName)) {
96
+ return result;
97
+ }
98
+ result = removeQuotes(identifierName, " `" );
99
+ if (! result. equals(identifierName)) {
100
+ return result;
101
+ }
78
102
}
79
103
return identifierName;
80
104
}
0 commit comments