001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins.cmp.ejbql;
023:
024: import org.jboss.ejb.plugins.cmp.jdbc.SQLUtil;
025:
026: /**
027: * This a basic abstract syntax tree visitor. It simply converts the tree
028: * back into ejbql. This is useful for testing and extensions, as most
029: * extensions translate just a few elements of the tree.
030: *
031: * @author <a href="mailto:dain@daingroup.com">Dain Sundstrom</a>
032: * @version $Revision: 63348 $
033: */
034: public class BasicVisitor implements JBossQLParserVisitor {
035: public Object visit(SimpleNode node, Object data) {
036: return data;
037: }
038:
039: public Object visit(ASTEJBQL node, Object data) {
040: StringBuffer buf = (StringBuffer) data;
041: for (int i = 0; i < node.jjtGetNumChildren(); i++) {
042: if (i > 0) {
043: buf.append(' ');
044: }
045: node.jjtGetChild(i).jjtAccept(this , data);
046: }
047: return data;
048: }
049:
050: public Object visit(ASTFrom node, Object data) {
051: StringBuffer buf = (StringBuffer) data;
052: buf.append(SQLUtil.FROM);
053: for (int i = 0; i < node.jjtGetNumChildren(); i++) {
054: if (i > 0) {
055: buf.append(SQLUtil.COMMA);
056: }
057: node.jjtGetChild(i).jjtAccept(this , data);
058: }
059: return data;
060: }
061:
062: public Object visit(ASTCollectionMemberDeclaration node, Object data) {
063: StringBuffer buf = (StringBuffer) data;
064: buf.append(SQLUtil.IN).append('(');
065: node.jjtGetChild(0).jjtAccept(this , data);
066: buf.append(')').append(' ');
067: node.jjtGetChild(1).jjtAccept(this , data);
068: return data;
069: }
070:
071: public Object visit(ASTRangeVariableDeclaration node, Object data) {
072: StringBuffer buf = (StringBuffer) data;
073: node.jjtGetChild(0).jjtAccept(this , data);
074: buf.append(' ');
075: node.jjtGetChild(1).jjtAccept(this , data);
076: return data;
077: }
078:
079: public Object visit(ASTSelect node, Object data) {
080: StringBuffer buf = (StringBuffer) data;
081: buf.append(SQLUtil.SELECT);
082: node.jjtGetChild(0).jjtAccept(this , data);
083: return data;
084: }
085:
086: public Object visit(ASTWhere node, Object data) {
087: StringBuffer buf = (StringBuffer) data;
088: buf.append(SQLUtil.WHERE);
089: node.jjtGetChild(0).jjtAccept(this , data);
090: return data;
091: }
092:
093: public Object visit(ASTOr node, Object data) {
094: StringBuffer buf = (StringBuffer) data;
095: node.jjtGetChild(0).jjtAccept(this , data);
096: for (int i = 1; i < node.jjtGetNumChildren(); ++i) {
097: buf.append(SQLUtil.OR);
098: node.jjtGetChild(i).jjtAccept(this , data);
099: }
100: return data;
101: }
102:
103: public Object visit(ASTWhereConditionalTerm node, Object data) {
104: for (int i = 0; i < node.jjtGetNumChildren(); ++i) {
105: node.jjtGetChild(i).jjtAccept(this , data);
106: }
107: return data;
108: }
109:
110: public Object visit(ASTAnd node, Object data) {
111: StringBuffer buf = (StringBuffer) data;
112: node.jjtGetChild(0).jjtAccept(this , data);
113: for (int i = 1; i < node.jjtGetNumChildren(); i++) {
114: buf.append(SQLUtil.AND);
115: node.jjtGetChild(i).jjtAccept(this , data);
116: }
117: return data;
118: }
119:
120: public Object visit(ASTNot node, Object data) {
121: StringBuffer buf = (StringBuffer) data;
122: buf.append(SQLUtil.NOT);
123: node.jjtGetChild(0).jjtAccept(this , data);
124: return data;
125: }
126:
127: public Object visit(ASTConditionalParenthetical node, Object data) {
128: StringBuffer buf = (StringBuffer) data;
129: buf.append('(');
130: node.jjtGetChild(0).jjtAccept(this , data);
131: buf.append(')');
132: return data;
133: }
134:
135: public Object visit(ASTBetween node, Object data) {
136: StringBuffer buf = (StringBuffer) data;
137: node.jjtGetChild(0).jjtAccept(this , data);
138: if (node.not) {
139: buf.append(SQLUtil.NOT);
140: }
141: buf.append(SQLUtil.BETWEEN);
142: node.jjtGetChild(1).jjtAccept(this , data);
143: buf.append(SQLUtil.AND);
144: node.jjtGetChild(2).jjtAccept(this , data);
145: return data;
146: }
147:
148: public Object visit(ASTIn node, Object data) {
149: StringBuffer buf = (StringBuffer) data;
150: node.jjtGetChild(0).jjtAccept(this , data);
151: if (node.not) {
152: buf.append(SQLUtil.NOT);
153: }
154: buf.append(SQLUtil.IN).append('(');
155: node.jjtGetChild(1).jjtAccept(this , data);
156: for (int i = 2; i < node.jjtGetNumChildren(); i++) {
157: buf.append(SQLUtil.COMMA);
158: node.jjtGetChild(i).jjtAccept(this , data);
159: }
160: buf.append(')');
161: return data;
162: }
163:
164: public Object visit(ASTLike node, Object data) {
165: StringBuffer buf = (StringBuffer) data;
166: node.jjtGetChild(0).jjtAccept(this , data);
167: if (node.not) {
168: buf.append(SQLUtil.NOT);
169: }
170: buf.append(SQLUtil.LIKE);
171: node.jjtGetChild(1).jjtAccept(this , data);
172: if (node.jjtGetNumChildren() == 3) {
173: buf.append(" {ESCAPE ");
174: node.jjtGetChild(2).jjtAccept(this , data);
175: buf.append('}');
176: }
177: return data;
178: }
179:
180: public Object visit(ASTNullComparison node, Object data) {
181: StringBuffer buf = (StringBuffer) data;
182: node.jjtGetChild(0).jjtAccept(this , data);
183: buf.append(SQLUtil.IS);
184: if (node.not) {
185: buf.append(SQLUtil.NOT);
186: }
187: buf.append(SQLUtil.NULL);
188: return data;
189: }
190:
191: public Object visit(ASTIsEmpty node, Object data) {
192: StringBuffer buf = (StringBuffer) data;
193: node.jjtGetChild(0).jjtAccept(this , data);
194: buf.append(SQLUtil.IS);
195: if (node.not) {
196: buf.append(SQLUtil.NOT);
197: }
198: buf.append(SQLUtil.EMPTY);
199: return data;
200: }
201:
202: public Object visit(ASTMemberOf node, Object data) {
203: StringBuffer buf = (StringBuffer) data;
204: node.jjtGetChild(0).jjtAccept(this , data);
205: if (node.not) {
206: buf.append(SQLUtil.NOT);
207: }
208: buf.append(SQLUtil.MEMBER_OF);
209: node.jjtGetChild(1).jjtAccept(this , data);
210: return data;
211: }
212:
213: public Object visit(ASTStringComparison node, Object data) {
214: StringBuffer buf = (StringBuffer) data;
215: node.jjtGetChild(0).jjtAccept(this , data);
216: buf.append(' ').append(node.opp).append(' ');
217: node.jjtGetChild(1).jjtAccept(this , data);
218: return data;
219: }
220:
221: public Object visit(ASTBooleanComparison node, Object data) {
222: StringBuffer buf = (StringBuffer) data;
223: node.jjtGetChild(0).jjtAccept(this , data);
224: if (node.jjtGetNumChildren() == 2) {
225: buf.append(' ').append(node.opp).append(' ');
226: node.jjtGetChild(1).jjtAccept(this , data);
227: }
228: return data;
229: }
230:
231: public Object visit(ASTDatetimeComparison node, Object data) {
232: StringBuffer buf = (StringBuffer) data;
233: node.jjtGetChild(0).jjtAccept(this , data);
234: buf.append(' ').append(node.opp).append(' ');
235: node.jjtGetChild(1).jjtAccept(this , data);
236: return data;
237: }
238:
239: public Object visit(ASTEntityComparison node, Object data) {
240: StringBuffer buf = (StringBuffer) data;
241: node.jjtGetChild(0).jjtAccept(this , data);
242: buf.append(' ').append(node.opp).append(' ');
243: node.jjtGetChild(1).jjtAccept(this , data);
244: return data;
245: }
246:
247: public Object visit(ASTValueClassComparison node, Object data) {
248: StringBuffer buf = (StringBuffer) data;
249: node.jjtGetChild(0).jjtAccept(this , data);
250: buf.append(' ').append(node.opp).append(' ');
251: node.jjtGetChild(1).jjtAccept(this , data);
252: return data;
253: }
254:
255: public Object visit(ASTArithmeticComparison node, Object data) {
256: StringBuffer buf = (StringBuffer) data;
257: node.jjtGetChild(0).jjtAccept(this , data);
258: buf.append(' ').append(node.opp).append(' ');
259: node.jjtGetChild(1).jjtAccept(this , data);
260: return data;
261: }
262:
263: public Object visit(ASTPlusMinus node, Object data) {
264: StringBuffer buf = (StringBuffer) data;
265: for (int i = 0; i < node.jjtGetNumChildren(); i++) {
266: if (i > 0) {
267: buf.append(' ').append(node.opps.get(i - 1))
268: .append(' ');
269: }
270: node.jjtGetChild(i).jjtAccept(this , data);
271: }
272: return data;
273: }
274:
275: public Object visit(ASTMultDiv node, Object data) {
276: StringBuffer buf = (StringBuffer) data;
277: for (int i = 0; i < node.jjtGetNumChildren(); i++) {
278: if (i > 0) {
279: buf.append(' ').append(node.opps.get(i - 1))
280: .append(' ');
281: }
282: node.jjtGetChild(i).jjtAccept(this , data);
283: }
284: return data;
285: }
286:
287: public Object visit(ASTNegation node, Object data) {
288: StringBuffer buf = (StringBuffer) data;
289: buf.append('-');
290: node.jjtGetChild(0).jjtAccept(this , data);
291: return data;
292: }
293:
294: public Object visit(ASTArithmeticParenthetical node, Object data) {
295: StringBuffer buf = (StringBuffer) data;
296: buf.append('(');
297: node.jjtGetChild(0).jjtAccept(this , data);
298: buf.append(')');
299: return data;
300: }
301:
302: public Object visit(ASTStringParenthetical node, Object data) {
303: StringBuffer buf = (StringBuffer) data;
304: buf.append('(');
305: node.jjtGetChild(0).jjtAccept(this , data);
306: buf.append(')');
307: return data;
308: }
309:
310: public Object visit(ASTConcat node, Object data) {
311: StringBuffer buf = (StringBuffer) data;
312: buf.append(SQLUtil.CONCAT).append('(');
313: node.jjtGetChild(0).jjtAccept(this , data);
314: buf.append(SQLUtil.COMMA);
315: node.jjtGetChild(1).jjtAccept(this , data);
316: buf.append(')');
317: return data;
318: }
319:
320: public Object visit(ASTSubstring node, Object data) {
321: StringBuffer buf = (StringBuffer) data;
322: buf.append(SQLUtil.SUBSTRING).append('(');
323: node.jjtGetChild(0).jjtAccept(this , data);
324: buf.append(SQLUtil.COMMA);
325: node.jjtGetChild(1).jjtAccept(this , data);
326: buf.append(SQLUtil.COMMA);
327: node.jjtGetChild(2).jjtAccept(this , data);
328: buf.append(')');
329: return data;
330: }
331:
332: public Object visit(ASTLCase node, Object data) {
333: StringBuffer buf = (StringBuffer) data;
334: buf.append(SQLUtil.LCASE).append('(');
335: node.jjtGetChild(0).jjtAccept(this , data);
336: buf.append(')');
337: return data;
338: }
339:
340: public Object visit(ASTUCase node, Object data) {
341: StringBuffer buf = (StringBuffer) data;
342: buf.append(SQLUtil.UCASE).append('(');
343: node.jjtGetChild(0).jjtAccept(this , data);
344: buf.append(')');
345: return data;
346: }
347:
348: public Object visit(ASTLength node, Object data) {
349: StringBuffer buf = (StringBuffer) data;
350: buf.append(SQLUtil.LENGTH).append('(');
351: node.jjtGetChild(0).jjtAccept(this , data);
352: buf.append(')');
353: return data;
354: }
355:
356: public Object visit(ASTLocate node, Object data) {
357: StringBuffer buf = (StringBuffer) data;
358: buf.append(SQLUtil.LOCATE).append('(');
359: node.jjtGetChild(0).jjtAccept(this , data);
360: buf.append(SQLUtil.COMMA);
361: node.jjtGetChild(1).jjtAccept(this , data);
362: if (node.jjtGetNumChildren() == 3) {
363: buf.append(SQLUtil.COMMA);
364: node.jjtGetChild(2).jjtAccept(this , data);
365: }
366: buf.append(')');
367: return data;
368: }
369:
370: public Object visit(ASTAbs node, Object data) {
371: StringBuffer buf = (StringBuffer) data;
372: buf.append(SQLUtil.ABS).append('(');
373: node.jjtGetChild(0).jjtAccept(this , data);
374: buf.append(')');
375: return data;
376: }
377:
378: public Object visit(ASTSqrt node, Object data) {
379: StringBuffer buf = (StringBuffer) data;
380: buf.append(SQLUtil.SQRT).append('(');
381: node.jjtGetChild(0).jjtAccept(this , data);
382: buf.append(')');
383: return data;
384: }
385:
386: public Object visit(ASTCount node, Object data) {
387: StringBuffer buf = (StringBuffer) data;
388: buf.append(SQLUtil.COUNT).append('(');
389: ASTPath path = (ASTPath) node.jjtGetChild(0);
390: path.children[0].jjtAccept(this , data);
391: buf.append(')');
392: return data;
393: }
394:
395: public Object visit(ASTMax node, Object data) {
396: StringBuffer buf = (StringBuffer) data;
397: buf.append(SQLUtil.MAX).append('(');
398: ASTPath path = (ASTPath) node.jjtGetChild(0);
399: path.children[0].jjtAccept(this , data);
400: buf.append(')');
401: return data;
402: }
403:
404: public Object visit(ASTMin node, Object data) {
405: StringBuffer buf = (StringBuffer) data;
406: buf.append(SQLUtil.MIN).append('(');
407: ASTPath path = (ASTPath) node.jjtGetChild(0);
408: path.children[0].jjtAccept(this , data);
409: buf.append(')');
410: return data;
411: }
412:
413: public Object visit(ASTAvg node, Object data) {
414: StringBuffer buf = (StringBuffer) data;
415: buf.append(SQLUtil.AVG).append('(');
416: ASTPath path = (ASTPath) node.jjtGetChild(0);
417: path.children[0].jjtAccept(this , data);
418: buf.append(')');
419: return data;
420: }
421:
422: public Object visit(ASTSum node, Object data) {
423: StringBuffer buf = (StringBuffer) data;
424: buf.append(SQLUtil.SUM).append('(');
425: ASTPath path = (ASTPath) node.jjtGetChild(0);
426: path.children[0].jjtAccept(this , data);
427: buf.append(')');
428: return data;
429: }
430:
431: public Object visit(ASTOrderBy node, Object data) {
432: StringBuffer buf = (StringBuffer) data;
433: for (int i = 0; i < node.jjtGetNumChildren(); i++) {
434: if (i > 0) {
435: buf.append(SQLUtil.COMMA);
436: }
437: node.jjtGetChild(i).jjtAccept(this , data);
438: }
439: return data;
440: }
441:
442: public Object visit(ASTOrderByPath node, Object data) {
443: StringBuffer buf = (StringBuffer) data;
444:
445: node.jjtGetChild(0).jjtAccept(this , data);
446: if (node.ascending) {
447: buf.append(SQLUtil.ASC);
448: } else {
449: buf.append(SQLUtil.DESC);
450: }
451: return data;
452: }
453:
454: public Object visit(ASTPath node, Object data) {
455: StringBuffer buf = (StringBuffer) data;
456: buf.append(node.getPath());
457: return data;
458: }
459:
460: public Object visit(ASTIdentifier node, Object data) {
461: StringBuffer buf = (StringBuffer) data;
462: buf.append(node.identifier);
463: return data;
464: }
465:
466: public Object visit(ASTAbstractSchema node, Object data) {
467: StringBuffer buf = (StringBuffer) data;
468: buf.append(node.abstractSchemaName);
469: return data;
470: }
471:
472: public Object visit(ASTParameter node, Object data) {
473: StringBuffer buf = (StringBuffer) data;
474: buf.append("?").append(node.number);
475: return data;
476: }
477:
478: public Object visit(ASTExactNumericLiteral node, Object data) {
479: StringBuffer buf = (StringBuffer) data;
480: buf.append(node.literal);
481: return data;
482: }
483:
484: public Object visit(ASTApproximateNumericLiteral node, Object data) {
485: StringBuffer buf = (StringBuffer) data;
486: buf.append(node.literal);
487: return data;
488: }
489:
490: public Object visit(ASTStringLiteral node, Object data) {
491: StringBuffer buf = (StringBuffer) data;
492: buf.append(node.value);
493: return data;
494: }
495:
496: public Object visit(ASTBooleanLiteral node, Object data) {
497: StringBuffer buf = (StringBuffer) data;
498: buf.append(node.value);
499: return data;
500: }
501:
502: public Object visit(ASTLimitOffset node, Object data) {
503: StringBuffer buf = (StringBuffer) data;
504: int child = 0;
505: if (node.hasOffset) {
506: buf.append(SQLUtil.OFFSET);
507: node.jjtGetChild(child++).jjtAccept(this , data);
508: }
509: if (node.hasLimit) {
510: buf.append(SQLUtil.LIMIT);
511: node.jjtGetChild(child).jjtAccept(this , data);
512: }
513: return data;
514: }
515:
516: public Object visit(ASTMod node, Object data) {
517: StringBuffer buf = (StringBuffer) data;
518: buf.append(SQLUtil.MOD).append('(');
519: node.jjtGetChild(0).jjtAccept(this , data);
520: buf.append(SQLUtil.COMMA);
521: node.jjtGetChild(1).jjtAccept(this , data);
522: buf.append(')');
523: return data;
524: }
525: }
|