001: /*
002: * Copyright 2002 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: CharacterExpression.java,v 1.4 2003/08/11 16:01:51 pierreg0 Exp $
009: */
010:
011: package com.triactive.jdo.store;
012:
013: import java.math.BigInteger;
014: import java.util.ArrayList;
015: import java.util.List;
016:
017: class CharacterExpression extends SQLExpression {
018: public CharacterExpression(QueryStatement qs) {
019: super (qs);
020: }
021:
022: public CharacterExpression(QueryStatement qs,
023: QueryStatement.QueryColumn qsc) {
024: super (qs, qsc);
025: }
026:
027: public CharacterExpression(String functionName, List args) {
028: super (functionName, args);
029: }
030:
031: public CharacterExpression(SQLExpression operand1,
032: DyadicOperator op, SQLExpression operand2) {
033: super (operand1, op, operand2);
034: }
035:
036: public BooleanExpression eq(SQLExpression expr) {
037: if (expr instanceof NullLiteral)
038: return expr.eq(this );
039: else if (expr instanceof CharacterExpression)
040: return new BooleanExpression(this , OP_EQ, expr);
041: else
042: return super .eq(expr);
043: }
044:
045: public BooleanExpression noteq(SQLExpression expr) {
046: if (expr instanceof NullLiteral)
047: return expr.noteq(this );
048: else if (expr instanceof CharacterExpression)
049: return new BooleanExpression(this , OP_NOTEQ, expr);
050: else
051: return super .noteq(expr);
052: }
053:
054: public BooleanExpression lt(SQLExpression expr) {
055: if (expr instanceof CharacterExpression)
056: return new BooleanExpression(this , OP_LT, expr);
057: else
058: return super .lt(expr);
059: }
060:
061: public BooleanExpression lteq(SQLExpression expr) {
062: if (expr instanceof CharacterExpression)
063: return new BooleanExpression(this , OP_LTEQ, expr);
064: else
065: return super .lteq(expr);
066: }
067:
068: public BooleanExpression gt(SQLExpression expr) {
069: if (expr instanceof CharacterExpression)
070: return new BooleanExpression(this , OP_GT, expr);
071: else
072: return super .gt(expr);
073: }
074:
075: public BooleanExpression gteq(SQLExpression expr) {
076: if (expr instanceof CharacterExpression)
077: return new BooleanExpression(this , OP_GTEQ, expr);
078: else
079: return super .gteq(expr);
080: }
081:
082: public SQLExpression add(SQLExpression expr) {
083: if (expr instanceof CharacterExpression)
084: return new CharacterExpression(this , OP_CONCAT, expr);
085: else
086: return super .add(expr);
087: }
088:
089: public BooleanExpression in(SQLExpression expr) {
090: return new BooleanExpression(this , OP_IN, expr);
091: }
092:
093: public CharacterExpression charAtMethod(SQLExpression index) {
094: if (!(index instanceof NumericExpression))
095: throw new IllegalArgumentTypeException(index);
096:
097: NumericExpression begin = (NumericExpression) index;
098: NumericExpression end = (NumericExpression) begin
099: .add(new IntegerLiteral(qs, BigInteger.ONE));
100:
101: return substringMethod(begin, end);
102: }
103:
104: public BooleanExpression endsWithMethod(SQLExpression str) {
105: if (!(str instanceof CharacterExpression))
106: throw new IllegalArgumentTypeException(str);
107:
108: CharacterLiteral pct = new CharacterLiteral(qs, '%');
109:
110: return new BooleanExpression(this , OP_LIKE, pct.add(str));
111: }
112:
113: public NumericExpression indexOfMethod(SQLExpression str) {
114: if (!(str instanceof CharacterExpression))
115: throw new IllegalArgumentTypeException(str);
116:
117: return new IndexOfExpression(this , (CharacterExpression) str);
118: }
119:
120: public NumericExpression lengthMethod() {
121: return qs.getStoreManager().getDatabaseAdapter().lengthMethod(
122: this );
123: }
124:
125: public BooleanExpression startsWithMethod(SQLExpression str) {
126: if (!(str instanceof CharacterExpression))
127: throw new IllegalArgumentTypeException(str);
128:
129: CharacterLiteral pct = new CharacterLiteral(qs, '%');
130:
131: return new BooleanExpression(this , OP_LIKE, str.add(pct));
132: }
133:
134: public BooleanExpression startsWithMethod(SQLExpression str,
135: SQLExpression toffset) {
136: return substringMethod(toffset).startsWithMethod(str);
137: }
138:
139: public CharacterExpression substringMethod(SQLExpression begin) {
140: if (!(begin instanceof NumericExpression))
141: throw new IllegalArgumentTypeException(begin);
142:
143: return qs.getStoreManager().getDatabaseAdapter()
144: .substringMethod(this , (NumericExpression) begin);
145: }
146:
147: public CharacterExpression substringMethod(SQLExpression begin,
148: SQLExpression end) {
149: if (!(begin instanceof NumericExpression))
150: throw new IllegalArgumentTypeException(begin);
151: if (!(end instanceof NumericExpression))
152: throw new IllegalArgumentTypeException(end);
153:
154: return qs.getStoreManager().getDatabaseAdapter()
155: .substringMethod(this , (NumericExpression) begin,
156: (NumericExpression) end);
157: }
158:
159: public CharacterExpression toLowerCaseMethod() {
160: ArrayList args = new ArrayList();
161: args.add(this );
162:
163: return new CharacterExpression("LOWER", args);
164: }
165:
166: public CharacterExpression toUpperCaseMethod() {
167: ArrayList args = new ArrayList();
168: args.add(this );
169:
170: return new CharacterExpression("UPPER", args);
171: }
172:
173: public CharacterExpression trim() {
174: ArrayList args = new ArrayList();
175: args.add(this );
176:
177: return new CharacterExpression("TRIM", args);
178: }
179: }
|