Skip to content

Commit fe34f26

Browse files
committed
Fixed OracleSQLTreProcessor and SQLServerTreProcessor
1 parent a28daa8 commit fe34f26

File tree

4 files changed

+42
-58
lines changed

4 files changed

+42
-58
lines changed

cayenne-server/src/main/java/org/apache/cayenne/access/translator/select/BaseSQLTreeProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
/**
3535
* @since 4.2
3636
*/
37-
public class BaseSQLTreeProcessor extends SimpleNodeTreeVisitor implements SQLTreeProcessor {
37+
public class BaseSQLTreeProcessor extends TypeAwareSQLTreeProcessor implements SQLTreeProcessor {
3838

3939
@Override
4040
public Node process(Node node) {

cayenne-server/src/main/java/org/apache/cayenne/dba/oracle/OracleSQLTreeProcessor.java

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,20 @@
2121

2222
import java.util.ArrayList;
2323
import java.util.List;
24-
import java.util.Optional;
2524

2625
import org.apache.cayenne.access.sqlbuilder.ExpressionNodeBuilder;
2726
import org.apache.cayenne.access.sqlbuilder.QuotingAppendable;
2827
import org.apache.cayenne.access.sqlbuilder.SelectBuilder;
2928
import org.apache.cayenne.access.sqlbuilder.sqltree.*;
30-
import org.apache.cayenne.access.translator.select.TypeAwareSQLTreeProcessor;
29+
import org.apache.cayenne.access.translator.select.BaseSQLTreeProcessor;
3130
import org.apache.cayenne.util.ArrayUtil;
32-
import org.apache.cayenne.value.GeoJson;
33-
import org.apache.cayenne.value.Json;
34-
import org.apache.cayenne.value.Wkt;
3531

3632
import static org.apache.cayenne.access.sqlbuilder.SQLBuilder.*;
3733

3834
/**
3935
* @since 4.2
4036
*/
41-
public class OracleSQLTreeProcessor extends TypeAwareSQLTreeProcessor {
37+
public class OracleSQLTreeProcessor extends BaseSQLTreeProcessor {
4238

4339
private static final int ORACLE_IN_BATCH_SIZE = 1000;
4440

@@ -52,25 +48,22 @@ public static OracleSQLTreeProcessor getInstance() {
5248
return INSTANCE;
5349
}
5450

55-
protected OracleSQLTreeProcessor() {
56-
registerProcessor(NodeType.IN, (ChildProcessor<InNode>) this::onInNode);
57-
registerProcessor(NodeType.LIMIT_OFFSET, (ChildProcessor<LimitOffsetNode>) this::onLimitOffsetNode);
58-
registerProcessor(NodeType.FUNCTION, (ChildProcessor<FunctionNode>) this::onFunctionNode);
59-
60-
registerColumnProcessor(Wkt.class, (parent, child, i)
61-
-> Optional.of(wrapInFunction(child, "ST_AsText")));
62-
registerColumnProcessor(GeoJson.class, (parent, child, i)
63-
-> Optional.of(wrapInFunction(child, "ST_AsGeoJSON")));
64-
65-
registerValueProcessor(Wkt.class, (parent, child, i)
66-
-> Optional.of(wrapInFunction(child, "ST_GeomFromText")));
67-
registerValueProcessor(GeoJson.class, (parent, child, i)
68-
-> Optional.of(wrapInFunction(child, "ST_GeomFromGeoJSON")));
69-
registerValueProcessor(Json.class, (parent, child, i)
70-
-> Optional.of(wrapInFunction(child, "ST_AsCLOB")));
51+
protected OracleSQLTreeProcessor() { }
52+
53+
@Override
54+
protected void onResultNode(Node parent, Node child, int index) {
55+
for(int i=0; i<child.getChildrenCount(); i++) {
56+
child.replaceChild(i, aliased(child.getChild(i), "c" + i).build());
57+
}
58+
}
59+
60+
@Override
61+
protected void onColumnNode(Node parent, ColumnNode child, int index) {
62+
replaceChild(parent, index, new TrimmingColumnNode(child));
7163
}
7264

73-
protected Optional<Node> onLimitOffsetNode(Node parent, LimitOffsetNode child, int index) {
65+
@Override
66+
protected void onLimitOffsetNode(Node parent, LimitOffsetNode child, int index) {
7467
if(child.getLimit() > 0 || child.getOffset() > 0) {
7568
int limit = child.getLimit();
7669
int offset = child.getOffset();
@@ -91,21 +84,21 @@ protected Optional<Node> onLimitOffsetNode(Node parent, LimitOffsetNode child, i
9184
.where(exp(text(" rnum")).gt(value(offset)));
9285
}
9386
parent.replaceChild(index, new EmptyNode());
94-
return Optional.of(new LimitOffsetNode(child.getLimit(), child.getOffset()));
9587
}
9688

97-
protected Optional<Node> onInNode(Node parent, InNode child, int index) {
89+
@Override
90+
protected void onInNode(Node parent, InNode child, int index) {
9891
boolean not = child.isNot();
9992
Node arg = child.getChild(0);
10093
Node childNode = child.getChild(1);
10194
if(childNode.getType() != NodeType.VALUE) {
102-
return Optional.of(child);
95+
return;
10396
}
10497

10598
ValueNode valueNode = (ValueNode)childNode;
10699
Object value = valueNode.getValue();
107100
if(!value.getClass().isArray()) {
108-
return Optional.of(child);
101+
return;
109102
}
110103

111104
List<Node> newChildren = new ArrayList<>();
@@ -158,7 +151,6 @@ protected Optional<Node> onInNode(Node parent, InNode child, int index) {
158151
}
159152
}
160153
parent.replaceChild(index, exp.build());
161-
return Optional.of(child);
162154
}
163155

164156
private InNode newSliceNode(InNode child, Node arg, ValueNode valueNode, Object slice) {
@@ -168,7 +160,8 @@ private InNode newSliceNode(InNode child, Node arg, ValueNode valueNode, Object
168160
return nextNode;
169161
}
170162

171-
protected Optional<Node> onFunctionNode(Node parent, FunctionNode child, int index) {
163+
@Override
164+
protected void onFunctionNode(Node parent, FunctionNode child, int index) {
172165
String functionName = child.getFunctionName();
173166
Node functionReplacement = null;
174167
switch (functionName) {
@@ -178,7 +171,7 @@ protected Optional<Node> onFunctionNode(Node parent, FunctionNode child, int ind
178171
functionReplacement.addChild(child.getChild(1-i));
179172
}
180173
parent.replaceChild(index, functionReplacement);
181-
return Optional.of(child);
174+
return;
182175

183176
case "DAY_OF_YEAR":
184177
case "DAY_OF_WEEK":
@@ -194,7 +187,7 @@ protected Optional<Node> onFunctionNode(Node parent, FunctionNode child, int ind
194187
}
195188
functionReplacement.addChild(new TextNode(functionName));
196189
parent.replaceChild(index, functionReplacement);
197-
return Optional.of(child);
190+
return;
198191

199192
case "SUBSTRING":
200193
functionReplacement = new FunctionNode("SUBSTR", child.getAlias(), true);
@@ -232,9 +225,9 @@ public void appendChildrenSeparator(QuotingAppendable buffer, int childIdx) {
232225
if(functionReplacement != null) {
233226
replaceChild(parent, index, functionReplacement);
234227
}
235-
return Optional.of(child);
236228
}
237229

230+
@Override
238231
public Node process(Node node) {
239232
root = node;
240233
super.process(node);

cayenne-server/src/main/java/org/apache/cayenne/dba/sqlserver/SQLServerTreeProcessor.java

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,18 @@
1919

2020
package org.apache.cayenne.dba.sqlserver;
2121

22-
import org.apache.cayenne.access.translator.select.TypeAwareSQLTreeProcessor;
23-
import org.apache.cayenne.value.GeoJson;
24-
import org.apache.cayenne.value.Json;
25-
import org.apache.cayenne.value.Wkt;
26-
27-
import java.util.Optional;
22+
import org.apache.cayenne.access.sqlbuilder.sqltree.ColumnNode;
23+
import org.apache.cayenne.access.sqlbuilder.sqltree.Node;
24+
import org.apache.cayenne.dba.sqlserver.sqltree.SQLServerColumnNode;
25+
import org.apache.cayenne.dba.sybase.SybaseSQLTreeProcessor;
2826

2927
/**
3028
* @since 4.2
3129
*/
32-
public class SQLServerTreeProcessor extends TypeAwareSQLTreeProcessor {
33-
34-
private static final SQLServerTreeProcessor INSTANCE = new SQLServerTreeProcessor();
35-
36-
public static SQLServerTreeProcessor getInstance() {
37-
return INSTANCE;
38-
}
39-
40-
protected SQLServerTreeProcessor() {
41-
registerColumnProcessor(Wkt.class, (parent, child, i)
42-
-> Optional.of(wrapInFunction(child, "ST_AsText")));
43-
registerColumnProcessor(GeoJson.class, (parent, child, i)
44-
-> Optional.of(wrapInFunction(child, "ST_AsGeoJSON")));
30+
public class SQLServerTreeProcessor extends SybaseSQLTreeProcessor {
4531

46-
registerValueProcessor(Wkt.class, (parent, child, i)
47-
-> Optional.of(wrapInFunction(child, "ST_GeomFromText")));
48-
registerValueProcessor(GeoJson.class, (parent, child, i)
49-
-> Optional.of(wrapInFunction(child, "ST_GeomFromGeoJSON")));
50-
registerValueProcessor(Json.class, (parent, child, i)
51-
-> Optional.of(wrapInFunction(child, "ST_AsNVARCHAR")));
32+
@Override
33+
protected void onColumnNode(Node parent, ColumnNode child, int index) {
34+
replaceChild(parent, index, new SQLServerColumnNode(child));
5235
}
5336
}

cayenne-server/src/main/java/org/apache/cayenne/dba/sybase/SybaseSQLTreeProcessor.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@
3434
*/
3535
public class SybaseSQLTreeProcessor extends BaseSQLTreeProcessor {
3636

37+
private static final SybaseSQLTreeProcessor INSTANCE = new SybaseSQLTreeProcessor();
38+
39+
public static SybaseSQLTreeProcessor getInstance() {
40+
return INSTANCE;
41+
}
42+
43+
protected SybaseSQLTreeProcessor() { }
44+
3745
@Override
3846
protected void onLimitOffsetNode(Node parent, LimitOffsetNode child, int index) {
3947
// SQLServer uses "SELECT DISTINCT TOP N" or "SELECT TOP N" instead of LIMIT

0 commit comments

Comments
 (0)