Skip to content

Commit 68c6576

Browse files
committed
SQLParser : handle identifier escape sequence, allow keywords as words at some locations
1 parent 817d9e5 commit 68c6576

27 files changed

+327
-251
lines changed

core/src/main/antlr4/com/orientechnologies/orient/core/sql/parser/OSQL.g4

Lines changed: 113 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ UNSET : '?';
135135
NULL : N U L L ;
136136
ORID : '#';
137137

138-
TEXT : ('\'' ( ESC_SEQ | '\'\'' | ~('\\'|'\'') )* '\'')
139-
| ('"' ( ESC_SEQ | ~('\\'|'"' ) )* '"' );
138+
TEXT : ('\'' ( ESC_SEQ | '\'\'' | ~('\\'|'\'') )* '\'') ;
140139

141140
INT : DIGIT+ ;
142141
FLOAT
@@ -147,6 +146,7 @@ FLOAT
147146
;
148147

149148
WORD : LETTER (DIGIT|LETTER)* ;
149+
ESCWORD : ('"' ( ESC_SEQ | ~('\\'|'"' ) )* '"' ) ;
150150

151151

152152

@@ -181,9 +181,73 @@ UNICODE_ESC
181181
// PARSER
182182
//-----------------------------------------------------------------//
183183

184-
word : WORD ;
185-
identifier : ORID INT ':' INT;
186-
unset : UNSET | (DOUBLEDOT word);
184+
keywords
185+
: SELECT
186+
| INSERT
187+
| UPDATE
188+
| CREATE
189+
| DELETE
190+
| FROM
191+
| WHERE
192+
| INTO
193+
| DROP
194+
| FORCE
195+
| VALUES
196+
| SET
197+
| ADD
198+
| REMOVE
199+
| AND
200+
| OR
201+
| ORDER
202+
| BY
203+
| LIMIT
204+
| LIKE
205+
| RANGE
206+
| ASC
207+
| AS
208+
| DESC
209+
| OTHIS
210+
| ORID_ATTR
211+
| OCLASS_ATTR
212+
| OVERSION_ATTR
213+
| OSIZE_ATTR
214+
| OTYPE_ATTR
215+
| CLUSTER
216+
| DATABASE
217+
| PROPERTY
218+
| TRUNCATE
219+
| EXTENDS
220+
| ABSTRACT
221+
| RECORD
222+
| INDEX
223+
| DICTIONARY
224+
| ALTER
225+
| CLASS
226+
| SKIP
227+
| GRANT
228+
| REVOKE
229+
| IN
230+
| ON
231+
| TO
232+
| IS
233+
| NOT
234+
| GROUP
235+
| DATASEGMENT
236+
| LOCATION
237+
| POSITION
238+
| RUNTIME
239+
| EDGE
240+
| FUNCTION
241+
| LINK
242+
| VERTEX
243+
| TYPE
244+
| INVERSE
245+
| IDEMPOTENT
246+
| LANGUAGE
247+
;
248+
reference : WORD | ESCWORD | keywords;
249+
orid : ORID INT ':' INT;
250+
unset : UNSET | (DOUBLEDOT reference);
187251
number : (UNARY^)? (INT|FLOAT) ;
188252
map : LACCOLADE (literal DOUBLEDOT expression (COMMA literal DOUBLEDOT expression)*)? RACCOLADE ;
189253
collection : LBRACKET (expression (COMMA expression)*)? RBRACKET ;
@@ -194,16 +258,16 @@ literal
194258
;
195259

196260
arguments : LPAREN (expression (COMMA expression)*)? RPAREN ;
197-
functionCall: word arguments ;
198-
methodCall : DOT word arguments* ;
261+
functionCall: reference arguments ;
262+
methodCall : DOT reference arguments* ;
199263

200264
expression
201265
: literal
202266
| map
203267
| collection
204-
| identifier
268+
| orid
205269
| unset
206-
| word
270+
| reference
207271
| LPAREN expression RPAREN
208272
| functionCall
209273
| expression methodCall
@@ -216,7 +280,7 @@ filter
216280
: LPAREN filter RPAREN
217281
| filter filterAnd
218282
| filter filterOr
219-
| filter filterIn
283+
| expression filterIn
220284
| NOT filter
221285
| expression COMPARE_EQL expression
222286
| expression COMPARE_INF expression
@@ -237,20 +301,20 @@ anything : .*? ;
237301
commandUnknowned : expression (expression)* ;
238302

239303
commandInsertIntoByValues
240-
: INSERT INTO ((CLUSTER|INDEX) DOUBLEDOT)? word insertCluster? insertFields VALUES insertSource
304+
: INSERT INTO ((CLUSTER|INDEX) DOUBLEDOT)? reference insertCluster? insertFields VALUES insertSource
241305
;
242306
commandInsertIntoBySet
243-
: INSERT INTO ((CLUSTER|INDEX) DOUBLEDOT)? word insertCluster? SET insertSet (COMMA insertSet)*
307+
: INSERT INTO ((CLUSTER|INDEX) DOUBLEDOT)? reference insertCluster? SET insertSet (COMMA insertSet)*
244308
;
245309
insertSource
246310
: commandSelect
247311
| LPAREN insertSource RPAREN
248312
| insertEntry (COMMA insertEntry)*
249313
;
250-
insertCluster : CLUSTER word ;
314+
insertCluster : CLUSTER reference ;
251315
insertEntry : LPAREN expression (COMMA expression)* RPAREN ;
252-
insertSet : word COMPARE_EQL expression ;
253-
insertFields : LPAREN word(COMMA word)* RPAREN ;
316+
insertSet : reference COMPARE_EQL expression ;
317+
insertFields : LPAREN reference(COMMA reference)* RPAREN ;
254318

255319
commandSelect
256320
: SELECT (projection (COMMA projection)*)? from (WHERE filter)? groupBy? orderBy? skip? limit?
@@ -266,11 +330,11 @@ projection
266330
| OTYPE_ATTR )
267331
(alias)?
268332
;
269-
alias : AS word ;
333+
alias : AS reference ;
270334
from
271335
: FROM
272-
( ((CLUSTER|INDEX|DICTIONARY) DOUBLEDOT)? word
273-
| identifier
336+
( ((CLUSTER|INDEX|DICTIONARY) DOUBLEDOT)? reference
337+
| orid
274338
| collection
275339
| commandSelect
276340
| LPAREN commandSelect RPAREN )
@@ -281,40 +345,39 @@ orderByElement : expression (ASC|DESC)? ;
281345
skip : SKIP INT ;
282346
limit : LIMIT INT ;
283347

284-
commandCreateClass : CREATE CLASS word (EXTENDS word)? (CLUSTER numberOrWord(COMMA numberOrWord)*)? ABSTRACT?;
285-
numberOrWord : number | word ;
286-
commandCreateCluster : CREATE CLUSTER word word (DATASEGMENT word)? (LOCATION word)? (POSITION word)? ;
287-
commandCreateIndex : CREATE INDEX word (indexOn)? word (NULL | RUNTIME INT | (word (COMMA word)*))?;
288-
indexOn : ON word LPAREN word (COMMA word)* RPAREN ;
289-
commandCreateProperty : CREATE PROPERTY word DOT word word word?;
290-
commandCreateEdge : CREATE EDGE word? (edgeCluster)? FROM edgeEnd TO edgeEnd (SET insertSet (COMMA insertSet)*)?;
291-
edgeCluster : CLUSTER word ;
292-
edgeEnd : identifier | collection | commandSelect ;
293-
commandCreateFunction : CREATE FUNCTION word TEXT (IDEMPOTENT word)? (LANGUAGE word)? ;
294-
commandCreateLink : CREATE LINK linkName? (TYPE word)? FROM word.word TO word.word INVERSE?;
295-
linkName : word ;
296-
commandCreateVertex : CREATE VERTEX word (CLUSTER word)? (SET insertSet (COMMA insertSet)*)?;
297-
298-
commandAlterClass : ALTER CLASS word word cword ;
299-
commandAlterCluster : ALTER CLUSTER (word|number) word cword;
300-
commandAlterDatabase : ALTER DATABASE word cword;
301-
commandAlterProperty : ALTER PROPERTY word DOT word word cword ;
302-
303-
commandDropClass : DROP CLASS word ;
304-
commandDropCluster : DROP CLUSTER word ;
305-
commandDropIndex : DROP INDEX word ;
306-
commandDropProperty : DROP PROPERTY word DOT word FORCE ;
307-
308-
commandTruncateClass : TRUNCATE CLASS word ;
309-
commandTruncateCluster : TRUNCATE CLUSTER word ;
310-
commandTruncateRecord : TRUNCATE RECORD (identifier|collection) ;
311-
312-
commandGrant : GRANT word ON word TO word ;
313-
commandRevoke : REVOKE word ON word FROM word ;
348+
commandCreateClass : CREATE CLASS reference (EXTENDS reference)? (CLUSTER numberOrWord(COMMA numberOrWord)*)? ABSTRACT?;
349+
numberOrWord : number | reference ;
350+
commandCreateCluster : CREATE CLUSTER reference reference (DATASEGMENT reference)? (LOCATION reference)? (POSITION reference)? ;
351+
commandCreateIndex : CREATE INDEX reference (indexOn)? reference (NULL | RUNTIME INT | (reference (COMMA reference)*))?;
352+
indexOn : ON reference LPAREN reference (COMMA reference)* RPAREN ;
353+
commandCreateProperty : CREATE PROPERTY reference DOT reference reference reference?;
354+
commandCreateEdge : CREATE EDGE reference? (edgeCluster)? FROM edgeEnd TO edgeEnd (SET insertSet (COMMA insertSet)*)?;
355+
edgeCluster : CLUSTER reference ;
356+
edgeEnd : orid | collection | commandSelect ;
357+
commandCreateFunction : CREATE FUNCTION reference TEXT (IDEMPOTENT reference)? (LANGUAGE reference)? ;
358+
commandCreateLink : CREATE LINK linkName? (TYPE reference)? FROM reference DOT reference TO reference DOT reference INVERSE?;
359+
linkName : reference ;
360+
commandCreateVertex : CREATE VERTEX reference (CLUSTER reference)? (SET insertSet (COMMA insertSet)*)?;
361+
362+
commandAlterClass : ALTER CLASS reference reference cword ;
363+
commandAlterCluster : ALTER CLUSTER (reference|number) reference cword;
364+
commandAlterDatabase : ALTER DATABASE reference cword;
365+
commandAlterProperty : ALTER PROPERTY reference DOT reference reference cword ;
366+
367+
commandDropClass : DROP CLASS reference ;
368+
commandDropCluster : DROP CLUSTER reference ;
369+
commandDropIndex : DROP INDEX reference ;
370+
commandDropProperty : DROP PROPERTY reference DOT reference FORCE ;
371+
372+
commandTruncateClass : TRUNCATE CLASS reference ;
373+
commandTruncateCluster : TRUNCATE CLUSTER reference ;
374+
commandTruncateRecord : TRUNCATE RECORD (orid|collection) ;
375+
376+
commandGrant : GRANT reference ON reference TO reference ;
377+
commandRevoke : REVOKE reference ON reference FROM reference ;
314378

315379
command
316-
: (commandUnknowned
317-
| commandCreateClass
380+
: (commandCreateClass
318381
| commandCreateCluster
319382
| commandCreateIndex
320383
| commandCreateProperty

core/src/main/java/com/orientechnologies/orient/core/sql/command/OCommandAlterClass.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import com.orientechnologies.orient.core.metadata.security.ORole;
3232
import com.orientechnologies.orient.core.sql.OCommandSQL;
3333
import com.orientechnologies.orient.core.sql.parser.OSQLParser;
34-
import com.orientechnologies.orient.core.sql.parser.SQLGrammarUtils;
34+
import static com.orientechnologies.orient.core.sql.parser.SQLGrammarUtils.*;
3535

3636
/**
3737
* SQL ALTER PROPERTY command: Changes an attribute of an existent property in the target class.
@@ -61,12 +61,11 @@ public OCommandAlterClass parse(final OCommandRequest iRequest) {
6161
final ODatabaseRecord database = getDatabase();
6262
database.checkSecurity(ODatabaseSecurityResources.COMMAND, ORole.PERMISSION_READ);
6363

64-
final OSQLParser.CommandAlterClassContext candidate = SQLGrammarUtils
65-
.getCommand(iRequest, OSQLParser.CommandAlterClassContext.class);
66-
className = candidate.word(0).getText();
67-
final String attributeAsString = candidate.word(1).getText();
64+
final OSQLParser.CommandAlterClassContext candidate = getCommand(iRequest, OSQLParser.CommandAlterClassContext.class);
65+
className = visitAsString(candidate.reference(0));
66+
final String attributeAsString = visitAsString(candidate.reference(1));
6867
attribute = OClass.ATTRIBUTES.valueOf(attributeAsString.toUpperCase(Locale.ENGLISH));
69-
value = SQLGrammarUtils.visit(candidate.cword(), iRequest);
68+
value = visit(candidate.cword(), iRequest);
7069

7170
return this;
7271
}

core/src/main/java/com/orientechnologies/orient/core/sql/command/OCommandAlterCluster.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import com.orientechnologies.orient.core.metadata.security.ORole;
2929
import com.orientechnologies.orient.core.sql.OCommandSQLParsingException;
3030
import com.orientechnologies.orient.core.sql.parser.OSQLParser;
31-
import com.orientechnologies.orient.core.sql.parser.SQLGrammarUtils;
3231
import com.orientechnologies.orient.core.storage.OCluster;
32+
import static com.orientechnologies.orient.core.sql.parser.SQLGrammarUtils.*;
3333

3434
/**
3535
* SQL ALTER PROPERTY command: Changes an attribute of an existent property in the target class.
@@ -55,17 +55,16 @@ public OCommandAlterCluster parse(final OCommandRequest iRequest) throws OComman
5555
final ODatabaseRecord database = getDatabase();
5656
database.checkSecurity(ODatabaseSecurityResources.COMMAND, ORole.PERMISSION_READ);
5757

58-
final OSQLParser.CommandAlterClusterContext candidate = SQLGrammarUtils
59-
.getCommand(iRequest, OSQLParser.CommandAlterClusterContext.class);
58+
final OSQLParser.CommandAlterClusterContext candidate = getCommand(iRequest, OSQLParser.CommandAlterClusterContext.class);
6059

6160
if(candidate.number() != null){
62-
clusterId = SQLGrammarUtils.visit(candidate.number()).intValue();
63-
attribute = OCluster.ATTRIBUTES.valueOf(candidate.word(0).getText().toUpperCase(Locale.ENGLISH));
61+
clusterId = visit(candidate.number()).intValue();
62+
attribute = OCluster.ATTRIBUTES.valueOf(visitAsString(candidate.reference(0)).toUpperCase(Locale.ENGLISH));
6463
}else{
65-
clusterName = candidate.word(0).getText();
66-
attribute = OCluster.ATTRIBUTES.valueOf(candidate.word(1).getText().toUpperCase(Locale.ENGLISH));
64+
clusterName = visitAsString(candidate.reference(0));
65+
attribute = OCluster.ATTRIBUTES.valueOf(visitAsString(candidate.reference(1)).toUpperCase(Locale.ENGLISH));
6766
}
68-
value = SQLGrammarUtils.visit(candidate.cword(), iRequest);
67+
value = visit(candidate.cword(), iRequest);
6968

7069
return this;
7170
}

core/src/main/java/com/orientechnologies/orient/core/sql/command/OCommandAlterDatabase.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import com.orientechnologies.orient.core.metadata.security.ORole;
3030
import com.orientechnologies.orient.core.sql.OCommandSQLParsingException;
3131
import com.orientechnologies.orient.core.sql.parser.OSQLParser;
32-
import com.orientechnologies.orient.core.sql.parser.SQLGrammarUtils;
32+
import static com.orientechnologies.orient.core.sql.parser.SQLGrammarUtils.*;
3333

3434
/**
3535
* SQL ALTER DATABASE command: Changes an attribute of the current database.
@@ -53,16 +53,15 @@ public OCommandAlterDatabase parse(final OCommandRequest iRequest) {
5353
final ODatabaseRecord database = getDatabase();
5454
database.checkSecurity(ODatabaseSecurityResources.COMMAND, ORole.PERMISSION_READ);
5555

56-
final OSQLParser.CommandAlterDatabaseContext candidate = SQLGrammarUtils
57-
.getCommand(iRequest, OSQLParser.CommandAlterDatabaseContext.class);
56+
final OSQLParser.CommandAlterDatabaseContext candidate = getCommand(iRequest, OSQLParser.CommandAlterDatabaseContext.class);
5857

59-
final String attributeAsString = candidate.word().getText();
58+
final String attributeAsString = visitAsString(candidate.reference());
6059
try {
6160
attribute = ODatabase.ATTRIBUTES.valueOf(attributeAsString.toUpperCase(Locale.ENGLISH));
6261
} catch (IllegalArgumentException e) {
6362
throw new OCommandSQLParsingException("Unknown database attribute '" + attributeAsString);
6463
}
65-
value = SQLGrammarUtils.visit(candidate.cword(), iRequest);
64+
value = visit(candidate.cword(), iRequest);
6665

6766
return this;
6867
}

core/src/main/java/com/orientechnologies/orient/core/sql/command/OCommandAlterProperty.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.orientechnologies.orient.core.sql.command;
1818

1919
import java.util.Map;
20+
import java.util.Locale;
2021

2122
import com.orientechnologies.orient.core.command.OCommandDistributedReplicateRequest;
2223
import com.orientechnologies.orient.core.command.OCommandRequest;
@@ -29,8 +30,7 @@
2930
import com.orientechnologies.orient.core.metadata.security.ORole;
3031
import com.orientechnologies.orient.core.sql.OCommandSQLParsingException;
3132
import com.orientechnologies.orient.core.sql.parser.OSQLParser;
32-
import com.orientechnologies.orient.core.sql.parser.SQLGrammarUtils;
33-
import java.util.Locale;
33+
import static com.orientechnologies.orient.core.sql.parser.SQLGrammarUtils.*;
3434

3535
/**
3636
* SQL ALTER PROPERTY command: Changes an attribute of an existent property in the target class.
@@ -56,19 +56,18 @@ public OCommandAlterProperty parse(final OCommandRequest iRequest) {
5656
final ODatabaseRecord database = getDatabase();
5757
database.checkSecurity(ODatabaseSecurityResources.COMMAND, ORole.PERMISSION_READ);
5858

59-
final OSQLParser.CommandAlterPropertyContext candidate = SQLGrammarUtils
60-
.getCommand(iRequest, OSQLParser.CommandAlterPropertyContext.class);
59+
final OSQLParser.CommandAlterPropertyContext candidate = getCommand(iRequest, OSQLParser.CommandAlterPropertyContext.class);
6160

62-
className = candidate.word(0).getText();
63-
fieldName = candidate.word(1).getText();
61+
className = visitAsString(candidate.reference(0));
62+
fieldName = visitAsString(candidate.reference(1));
6463

65-
final String attributeAsString = candidate.word(2).getText();
64+
final String attributeAsString = visitAsString(candidate.reference(2));
6665
try {
6766
attribute = OProperty.ATTRIBUTES.valueOf(attributeAsString.toUpperCase(Locale.ENGLISH));
6867
} catch (IllegalArgumentException e) {
6968
throw new OCommandSQLParsingException("Unknown property attribute '" + attributeAsString);
7069
}
71-
value = SQLGrammarUtils.visit(candidate.cword(), iRequest);
70+
value = visit(candidate.cword(), iRequest);
7271
return this;
7372
}
7473

0 commit comments

Comments
 (0)