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.query;
030:
031: import com.caucho.amber.type.*;
032:
033: /**
034: * Represents the arguments to a query.
035: */
036: public class QueryArgs {
037: private Type[] _argTypes;
038: private Object[] _argValues;
039:
040: public QueryArgs(int length) {
041: _argTypes = new Type[length];
042: _argValues = new Object[length];
043: }
044:
045: /**
046: * Returns the arg type array.
047: */
048: Type[] getArgTypes() {
049: return _argTypes;
050: }
051:
052: /**
053: * Returns the arg values
054: */
055: Object[] getArgValues() {
056: return _argValues;
057: }
058:
059: /**
060: * Sets the argument with a string
061: */
062: public void setString(int index, String v) {
063: _argTypes[index - 1] = StringType.create();
064: _argValues[index - 1] = v;
065: }
066:
067: /**
068: * Sets the argument with a byte
069: */
070: public void setByte(int index, byte v) {
071: _argTypes[index - 1] = ByteType.create();
072: _argValues[index - 1] = new Integer(v);
073: }
074:
075: /**
076: * Sets the argument with a short
077: */
078: public void setShort(int index, short v) {
079: _argTypes[index - 1] = ShortType.create();
080: _argValues[index - 1] = new Integer(v);
081: }
082:
083: /**
084: * Sets the argument with an int
085: */
086: public void setInt(int index, int v) {
087: _argTypes[index - 1] = IntegerType.create();
088: _argValues[index - 1] = new Integer(v);
089: }
090:
091: /**
092: * Sets the argument with a string
093: */
094: public void setLong(int index, long v) {
095: _argTypes[index - 1] = LongType.create();
096: _argValues[index - 1] = new Long(v);
097: }
098:
099: /**
100: * Sets the argument with a double
101: */
102: public void setDouble(int index, double v) {
103: _argTypes[index - 1] = DoubleType.create();
104: _argValues[index - 1] = new Double(v);
105: }
106:
107: /**
108: * Sets the argument with a timestamp
109: */
110: public void setTimestamp(int index, java.sql.Timestamp v) {
111: _argTypes[index - 1] = SqlTimestampType.create();
112: _argValues[index - 1] = v;
113: }
114:
115: /**
116: * Sets the argument with a date
117: */
118: public void setDate(int index, java.sql.Date v) {
119: _argTypes[index - 1] = SqlDateType.create();
120: _argValues[index - 1] = v;
121: }
122:
123: /**
124: * Sets the argument with a null
125: */
126: public void setNull(int index, int v) {
127: _argTypes[index - 1] = StringType.create();
128: _argValues[index - 1] = null;
129: }
130:
131: /**
132: * Returns a hash code.
133: */
134: public int hashCode() {
135: int hash = 37;
136:
137: for (int i = _argValues.length - 1; i >= 0; i--) {
138: Object v = _argValues[i];
139:
140: if (v != null)
141: hash = 31 * hash + v.hashCode();
142: else
143: hash = 31 * hash + 17;
144: }
145:
146: return hash;
147: }
148:
149: /**
150: * Returns true if equals.
151: */
152: public boolean equals(Object o) {
153: if (!(o instanceof QueryArgs))
154: return false;
155:
156: QueryArgs args = (QueryArgs) o;
157:
158: if (_argValues.length != args._argValues.length)
159: return false;
160:
161: for (int i = _argValues.length - 1; i >= 0; i--) {
162: Object a = args._argValues[i];
163: Object b = _argValues[i];
164:
165: if (a != b && (a == null || !a.equals(b)))
166: return false;
167: }
168:
169: return true;
170: }
171:
172: public String toString() {
173: return "QueryArgs[]";
174: }
175: }
|