-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathtestModifyExpr.java
More file actions
291 lines (258 loc) · 11.9 KB
/
Copy pathtestModifyExpr.java
File metadata and controls
291 lines (258 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package test.scriptWriter;
import gudusoft.gsqlparser.EDbVendor;
import gudusoft.gsqlparser.EExpressionType;
import gudusoft.gsqlparser.TGSqlParser;
import gudusoft.gsqlparser.nodes.TExpression;
import gudusoft.gsqlparser.nodes.TExpressionList;
import gudusoft.gsqlparser.stmt.TSelectSqlStatement;
import junit.framework.TestCase;
import gudusoft.gsqlparser.scriptWriter.TScriptGenerator;
/**
* Code illustrates how to modify expression.
*/
public class testModifyExpr extends TestCase {
private TGSqlParser parser = null;
private TScriptGenerator scriptGenerator = null;
protected void setUp() throws Exception {
super.setUp();
parser = new TGSqlParser(EDbVendor.dbvoracle);
scriptGenerator = new TScriptGenerator();
}
protected void tearDown() throws Exception {
parser = null;
super.tearDown();
}
/**
* set the status of an expression to be removed from the parent expression.
*/
public void testRemoveObjectExpr() {
TExpression expression = parser.parseExpression("columnA");
assertTrue(expression.getExpressionType() == EExpressionType.simple_object_name_t);
expression.remove();
assertTrue(expression.getExpressionType() == EExpressionType.removed_t);
}
/**
* Remove an expression which includes a function call from the parent expression.
*/
public void testRemoveFunctionCallExpr() {
TExpression expression = parser.parseExpression("fx(columnA)");
assertTrue(expression.getExpressionType() == EExpressionType.function_t);
expression.remove();
assertTrue(expression.getExpressionType() == EExpressionType.removed_t);
}
/**
* Remove <code>columnC</code> in <code>fx(columnA,columnB,fx2(1+columnC))</code> cause the whole function
* removed from the parent expression.
*/
public void testRemoveColumnInFunctionCall() {
TExpression expression = parser.parseExpression("fx(columnA,columnB,fx2(1+columnC))");
assertTrue(expression.getExpressionType() == EExpressionType.function_t);
TExpressionList resultList = expression.searchColumn("columnC");
assertTrue(resultList.size() == 1);
TExpression columnCExpr = resultList.getExpression(0);
columnCExpr.remove();
assertTrue(expression.getExpressionType() == EExpressionType.removed_t);
}
/**
* Remove <code>columnB</code> in
* {@code
* columnA+(columnB*2)+columnC
* }
* will cause the whole expression removed from the parent expression or parse tree node.
*/
public void testSearchColumn() {
TExpression expression = parser.parseExpression("columnA+(columnB*2)+columnC");
TExpressionList resultList = expression.searchColumn("columnB");
assertTrue(resultList.size() == 1);
TExpression columnBExpr = resultList.getExpression(0);
assertTrue(columnBExpr.getExpressionType() == EExpressionType.simple_object_name_t);
assertTrue(columnBExpr.toString().equalsIgnoreCase("columnB"));
assertTrue(expression.toScript().equalsIgnoreCase("columnA + (columnB * 2) + columnC"));
columnBExpr.remove();
assertTrue(expression.toScript().equalsIgnoreCase(""));
}
/**
* Remove <code>columnA</code> in
* <pre>
* {@code
* columnA+(columnB*2)>columnC
* }
* </pre>
* will cause the whole expression removed from the parent expression or parse tree node.
*/
public void testColumnInComparision() {
TExpression expression = parser.parseExpression("columnA+(columnB*2)>columnC");
TExpressionList resultList = expression.searchColumn("columnA");
assertTrue(resultList.size() == 1);
TExpression columnAExpr = resultList.getExpression(0);
assertTrue(columnAExpr.getExpressionType() == EExpressionType.simple_object_name_t);
assertTrue(columnAExpr.toString().equalsIgnoreCase("columnA"));
assertTrue(expression.toScript().equalsIgnoreCase("columnA + (columnB * 2) > columnC"));
columnAExpr.remove();
assertTrue(expression.toScript().equalsIgnoreCase(""));
}
/**
* Remove <code>columnA</code> in
* <pre>{@code columnA+(columnB*2)>columnC and columnD=columnE-9}</pre>
* will cause the expression
* <pre>
* {@code columnA+(columnB*2)>columnC}
* </pre>
* removed from the parent expression and keep this expression unchange:
* <pre>
* {@code columnD = columnE - 9}
* </pre>
*/
public void testColumnInAndOr() {
TExpression expression = parser.parseExpression("columnA+(columnB*2)>columnC and columnD=columnE-9");
TExpressionList resultList = expression.searchColumn("columnA");
assertTrue(resultList.size() == 1);
TExpression columnAExpr = resultList.getExpression(0);
assertTrue(columnAExpr.getExpressionType() == EExpressionType.simple_object_name_t);
assertTrue(columnAExpr.toString().equalsIgnoreCase("columnA"));
assertTrue(expression.toScript().equalsIgnoreCase("columnA + (columnB * 2) > columnC and columnD = columnE - 9"));
columnAExpr.remove();
assertTrue(expression.toScript().equalsIgnoreCase("columnD = columnE - 9"));
}
/**
* Remove column: <code>application_location_id</code> from the where condition,
* <pre>
* {@code
* (pal.application_location_id = pualr.application_location_id +
* AND pu.jbp_uid = pualr.jbp_uid" +
* AND pu.username = 'USERID')
* }
* </pre>
* Keep the following condition unchanged:
* <pre>
* {@code
* (pu.jbp_uid = pualr.jbp_uid and pu.username = 'USERID')
* }
* </pre>
*/
public void testColumnInAndOr1(){
parser.sqltext = "select *\n" +
"from table1 pal, table2 pualr, table3 pu\n" +
"WHERE (pal.application_location_id = pualr.application_location_id \n" +
" AND pu.jbp_uid = pualr.jbp_uid \n" +
" AND pu.username = 'USERID')";
int ret = parser.parse();
assertTrue(ret == 0);
TSelectSqlStatement selectSqlStatement = (TSelectSqlStatement)parser.sqlstatements.get(0);
TExpression expression = selectSqlStatement.getWhereClause().getCondition();
TExpressionList resultList = expression.searchColumn("application_location_id");
assertTrue(resultList.size() == 2);
TExpression expression1 = resultList.getExpression(0);
assertTrue(expression1.getExpressionType() == EExpressionType.simple_object_name_t);
assertTrue(expression1.toString().equalsIgnoreCase("pal.application_location_id"));
expression1.remove();
assertTrue(expression.toScript().equalsIgnoreCase("(pu.jbp_uid = pualr.jbp_uid and pu.username = 'USERID')"));
}
/**
* Remove the right operand of a condition in where clause.
* <pre>
* {@code
* m.id = ? and m.id = altname.id(+) and m.id = ccu.id(+)
* }
* </pre>
* After remove the right operand, the condition become like this:
* <pre>
* {@code
* m.id = ? and m.id = altname.id(+)
* }
* </pre>
* then, remove the right operand again, the condition become like this:
* <pre>
* {@code
* m.id = ?
* }
* </pre>
*/
public void testColumnInAndOr2(){
parser.sqltext = "SELECT m.*, \n" +
" altname.last_name last_name_student, \n" +
" altname.first_name first_name_student, \n" +
" ccu.date_joined, \n" +
" ccu.last_login, \n" +
" ccu.photo_id, \n" +
" ccu.last_updated \n" +
"FROM summit.mstr m, \n" +
" summit.alt_name altname, \n" +
" smmtccon.ccn_user ccu \n" +
"WHERE m.id =?\n" +
" AND m.id = altname.id(+) \n" +
" AND m.id = ccu.id(+) \n" +
" AND altname.grad_name_ind(+) = '*'";
int ret = parser.parse();
assertTrue(ret == 0);
TSelectSqlStatement selectSqlStatement = (TSelectSqlStatement)parser.sqlstatements.get(0);
TExpression expression = selectSqlStatement.getWhereClause().getCondition();
expression.getRightOperand().remove();
assertTrue(expression.toScript().equalsIgnoreCase("m.id = ? and m.id = altname.id(+) and m.id = ccu.id(+)"));
expression.getRightOperand().remove();
assertTrue(expression.toScript().equalsIgnoreCase("m.id = ? and m.id = altname.id(+)"));
expression.getRightOperand().remove();
assertTrue(expression.toScript().equalsIgnoreCase("m.id = ?"));
}
/**
* Remove those columns from the condition: lst.soort_adres, nat.prs_id, adr.id, prs.id
* <pre>
* {@code
* pas.soort_adres = lst.soort_adres
* and prs.id(+) = nat.prs_id
* and adr.id = pas.adr_id
* and prs.id = pas.prs_id
* and lst.persoonssoort = 'PERSOON'
* and pas.einddatumrelatie is null;
* }
* </pre>
* The result condition is:
* <pre>
* {@code
* lst.persoonssoort = 'PERSOON'
* and pas.einddatumrelatie is null"
* }
* </pre>
*/
public void testColumnInAndOr3(){
parser.sqltext = "select *\n" +
"from ods_trf_pnb_stuf_lijst_adrsrt2 lst\n" +
"\t\t, ods_stg_pnb_stuf_pers_adr pas\n" +
"\t\t, ods_stg_pnb_stuf_pers_nat nat\n" +
"\t\t, ods_stg_pnb_stuf_adr adr\n" +
"\t\t, ods_stg_pnb_stuf_np prs\n" +
"where \n" +
"\tpas.soort_adres = lst.soort_adres\n" +
"\tand prs.id(+) = nat.prs_id\n" +
"\tand adr.id = pas.adr_id\n" +
"\tand prs.id = pas.prs_id\n" +
" and lst.persoonssoort = 'PERSOON'\n" +
" and pas.einddatumrelatie is null";
int ret = parser.parse();
assertTrue(ret == 0);
TSelectSqlStatement selectSqlStatement = (TSelectSqlStatement)parser.sqlstatements.get(0);
TExpression expression = selectSqlStatement.getWhereClause().getCondition();
TExpressionList resultList = expression.searchColumn("lst.soort_adres");
assertTrue(resultList.size() == 1);
TExpression expression1 = resultList.getExpression(0);
assertTrue(expression1.getExpressionType() == EExpressionType.simple_object_name_t);
expression1.remove();
resultList = expression.searchColumn("nat.prs_id");
assertTrue(resultList.size() == 1);
expression1 = resultList.getExpression(0);
assertTrue(expression1.getExpressionType() == EExpressionType.simple_object_name_t);
expression1.remove();
resultList = expression.searchColumn("adr.id");
assertTrue(resultList.size() == 1);
expression1 = resultList.getExpression(0);
assertTrue(expression1.getExpressionType() == EExpressionType.simple_object_name_t);
expression1.remove();
resultList = expression.searchColumn("prs.id");
assertTrue(resultList.size() == 1);
expression1 = resultList.getExpression(0);
assertTrue(expression1.getExpressionType() == EExpressionType.simple_object_name_t);
expression1.remove();
assertTrue(expression.toScript().trim().equalsIgnoreCase("lst.persoonssoort = \'PERSOON\' and pas.einddatumrelatie is null"));
// assertTrue(expression.toScript().equalsIgnoreCase("(pu.jbp_uid = pualr.jbp_uid and pu.username = 'USERID')"));
}
}