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.manager.AmberConnection;
033: import com.caucho.amber.query.QueryParser;
034: import com.caucho.util.CharBuffer;
035: import com.caucho.util.L10N;
036:
037: import java.sql.ResultSet;
038: import java.sql.SQLException;
039: import java.util.ArrayList;
040:
041: /**
042: * LOCATE function expression
043: */
044: public class LocateFunExpr extends FunExpr {
045: private static final L10N L = new L10N(LocateFunExpr.class);
046:
047: /**
048: * Creates a new expression
049: */
050: protected LocateFunExpr(QueryParser parser,
051: ArrayList<AmberExpr> args) {
052: super (parser, "locate", args, false);
053: }
054:
055: public static FunExpr create(QueryParser parser,
056: ArrayList<AmberExpr> args) {
057: return new LocateFunExpr(parser, args);
058: }
059:
060: /**
061: * Generates the where expression.
062: */
063: public void generateWhere(CharBuffer cb) {
064: generateInternalWhere(cb, true);
065: }
066:
067: /**
068: * Generates the (update) where expression.
069: */
070: public void generateUpdateWhere(CharBuffer cb) {
071: generateInternalWhere(cb, false);
072: }
073:
074: /**
075: * Returns the object for the expr.
076: */
077: public Object getObject(AmberConnection aConn, ResultSet rs,
078: int index) throws SQLException {
079: return rs.getInt(index);
080: }
081:
082: //
083: // private/protected
084:
085: void generateInternalWhere(CharBuffer cb, boolean select) {
086: // Translate to => POSITION('word' in SUBSTRING(data,i,LENGTH(data)))+(i-1)
087:
088: ArrayList<AmberExpr> args = getArgs();
089:
090: int n = args.size();
091:
092: // XXX:
093: // if (n < 2)
094: // throw _parser.error(L.l("expected at least 2 string arguments for LOCATE"));
095: //
096: // if (n > 3)
097: // throw _parser.error(L.l("expected at most 3 arguments for LOCATE"));
098:
099: if (!_parser.isPostgresDBMS()) {
100:
101: // Derby (jpa/119k), MySql (ejb/0a40, jpa/119p)
102:
103: cb.append("locate(");
104:
105: if (select)
106: args.get(0).generateWhere(cb);
107: else
108: args.get(0).generateUpdateWhere(cb);
109:
110: cb.append(',');
111:
112: if (select)
113: args.get(1).generateWhere(cb);
114: else
115: args.get(1).generateUpdateWhere(cb);
116:
117: if (n > 2) {
118: cb.append(',');
119:
120: AmberExpr expr = args.get(2);
121:
122: if (select)
123: expr.generateWhere(cb);
124: else
125: expr.generateUpdateWhere(cb);
126: }
127:
128: cb.append(')');
129:
130: return;
131: }
132:
133: // Postgres: jpa/1190, jpa/119b, jpa/119c
134:
135: cb.append("position(");
136:
137: if (select)
138: args.get(0).generateWhere(cb);
139: else
140: args.get(0).generateUpdateWhere(cb);
141:
142: cb.append(" in substring(");
143:
144: if (select)
145: args.get(1).generateWhere(cb);
146: else
147: args.get(1).generateUpdateWhere(cb);
148:
149: cb.append(" from ");
150:
151: int fromIndex = 1;
152:
153: AmberExpr expr = null;
154:
155: if (n == 2) {
156: cb.append('1');
157: } else {
158: expr = args.get(2);
159:
160: try {
161: fromIndex = Integer.parseInt(expr.toString());
162: } catch (Exception ex) {
163: // XXX: this validation should be moved to QueryParser
164: // throw new QueryParseException(L.l("expected an integer for LOCATE 3rd argument"));
165: }
166:
167: if (select)
168: expr.generateWhere(cb);
169: else
170: expr.generateUpdateWhere(cb);
171: }
172:
173: cb.append("))");
174: }
175: }
|