001: /*
002: * Copyright 2004 (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: CharacterLiteral.java,v 1.4 2004/01/18 03:01:06 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.store;
012:
013: import java.math.BigInteger;
014:
015: class CharacterLiteral extends CharacterExpression {
016: private final String value;
017:
018: public CharacterLiteral(QueryStatement qs, char value) {
019: super (qs);
020:
021: this .value = String.valueOf(value);
022: st.append('\'').append(value).append('\'');
023: }
024:
025: public CharacterLiteral(QueryStatement qs, String value) {
026: super (qs);
027:
028: this .value = value;
029:
030: DatabaseAdapter dba = qs.getStoreManager().getDatabaseAdapter();
031: st.appendParameter(
032: (ColumnMapping) dba.getMapping(String.class), value);
033: }
034:
035: public BooleanExpression eq(SQLExpression expr) {
036: if (expr instanceof CharacterLiteral)
037: return new BooleanLiteral(qs, value
038: .equals(((CharacterLiteral) expr).value));
039: else
040: return super .eq(expr);
041: }
042:
043: public BooleanExpression noteq(SQLExpression expr) {
044: if (expr instanceof CharacterLiteral)
045: return new BooleanLiteral(qs, !value
046: .equals(((CharacterLiteral) expr).value));
047: else
048: return super .noteq(expr);
049: }
050:
051: public BooleanExpression lt(SQLExpression expr) {
052: if (expr instanceof CharacterLiteral)
053: return new BooleanLiteral(qs, value
054: .compareTo(((CharacterLiteral) expr).value) < 0);
055: else
056: return super .lt(expr);
057: }
058:
059: public BooleanExpression lteq(SQLExpression expr) {
060: if (expr instanceof CharacterLiteral)
061: return new BooleanLiteral(qs, value
062: .compareTo(((CharacterLiteral) expr).value) <= 0);
063: else
064: return super .lteq(expr);
065: }
066:
067: public BooleanExpression gt(SQLExpression expr) {
068: if (expr instanceof CharacterLiteral)
069: return new BooleanLiteral(qs, value
070: .compareTo(((CharacterLiteral) expr).value) > 0);
071: else
072: return super .gt(expr);
073: }
074:
075: public BooleanExpression gteq(SQLExpression expr) {
076: if (expr instanceof CharacterLiteral)
077: return new BooleanLiteral(qs, value
078: .compareTo(((CharacterLiteral) expr).value) >= 0);
079: else
080: return super .gteq(expr);
081: }
082:
083: public SQLExpression add(SQLExpression expr) {
084: if (expr instanceof CharacterLiteral)
085: return new CharacterLiteral(qs, value
086: .concat(((CharacterLiteral) expr).value));
087: else
088: return super .add(expr);
089: }
090:
091: public BooleanExpression endsWithMethod(SQLExpression str) {
092: if (str instanceof CharacterLiteral)
093: return new BooleanLiteral(qs, value
094: .endsWith(((CharacterLiteral) str).value));
095: else
096: return super .endsWithMethod(str);
097: }
098:
099: public NumericExpression indexOfMethod(SQLExpression str) {
100: if (str instanceof CharacterLiteral)
101: return new IntegerLiteral(qs, BigInteger
102: .valueOf((long) value
103: .indexOf(((CharacterLiteral) str).value)));
104: else
105: return super .indexOfMethod(str);
106: }
107:
108: public NumericExpression lengthMethod() {
109: return new IntegerLiteral(qs, BigInteger.valueOf((long) value
110: .length()));
111: }
112:
113: public BooleanExpression startsWithMethod(SQLExpression str) {
114: if (str instanceof CharacterLiteral)
115: return new BooleanLiteral(qs, value
116: .startsWith(((CharacterLiteral) str).value));
117: else
118: return super .startsWithMethod(str);
119: }
120:
121: public BooleanExpression startsWithMethod(SQLExpression str,
122: SQLExpression toffset) {
123: if (str instanceof CharacterLiteral
124: && toffset instanceof IntegerLiteral)
125: return new BooleanLiteral(qs, value.startsWith(
126: ((CharacterLiteral) str).value,
127: ((IntegerLiteral) toffset).getValue().intValue()));
128: else
129: return super .startsWithMethod(str, toffset);
130: }
131:
132: public CharacterExpression substringMethod(NumericExpression begin) {
133: if (begin instanceof IntegerLiteral)
134: return new CharacterLiteral(qs, value
135: .substring(((IntegerLiteral) begin).getValue()
136: .intValue()));
137: else
138: return super .substringMethod(begin);
139: }
140:
141: public CharacterExpression substringMethod(NumericExpression begin,
142: NumericExpression end) {
143: if (begin instanceof IntegerLiteral
144: && end instanceof IntegerLiteral)
145: return new CharacterLiteral(qs, value.substring(
146: ((IntegerLiteral) begin).getValue().intValue(),
147: ((IntegerLiteral) end).getValue().intValue()));
148: else
149: return super .substringMethod(begin, end);
150: }
151:
152: public CharacterExpression toLowerCase() {
153: return new CharacterLiteral(qs, value.toLowerCase());
154: }
155:
156: public CharacterExpression toUpperCase() {
157: return new CharacterLiteral(qs, value.toUpperCase());
158: }
159:
160: public CharacterExpression trim() {
161: return new CharacterLiteral(qs, value.trim());
162: }
163: }
|