001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.amber.expr.fun;
030:
031: import com.caucho.amber.expr.AmberExpr;
032: import com.caucho.amber.query.QueryParser;
033: import com.caucho.util.CharBuffer;
034: import com.caucho.util.L10N;
035:
036: import java.util.ArrayList;
037:
038: /**
039: * TRIM function expression
040: */
041: public class TrimFunExpr extends FunExpr {
042: private static final L10N L = new L10N(TrimFunExpr.class);
043:
044: public enum TrimSemantics {
045: LEADING, TRAILING, BOTH
046: }
047:
048: private TrimSemantics _trimSemantics = TrimSemantics.BOTH;
049:
050: private AmberExpr _trimChar;
051:
052: /**
053: * Creates a new expression
054: */
055: protected TrimFunExpr(QueryParser parser, ArrayList<AmberExpr> args) {
056: super (parser, "trim", args, false);
057: }
058:
059: public static TrimFunExpr create(QueryParser parser,
060: ArrayList<AmberExpr> args) {
061: return new TrimFunExpr(parser, args);
062: }
063:
064: /**
065: * Sets the trim character.
066: */
067: public void setTrimChar(AmberExpr trimChar) {
068: _trimChar = trimChar;
069: }
070:
071: /**
072: * Sets the trim semantics.
073: */
074: public void setTrimSemantics(TrimSemantics trimSemantics) {
075: _trimSemantics = trimSemantics;
076: }
077:
078: /**
079: * Generates the where expression.
080: */
081: public void generateWhere(CharBuffer cb) {
082: generateInternalWhere(cb, true);
083: }
084:
085: /**
086: * Generates the (update) where expression.
087: */
088: public void generateUpdateWhere(CharBuffer cb) {
089: generateInternalWhere(cb, false);
090: }
091:
092: //
093: // private/protected
094:
095: void generateInternalWhere(CharBuffer cb, boolean select) {
096: ArrayList<AmberExpr> args = getArgs();
097:
098: int n = args.size();
099:
100: // XXX: this validation should be moved to QueryParser
101: // if (n != 1)
102: // throw new QueryParseException(L.l("expected 1 string argument for TRIM"));
103:
104: if (_parser.isDerbyDBMS()) {
105:
106: // Derby.
107:
108: switch (_trimSemantics) {
109:
110: case LEADING:
111: cb.append("ltrim(");
112: break;
113:
114: case TRAILING:
115: cb.append("rtrim(");
116: break;
117:
118: default:
119: cb.append("ltrim(rtrim(");
120: }
121:
122: if (_trimChar != null) {
123: if (select)
124: _trimChar.generateWhere(cb);
125: else
126: _trimChar.generateUpdateWhere(cb);
127:
128: cb.append(" from ");
129: }
130:
131: if (select)
132: args.get(0).generateWhere(cb);
133: else
134: args.get(0).generateUpdateWhere(cb);
135:
136: cb.append(')');
137:
138: if (_trimSemantics == TrimSemantics.BOTH)
139: cb.append(')');
140:
141: return;
142: }
143:
144: cb.append("trim(");
145:
146: switch (_trimSemantics) {
147:
148: case LEADING:
149: cb.append("leading ");
150: break;
151:
152: case TRAILING:
153: cb.append("trailing ");
154: break;
155:
156: default:
157: cb.append("both ");
158: }
159:
160: if (_trimChar != null) {
161: if (select)
162: _trimChar.generateWhere(cb);
163: else
164: _trimChar.generateUpdateWhere(cb);
165: }
166:
167: cb.append(" from ");
168:
169: if (select)
170: args.get(0).generateWhere(cb);
171: else
172: args.get(0).generateUpdateWhere(cb);
173:
174: cb.append(")");
175: }
176: }
|