Skip to content

Commit 37cd9fc

Browse files
committed
SQLParser : use registered methods
1 parent 30a4f86 commit 37cd9fc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+950
-612
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.orientechnologies.orient.core.sql.parser.OSQL;
3838
import com.orientechnologies.orient.core.sql.parser.OSQLParser;
3939
import com.orientechnologies.orient.core.sql.parser.SQLGrammarUtils;
40+
import com.orientechnologies.orient.core.sql.parser.SyntaxException;
4041
import com.orientechnologies.orient.core.sql.parser.UnknownResolverVisitor;
4142
import java.util.ArrayList;
4243
import java.util.HashMap;
@@ -78,7 +79,11 @@ public <RET extends OCommandExecutor> RET parse(OCommandRequest iRequest) {
7879
final String sql = ((OCommandRequestText) iRequest).getText();
7980
final ParseTree tree = OSQL.compileExpression(sql);
8081
if(tree instanceof OSQLParser.CommandContext){
81-
visit((OSQLParser.CommandContext)tree);
82+
try {
83+
visit((OSQLParser.CommandContext)tree);
84+
} catch (SyntaxException ex) {
85+
throw new OException(ex.getMessage(), ex);
86+
}
8287
}else{
8388
throw new OException("Parse error, query is not a valid INSERT INTO.");
8489
}
@@ -234,7 +239,7 @@ public String getSyntax() {
234239
// GRAMMAR PARSING ///////////////////////////////////////////////////////////
235240

236241

237-
private void visit(OSQLParser.CommandContext candidate) {
242+
private void visit(OSQLParser.CommandContext candidate) throws SyntaxException {
238243
final Object commandTree = candidate.getChild(0);
239244
if(commandTree instanceof OSQLParser.CommandInsertIntoByValuesContext){
240245
visit((OSQLParser.CommandInsertIntoByValuesContext)commandTree);
@@ -245,7 +250,7 @@ private void visit(OSQLParser.CommandContext candidate) {
245250
}
246251
}
247252

248-
private void visit(OSQLParser.CommandInsertIntoByValuesContext candidate){
253+
private void visit(OSQLParser.CommandInsertIntoByValuesContext candidate) throws SyntaxException{
249254
//variables
250255
String target;
251256
final List<String> fields = new ArrayList<String>();
@@ -281,7 +286,7 @@ private void visit(OSQLParser.CommandInsertIntoByValuesContext candidate){
281286
this.newRecords = entries;
282287
}
283288

284-
private void visit(OSQLParser.CommandInsertIntoBySetContext candidate){
289+
private void visit(OSQLParser.CommandInsertIntoBySetContext candidate) throws SyntaxException{
285290
//variables
286291
final String target;
287292
final List<String> fields = new ArrayList<String>();

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
import com.orientechnologies.orient.core.sql.parser.OSQL;
3535
import com.orientechnologies.orient.core.sql.parser.OSQLParser;
3636
import com.orientechnologies.orient.core.sql.parser.SQLGrammarUtils;
37+
import com.orientechnologies.orient.core.sql.parser.SyntaxException;
3738
import com.orientechnologies.orient.core.sql.parser.UnknownResolverVisitor;
3839
import com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery;
39-
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
4040
import java.util.ArrayList;
4141
import java.util.List;
4242
import java.util.Map;
@@ -75,7 +75,11 @@ public <RET extends OCommandExecutor> RET parse(OCommandRequest iRequest) {
7575
System.err.println("|||||||||||||||||||| "+ sql);
7676
final ParseTree tree = OSQL.compileExpression(sql);
7777
if(tree instanceof OSQLParser.CommandContext){
78-
visit((OSQLParser.CommandContext)tree);
78+
try {
79+
visit((OSQLParser.CommandContext)tree);
80+
} catch (SyntaxException ex) {
81+
throw new OException(ex.getMessage(), ex);
82+
}
7983
}else{
8084
throw new OException("Parse error, query is not a valid INSERT INTO.");
8185
}
@@ -171,7 +175,7 @@ public Object execute(final Map<Object, Object> iArgs) {
171175

172176
// GRAMMAR PARSING ///////////////////////////////////////////////////////////
173177

174-
private void visit(OSQLParser.CommandContext candidate) {
178+
private void visit(OSQLParser.CommandContext candidate) throws SyntaxException {
175179
final Object commandTree = candidate.getChild(0);
176180
if(commandTree instanceof OSQLParser.CommandSelectContext){
177181
visit((OSQLParser.CommandSelectContext)commandTree);
@@ -180,7 +184,7 @@ private void visit(OSQLParser.CommandContext candidate) {
180184
}
181185
}
182186

183-
private void visit(OSQLParser.CommandSelectContext candidate){
187+
private void visit(OSQLParser.CommandSelectContext candidate) throws SyntaxException{
184188
//variables
185189
projections = new ArrayList<OExpression>();
186190
setLimit(-1);

core/src/main/java/com/orientechnologies/orient/core/sql/filter/OSQLFilterItemAbstract.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,10 @@ public Object transformValue(final OIdentifiable iRecord, final OCommandContext
103103
// APPLY OPERATIONS FOLLOWING THE STACK ORDER
104104
OSQLMethod operator = null;
105105

106-
try {
107106
for (OPair<OSQLMethod, Object[]> op : operationsChain) {
108107
operator = op.getKey();
109-
ioResult = operator.execute(iRecord, null, ioResult, op.getValue());
108+
ioResult = operator.evaluate(iContext, iRecord);
110109
}
111-
} catch (ParseException e) {
112-
OLogManager.instance().exception("Error on conversion of value '%s' using field operator %s", e,
113-
OCommandExecutionException.class, ioResult, operator.getName());
114-
}
115110
}
116111

117112
return ioResult;

core/src/main/java/com/orientechnologies/orient/core/sql/method/OSQLMethod.java

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,28 @@
1515
*/
1616
package com.orientechnologies.orient.core.sql.method;
1717

18-
import java.text.ParseException;
19-
20-
import com.orientechnologies.orient.core.command.OCommandContext;
21-
import com.orientechnologies.orient.core.db.record.OIdentifiable;
18+
import com.orientechnologies.orient.core.sql.model.OExpression;
19+
import com.orientechnologies.orient.core.sql.model.OExpressionVisitor;
20+
import com.orientechnologies.orient.core.sql.model.OFunction;
21+
import java.util.ArrayList;
22+
import java.util.Collections;
23+
import java.util.List;
2224

2325
/**
2426
* Methods can be used on various objects with different number of arguments. SQL syntax : <object_name>.<method_name>([parameters])
2527
*
2628
* @author Johann Sorel (Geomatys)
2729
*/
28-
public interface OSQLMethod extends Comparable<OSQLMethod> {
30+
public abstract class OSQLMethod extends OFunction implements Comparable<OSQLMethod> {
2931

30-
/**
31-
* @return method name
32-
*/
33-
String getName();
32+
public OSQLMethod(String name, List<OExpression> arguments) {
33+
super(name, arguments);
34+
}
3435

36+
public OSQLMethod(String name, String alias, List<OExpression> arguments) {
37+
super(name, alias, arguments);
38+
}
39+
3540
/**
3641
* Returns a convinient SQL String representation of the method.
3742
* <p>
@@ -45,32 +50,39 @@ public interface OSQLMethod extends Comparable<OSQLMethod> {
4550
*
4651
* @return String , never null.
4752
*/
48-
public String getSyntax();
53+
public abstract String getSyntax();
4954

5055
/**
5156
* @return minimum number of arguments requiered by this method
5257
*/
53-
int getMinParams();
58+
public abstract int getMinParams();
5459

5560
/**
5661
* @return maximum number of arguments requiered by this method
5762
*/
58-
int getMaxParams();
63+
public abstract int getMaxParams();
5964

60-
/**
61-
* Process a record.
62-
*
63-
* @param iCurrentRecord
64-
* : current record
65-
* @param iContext
66-
* execution context
67-
* @param ioResult
68-
* : field value
69-
* @param iMethodParams
70-
* : function parameters, number is ensured to be within minParams and maxParams.
71-
* @return evaluation result
72-
*/
73-
Object execute(final OIdentifiable iCurrentRecord, OCommandContext iContext, Object ioResult, Object[] iMethodParams)
74-
throws ParseException;
65+
public OExpression getSource(){
66+
return getArguments().get(0);
67+
}
68+
69+
public List<OExpression> getMethodArguments(){
70+
final List<OExpression> args = new ArrayList<OExpression>(getArguments());
71+
args.remove(0);
72+
return Collections.unmodifiableList(args);
73+
}
74+
75+
@Override
76+
public Object accept(OExpressionVisitor visitor, Object data) {
77+
return visitor.visit(this, data);
78+
}
79+
80+
@Override
81+
protected String thisToString() {
82+
return "(Method) "+getName();
83+
}
7584

85+
@Override
86+
public abstract OSQLMethod copy();
87+
7688
}

core/src/main/java/com/orientechnologies/orient/core/sql/method/OSQLMethodFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.util.Set;
1919

2020
import com.orientechnologies.orient.core.exception.OCommandExecutionException;
21+
import com.orientechnologies.orient.core.sql.model.OExpression;
22+
import java.util.List;
2123

2224
/**
2325
*
@@ -40,6 +42,6 @@ public interface OSQLMethodFactory {
4042
* @throws OCommandExecutionException
4143
* : when method creation fail
4244
*/
43-
OSQLMethod createMethod(String name) throws OCommandExecutionException;
44-
45+
OSQLMethod createMethod(String name);
46+
4547
}

core/src/main/java/com/orientechnologies/orient/core/sql/method/misc/OAbstractSQLMethod.java

Lines changed: 51 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -15,94 +15,71 @@
1515
*/
1616
package com.orientechnologies.orient.core.sql.method.misc;
1717

18-
import com.orientechnologies.orient.core.db.record.OIdentifiable;
19-
import com.orientechnologies.orient.core.record.impl.ODocument;
2018
import com.orientechnologies.orient.core.sql.method.OSQLMethod;
2119

2220
/**
2321
*
2422
* @author Johann Sorel (Geomatys)
2523
*/
26-
public abstract class OAbstractSQLMethod implements OSQLMethod {
24+
public abstract class OAbstractSQLMethod extends OSQLMethod {
2725

28-
private final String name;
29-
private final int minparams;
30-
private final int maxparams;
26+
private final int minparams;
27+
private final int maxparams;
3128

32-
public OAbstractSQLMethod(String name) {
33-
this(name,0);
34-
}
35-
public OAbstractSQLMethod(String name, int nbparams) {
36-
this(name,nbparams,nbparams);
37-
}
38-
39-
public OAbstractSQLMethod(String name, int minparams, int maxparams) {
40-
this.name = name;
41-
this.minparams = minparams;
42-
this.maxparams = maxparams;
43-
}
44-
45-
@Override
46-
public String getName() {
47-
return name;
48-
}
29+
public OAbstractSQLMethod(String name) {
30+
this(name, 0);
31+
}
4932

50-
@Override
51-
public String getSyntax() {
52-
final StringBuilder sb = new StringBuilder("<field>.");
53-
sb.append(getName());
54-
sb.append('(');
55-
for(int i=0;i<minparams;i++){
56-
if(i!=0){
57-
sb.append(", ");
58-
}
59-
sb.append("param");
60-
sb.append(i+1);
61-
}
62-
if(minparams != maxparams){
63-
sb.append('[');
64-
for(int i=minparams;i<maxparams;i++){
65-
if(i!=0){
66-
sb.append(", ");
67-
}
68-
sb.append("param");
69-
sb.append(i+1);
70-
}
71-
sb.append(']');
72-
}
73-
sb.append(')');
74-
75-
return sb.toString();
76-
}
33+
public OAbstractSQLMethod(String name, int nbparams) {
34+
this(name, nbparams, nbparams);
35+
}
7736

78-
@Override
79-
public int getMinParams() {
80-
return minparams;
81-
}
37+
public OAbstractSQLMethod(String name, int minparams, int maxparams) {
38+
super(name, null);
39+
this.minparams = minparams;
40+
this.maxparams = maxparams;
41+
}
8242

83-
@Override
84-
public int getMaxParams() {
85-
return maxparams;
43+
@Override
44+
public String getSyntax() {
45+
final StringBuilder sb = new StringBuilder("<field>.");
46+
sb.append(getName());
47+
sb.append('(');
48+
for (int i = 0; i < minparams; i++) {
49+
if (i != 0) {
50+
sb.append(", ");
51+
}
52+
sb.append("param");
53+
sb.append(i + 1);
8654
}
87-
88-
protected Object getParameterValue(final OIdentifiable iRecord, final String iValue) {
89-
if(iValue == null){
90-
return null;
55+
if (minparams != maxparams) {
56+
sb.append('[');
57+
for (int i = minparams; i < maxparams; i++) {
58+
if (i != 0) {
59+
sb.append(", ");
9160
}
61+
sb.append("param");
62+
sb.append(i + 1);
63+
}
64+
sb.append(']');
65+
}
66+
sb.append(')');
9267

93-
if(iValue.charAt(0) == '\'' || iValue.charAt(0) == '"'){
94-
// GET THE VALUE AS STRING
95-
return iValue.substring(1, iValue.length() - 1);
96-
}
68+
return sb.toString();
69+
}
9770

98-
// SEARCH FOR FIELD
99-
return ((ODocument) iRecord.getRecord()).field(iValue);
100-
}
71+
@Override
72+
public int getMinParams() {
73+
return minparams;
74+
}
10175

102-
@Override
103-
public int compareTo(OSQLMethod o) {
104-
return this.getName().compareTo(o.getName());
105-
}
106-
107-
76+
@Override
77+
public int getMaxParams() {
78+
return maxparams;
79+
}
80+
81+
@Override
82+
public int compareTo(OSQLMethod o) {
83+
return this.getName().compareTo(o.getName());
84+
}
10885
}

0 commit comments

Comments
 (0)