KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > CharacterExpression


1 /*
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: CharacterExpression.java,v 1.4 2003/08/11 16:01:51 pierreg0 Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import java.math.BigInteger JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17
18 class CharacterExpression extends SQLExpression
19 {
20     public CharacterExpression(QueryStatement qs)
21     {
22         super(qs);
23     }
24
25     public CharacterExpression(QueryStatement qs, QueryStatement.QueryColumn qsc)
26     {
27         super(qs, qsc);
28     }
29
30     public CharacterExpression(String JavaDoc functionName, List JavaDoc args)
31     {
32         super(functionName, args);
33     }
34
35     public CharacterExpression(SQLExpression operand1, DyadicOperator op, SQLExpression operand2)
36     {
37         super(operand1, op, operand2);
38     }
39
40     public BooleanExpression eq(SQLExpression expr)
41     {
42         if (expr instanceof NullLiteral)
43             return expr.eq(this);
44         else if (expr instanceof CharacterExpression)
45             return new BooleanExpression(this, OP_EQ, expr);
46         else
47             return super.eq(expr);
48     }
49
50     public BooleanExpression noteq(SQLExpression expr)
51     {
52         if (expr instanceof NullLiteral)
53             return expr.noteq(this);
54         else if (expr instanceof CharacterExpression)
55             return new BooleanExpression(this, OP_NOTEQ, expr);
56         else
57             return super.noteq(expr);
58     }
59
60     public BooleanExpression lt(SQLExpression expr)
61     {
62         if (expr instanceof CharacterExpression)
63             return new BooleanExpression(this, OP_LT, expr);
64         else
65             return super.lt(expr);
66     }
67
68     public BooleanExpression lteq(SQLExpression expr)
69     {
70         if (expr instanceof CharacterExpression)
71             return new BooleanExpression(this, OP_LTEQ, expr);
72         else
73             return super.lteq(expr);
74     }
75
76     public BooleanExpression gt(SQLExpression expr)
77     {
78         if (expr instanceof CharacterExpression)
79             return new BooleanExpression(this, OP_GT, expr);
80         else
81             return super.gt(expr);
82     }
83
84     public BooleanExpression gteq(SQLExpression expr)
85     {
86         if (expr instanceof CharacterExpression)
87             return new BooleanExpression(this, OP_GTEQ, expr);
88         else
89             return super.gteq(expr);
90     }
91
92     public SQLExpression add(SQLExpression expr)
93     {
94         if (expr instanceof CharacterExpression)
95             return new CharacterExpression(this, OP_CONCAT, expr);
96         else
97             return super.add(expr);
98     }
99     
100     public BooleanExpression in(SQLExpression expr)
101     {
102         return new BooleanExpression(this, OP_IN, expr);
103     }
104
105     public CharacterExpression charAtMethod(SQLExpression index)
106     {
107         if (!(index instanceof NumericExpression))
108             throw new IllegalArgumentTypeException(index);
109
110         NumericExpression begin = (NumericExpression)index;
111         NumericExpression end = (NumericExpression)begin.add(new IntegerLiteral(qs, BigInteger.ONE));
112
113         return substringMethod(begin, end);
114     }
115
116     public BooleanExpression endsWithMethod(SQLExpression str)
117     {
118         if (!(str instanceof CharacterExpression))
119             throw new IllegalArgumentTypeException(str);
120
121         CharacterLiteral pct = new CharacterLiteral(qs, '%');
122
123         return new BooleanExpression(this, OP_LIKE, pct.add(str));
124     }
125
126     public NumericExpression indexOfMethod(SQLExpression str)
127     {
128         if (!(str instanceof CharacterExpression))
129             throw new IllegalArgumentTypeException(str);
130
131         return new IndexOfExpression(this, (CharacterExpression)str);
132     }
133             
134     public NumericExpression lengthMethod()
135     {
136         return qs.getStoreManager().getDatabaseAdapter().lengthMethod(this);
137     }
138
139     public BooleanExpression startsWithMethod(SQLExpression str)
140     {
141         if (!(str instanceof CharacterExpression))
142             throw new IllegalArgumentTypeException(str);
143             
144         CharacterLiteral pct = new CharacterLiteral(qs, '%');
145
146         return new BooleanExpression(this, OP_LIKE, str.add(pct));
147     }
148
149     public BooleanExpression startsWithMethod(SQLExpression str, SQLExpression toffset)
150     {
151         return substringMethod(toffset).startsWithMethod(str);
152     }
153
154     public CharacterExpression substringMethod(SQLExpression begin)
155     {
156         if (!(begin instanceof NumericExpression))
157             throw new IllegalArgumentTypeException(begin);
158
159         return qs.getStoreManager().getDatabaseAdapter().substringMethod(this,
160                                                                          (NumericExpression)begin);
161     }
162
163     public CharacterExpression substringMethod(SQLExpression begin, SQLExpression end)
164     {
165         if (!(begin instanceof NumericExpression))
166             throw new IllegalArgumentTypeException(begin);
167         if (!(end instanceof NumericExpression))
168             throw new IllegalArgumentTypeException(end);
169
170         return qs.getStoreManager().getDatabaseAdapter().substringMethod(this,
171                                                                          (NumericExpression)begin,
172                                                                          (NumericExpression)end);
173     }
174
175     public CharacterExpression toLowerCaseMethod()
176     {
177         ArrayList JavaDoc args = new ArrayList JavaDoc();
178         args.add(this);
179
180         return new CharacterExpression("LOWER", args);
181     }
182
183     public CharacterExpression toUpperCaseMethod()
184     {
185         ArrayList JavaDoc args = new ArrayList JavaDoc();
186         args.add(this);
187
188         return new CharacterExpression("UPPER", args);
189     }
190
191     public CharacterExpression trim()
192     {
193         ArrayList JavaDoc args = new ArrayList JavaDoc();
194         args.add(this);
195
196         return new CharacterExpression("TRIM", args);
197     }
198 }
199
Popular Tags