KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > expressions > ExpressionOperator


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.expressions;
23
24 import java.security.AccessController JavaDoc;
25 import java.security.PrivilegedActionException JavaDoc;
26 import java.util.*;
27 import java.io.*;
28 import oracle.toplink.essentials.internal.expressions.*;
29 import oracle.toplink.essentials.internal.helper.*;
30 import oracle.toplink.essentials.exceptions.*;
31 import oracle.toplink.essentials.internal.helper.ClassConstants;
32 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper;
33 import oracle.toplink.essentials.internal.security.PrivilegedNewInstanceFromClass;
34
35 /**
36  * <p>
37  * <b>Purpose</b>: ADVANCED: The expression operator is used internally to define SQL operations and functions.
38  * It is possible for an advanced user to define their own operators.
39  */

40 public class ExpressionOperator implements Serializable {
41
42     /** Required for serialization compatibility. */
43     static final long serialVersionUID = -7066100204792043980L;
44     protected int selector;
45     protected String JavaDoc[] databaseStrings;
46     protected boolean isPrefix = false;
47     protected boolean isRepeating = false;
48     protected Class JavaDoc nodeClass;
49     protected int type;
50     protected int[] argumentIndices = null;
51     protected static Hashtable allOperators;
52     protected static Hashtable platformOperatorNames;
53     protected String JavaDoc[] javaStrings;
54
55     /** Operator types */
56     public static final int LogicalOperator = 1;
57     public static final int ComparisonOperator = 2;
58     public static final int AggregateOperator = 3;
59     public static final int OrderOperator = 4;
60     public static final int FunctionOperator = 5;
61
62     /** Logical operators */
63     public static final int And = 1;
64     public static final int Or = 2;
65     public static final int Not = 3;
66
67     /** Comparison operators */
68     public static final int Equal = 4;
69     public static final int NotEqual = 5;
70     public static final int EqualOuterJoin = 6;
71     public static final int LessThan = 7;
72     public static final int LessThanEqual = 8;
73     public static final int GreaterThan = 9;
74     public static final int GreaterThanEqual = 10;
75     public static final int Like = 11;
76     public static final int NotLike = 12;
77     public static final int In = 13;
78     public static final int NotIn = 14;
79     public static final int Between = 15;
80     public static final int NotBetween = 16;
81     public static final int IsNull = 17;
82     public static final int NotNull = 18;
83     public static final int Exists = 86;
84     public static final int NotExists = 88;
85     public static final int LikeEscape = 89;
86     public static final int Decode = 105;
87     public static final int Case = 117;
88
89     /** Aggregate operators */
90     public static final int Count = 19;
91     public static final int Sum = 20;
92     public static final int Average = 21;
93     public static final int Maximum = 22;
94     public static final int Minimum = 23;
95     public static final int StandardDeviation = 24;
96     public static final int Variance = 25;
97     public static final int Distinct = 87;
98
99     /** Ordering operators */
100     public static final int Ascending = 26;
101     public static final int Descending = 27;
102
103     /** Function operators */
104
105     // General
106
public static final int ToUpperCase = 28;
107     public static final int ToLowerCase = 29;
108     public static final int Chr = 30;
109     public static final int Concat = 31;
110     public static final int HexToRaw = 32;
111     public static final int Initcap = 33;
112     public static final int Instring = 34;
113     public static final int Soundex = 35;
114     public static final int LeftPad = 36;
115     public static final int LeftTrim = 37;
116     public static final int Replace = 38;
117     public static final int RightPad = 39;
118     public static final int RightTrim = 40;
119     public static final int Substring = 41;
120     public static final int ToNumber = 42;
121     public static final int Translate = 43;
122     public static final int Trim = 44;
123     public static final int Ascii = 45;
124     public static final int Length = 46;
125     public static final int CharIndex = 96;
126     public static final int CharLength = 97;
127     public static final int Difference = 98;
128     public static final int Reverse = 99;
129     public static final int Replicate = 100;
130     public static final int Right = 101;
131     public static final int Locate = 112;
132     public static final int Locate2 = 113;
133     public static final int ToChar = 114;
134     public static final int ToCharWithFormat = 115;
135     public static final int RightTrim2 = 116;
136     public static final int Any = 118;
137     public static final int Some = 119;
138     public static final int All = 120;
139     public static final int Trim2 = 121;
140     public static final int LeftTrim2 = 122;
141
142     // Date
143
public static final int AddMonths = 47;
144     public static final int DateToString = 48;
145     public static final int LastDay = 49;
146     public static final int MonthsBetween = 50;
147     public static final int NextDay = 51;
148     public static final int RoundDate = 52;
149     public static final int ToDate = 53;
150     public static final int Today = 54;
151     public static final int AddDate = 90;
152     public static final int DateName = 92;
153     public static final int DatePart = 93;
154     public static final int DateDifference = 94;
155     public static final int TruncateDate = 102;
156     public static final int NewTime = 103;
157     public static final int Nvl = 104;
158     public static final int currentDate = 123;
159
160     // Math
161
public static final int Ceil = 55;
162     public static final int Cos = 56;
163     public static final int Cosh = 57;
164     public static final int Abs = 58;
165     public static final int Acos = 59;
166     public static final int Asin = 60;
167     public static final int Atan = 61;
168     public static final int Exp = 62;
169     public static final int Sqrt = 63;
170     public static final int Floor = 64;
171     public static final int Ln = 65;
172     public static final int Log = 66;
173     public static final int Mod = 67;
174     public static final int Power = 68;
175     public static final int Round = 69;
176     public static final int Sign = 70;
177     public static final int Sin = 71;
178     public static final int Sinh = 72;
179     public static final int Tan = 73;
180     public static final int Tanh = 74;
181     public static final int Trunc = 75;
182     public static final int Greatest = 76;
183     public static final int Least = 77;
184     public static final int Add = 78;
185     public static final int Subtract = 79;
186     public static final int Divide = 80;
187     public static final int Multiply = 81;
188     public static final int Atan2 = 91;
189     public static final int Cot = 95;
190
191     // Object-relational
192
public static final int Deref = 82;
193     public static final int Ref = 83;
194     public static final int RefToHex = 84;
195     public static final int Value = 85;
196
197     //XML Specific
198
public static final int Extract = 106;
199     public static final int ExtractValue = 107;
200     public static final int ExistsNode = 108;
201     public static final int GetStringVal = 109;
202     public static final int GetNumberVal = 110;
203     public static final int IsFragment = 111;
204
205     /**
206      * ADVANCED:
207      * Create a new operator.
208      */

209     public ExpressionOperator() {
210         this.type = FunctionOperator;
211         // For bug 2780072 provide default behavior to make this class more useable.
212
setNodeClass(ClassConstants.FunctionExpression_Class);
213     }
214
215     /**
216      * ADVANCED:
217      * Create a new operator with the given name(s) and strings to print.
218      */

219     public ExpressionOperator(int selector, Vector newDatabaseStrings) {
220         this.type = FunctionOperator;
221         // For bug 2780072 provide default behavior to make this class more useable.
222
setNodeClass(ClassConstants.FunctionExpression_Class);
223         this.selector = selector;
224         this.printsAs(newDatabaseStrings);
225     }
226
227     /**
228      * INTERNAL:
229      * Build operator.
230      */

231     public static ExpressionOperator abs() {
232         return simpleFunction(Abs, "ABS");
233     }
234
235     /**
236      * INTERNAL:
237      * Build operator.
238      */

239     public static ExpressionOperator acos() {
240         return simpleFunction(Acos, "ACOS");
241     }
242
243     /**
244      * INTERNAL:
245      * Build operator.
246      */

247     public static ExpressionOperator addDate() {
248         ExpressionOperator exOperator = simpleThreeArgumentFunction(AddDate, "DATEADD");
249         int[] indices = new int[3];
250         indices[0] = 1;
251         indices[1] = 2;
252         indices[2] = 0;
253
254         exOperator.setArgumentIndices(indices);
255         return exOperator;
256     }
257
258     /**
259      * INTERNAL:
260      * Build operator.
261      */

262     public static ExpressionOperator addMonths() {
263         return simpleTwoArgumentFunction(AddMonths, "ADD_MONTHS");
264     }
265
266     /**
267      * ADVANCED:
268      * Add an operator to the global list of operators.
269      */

270     public static void addOperator(ExpressionOperator exOperator) {
271         allOperators.put(new Integer JavaDoc(exOperator.getSelector()), exOperator);
272     }
273
274     /**
275      * INTERNAL:
276      * Create the AND operator.
277      */

278     public static ExpressionOperator and() {
279         return simpleLogical(And, "AND", "and");
280     }
281
282     /**
283      * INTERNAL:
284      * Apply this to an object in memory.
285      * Throw an error if the function is not supported.
286      */

287     public Object JavaDoc applyFunction(Object JavaDoc source, Vector arguments) {
288         if (source instanceof String JavaDoc) {
289             if (getSelector() == ToUpperCase) {
290                 return ((String JavaDoc)source).toUpperCase();
291             } else if (getSelector() == ToLowerCase) {
292                 return ((String JavaDoc)source).toLowerCase();
293             } else if ((getSelector() == Concat) && (arguments.size() == 1) && (arguments.elementAt(0) instanceof String JavaDoc)) {
294                 return ((String JavaDoc)source).concat((String JavaDoc)arguments.elementAt(0));
295             } else if ((getSelector() == Substring) && (arguments.size() == 2) && (arguments.elementAt(0) instanceof Number JavaDoc) && (arguments.elementAt(1) instanceof Number JavaDoc)) {
296                 // assume the first parameter to be 1-based first index of the substring, the second - substring length.
297
int beginIndexInclusive = ((Number JavaDoc)arguments.elementAt(0)).intValue() - 1;
298                 int endIndexExclusive = beginIndexInclusive + ((Number JavaDoc)arguments.elementAt(1)).intValue();
299                 return ((String JavaDoc)source).substring(beginIndexInclusive, endIndexExclusive);
300             } else if (getSelector() == ToNumber) {
301                 return new java.math.BigDecimal JavaDoc((String JavaDoc)source);
302             } else if (getSelector() == Trim) {
303                 return ((String JavaDoc)source).trim();
304             } else if (getSelector() == Length) {
305                 return new Integer JavaDoc(((String JavaDoc)source).length());
306             }
307         } else if (source instanceof Number JavaDoc) {
308             if (getSelector() == Ceil) {
309                 return new Double JavaDoc(Math.ceil(((Number JavaDoc)source).doubleValue()));
310             } else if (getSelector() == Cos) {
311                 return new Double JavaDoc(Math.cos(((Number JavaDoc)source).doubleValue()));
312             } else if (getSelector() == Abs) {
313                 return new Double JavaDoc(Math.abs(((Number JavaDoc)source).doubleValue()));
314             } else if (getSelector() == Acos) {
315                 return new Double JavaDoc(Math.acos(((Number JavaDoc)source).doubleValue()));
316             } else if (getSelector() == Asin) {
317                 return new Double JavaDoc(Math.asin(((Number JavaDoc)source).doubleValue()));
318             } else if (getSelector() == Atan) {
319                 return new Double JavaDoc(Math.atan(((Number JavaDoc)source).doubleValue()));
320             } else if (getSelector() == Exp) {
321                 return new Double JavaDoc(Math.exp(((Number JavaDoc)source).doubleValue()));
322             } else if (getSelector() == Sqrt) {
323                 return new Double JavaDoc(Math.sqrt(((Number JavaDoc)source).doubleValue()));
324             } else if (getSelector() == Floor) {
325                 return new Double JavaDoc(Math.floor(((Number JavaDoc)source).doubleValue()));
326             } else if (getSelector() == Log) {
327                 return new Double JavaDoc(Math.log(((Number JavaDoc)source).doubleValue()));
328             } else if ((getSelector() == Power) && (arguments.size() == 1) && (arguments.elementAt(0) instanceof Number JavaDoc)) {
329                 return new Double JavaDoc(Math.pow(((Number JavaDoc)source).doubleValue(), (((Number JavaDoc)arguments.elementAt(0)).doubleValue())));
330             } else if (getSelector() == Round) {
331                 return new Double JavaDoc(Math.round(((Number JavaDoc)source).doubleValue()));
332             } else if (getSelector() == Sin) {
333                 return new Double JavaDoc(Math.sin(((Number JavaDoc)source).doubleValue()));
334             } else if (getSelector() == Tan) {
335                 return new Double JavaDoc(Math.tan(((Number JavaDoc)source).doubleValue()));
336             } else if ((getSelector() == Greatest) && (arguments.size() == 1) && (arguments.elementAt(0) instanceof Number JavaDoc)) {
337                 return new Double JavaDoc(Math.max(((Number JavaDoc)source).doubleValue(), (((Number JavaDoc)arguments.elementAt(0)).doubleValue())));
338             } else if ((getSelector() == Least) && (arguments.size() == 1) && (arguments.elementAt(0) instanceof Number JavaDoc)) {
339                 return new Double JavaDoc(Math.min(((Number JavaDoc)source).doubleValue(), (((Number JavaDoc)arguments.elementAt(0)).doubleValue())));
340             } else if ((getSelector() == Add) && (arguments.size() == 1) && (arguments.elementAt(0) instanceof Number JavaDoc)) {
341                 return new Double JavaDoc(((Number JavaDoc)source).doubleValue() + (((Number JavaDoc)arguments.elementAt(0)).doubleValue()));
342             } else if ((getSelector() == Subtract) && (arguments.size() == 1) && (arguments.elementAt(0) instanceof Number JavaDoc)) {
343                 return new Double JavaDoc(((Number JavaDoc)source).doubleValue() - (((Number JavaDoc)arguments.elementAt(0)).doubleValue()));
344             } else if ((getSelector() == Divide) && (arguments.size() == 1) && (arguments.elementAt(0) instanceof Number JavaDoc)) {
345                 return new Double JavaDoc(((Number JavaDoc)source).doubleValue() / (((Number JavaDoc)arguments.elementAt(0)).doubleValue()));
346             } else if ((getSelector() == Multiply) && (arguments.size() == 1) && (arguments.elementAt(0) instanceof Number JavaDoc)) {
347                 return new Double JavaDoc(((Number JavaDoc)source).doubleValue() * (((Number JavaDoc)arguments.elementAt(0)).doubleValue()));
348             }
349         }
350
351         throw QueryException.cannotConformExpression();
352     }
353
354     /**
355      * INTERNAL:
356      * Create the ASCENDING operator.
357      */

358     public static ExpressionOperator ascending() {
359         return simpleOrdering(Ascending, "ASC", "ascending");
360     }
361
362     /**
363      * INTERNAL:
364      * Build operator.
365      */

366     public static ExpressionOperator ascii() {
367         return simpleFunction(Ascii, "ASCII");
368     }
369
370     /**
371      * INTERNAL:
372      * Build operator.
373      */

374     public static ExpressionOperator asin() {
375         return simpleFunction(Asin, "ASIN");
376     }
377
378     /**
379      * INTERNAL:
380      * Build operator.
381      */

382     public static ExpressionOperator atan() {
383         return simpleFunction(Atan, "ATAN");
384     }
385
386     /**
387      * INTERNAL:
388      * Create the AVERAGE operator.
389      */

390     public static ExpressionOperator average() {
391         return simpleAggregate(Average, "AVG", "average");
392     }
393
394     /**
395      * ADVANCED:
396      * Tell the operator to be postfix, i.e. its strings start printing after
397      * those of its first argument.
398      */

399     public void bePostfix() {
400         isPrefix = false;
401     }
402
403     /**
404      * ADVANCED:
405      * Tell the operator to be pretfix, i.e. its strings start printing before
406      * those of its first argument.
407      */

408     public void bePrefix() {
409         isPrefix = true;
410     }
411
412     /**
413      * INTERNAL:
414      * Make this a repeating argument. Currently unused.
415      */

416     public void beRepeating() {
417         isRepeating = true;
418     }
419
420     /**
421      * INTERNAL:
422      * Create the BETWEEN Operator
423      */

424     public static ExpressionOperator between() {
425         ExpressionOperator result = new ExpressionOperator();
426         result.setSelector(Between);
427         result.setType(ComparisonOperator);
428         Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance();
429         v.addElement("(");
430         v.addElement(" BETWEEN ");
431         v.addElement(" AND ");
432         v.addElement(")");
433         result.printsAs(v);
434         result.bePrefix();
435         result.setNodeClass(ClassConstants.FunctionExpression_Class);
436         return result;
437     }
438
439     /**
440      * INTERNAL:
441      * Build operator.
442      * Note: This operator works differently from other operators.
443      * @see Expression#caseStatement(Hashtable, String)
444      */

445     public static ExpressionOperator caseStatement() {
446         ExpressionOperator exOperator = new ExpressionOperator();
447         exOperator.setType(FunctionOperator);
448         exOperator.setSelector(Case);
449         exOperator.bePrefix();
450         exOperator.setNodeClass(FunctionExpression.class);
451         return exOperator;
452     }
453
454     /**
455      * INTERNAL:
456      * Build operator.
457      */

458     public static ExpressionOperator ceil() {
459         return simpleFunction(Ceil, "CEIL");
460     }
461
462     /**
463      * INTERNAL:
464      * Build operator.
465      */

466     public static ExpressionOperator charIndex() {
467         return simpleTwoArgumentFunction(CharIndex, "CHARINDEX");
468     }
469
470     /**
471      * INTERNAL:
472      * Build operator.
473      */

474     public static ExpressionOperator charLength() {
475         return simpleFunction(CharLength, "CHAR_LENGTH");
476     }
477
478     /**
479      * INTERNAL:
480      * Build operator.
481      */

482     public static ExpressionOperator chr() {
483         return simpleFunction(Chr, "CHR");
484     }
485
486     /**
487      * INTERNAL:
488      * Build operator.
489      */

490     public static ExpressionOperator concat() {
491         return simpleMath(Concat, "+");
492     }
493
494     /**
495      * INTERNAL:
496      * Compare bewteen in memory.
497      */

498     public boolean conformBetween(Object JavaDoc left, Object JavaDoc right) {
499         Object JavaDoc start = ((Vector)right).elementAt(0);
500         Object JavaDoc end = ((Vector)right).elementAt(1);
501         if ((left == null) || (start == null) || (end == null)) {
502             return false;
503         }
504         if ((left instanceof Number JavaDoc) && (start instanceof Number JavaDoc) && (end instanceof Number JavaDoc)) {
505             return ((((Number JavaDoc)left).doubleValue()) >= (((Number JavaDoc)start).doubleValue())) && ((((Number JavaDoc)left).doubleValue()) <= (((Number JavaDoc)end).doubleValue()));
506         } else if ((left instanceof String JavaDoc) && (start instanceof String JavaDoc) && (end instanceof String JavaDoc)) {
507             return ((((String JavaDoc)left).compareTo(((String JavaDoc)start)) > 0) || (((String JavaDoc)left).compareTo(((String JavaDoc)start)) == 0)) && ((((String JavaDoc)left).compareTo(((String JavaDoc)end)) < 0) || (((String JavaDoc)left).compareTo(((String JavaDoc)end)) == 0));
508         } else if ((left instanceof java.util.Date JavaDoc) && (start instanceof java.util.Date JavaDoc) && (end instanceof java.util.Date JavaDoc)) {
509             return (((java.util.Date JavaDoc)left).after(((java.util.Date JavaDoc)start)) || ((java.util.Date JavaDoc)left).equals(((java.util.Date JavaDoc)start))) && (((java.util.Date JavaDoc)left).before(((java.util.Date JavaDoc)end)) || ((java.util.Date JavaDoc)left).equals(((java.util.Date JavaDoc)end)));
510         }
511
512         throw QueryException.cannotConformExpression();
513     }
514
515     /**
516      * INTERNAL:
517      * Compare like in memory.
518      * This only works for % not _.
519      * @author Christian Weeks aka ChristianLink
520      */

521     public boolean conformLike(Object JavaDoc left, Object JavaDoc right) {
522         if ((right == null) && (left == null)) {
523             return true;
524         }
525         if (!(right instanceof String JavaDoc) || !(left instanceof String JavaDoc)) {
526             throw QueryException.cannotConformExpression();
527         }
528         String JavaDoc likeString = (String JavaDoc)right;
529         if (likeString.indexOf("_") != -1) {
530             throw QueryException.cannotConformExpression();
531         }
532         String JavaDoc value = (String JavaDoc)left;
533         if (likeString.indexOf("%") == -1) {
534             // No % symbols
535
return left.equals(right);
536         }
537         boolean strictStart = !likeString.startsWith("%");
538         boolean strictEnd = !likeString.endsWith("%");
539         StringTokenizer tokens = new StringTokenizer(likeString, "%");
540         int lastPosition = 0;
541         String JavaDoc lastToken = null;
542         if (strictStart) {
543             lastToken = tokens.nextToken();
544             if (!value.startsWith(lastToken)) {
545                 return false;
546             }
547         }
548         while (tokens.hasMoreTokens()) {
549             lastToken = tokens.nextToken();
550             lastPosition = value.indexOf(lastToken, lastPosition);
551             if (lastPosition < 0) {
552                 return false;
553             }
554         }
555         if (strictEnd) {
556             return value.endsWith(lastToken);
557         }
558         return true;
559     }
560
561     /**
562      * INTERNAL:
563      * Build operator.
564      */

565     public static ExpressionOperator cos() {
566         return simpleFunction(Cos, "COS");
567     }
568
569     /**
570      * INTERNAL:
571      * Build operator.
572      */

573     public static ExpressionOperator cosh() {
574         return simpleFunction(Cosh, "COSH");
575     }
576
577     /**
578      * INTERNAL:
579      * Build operator.
580      */

581     public static ExpressionOperator cot() {
582         return simpleFunction(Cot, "COT");
583     }
584
585     /**
586      * INTERNAL:
587      * Create the COUNT operator.
588      */

589     public static ExpressionOperator count() {
590         return simpleAggregate(Count, "COUNT", "count");
591     }
592
593     /**
594      * INTERNAL:
595      * Build operator.
596      */

597     public static ExpressionOperator dateDifference() {
598         return simpleThreeArgumentFunction(DateDifference, "DATEDIFF");
599     }
600
601     /**
602      * INTERNAL:
603      * Build operator.
604      */

605     public static ExpressionOperator dateName() {
606         return simpleTwoArgumentFunction(DateName, "DATENAME");
607     }
608
609     /**
610      * INTERNAL:
611      * Oracle equivalent to DATENAME is TO_CHAR.
612      */

613     public static ExpressionOperator oracleDateName() {
614         ExpressionOperator exOperator = new ExpressionOperator();
615         exOperator.setType(FunctionOperator);
616         exOperator.setSelector(DateName);
617         Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(3);
618         v.addElement("TO_CHAR(");
619         v.addElement(", '");
620         v.addElement("')");
621         exOperator.printsAs(v);
622         exOperator.bePrefix();
623         int[] indices = { 1, 0 };
624         exOperator.setArgumentIndices(indices);
625         exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
626         return exOperator;
627     }
628
629     /**
630      * INTERNAL:
631      * Build operator.
632      */

633     public static ExpressionOperator datePart() {
634         return simpleTwoArgumentFunction(DatePart, "DATEPART");
635     }
636
637     /**
638      * INTERNAL:
639      * Build operator.
640      */

641     public static ExpressionOperator dateToString() {
642         return simpleFunction(DateToString, "TO_CHAR");
643     }
644
645     /**
646      * INTERNAL:
647      * Build operator.
648      */

649     public static ExpressionOperator toChar() {
650         return simpleFunction(ToChar, "TO_CHAR");
651     }
652
653     /**
654      * INTERNAL:
655      * Build operator.
656      */

657     public static ExpressionOperator toCharWithFormat() {
658         return simpleTwoArgumentFunction(ToCharWithFormat, "TO_CHAR");
659     }
660
661     /**
662      * INTERNAL:
663      * Build operator.
664      * Note: This operator works differently from other operators.
665      * @see Expression#decode(Hashtable, String)
666      */

667     public static ExpressionOperator decode() {
668         ExpressionOperator exOperator = new ExpressionOperator();
669
670         exOperator.setSelector(Decode);
671
672         exOperator.setNodeClass(FunctionExpression.class);
673         exOperator.setType(FunctionOperator);
674         exOperator.bePrefix();
675         return exOperator;
676     }
677
678     /**
679      * INTERNAL:
680      * Build operator.
681      */

682     public static ExpressionOperator deref() {
683         return simpleFunction(Deref, "DEREF");
684     }
685
686     /**
687      * INTERNAL:
688      * Create the DESCENDING operator.
689      */

690     public static ExpressionOperator descending() {
691         return simpleOrdering(Descending, "DESC", "descending");
692     }
693
694     /**
695      * INTERNAL:
696      * Build operator.
697      */

698     public static ExpressionOperator difference() {
699         return simpleTwoArgumentFunction(Difference, "DIFFERENCE");
700     }
701
702     /**
703      * INTERNAL:
704      * Create the DISTINCT operator.
705      */

706     public static ExpressionOperator distinct() {
707         return simpleFunction(Distinct, "DISTINCT", "distinct");
708     }
709
710     /**
711      * INTERNAL:
712      * Compare the values in memory.
713      * Used for in-memory querying, all operators are not support.
714      */

715     public boolean doesRelationConform(Object JavaDoc left, Object JavaDoc right) {
716         // Big case statement follows.
717
// Note, compareTo for String returns a number <= -1 if the String is less than. We assumed that
718
// it would return -1. The same thing for strings that are greater than (ie it returns >= 1). PWK
719
// Equals
720
if (getSelector() == Equal) {
721             if ((left == null) && (right == null)) {
722                 return true;
723             } else if ((left == null) || (right == null)) {
724                 return false;
725             }
726             if (((left instanceof Number JavaDoc) && (right instanceof Number JavaDoc)) && (left.getClass() != right.getClass())) {
727                 return ((Number JavaDoc)left).doubleValue() == ((Number JavaDoc)right).doubleValue();
728             }
729             return left.equals(right);
730         } else if (getSelector() == NotEqual) {
731             if ((left == null) && (right == null)) {
732                 return false;
733             } else if ((left == null) || (right == null)) {
734                 return true;
735             }
736             return !left.equals(right);
737         } else if (getSelector() == IsNull) {
738             return (left == null);
739         }
740         if (getSelector() == NotNull) {
741             return (left != null);
742         }
743         // Less thans, greater thans
744
else if (getSelector() == LessThan) {// You have gottan love polymorphism in Java, NOT!!!
745
if ((left == null) || (right == null)) {
746                 return false;
747             }
748             if ((left instanceof Number JavaDoc) && (right instanceof Number JavaDoc)) {
749                 return (((Number JavaDoc)left).doubleValue()) < (((Number JavaDoc)right).doubleValue());
750             } else if ((left instanceof String JavaDoc) && (right instanceof String JavaDoc)) {
751                 return ((String JavaDoc)left).compareTo(((String JavaDoc)right)) < 0;
752             } else if ((left instanceof java.util.Date JavaDoc) && (right instanceof java.util.Date JavaDoc)) {
753                 return ((java.util.Date JavaDoc)left).before(((java.util.Date JavaDoc)right));
754             }
755         } else if (getSelector() == LessThanEqual) {
756             if ((left == null) && (right == null)) {
757                 return true;
758             } else if ((left == null) || (right == null)) {
759                 return false;
760             }
761             if ((left instanceof Number JavaDoc) && (right instanceof Number JavaDoc)) {
762                 return (((Number JavaDoc)left).doubleValue()) <= (((Number JavaDoc)right).doubleValue());
763             } else if ((left instanceof String JavaDoc) && (right instanceof String JavaDoc)) {
764                 int compareValue = ((String JavaDoc)left).compareTo(((String JavaDoc)right));
765                 return (compareValue < 0) || (compareValue == 0);
766             } else if ((left instanceof java.util.Date JavaDoc) && (right instanceof java.util.Date JavaDoc)) {
767                 return ((java.util.Date JavaDoc)left).equals(((java.util.Date JavaDoc)right)) || ((java.util.Date JavaDoc)left).before(((java.util.Date JavaDoc)right));
768             }
769         } else if (getSelector() == GreaterThan) {
770             if ((left == null) || (right == null)) {
771                 return false;
772             }
773             if ((left instanceof Number JavaDoc) && (right instanceof Number JavaDoc)) {
774                 return (((Number JavaDoc)left).doubleValue()) > (((Number JavaDoc)right).doubleValue());
775             } else if ((left instanceof String JavaDoc) && (right instanceof String JavaDoc)) {
776                 int compareValue = ((String JavaDoc)left).compareTo(((String JavaDoc)right));
777                 return (compareValue > 0);
778             } else if ((left instanceof java.util.Date JavaDoc) && (right instanceof java.util.Date JavaDoc)) {
779                 return ((java.util.Date JavaDoc)left).after(((java.util.Date JavaDoc)right));
780             }
781         } else if (getSelector() == GreaterThanEqual) {
782             if ((left == null) && (right == null)) {
783                 return true;
784             } else if ((left == null) || (right == null)) {
785                 return false;
786             }
787             if ((left instanceof Number JavaDoc) && (right instanceof Number JavaDoc)) {
788                 return (((Number JavaDoc)left).doubleValue()) >= (((Number JavaDoc)right).doubleValue());
789             } else if ((left instanceof String JavaDoc) && (right instanceof String JavaDoc)) {
790                 int compareValue = ((String JavaDoc)left).compareTo(((String JavaDoc)right));
791                 return (compareValue > 0) || (compareValue == 0);
792             } else if ((left instanceof java.util.Date JavaDoc) && (right instanceof java.util.Date JavaDoc)) {
793                 return ((java.util.Date JavaDoc)left).equals(((java.util.Date JavaDoc)right)) || ((java.util.Date JavaDoc)left).after(((java.util.Date JavaDoc)right));
794             }
795         }
796         // Between
797
else if ((getSelector() == Between) && (right instanceof Vector) && (((Vector)right).size() == 2)) {
798             return conformBetween(left, right);
799         } else if ((getSelector() == NotBetween) && (right instanceof Vector) && (((Vector)right).size() == 2)) {
800             return !conformBetween(left, right);
801         }
802         // In
803
else if ((getSelector() == In) && (right instanceof Vector)) {
804             return ((Vector)right).contains(left);
805         } else if ((getSelector() == NotIn) && (right instanceof Vector)) {
806             return !((Vector)right).contains(left);
807         }
808         // Like
809
//conformLike(left, right);
810
else if ((getSelector() == Like) || (getSelector() == NotLike)) {
811             // the regular expression framework we use to conform like is only supported in
812
// JDK 1.4 and later. We will ask our JavaPlatform to do this for us.
813
int doesLikeConform = JavaPlatform.conformLike(left, right);
814             if (doesLikeConform == JavaPlatform.TRUE) {
815                 return getSelector() == Like;// Negate for NotLike
816
} else if (doesLikeConform == JavaPlatform.FALSE) {
817                 return getSelector() != Like;// Negate for NotLike
818
}
819         }
820
821         throw QueryException.cannotConformExpression();
822     }
823
824     /**
825      * INTERNAL:
826      * Initialize the outer join operator
827      * Note: This is merely a shell which is incomplete, and
828      * so will be replaced by the platform's operator when we
829      * go to print. We need to create this here so that the expression
830      * class is correct, normally it assumes functions for unknown operators.
831      */

832     public static ExpressionOperator equalOuterJoin() {
833         return simpleRelation(EqualOuterJoin, "=*");
834     }
835
836     /**
837      * PUBLIC:
838      * Test for equality
839      */

840     public boolean equals(Object JavaDoc object) {
841         if (!(object instanceof ExpressionOperator)) {
842             return false;
843         }
844         return getSelector() == ((ExpressionOperator)object).getSelector();
845     }
846
847     /**
848      * PUBLIC:
849      * Return the hash-code based on the unique selector.
850      */

851     public int hashCode() {
852         return getSelector();
853     }
854
855     /**
856      * INTERNAL:
857      * Create the EXISTS operator.
858      */

859     public static ExpressionOperator exists() {
860         ExpressionOperator exOperator = new ExpressionOperator();
861         exOperator.setType(FunctionOperator);
862         exOperator.setSelector(Exists);
863         Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(2);
864         v.addElement("EXISTS" + " ");
865         v.addElement(" ");
866         exOperator.printsAs(v);
867         exOperator.bePrefix();
868         exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
869         return exOperator;
870     }
871
872     /**
873      * INTERNAL:
874      * Build operator.
875      */

876     public static ExpressionOperator exp() {
877         return simpleFunction(Exp, "EXP");
878     }
879
880     /**
881      * INTERNAL:
882      * Create an expression for this operator, using the given base.
883      */

884     public Expression expressionFor(Expression base) {
885         return expressionForArguments(base, oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(0));
886     }
887
888     /**
889      * INTERNAL:
890      * Create an expression for this operator, using the given base and a single argument.
891      */

892     public Expression expressionFor(Expression base, Object JavaDoc value) {
893         return newExpressionForArgument(base, value);
894     }
895
896     /**
897      * INTERNAL:
898      * Create an expression for this operator, using the given base and a single argument.
899      * Base is used last in the expression
900      */

901     public Expression expressionForWithBaseLast(Expression base, Object JavaDoc value) {
902         return newExpressionForArgumentWithBaseLast(base, value);
903     }
904
905     /**
906      * INTERNAL:
907      * Create an expression for this operator, using the given base and arguments.
908      */

909     public Expression expressionForArguments(Expression base, Vector arguments) {
910         return newExpressionForArguments(base, arguments);
911     }
912
913     /**
914      * INTERNAL:
915      * Create the extract expression operator
916      */

917     public static ExpressionOperator extract() {
918         ExpressionOperator result = new ExpressionOperator();
919         Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance();
920         v.addElement("extract(");
921         v.addElement(",");
922         v.addElement(")");
923         result.printsAs(v);
924         result.bePrefix();
925         result.setSelector(Extract);
926         result.setNodeClass(ClassConstants.FunctionExpression_Class);
927         return result;
928     }
929
930     /**
931      * INTERNAL:
932      * Create the extractValue expression operator
933      */

934     public static ExpressionOperator extractValue() {
935         ExpressionOperator result = new ExpressionOperator();
936         Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance();
937         v.addElement("extractValue(");
938         v.addElement(",");
939         v.addElement(")");
940         result.printsAs(v);
941         result.bePrefix();
942         result.setSelector(ExtractValue);
943         result.setNodeClass(ClassConstants.FunctionExpression_Class);
944         return result;
945     }
946
947     /**
948      * INTERNAL:
949      * Create the existsNode expression operator
950      */

951     public static ExpressionOperator existsNode() {
952         ExpressionOperator result = new ExpressionOperator();
953         Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance();
954         v.addElement("existsNode(");
955         v.addElement(",");
956         v.addElement(")");
957         result.printsAs(v);
958         result.bePrefix();
959         result.setSelector(ExistsNode);
960         result.setNodeClass(ClassConstants.FunctionExpression_Class);
961         return result;
962     }
963
964     public static ExpressionOperator getStringVal() {
965         ExpressionOperator result = new ExpressionOperator();
966         Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance();
967         v.addElement(".getStringVal()");
968         result.printsAs(v);
969         result.bePostfix();
970         result.setSelector(GetStringVal);
971         result.setNodeClass(ClassConstants.FunctionExpression_Class);
972         return result;
973     }
974
975     public static ExpressionOperator getNumberVal() {
976         ExpressionOperator result = new ExpressionOperator();
977         Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance();
978         v.addElement(".getNumberVal()");
979         result.printsAs(v);
980         result.bePostfix();
981         result.setSelector(GetNumberVal);
982         result.setNodeClass(ClassConstants.FunctionExpression_Class);
983         return result;
984     }
985
986     public static ExpressionOperator isFragment() {
987         ExpressionOperator result = new ExpressionOperator();
988         Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance();
989         v.addElement(".isFragment()");
990         result.printsAs(v);
991         result.bePostfix();
992         result.setSelector(IsFragment);
993         result.setNodeClass(ClassConstants.FunctionExpression_Class);
994         return result;
995     }
996
997     /**
998      * INTERNAL:
999      * Build operator.
1000     */

1001    public static ExpressionOperator floor() {
1002        return simpleFunction(Floor, "FLOOR");
1003    }
1004
1005    /**
1006     * ADVANCED:
1007     * Return the hashtable of all operators.
1008     */

1009    public static synchronized Hashtable getAllOperators() {
1010        if (allOperators == null) {
1011            initializeOperators();
1012        }
1013        return allOperators;
1014    }
1015
1016    /**
1017     * INTERNAL:
1018     */

1019    public String JavaDoc[] getDatabaseStrings() {
1020        return databaseStrings;
1021    }
1022
1023    /**
1024     * INTERNAL:
1025     */

1026    public String JavaDoc[] getJavaStrings() {
1027        return javaStrings;
1028    }
1029
1030    /**
1031     * INTERNAL:
1032     */

1033    public Class JavaDoc getNodeClass() {
1034        return nodeClass;
1035    }
1036
1037    /**
1038     * INTERNAL:
1039     * Lookup the operator with the given name.
1040     */

1041    public static ExpressionOperator getOperator(Integer JavaDoc selector) {
1042        return (ExpressionOperator)getAllOperators().get(selector);
1043    }
1044
1045    /**
1046     * INTERNAL:
1047     * Return the selector id.
1048     */

1049    public int getSelector() {
1050        return selector;
1051    }
1052
1053    /**
1054     * ADVANCED:
1055     * Return the type of function.
1056     * This must be one of the static function types defined in this class.
1057     */

1058    public int getType() {
1059        return this.type;
1060    }
1061
1062    /**
1063     * INTERNAL:
1064     * Build operator.
1065     */

1066    public static ExpressionOperator greatest() {
1067        return simpleTwoArgumentFunction(Greatest, "GREATEST");
1068    }
1069
1070    /**
1071     * INTERNAL:
1072     * Build operator.
1073     */

1074    public static ExpressionOperator hexToRaw() {
1075        return simpleFunction(HexToRaw, "HEXTORAW");
1076    }
1077
1078    /**
1079     * INTERNAL:
1080     * Build operator.
1081     */

1082    public static ExpressionOperator ifNull() {
1083        return simpleTwoArgumentFunction(Nvl, "NVL");
1084    }
1085
1086    /**
1087     * INTERNAL:
1088     * Create the IN operator.
1089     */

1090    public static ExpressionOperator in() {
1091        ExpressionOperator result = new ExpressionOperator();
1092        result.setType(ExpressionOperator.FunctionOperator);
1093        result.setSelector(In);
1094        Vector v = new Vector(2);
1095        v.addElement(" IN (");
1096        v.addElement(")");
1097        result.printsAs(v);
1098        result.bePostfix();
1099        result.setNodeClass(ClassConstants.FunctionExpression_Class);
1100        return result;
1101    }
1102
1103    /**
1104     * INTERNAL:
1105     * Build operator.
1106     */

1107    public static ExpressionOperator initcap() {
1108        return simpleFunction(Initcap, "INITCAP");
1109    }
1110
1111    /**
1112     * INTERNAL:
1113     */

1114    protected static void initializeAggregateFunctionOperators() {
1115        addOperator(count());
1116        addOperator(sum());
1117        addOperator(average());
1118        addOperator(minimum());
1119        addOperator(maximum());
1120        addOperator(variance());
1121        addOperator(standardDeviation());
1122        addOperator(distinct());
1123    }
1124
1125    /**
1126     * INTERNAL:
1127     */

1128    protected static void initializeFunctionOperators() {
1129        addOperator(notOperator());
1130        addOperator(ascending());
1131        addOperator(descending());
1132        addOperator(any());
1133        addOperator(some());
1134        addOperator(all());
1135        addOperator(in());
1136        addOperator(notIn());
1137    }
1138
1139    /**
1140     * INTERNAL:
1141     */

1142    protected static void initializeLogicalOperators() {
1143        addOperator(and());
1144        addOperator(or());
1145        addOperator(isNull());
1146        addOperator(notNull());
1147
1148    }
1149
1150    /**
1151     * INTERNAL:
1152     */

1153    public static Hashtable initializeOperators() {
1154        resetOperators();
1155        initializeFunctionOperators();
1156        initializeRelationOperators();
1157        initializeLogicalOperators();
1158        initializeAggregateFunctionOperators();
1159        return allOperators;
1160    }
1161
1162    /**
1163     * INTERNAL:
1164     * Initialize a mapping to the platform operator names for usage with exceptions.
1165     */

1166    public static String JavaDoc getPlatformOperatorName(int operator) {
1167        String JavaDoc name = (String JavaDoc)getPlatformOperatorNames().get(new Integer JavaDoc(operator));
1168        if (name == null) {
1169            name = String.valueOf(operator);
1170        }
1171        return name;
1172    }
1173
1174    /**
1175     * INTERNAL:
1176     * Initialize a mapping to the platform operator names for usage with exceptions.
1177     */

1178    public static synchronized Hashtable getPlatformOperatorNames() {
1179        if (platformOperatorNames == null) {
1180            platformOperatorNames = new Hashtable();
1181            platformOperatorNames.put(new Integer JavaDoc(ToUpperCase), "ToUpperCase");
1182            platformOperatorNames.put(new Integer JavaDoc(ToLowerCase), "ToLowerCase");
1183            platformOperatorNames.put(new Integer JavaDoc(Chr), "Chr");
1184            platformOperatorNames.put(new Integer JavaDoc(Concat), "Concat");
1185            platformOperatorNames.put(new Integer JavaDoc(HexToRaw), "HexToRaw");
1186            platformOperatorNames.put(new Integer JavaDoc(Initcap), "Initcap");
1187            platformOperatorNames.put(new Integer JavaDoc(Instring), "Instring");
1188            platformOperatorNames.put(new Integer JavaDoc(Soundex), "Soundex");
1189            platformOperatorNames.put(new Integer JavaDoc(LeftPad), "LeftPad");
1190            platformOperatorNames.put(new Integer JavaDoc(LeftTrim), "LeftTrim");
1191            platformOperatorNames.put(new Integer JavaDoc(RightPad), "RightPad");
1192            platformOperatorNames.put(new Integer JavaDoc(RightTrim), "RightTrim");
1193            platformOperatorNames.put(new Integer JavaDoc(Substring), "Substring");
1194            platformOperatorNames.put(new Integer JavaDoc(Translate), "Translate");
1195            platformOperatorNames.put(new Integer JavaDoc(Ascii), "Ascii");
1196            platformOperatorNames.put(new Integer JavaDoc(Length), "Length");
1197            platformOperatorNames.put(new Integer JavaDoc(CharIndex), "CharIndex");
1198            platformOperatorNames.put(new Integer JavaDoc(CharLength), "CharLength");
1199            platformOperatorNames.put(new Integer JavaDoc(Difference), "Difference");
1200            platformOperatorNames.put(new Integer JavaDoc(Reverse), "Reverse");
1201            platformOperatorNames.put(new Integer JavaDoc(Replicate), "Replicate");
1202            platformOperatorNames.put(new Integer JavaDoc(Right), "Right");
1203            platformOperatorNames.put(new Integer JavaDoc(Locate), "Locate");
1204            platformOperatorNames.put(new Integer JavaDoc(Locate2), "Locate");
1205            platformOperatorNames.put(new Integer JavaDoc(ToNumber), "ToNumber");
1206            platformOperatorNames.put(new Integer JavaDoc(ToChar), "ToChar");
1207            platformOperatorNames.put(new Integer JavaDoc(ToCharWithFormat), "ToChar");
1208            platformOperatorNames.put(new Integer JavaDoc(AddMonths), "AddMonths");
1209            platformOperatorNames.put(new Integer JavaDoc(DateToString), "DateToString");
1210            platformOperatorNames.put(new Integer JavaDoc(MonthsBetween), "MonthsBetween");
1211            platformOperatorNames.put(new Integer JavaDoc(NextDay), "NextDay");
1212            platformOperatorNames.put(new Integer JavaDoc(RoundDate), "RoundDate");
1213            platformOperatorNames.put(new Integer JavaDoc(AddDate), "AddDate");
1214            platformOperatorNames.put(new Integer JavaDoc(DateName), "DateName");
1215            platformOperatorNames.put(new Integer JavaDoc(DatePart), "DatePart");
1216            platformOperatorNames.put(new Integer JavaDoc(DateDifference), "DateDifference");
1217            platformOperatorNames.put(new Integer JavaDoc(TruncateDate), "TruncateDate");
1218            platformOperatorNames.put(new Integer JavaDoc(NewTime), "NewTime");
1219            platformOperatorNames.put(new Integer JavaDoc(Nvl), "Nvl");
1220            platformOperatorNames.put(new Integer JavaDoc(NewTime), "NewTime");
1221            platformOperatorNames.put(new Integer JavaDoc(Ceil), "Ceil");
1222            platformOperatorNames.put(new Integer JavaDoc(Cos), "Cos");
1223            platformOperatorNames.put(new Integer JavaDoc(Cosh), "Cosh");
1224            platformOperatorNames.put(new Integer JavaDoc(Abs), "Abs");
1225            platformOperatorNames.put(new Integer JavaDoc(Acos), "Acos");
1226            platformOperatorNames.put(new Integer JavaDoc(Asin), "Asin");
1227            platformOperatorNames.put(new Integer JavaDoc(Atan), "Atan");
1228            platformOperatorNames.put(new Integer JavaDoc(Exp), "Exp");
1229            platformOperatorNames.put(new Integer JavaDoc(Sqrt), "Sqrt");
1230            platformOperatorNames.put(new Integer JavaDoc(Floor), "Floor");
1231            platformOperatorNames.put(new Integer JavaDoc(Ln), "Ln");
1232            platformOperatorNames.put(new Integer JavaDoc(Log), "Log");
1233            platformOperatorNames.put(new Integer JavaDoc(Mod), "Mod");
1234            platformOperatorNames.put(new Integer JavaDoc(Power), "Power");
1235            platformOperatorNames.put(new Integer JavaDoc(Round), "Round");
1236            platformOperatorNames.put(new Integer JavaDoc(Sign), "Sign");
1237            platformOperatorNames.put(new Integer JavaDoc(Sin), "Sin");
1238            platformOperatorNames.put(new Integer JavaDoc(Sinh), "Sinh");
1239            platformOperatorNames.put(new Integer JavaDoc(Tan), "Tan");
1240            platformOperatorNames.put(new Integer JavaDoc(Tanh), "Tanh");
1241            platformOperatorNames.put(new Integer JavaDoc(Trunc), "Trunc");
1242            platformOperatorNames.put(new Integer JavaDoc(Greatest), "Greatest");
1243            platformOperatorNames.put(new Integer JavaDoc(Least), "Least");
1244            platformOperatorNames.put(new Integer JavaDoc(Add), "Add");
1245            platformOperatorNames.put(new Integer JavaDoc(Subtract), "Subtract");
1246            platformOperatorNames.put(new Integer JavaDoc(Divide), "Divide");
1247            platformOperatorNames.put(new Integer JavaDoc(Multiply), "Multiply");
1248            platformOperatorNames.put(new Integer JavaDoc(Atan2), "Atan2");
1249            platformOperatorNames.put(new Integer JavaDoc(Cot), "Cot");
1250            platformOperatorNames.put(new Integer JavaDoc(Deref), "Deref");
1251            platformOperatorNames.put(new Integer JavaDoc(Ref), "Ref");
1252            platformOperatorNames.put(new Integer JavaDoc(RefToHex), "RefToHex");
1253            platformOperatorNames.put(new Integer JavaDoc(Value), "Value");
1254            platformOperatorNames.put(new Integer JavaDoc(Extract), "Extract");
1255            platformOperatorNames.put(new Integer JavaDoc(ExtractValue), "ExtractValue");
1256            platformOperatorNames.put(new Integer JavaDoc(ExistsNode), "ExistsNode");
1257            platformOperatorNames.put(new Integer JavaDoc(GetStringVal), "GetStringVal");
1258            platformOperatorNames.put(new Integer JavaDoc(GetNumberVal), "GetNumberVal");
1259            platformOperatorNames.put(new Integer JavaDoc(IsFragment), "IsFragment");
1260        }
1261        return platformOperatorNames;
1262    }
1263
1264    /**
1265     * INTERNAL:
1266     */

1267    protected static void initializeRelationOperators() {
1268        addOperator(simpleRelation(Equal, "=", "equal"));
1269        addOperator(simpleRelation(NotEqual, "<>", "notEqual"));
1270        addOperator(simpleRelation(LessThan, "<", "lessThan"));
1271        addOperator(simpleRelation(LessThanEqual, "<=", "lessThanEqual"));
1272        addOperator(simpleRelation(GreaterThan, ">", "greaterThan"));
1273        addOperator(simpleRelation(GreaterThanEqual, ">=", "greaterThanEqual"));
1274
1275        addOperator(like());
1276        addOperator(likeEscape());
1277        addOperator(notLike());
1278        addOperator(between());
1279
1280        addOperator(exists());
1281        addOperator(notExists());
1282    }
1283
1284    /**
1285     * INTERNAL:
1286     * Build operator.
1287     */

1288    public static ExpressionOperator instring() {
1289        return simpleTwoArgumentFunction(Instring, "INSTR");
1290    }
1291
1292    /**
1293     * Aggregate functions are function in the select such as COUNT.
1294     */

1295    public boolean isAggregateOperator() {
1296        return getType() == AggregateOperator;
1297    }
1298
1299    /**
1300     * Comparison functions are functions such as = and >.
1301     */

1302    public boolean isComparisonOperator() {
1303        return getType() == ComparisonOperator;
1304    }
1305
1306    /**
1307     * INTERNAL:
1308     * If we have all the required information, this operator is complete
1309     * and can be used as is. Otherwise we will need to look up a platform-
1310     * specific operator.
1311     */

1312    public boolean isComplete() {
1313        return (databaseStrings != null) && (databaseStrings.length != 0);
1314    }
1315
1316    /**
1317     * General functions are any normal function such as UPPER.
1318     */

1319    public boolean isFunctionOperator() {
1320        return getType() == FunctionOperator;
1321    }
1322
1323    /**
1324     * Logical functions are functions such as and and or.
1325     */

1326    public boolean isLogicalOperator() {
1327        return getType() == LogicalOperator;
1328    }
1329
1330    /**
1331     * INTERNAL:
1332     * Create the ISNULL operator.
1333     */

1334    public static ExpressionOperator isNull() {
1335        ExpressionOperator result = new ExpressionOperator();
1336        result.setType(ComparisonOperator);
1337        result.setSelector(IsNull);
1338        Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance();
1339        v.addElement("(");
1340        v.addElement(" IS NULL)");
1341        result.printsAs(v);
1342        result.bePrefix();
1343        result.printsJavaAs(".isNull()");
1344        result.setNodeClass(ClassConstants.FunctionExpression_Class);
1345        return result;
1346    }
1347
1348    /**
1349     * Order functions are used in the order by such as ASC.
1350     */

1351    public boolean isOrderOperator() {
1352        return getType() == OrderOperator;
1353    }
1354
1355    /**
1356     * ADVANCED:
1357     * Return true if this is a prefix operator.
1358     */

1359    public boolean isPrefix() {
1360        return isPrefix;
1361    }
1362
1363    /**
1364     * INTERNAL:
1365     * Build operator.
1366     */

1367    public static ExpressionOperator lastDay() {
1368        return simpleFunction(LastDay, "LAST_DAY");
1369    }
1370
1371    /**
1372     * INTERNAL:
1373     * Build operator.
1374     */

1375    public static ExpressionOperator least() {
1376        return simpleTwoArgumentFunction(Least, "LEAST");
1377    }
1378
1379    /**
1380     * INTERNAL:
1381     * Build operator.
1382     */

1383    public static ExpressionOperator leftPad() {
1384        return simpleThreeArgumentFunction(LeftPad, "LPAD");
1385    }
1386
1387    /**
1388     * INTERNAL:
1389     * Build operator.
1390     */

1391    public static ExpressionOperator leftTrim() {
1392        return simpleFunction(LeftTrim, "LTRIM");
1393    }
1394
1395    /**
1396     * INTERNAL:
1397     * Build leftTrim operator that takes one parameter.
1398     */

1399    public static ExpressionOperator leftTrim2() {
1400        return simpleTwoArgumentFunction(LeftTrim2, "LTRIM");
1401    }
1402
1403    /**
1404     * INTERNAL:
1405     * Build operator.
1406     */

1407    public static ExpressionOperator length() {
1408        return simpleFunction(Length, "LENGTH");
1409    }
1410
1411    /**
1412     * INTERNAL:
1413     * Create the LIKE operator.
1414     */

1415    public static ExpressionOperator like() {
1416        return simpleRelation(Like, "LIKE", "like");
1417    }
1418
1419    /**
1420     * INTERNAL:
1421     * Create the LIKE operator.
1422     */

1423    public static ExpressionOperator likeEscape() {
1424        ExpressionOperator result = new ExpressionOperator();
1425        result.setSelector(LikeEscape);
1426        result.setType(ComparisonOperator);
1427        Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance();
1428        v.addElement("(");
1429        v.addElement(" LIKE ");
1430        v.addElement(" ESCAPE ");
1431        v.addElement(")");
1432        result.printsAs(v);
1433        result.bePrefix();
1434        result.setNodeClass(ClassConstants.FunctionExpression_Class);
1435        return result;
1436    }
1437
1438    /**
1439     * INTERNAL:
1440     * Build operator.
1441     */

1442    public static ExpressionOperator ln() {
1443        return simpleFunction(Ln, "LN");
1444    }
1445
1446    /**
1447     * INTERNAL:
1448     * Build locate operator i.e. LOCATE("ob", t0.F_NAME)
1449     */

1450    public static ExpressionOperator locate() {
1451        ExpressionOperator expOperator = simpleTwoArgumentFunction(Locate, "LOCATE");
1452        int[] argumentIndices = new int[2];
1453        argumentIndices[0] = 1;
1454        argumentIndices[1] = 0;
1455        expOperator.setArgumentIndices(argumentIndices);
1456        return expOperator;
1457    }
1458
1459    /**
1460     * INTERNAL:
1461     * Build locate operator with 3 params i.e. LOCATE("coffee", t0.DESCRIP, 4).
1462     * Last parameter is a start at.
1463     */

1464    public static ExpressionOperator locate2() {
1465        ExpressionOperator expOperator = simpleThreeArgumentFunction(Locate2, "LOCATE");
1466        int[] argumentIndices = new int[3];
1467        argumentIndices[0] = 1;
1468        argumentIndices[1] = 0;
1469        argumentIndices[2] = 2;
1470        expOperator.setArgumentIndices(argumentIndices);
1471        return expOperator;
1472    }
1473
1474    /**
1475     * INTERNAL:
1476     * Build operator.
1477     */

1478    public static ExpressionOperator log() {
1479        return simpleFunction(Log, "LOG");
1480    }
1481
1482    /**
1483     * INTERNAL:
1484     * Create the MAXIMUM operator.
1485     */

1486    public static ExpressionOperator maximum() {
1487        return simpleAggregate(Maximum, "MAX", "maximum");
1488    }
1489
1490    /**
1491     * INTERNAL:
1492     * Create the MINIMUM operator.
1493     */

1494    public static ExpressionOperator minimum() {
1495        return simpleAggregate(Minimum, "MIN", "minimum");
1496    }
1497
1498    /**
1499     * INTERNAL:
1500     * Build operator.
1501     */

1502    public static ExpressionOperator mod() {
1503        return simpleTwoArgumentFunction(Mod, "MOD");
1504    }
1505
1506    /**
1507     * INTERNAL:
1508     * Build operator.
1509     */

1510    public static ExpressionOperator monthsBetween() {
1511        return simpleTwoArgumentFunction(MonthsBetween, "MONTHS_BETWEEN");
1512    }
1513
1514    /**
1515     * INTERNAL:
1516     * Create a new expression. Optimized for the single argument case.
1517     */

1518    public Expression newExpressionForArgument(Expression base, Object JavaDoc singleArgument) {
1519        if (representsEqualToNull(singleArgument)) {
1520            return base.isNull();
1521        }
1522        if (representsNotEqualToNull(singleArgument)) {
1523            return base.notNull();
1524        }
1525
1526        try {
1527            Expression exp = null;
1528            if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
1529                try {
1530                    exp = (Expression)AccessController.doPrivileged(new PrivilegedNewInstanceFromClass(getNodeClass()));
1531                } catch (PrivilegedActionException JavaDoc exception) {
1532                    return null;
1533                }
1534            } else {
1535                exp = (Expression)PrivilegedAccessHelper.newInstanceFromClass(getNodeClass());
1536            }
1537            exp.create(base, singleArgument, this);
1538            return exp;
1539        } catch (InstantiationException JavaDoc e) {
1540            return null;
1541        } catch (IllegalAccessException JavaDoc f) {
1542            return null;
1543        }
1544    }
1545
1546    /**
1547     * INTERNAL:
1548     * Create a new expression. Optimized for the single argument case with base last
1549     */

1550    public Expression newExpressionForArgumentWithBaseLast(Expression base, Object JavaDoc singleArgument) {
1551        if (representsEqualToNull(singleArgument)) {
1552            return base.isNull();
1553        }
1554        if (representsNotEqualToNull(singleArgument)) {
1555            return base.notNull();
1556        }
1557
1558        try {
1559            Expression exp = null;
1560            if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
1561                try {
1562                    exp = (Expression)AccessController.doPrivileged(new PrivilegedNewInstanceFromClass(getNodeClass()));
1563                } catch (PrivilegedActionException JavaDoc exception) {
1564                    return null;
1565                }
1566            } else {
1567                exp = (Expression)PrivilegedAccessHelper.newInstanceFromClass(getNodeClass());
1568            }
1569            exp.createWithBaseLast(base, singleArgument, this);
1570            return exp;
1571        } catch (InstantiationException JavaDoc e) {
1572            return null;
1573        } catch (IllegalAccessException JavaDoc f) {
1574            return null;
1575        }
1576    }
1577
1578    /**
1579     * INTERNAL:
1580     * The general case.
1581     */

1582    public Expression newExpressionForArguments(Expression base, Vector arguments) {
1583        if (representsEqualToNull(arguments)) {
1584            return base.isNull();
1585        }
1586        if (representsNotEqualToNull(arguments)) {
1587            return base.notNull();
1588        }
1589
1590        try {
1591            Expression exp = null;
1592            if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
1593                try {
1594                    exp = (Expression)AccessController.doPrivileged(new PrivilegedNewInstanceFromClass(getNodeClass()));
1595                } catch (PrivilegedActionException JavaDoc exception) {
1596                    return null;
1597                }
1598            } else {
1599                exp = (Expression)PrivilegedAccessHelper.newInstanceFromClass(getNodeClass());
1600            }
1601            exp.create(base, arguments, this);
1602            return exp;
1603        } catch (InstantiationException JavaDoc e) {
1604            return null;
1605        } catch (IllegalAccessException JavaDoc f) {
1606            return null;
1607        }
1608    }
1609
1610    /**
1611     * INTERNAL:
1612     * Build operator.
1613     */

1614    public static ExpressionOperator newTime() {
1615        return simpleThreeArgumentFunction(NewTime, "NEW_TIME");
1616    }
1617
1618    /**
1619     * INTERNAL:
1620     * Build operator.
1621     */

1622    public static ExpressionOperator nextDay() {
1623        return simpleTwoArgumentFunction(NextDay, "NEXT_DAY");
1624    }
1625
1626    /**
1627     * INTERNAL:
1628     * Create the NOT EXISTS operator.
1629     */

1630    public static ExpressionOperator notExists() {
1631        ExpressionOperator exOperator = new ExpressionOperator();
1632        exOperator.setType(FunctionOperator);
1633        exOperator.setSelector(NotExists);
1634        Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(2);
1635        v.addElement("NOT EXISTS" + " ");
1636        v.addElement(" ");
1637        exOperator.printsAs(v);
1638        exOperator.bePrefix();
1639        exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
1640        return exOperator;
1641    }
1642
1643    /**
1644     * INTERNAL:
1645     * Create the NOTIN operator.
1646     */

1647    public static ExpressionOperator notIn() {
1648        ExpressionOperator result = new ExpressionOperator();
1649        result.setType(ExpressionOperator.FunctionOperator);
1650        result.setSelector(NotIn);
1651        Vector v = new Vector(2);
1652        v.addElement(" NOT IN (");
1653        v.addElement(")");
1654        result.printsAs(v);
1655        result.bePostfix();
1656        result.setNodeClass(ClassConstants.FunctionExpression_Class);
1657        return result;
1658    }
1659
1660    /**
1661     * INTERNAL:
1662     * Create the NOTLIKE operator.
1663     */

1664    public static ExpressionOperator notLike() {
1665        return simpleRelation(NotLike, "NOT LIKE", "notLike");
1666    }
1667
1668    /**
1669     * INTERNAL:
1670     * Create the NOTNULL operator.
1671     */

1672    public static ExpressionOperator notNull() {
1673        ExpressionOperator result = new ExpressionOperator();
1674        result.setType(ComparisonOperator);
1675        result.setSelector(NotNull);
1676        Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance();
1677        v.addElement("(");
1678        v.addElement(" IS NOT NULL)");
1679        result.printsAs(v);
1680        result.bePrefix();
1681        result.printsJavaAs(".notNull()");
1682        result.setNodeClass(ClassConstants.FunctionExpression_Class);
1683        return result;
1684    }
1685
1686    /**
1687     * INTERNAL:
1688     * Create the NOT operator.
1689     */

1690    public static ExpressionOperator notOperator() {
1691        ExpressionOperator result = new ExpressionOperator();
1692        result.setSelector(Not);
1693        Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance();
1694        v.addElement("NOT (");
1695        v.addElement(")");
1696        result.printsAs(v);
1697        result.bePrefix();
1698        result.printsJavaAs(".not()");
1699        result.setNodeClass(ClassConstants.FunctionExpression_Class);
1700        return result;
1701    }
1702
1703    /**
1704     * INTERNAL:
1705     * Create the OR operator.
1706     */

1707    public static ExpressionOperator or() {
1708        return simpleLogical(Or, "OR", "or");
1709    }
1710
1711    /**
1712     * INTERNAL:
1713     * Build operator.
1714     */

1715    public static ExpressionOperator power() {
1716        return simpleTwoArgumentFunction(Power, "POWER");
1717    }
1718
1719    /**
1720     * INTERNAL: Print the collection onto the SQL stream.
1721     */

1722    public void printCollection(Vector items, ExpressionSQLPrinter printer) {
1723        int dbStringIndex = 0;
1724        try {
1725            if (isPrefix()) {
1726                printer.getWriter().write(getDatabaseStrings()[0]);
1727                dbStringIndex = 1;
1728            } else {
1729                dbStringIndex = 0;
1730            }
1731        } catch (IOException e) {
1732            e.printStackTrace();
1733        }
1734
1735        for (int i = 0; i < items.size(); i++) {
1736            int index = 0;
1737            if (argumentIndices == null) {
1738                index = i;
1739            } else {
1740                index = argumentIndices[i];
1741            }
1742            ;
1743            Expression item = (Expression)items.elementAt(index);
1744            if ((getSelector() == Ref) || ((getSelector() == Deref) && (item.isObjectExpression()))) {
1745                DatabaseTable alias = ((ObjectExpression)item).aliasForTable((DatabaseTable)((ObjectExpression)item).getDescriptor().getTables().firstElement());
1746                printer.printString(alias.getName());
1747            } else if ((getSelector() == Count) && (item.isExpressionBuilder())) {
1748                printer.printString("*");
1749            } else if (getType() == FunctionOperator) {
1750                item.printSQLWithoutConversion(printer);
1751            } else {
1752                item.printSQL(printer);
1753            }
1754            if (dbStringIndex < getDatabaseStrings().length) {
1755                printer.printString(getDatabaseStrings()[dbStringIndex++]);
1756            }
1757        }
1758    }
1759
1760    /**
1761     * INTERNAL: Print the collection onto the SQL stream.
1762     */

1763    public void printJavaCollection(Vector items, ExpressionJavaPrinter printer) {
1764        int javaStringIndex = 0;
1765
1766        for (int i = 0; i < items.size(); i++) {
1767            Expression item = (Expression)items.elementAt(i);
1768            item.printJava(printer);
1769            if (javaStringIndex < getJavaStrings().length) {
1770                printer.printString(getJavaStrings()[javaStringIndex++]);
1771            }
1772        }
1773    }
1774
1775    /**
1776     * INTERNAL:
1777     * For performance, special case printing two children, since it's by far the most common
1778     */

1779    public void printDuo(Expression first, Expression second, ExpressionSQLPrinter printer) {
1780        int dbStringIndex;
1781        if (isPrefix()) {
1782            printer.printString(getDatabaseStrings()[0]);
1783            dbStringIndex = 1;
1784        } else {
1785            dbStringIndex = 0;
1786        }
1787
1788        first.printSQL(printer);
1789        if (dbStringIndex < getDatabaseStrings().length) {
1790            printer.printString(getDatabaseStrings()[dbStringIndex++]);
1791        }
1792        if (second != null) {
1793            second.printSQL(printer);
1794            if (dbStringIndex < getDatabaseStrings().length) {
1795                printer.printString(getDatabaseStrings()[dbStringIndex++]);
1796            }
1797        }
1798    }
1799
1800    /**
1801     * INTERNAL:
1802     * For performance, special case printing two children, since it's by far the most common
1803     */

1804    public void printJavaDuo(Expression first, Expression second, ExpressionJavaPrinter printer) {
1805        int javaStringIndex = 0;
1806
1807        first.printJava(printer);
1808        if (javaStringIndex < getJavaStrings().length) {
1809            printer.printString(getJavaStrings()[javaStringIndex++]);
1810        }
1811        if (second != null) {
1812            second.printJava(printer);
1813            if (javaStringIndex < getJavaStrings().length) {
1814                printer.printString(getJavaStrings()[javaStringIndex]);
1815            }
1816        }
1817    }
1818
1819    /**
1820     * ADVANCED:
1821     * Set the single string for this operator.
1822     */

1823    public void printsAs(String JavaDoc s) {
1824        Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(1);
1825        v.addElement(s);
1826        printsAs(v);
1827    }
1828
1829    /**
1830     * ADVANCED:
1831     * Set the strings for this operator.
1832     */

1833    public void printsAs(Vector dbStrings) {
1834        this.databaseStrings = new String JavaDoc[dbStrings.size()];
1835        for (int i = 0; i < dbStrings.size(); i++) {
1836            getDatabaseStrings()[i] = (String JavaDoc)dbStrings.elementAt(i);
1837        }
1838    }
1839
1840    /**
1841     * ADVANCED:
1842     * Set the single string for this operator.
1843     */

1844    public void printsJavaAs(String JavaDoc s) {
1845        Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(1);
1846        v.addElement(s);
1847        printsJavaAs(v);
1848    }
1849
1850    /**
1851     * ADVANCED:
1852     * Set the strings for this operator.
1853     */

1854    public void printsJavaAs(Vector dbStrings) {
1855        this.javaStrings = new String JavaDoc[dbStrings.size()];
1856        for (int i = 0; i < dbStrings.size(); i++) {
1857            getJavaStrings()[i] = (String JavaDoc)dbStrings.elementAt(i);
1858        }
1859    }
1860
1861    /**
1862     * INTERNAL:
1863     * Build operator.
1864     */

1865    public static ExpressionOperator ref() {
1866        return simpleFunction(Ref, "REF");
1867    }
1868
1869    /**
1870     * INTERNAL:
1871     * Build operator.
1872     */

1873    public static ExpressionOperator refToHex() {
1874        return simpleFunction(RefToHex, "REFTOHEX");
1875    }
1876
1877    /**
1878     * INTERNAL:
1879     * Build operator.
1880     */

1881    public static ExpressionOperator replace() {
1882        return simpleThreeArgumentFunction(Replace, "REPLACE");
1883    }
1884
1885    /**
1886     * INTERNAL:
1887     * Build operator.
1888     */

1889    public static ExpressionOperator replicate() {
1890        return simpleTwoArgumentFunction(Replicate, "REPLICATE");
1891    }
1892
1893    /**
1894     * INTERNAL:
1895     * Test if this operator instance represents a comparison to null, which
1896     * we have to print specially. Also special-cased for performance.
1897     */

1898    public boolean representsEqualToNull(Object JavaDoc singleArgument) {
1899        if (singleArgument instanceof Vector) {
1900            return representsEqualToNull((Vector)singleArgument);
1901        }
1902        return (getSelector() == Equal) && (singleArgument == null);
1903    }
1904
1905    /**
1906     * INTERNAL:
1907     * Test if this operator instance represents a comparison to null, which
1908     * we have to print specially.
1909     */

1910    public boolean representsEqualToNull(Vector arguments) {
1911        return (getSelector() == Equal) && (arguments.size() == 1) && (arguments.elementAt(0) == null);
1912    }
1913
1914    /**
1915     * INTERNAL:
1916     * Test if this operator instance represents a comparison to null, which
1917     * we have to print specially. Also special-cased for performance.
1918     */

1919    public boolean representsNotEqualToNull(Object JavaDoc singleArgument) {
1920        if (singleArgument instanceof Vector) {
1921            return representsNotEqualToNull((Vector)singleArgument);
1922        }
1923        return (getSelector() == NotEqual) && (singleArgument == null);
1924    }
1925
1926    /**
1927     * INTERNAL:
1928     * Test if this operator instance represents a comparison to null, which
1929     * we have to print specially.
1930     */

1931    public boolean representsNotEqualToNull(Vector arguments) {
1932        return (getSelector() == NotEqual) && (arguments.size() == 1) && (arguments.elementAt(0) == null);
1933    }
1934
1935    /**
1936     * INTERNAL:
1937     * Reset all the operators.
1938     */

1939    public static void resetOperators() {
1940        allOperators = new Hashtable();
1941    }
1942
1943    /**
1944     * INTERNAL:
1945     * Build operator.
1946     */

1947    public static ExpressionOperator reverse() {
1948        return simpleFunction(Reverse, "REVERSE");
1949    }
1950
1951    /**
1952     * INTERNAL:
1953     * Build operator.
1954     */

1955    public static ExpressionOperator right() {
1956        return simpleTwoArgumentFunction(Right, "RIGHT");
1957    }
1958
1959    /**
1960     * INTERNAL:
1961     * Build operator.
1962     */

1963    public static ExpressionOperator rightPad() {
1964        return simpleThreeArgumentFunction(RightPad, "RPAD");
1965    }
1966
1967    /**
1968     * INTERNAL:
1969     * Build operator.
1970     */

1971    public static ExpressionOperator rightTrim() {
1972        return simpleFunction(RightTrim, "RTRIM");
1973    }
1974
1975    /**
1976     * INTERNAL:
1977     * Build rightTrim operator that takes one parameter.
1978     * @bug 2916893 rightTrim(substring) broken.
1979     */

1980    public static ExpressionOperator rightTrim2() {
1981        return simpleTwoArgumentFunction(RightTrim2, "RTRIM");
1982    }
1983
1984    /**
1985     * INTERNAL:
1986     * Build operator.
1987     */

1988    public static ExpressionOperator round() {
1989        return simpleTwoArgumentFunction(Round, "ROUND");
1990    }
1991
1992    /**
1993     * INTERNAL:
1994     * Build operator.
1995     */

1996    public static ExpressionOperator roundDate() {
1997        return simpleTwoArgumentFunction(RoundDate, "ROUND");
1998    }
1999
2000    /**
2001     * ADVANCED:
2002     * Set the array of indexes to use when building the SQL function.
2003     */

2004    public void setArgumentIndices(int[] indices) {
2005        argumentIndices = indices;
2006    }
2007
2008    /**
2009     * ADVANCED:
2010     * Set the node class for this operator. For user-defined functions this is
2011     * set automatically but can be changed.
2012     * <p>A list of Operator types, an example, and the node class used follows.
2013     * <p>LogicalOperator AND LogicalExpression
2014     * <p>ComparisonOperator <> RelationExpression
2015     * <p>AggregateOperator COUNT FunctionExpression
2016     * <p>OrderOperator ASCENDING "
2017     * <p>FunctionOperator RTRIM "
2018     * <p>Node classes given belong to oracle.toplink.essentials.internal.expressions.
2019     */

2020    public void setNodeClass(Class JavaDoc nodeClass) {
2021        this.nodeClass = nodeClass;
2022    }
2023
2024    /**
2025     * INTERNAL:
2026     * Set the selector id.
2027     */

2028    public void setSelector(int selector) {
2029        this.selector = selector;
2030    }
2031
2032    /**
2033     * ADVANCED:
2034     * Set the type of function.
2035     * This must be one of the static function types defined in this class.
2036     */

2037    public void setType(int type) {
2038        this.type = type;
2039    }
2040
2041    /**
2042     * INTERNAL:
2043     * Build operator.
2044     */

2045    public static ExpressionOperator sign() {
2046        return simpleFunction(Sign, "SIGN");
2047    }
2048
2049    /**
2050     * INTERNAL:
2051     * Create an operator for a simple aggregate given a Java name and a single
2052     * String for the database (parentheses will be added automatically).
2053     */

2054    public static ExpressionOperator simpleAggregate(int selector, String JavaDoc databaseName, String JavaDoc javaName) {
2055        ExpressionOperator exOperator = new ExpressionOperator();
2056        exOperator.setType(AggregateOperator);
2057        exOperator.setSelector(selector);
2058        Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(2);
2059        v.addElement(databaseName + "(");
2060        v.addElement(")");
2061        exOperator.printsAs(v);
2062        exOperator.bePrefix();
2063        exOperator.printsJavaAs("." + javaName + "()");
2064        exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
2065        return exOperator;
2066    }
2067
2068    /**
2069     * INTERNAL:
2070     * Create an operator for a simple function given a Java name and a single
2071     * String for the database (parentheses will be added automatically).
2072     */

2073    public static ExpressionOperator simpleFunction(int selector, String JavaDoc databaseName) {
2074        ExpressionOperator exOperator = new ExpressionOperator();
2075        exOperator.setType(FunctionOperator);
2076        exOperator.setSelector(selector);
2077        Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(2);
2078        v.addElement(databaseName + "(");
2079        v.addElement(")");
2080        exOperator.printsAs(v);
2081        exOperator.bePrefix();
2082        exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
2083        return exOperator;
2084    }
2085
2086    /**
2087     * INTERNAL:
2088     * Create an operator for a simple function call without parentheses
2089     */

2090    public static ExpressionOperator simpleFunctionNoParentheses(int selector, String JavaDoc databaseName) {
2091        ExpressionOperator exOperator = new ExpressionOperator();
2092        exOperator.setType(FunctionOperator);
2093        exOperator.setSelector(selector);
2094        Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(1);
2095        v.addElement(databaseName);
2096        exOperator.printsAs(v);
2097        exOperator.bePrefix();
2098        exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
2099        return exOperator;
2100    }
2101
2102
2103    /**
2104     * INTERNAL:
2105     * Create an operator for a simple function given a Java name and a single
2106     * String for the database (parentheses will be added automatically).
2107     */

2108    public static ExpressionOperator simpleFunction(int selector, String JavaDoc databaseName, String JavaDoc javaName) {
2109        ExpressionOperator exOperator = simpleFunction(selector, databaseName);
2110        exOperator.printsJavaAs("." + javaName + "()");
2111        return exOperator;
2112    }
2113
2114    /**
2115     * INTERNAL:
2116     * Create an operator for a simple logical given a Java name and a single
2117     * String for the database (parentheses will be added automatically).
2118     */

2119    public static ExpressionOperator simpleLogical(int selector, String JavaDoc databaseName, String JavaDoc javaName) {
2120        ExpressionOperator exOperator = new ExpressionOperator();
2121        exOperator.setType(LogicalOperator);
2122        exOperator.setSelector(selector);
2123        exOperator.printsAs(" " + databaseName + " ");
2124        exOperator.bePostfix();
2125        Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(2);
2126        v.addElement("." + javaName + "(");
2127        v.addElement(")");
2128        exOperator.printsJavaAs(v);
2129        exOperator.setNodeClass(ClassConstants.LogicalExpression_Class);
2130        return exOperator;
2131    }
2132
2133    /**
2134     * INTERNAL:
2135     * Create an operator for a simple math operatin, i.e. +, -, *, /
2136     */

2137    public static ExpressionOperator simpleMath(int selector, String JavaDoc databaseName) {
2138        ExpressionOperator exOperator = new ExpressionOperator();
2139        exOperator.setType(FunctionOperator);
2140        exOperator.setSelector(selector);
2141        Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(3);
2142        v.addElement("(");
2143        v.addElement(" " + databaseName + " ");
2144        v.addElement(")");
2145        exOperator.printsAs(v);
2146        exOperator.bePrefix();
2147        exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
2148        return exOperator;
2149    }
2150
2151    /**
2152     * INTERNAL:
2153     * Create an operator for a simple ordering given a Java name and a single
2154     * String for the database (parentheses will be added automatically).
2155     */

2156    public static ExpressionOperator simpleOrdering(int selector, String JavaDoc databaseName, String JavaDoc javaName) {
2157        ExpressionOperator exOperator = new ExpressionOperator();
2158        exOperator.setType(OrderOperator);
2159        exOperator.setSelector(selector);
2160        exOperator.printsAs(" " + databaseName);
2161        exOperator.bePostfix();
2162        exOperator.printsJavaAs("." + javaName + "()");
2163        exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
2164        return exOperator;
2165    }
2166
2167    /**
2168     * INTERNAL:
2169     * Create an operator for a simple relation given a Java name and a single
2170     * String for the database (parentheses will be added automatically).
2171     */

2172    public static ExpressionOperator simpleRelation(int selector, String JavaDoc databaseName) {
2173        ExpressionOperator exOperator = new ExpressionOperator();
2174        exOperator.setType(ComparisonOperator);
2175        exOperator.setSelector(selector);
2176        exOperator.printsAs(" " + databaseName + " ");
2177        exOperator.bePostfix();
2178        exOperator.setNodeClass(ClassConstants.RelationExpression_Class);
2179        return exOperator;
2180    }
2181
2182    /**
2183     * INTERNAL:
2184     * Create an operator for a simple relation given a Java name and a single
2185     * String for the database (parentheses will be added automatically).
2186     */

2187    public static ExpressionOperator simpleRelation(int selector, String JavaDoc databaseName, String JavaDoc javaName) {
2188        ExpressionOperator exOperator = simpleRelation(selector, databaseName);
2189        Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(2);
2190        v.addElement("." + javaName + "(");
2191        v.addElement(")");
2192        exOperator.printsJavaAs(v);
2193        return exOperator;
2194    }
2195
2196    /**
2197     * INTERNAL:
2198     * Build operator.
2199     */

2200    public static ExpressionOperator simpleThreeArgumentFunction(int selector, String JavaDoc dbString) {
2201        ExpressionOperator exOperator = new ExpressionOperator();
2202        exOperator.setType(FunctionOperator);
2203        exOperator.setSelector(selector);
2204        Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(4);
2205        v.addElement(dbString + "(");
2206        v.addElement(", ");
2207        v.addElement(", ");
2208        v.addElement(")");
2209        exOperator.printsAs(v);
2210        exOperator.bePrefix();
2211        exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
2212        return exOperator;
2213    }
2214
2215    /**
2216     * INTERNAL:
2217     * Build operator.
2218     */

2219    public static ExpressionOperator simpleTwoArgumentFunction(int selector, String JavaDoc dbString) {
2220        ExpressionOperator exOperator = new ExpressionOperator();
2221        exOperator.setType(FunctionOperator);
2222        exOperator.setSelector(selector);
2223        Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(5);
2224        v.addElement(dbString + "(");
2225        v.addElement(", ");
2226        v.addElement(")");
2227        exOperator.printsAs(v);
2228        exOperator.bePrefix();
2229        exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
2230        return exOperator;
2231    }
2232
2233    /**
2234     * INTERNAL:
2235     * e.g.: ... "Bob" CONCAT "Smith" ...
2236     * Parentheses will not be addded. [RMB - March 5 2000]
2237     */

2238    public static ExpressionOperator simpleLogicalNoParens(int selector, String JavaDoc dbString) {
2239        ExpressionOperator exOperator = new ExpressionOperator();
2240        exOperator.setType(FunctionOperator);
2241        exOperator.setSelector(selector);
2242        Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(5);
2243        v.addElement("");
2244        v.addElement(" " + dbString + " ");
2245        v.addElement("");
2246        exOperator.printsAs(v);
2247        exOperator.bePrefix();
2248        exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
2249        return exOperator;
2250    }
2251
2252    /**
2253     * INTERNAL:
2254     * Build operator.
2255     */

2256    public static ExpressionOperator sin() {
2257        return simpleFunction(Sin, "SIN");
2258    }
2259
2260    /**
2261     * INTERNAL:
2262     * Build operator.
2263     */

2264    public static ExpressionOperator sinh() {
2265        return simpleFunction(Sinh, "SINH");
2266    }
2267
2268    /**
2269     * INTERNAL:
2270     * Build operator.
2271     */

2272    public static ExpressionOperator soundex() {
2273        return simpleFunction(Soundex, "SOUNDEX");
2274    }
2275
2276    /**
2277     * INTERNAL:
2278     * Build operator.
2279     */

2280    public static ExpressionOperator sqrt() {
2281        return simpleFunction(Sqrt, "SQRT");
2282    }
2283
2284    /**
2285     * INTERNAL:
2286     * Build operator.
2287     */

2288    public static ExpressionOperator standardDeviation() {
2289        return simpleAggregate(StandardDeviation, "STDDEV", "standardDeviation");
2290    }
2291
2292    /**
2293     * INTERNAL:
2294     * Build operator.
2295     */

2296    public static ExpressionOperator substring() {
2297        return simpleThreeArgumentFunction(Substring, "SUBSTR");
2298    }
2299
2300    /**
2301     * INTERNAL:
2302     * Create the SUM operator.
2303     */

2304    public static ExpressionOperator sum() {
2305        return simpleAggregate(Sum, "SUM", "sum");
2306    }
2307
2308    /**
2309     * INTERNAL:
2310     * Function, to add months to a date.
2311     */

2312    public static ExpressionOperator sybaseAddMonthsOperator() {
2313        ExpressionOperator exOperator = new ExpressionOperator();
2314        exOperator.setType(FunctionOperator);
2315        exOperator.setSelector(AddMonths);
2316        Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(3);
2317        v.addElement("DATEADD(month, ");
2318        v.addElement(", ");
2319        v.addElement(")");
2320        exOperator.printsAs(v);
2321        exOperator.bePrefix();
2322        int[] indices = { 1, 0 };
2323        exOperator.setArgumentIndices(indices);
2324        exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
2325        return exOperator;
2326
2327    }
2328
2329    /**
2330     * INTERNAL:
2331     * Build operator.
2332     */

2333    public static ExpressionOperator sybaseAtan2Operator() {
2334        return ExpressionOperator.simpleTwoArgumentFunction(Atan2, "ATN2");
2335    }
2336
2337    /**
2338     * INTERNAL:
2339     * Build instring operator
2340     */

2341    public static ExpressionOperator sybaseInStringOperator() {
2342        ExpressionOperator exOperator = new ExpressionOperator();
2343        exOperator.setType(FunctionOperator);
2344        exOperator.setSelector(Instring);
2345        Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(3);
2346        v.addElement("CHARINDEX(");
2347        v.addElement(", ");
2348        v.addElement(")");
2349        exOperator.printsAs(v);
2350        exOperator.bePrefix();
2351        int[] indices = { 1, 0 };
2352        exOperator.setArgumentIndices(indices);
2353        exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
2354        return exOperator;
2355
2356    }
2357
2358    /**
2359     * INTERNAL:
2360     * Build Sybase equivalent to TO_NUMBER.
2361     */

2362    public static ExpressionOperator sybaseToNumberOperator() {
2363        ExpressionOperator exOperator = new ExpressionOperator();
2364        exOperator.setType(FunctionOperator);
2365        exOperator.setSelector(ToNumber);
2366        Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(2);
2367        v.addElement("CONVERT(NUMERIC, ");
2368        v.addElement(")");
2369        exOperator.printsAs(v);
2370        exOperator.bePrefix();
2371        exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
2372        return exOperator;
2373    }
2374
2375    /**
2376     * INTERNAL:
2377     * Build Sybase equivalent to TO_CHAR.
2378     */

2379    public static ExpressionOperator sybaseToDateToStringOperator() {
2380        ExpressionOperator exOperator = new ExpressionOperator();
2381        exOperator.setType(FunctionOperator);
2382        exOperator.setSelector(DateToString);
2383        Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(2);
2384        v.addElement("CONVERT(CHAR, ");
2385        v.addElement(")");
2386        exOperator.printsAs(v);
2387        exOperator.bePrefix();
2388        exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
2389        return exOperator;
2390    }
2391
2392    /**
2393     * INTERNAL:
2394     * Build Sybase equivalent to TO_DATE.
2395     */

2396    public static ExpressionOperator sybaseToDateOperator() {
2397        ExpressionOperator exOperator = new ExpressionOperator();
2398        exOperator.setType(FunctionOperator);
2399        exOperator.setSelector(ToDate);
2400        Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(2);
2401        v.addElement("CONVERT(DATETIME, ");
2402        v.addElement(")");
2403        exOperator.printsAs(v);
2404        exOperator.bePrefix();
2405        exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
2406        return exOperator;
2407    }
2408
2409    /**
2410     * INTERNAL:
2411     * Build Sybase equivalent to TO_CHAR.
2412     */

2413    public static ExpressionOperator sybaseToCharOperator() {
2414        ExpressionOperator exOperator = new ExpressionOperator();
2415        exOperator.setType(FunctionOperator);
2416        exOperator.setSelector(ToChar);
2417        Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(2);
2418        v.addElement("CONVERT(CHAR, ");
2419        v.addElement(")");
2420        exOperator.printsAs(v);
2421        exOperator.bePrefix();
2422        exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
2423        return exOperator;
2424    }
2425
2426    /**
2427     * INTERNAL:
2428     * Build Sybase equivalent to TO_CHAR.
2429     */

2430    public static ExpressionOperator sybaseToCharWithFormatOperator() {
2431        ExpressionOperator exOperator = new ExpressionOperator();
2432        exOperator.setType(FunctionOperator);
2433        exOperator.setSelector(ToCharWithFormat);
2434        Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(3);
2435        v.addElement("CONVERT(CHAR, ");
2436        v.addElement(",");
2437        v.addElement(")");
2438        exOperator.printsAs(v);
2439        exOperator.bePrefix();
2440        exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
2441        return exOperator;
2442    }
2443
2444    /**
2445     * INTERNAL:
2446     * Build the Sybase equivalent to Locate
2447     */

2448    public static ExpressionOperator sybaseLocateOperator() {
2449        ExpressionOperator result = simpleTwoArgumentFunction(ExpressionOperator.Locate, "CHARINDEX");
2450        int[] argumentIndices = new int[2];
2451        argumentIndices[0] = 1;
2452        argumentIndices[1] = 0;
2453        result.setArgumentIndices(argumentIndices);
2454        return result;
2455    }
2456
2457
2458    /**
2459     * INTERNAL:
2460     * Build operator.
2461     */

2462    public static ExpressionOperator tan() {
2463        return simpleFunction(Tan, "TAN");
2464    }
2465
2466    /**
2467     * INTERNAL:
2468     * Build operator.
2469     */

2470    public static ExpressionOperator tanh() {
2471        return simpleFunction(Tanh, "TANH");
2472    }
2473
2474    /**
2475     * INTERNAL:
2476     * Build operator.
2477     */

2478    public static ExpressionOperator toDate() {
2479        return simpleFunction(ToDate, "TO_DATE");
2480    }
2481
2482    /**
2483     * INTERNAL:
2484     * Build operator.
2485     */

2486    public static ExpressionOperator today() {
2487        return simpleFunctionNoParentheses(Today, "SYSDATE");
2488    }
2489
2490    /**
2491     * INTERNAL:
2492     * Build operator.
2493     */

2494    public static ExpressionOperator currentDate() {
2495        return simpleFunctionNoParentheses(currentDate, "TO_DATE( SYSDATE )");
2496    }
2497
2498    /**
2499     * INTERNAL:
2500     * Create the toLowerCase operator.
2501     */

2502    public static ExpressionOperator toLowerCase() {
2503        return simpleFunction(ToLowerCase, "LOWER", "toLowerCase");
2504    }
2505
2506    /**
2507     * INTERNAL:
2508     * Build operator.
2509     */

2510    public static ExpressionOperator toNumber() {
2511        return simpleFunction(ToNumber, "TO_NUMBER");
2512    }
2513
2514    /**
2515     * Print a debug representation of this operator.
2516     */

2517    public String JavaDoc toString() {
2518        if ((getDatabaseStrings() == null) || (getDatabaseStrings().length == 0)) {
2519            //CR#... Print a useful name for the missing plaftorm operator.
2520
return "platform operator - " + getPlatformOperatorName(getSelector());
2521        } else {
2522            return "operator " + getDatabaseStrings()[0];
2523        }
2524    }
2525
2526    /**
2527     * INTERNAL:
2528     * Create the TOUPPERCASE operator.
2529     */

2530    public static ExpressionOperator toUpperCase() {
2531        return simpleFunction(ToUpperCase, "UPPER", "toUpperCase");
2532    }
2533
2534    /**
2535     * INTERNAL:
2536     * Build operator.
2537     */

2538    public static ExpressionOperator translate() {
2539        return simpleThreeArgumentFunction(Translate, "TRANSLATE");
2540    }
2541
2542    /**
2543     * INTERNAL:
2544     * Build operator.
2545     */

2546    public static ExpressionOperator trim() {
2547        return simpleFunction(Trim, "TRIM");
2548    }
2549    
2550    /**
2551     * INTERNAL:
2552     * Build Trim operator.
2553     */

2554    public static ExpressionOperator trim2() {
2555        ExpressionOperator exOperator = new ExpressionOperator();
2556        exOperator.setType(FunctionOperator);
2557        exOperator.setSelector(Trim2);
2558        Vector v = oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance(5);
2559        v.addElement("TRIM(");
2560        v.addElement(" FROM ");
2561        v.addElement(")");
2562        exOperator.printsAs(v);
2563        exOperator.bePrefix();
2564        exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
2565        return exOperator;
2566    }
2567
2568    /**
2569     * INTERNAL:
2570     * Build operator.
2571     */

2572    public static ExpressionOperator trunc() {
2573        return simpleTwoArgumentFunction(Trunc, "TRUNC");
2574    }
2575
2576    /**
2577     * INTERNAL:
2578     * Build operator.
2579     */

2580    public static ExpressionOperator truncateDate() {
2581        return simpleTwoArgumentFunction(TruncateDate, "TRUNC");
2582    }
2583
2584    /**
2585     * INTERNAL:
2586     * Build operator.
2587     */

2588    public static ExpressionOperator value() {
2589        return simpleFunction(Value, "VALUE");
2590    }
2591
2592    /**
2593     * INTERNAL:
2594     * Build operator.
2595     */

2596    public static ExpressionOperator variance() {
2597        return simpleAggregate(Variance, "VARIANCE", "variance");
2598    }
2599    
2600    /**
2601     * INTERNAL:
2602     * Create the ANY operator.
2603     */

2604    public static ExpressionOperator any() {
2605        ExpressionOperator exOperator = new ExpressionOperator();
2606        exOperator.setType(FunctionOperator);
2607        exOperator.setSelector(Any);
2608        exOperator.printsAs("ANY");
2609        exOperator.bePostfix();
2610        exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
2611        return exOperator;
2612    }
2613    
2614    /**
2615     * INTERNAL:
2616     * Create the SOME operator.
2617     */

2618    public static ExpressionOperator some() {
2619        ExpressionOperator exOperator = new ExpressionOperator();
2620        exOperator.setType(FunctionOperator);
2621        exOperator.setSelector(Some);
2622        exOperator.printsAs("SOME");
2623        exOperator.bePostfix();
2624        exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
2625        return exOperator;
2626    }
2627    
2628    /**
2629     * INTERNAL:
2630     * Create the ALL operator.
2631     */

2632    public static ExpressionOperator all() {
2633        ExpressionOperator exOperator = new ExpressionOperator();
2634        exOperator.setType(FunctionOperator);
2635        exOperator.setSelector(All);
2636        exOperator.printsAs("ALL");
2637        exOperator.bePostfix();
2638        exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
2639        return exOperator;
2640    }
2641    
2642    /**
2643     * INTERNAL:
2644     * Indicates whether operator has selector Any or Some
2645     */

2646    public boolean isAny() {
2647        return selector == ExpressionOperator.Any ||
2648                selector == ExpressionOperator.Some;
2649    }
2650    /**
2651     * INTERNAL:
2652     * Indicates whether operator has selector All
2653     */

2654    public boolean isAll() {
2655        return selector == ExpressionOperator.All;
2656    }
2657    /**
2658     * INTERNAL:
2659     * Indicates whether operator has selector Any, Some or All
2660     */

2661    public boolean isAnyOrAll() {
2662        return isAny() || isAll();
2663    }
2664}
2665
Popular Tags