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;
030:
031: import com.caucho.amber.query.QueryParser;
032: import com.caucho.amber.type.*;
033: import com.caucho.util.CharBuffer;
034:
035: import java.sql.PreparedStatement;
036: import java.sql.SQLException;
037:
038: /**
039: * Parameter argument expression.
040: */
041: public class ArgExpr extends AbstractAmberExpr {
042: private QueryParser _parser;
043:
044: // argument index
045: private int _index;
046:
047: private int _sqlIndex;
048:
049: private String _name;
050:
051: private Type _type;
052:
053: /**
054: * Creates a new argument expression.
055: *
056: * @param index the argument index
057: */
058: public ArgExpr(QueryParser parser, int index) {
059: _parser = parser;
060:
061: _index = index;
062:
063: _sqlIndex = -1;
064: }
065:
066: /**
067: * Creates a new named argument expression.
068: *
069: * @param String the argument name
070: */
071: public ArgExpr(QueryParser parser, String name, int index) {
072: _parser = parser;
073:
074: _name = name;
075:
076: _index = index;
077:
078: _sqlIndex = -1;
079: }
080:
081: /**
082: * Returns the index value
083: */
084: public int getIndex() {
085: return _index;
086: }
087:
088: /**
089: * Returns the expr type.
090: */
091: public Type getType() {
092: return _type;
093: }
094:
095: /**
096: * Sets the expr type.
097: */
098: public void setType(Type type) {
099: _type = type;
100: }
101:
102: /**
103: * Binds the expression as a select item.
104: */
105: public AmberExpr bindSelect(QueryParser parser) {
106: parser.addArg(this );
107:
108: return this ;
109: }
110:
111: /**
112: * Returns true if the expression must exist
113: */
114: @Override
115: public boolean exists() {
116: // ejb/0h1k
117: return true;
118: }
119:
120: /**
121: * Generates the literal.
122: */
123: public void generateWhere(CharBuffer cb) {
124: generateInternalWhere(cb, true);
125: }
126:
127: /**
128: * Generates the (update) literal.
129: */
130: public void generateUpdateWhere(CharBuffer cb) {
131: generateInternalWhere(cb, false);
132: }
133:
134: /**
135: * Generates the having expression.
136: */
137: public void generateHaving(CharBuffer cb) {
138: generateWhere(cb);
139: }
140:
141: /**
142: * Returns the argument name, or null
143: * if it is a positional parameter.
144: */
145: public String getName() {
146: return _name;
147: }
148:
149: /**
150: * Sets the parameter.
151: */
152: public void setParameter(PreparedStatement pstmt, int i,
153: Type[] argTypes, Object[] argValues) throws SQLException {
154: try {
155: if (_name == null) {
156:
157: // jpa/141d (enum type)
158: if (getType() != null) {
159: if (!((getType() instanceof UtilDateType) || (getType() instanceof CalendarType))) {
160: argTypes[_index - 1] = getType();
161: }
162: }
163:
164: if (argTypes[_index - 1] != null) {
165: argTypes[_index - 1].setParameter(pstmt,
166: _sqlIndex + 1, argValues[_index - 1]);
167: // jpa/141e
168: } else
169: pstmt.setString(_sqlIndex + 1, null);
170: } else {
171: // jpa/141d (enum type)
172: if (getType() != null) {
173: // jpa/1410, jpa/1413
174: if (!((getType() instanceof UtilDateType) || (getType() instanceof CalendarType))) {
175: argTypes[i - 1] = getType();
176: }
177: }
178:
179: if (argTypes[i - 1] != null) {
180: // jpa/141g
181:
182: // jpa/1217 argTypes[i - 1].setParameter(pstmt, _sqlIndex + 1, argValues[i - 1]);
183: argTypes[i - 1].setParameter(pstmt, i,
184: argValues[i - 1]);
185: } else
186: pstmt.setString(_sqlIndex + 1, null);
187: }
188: } catch (Exception e) {
189: // jpa/141h
190: throw new IllegalArgumentException(e);
191: }
192: }
193:
194: public String toString() {
195: if (_name == null)
196: return "?" + _index;
197: else
198: return ":" + _name;
199: }
200:
201: //
202: // private
203:
204: private void generateInternalWhere(CharBuffer cb, boolean select) {
205: if (_sqlIndex < 0)
206: _sqlIndex = _parser.generateSQLArg();
207:
208: cb.append("?");
209: }
210: }
|