001: package net.sourceforge.jaxor;
002:
003: import net.sourceforge.jaxor.api.Mapper;
004: import net.sourceforge.jaxor.api.MapperRegistry;
005: import net.sourceforge.jaxor.mappers.LongMapper;
006: import net.sourceforge.jaxor.mappers.StringMapper;
007: import net.sourceforge.jaxor.util.ObjectUtils;
008: import net.sourceforge.jaxor.util.SystemException;
009:
010: import java.sql.PreparedStatement;
011: import java.sql.SQLException;
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: public class QueryParams {
016:
017: private List _params = new ArrayList();
018:
019: public void add(int num, Mapper sqlType, Object value) {
020: for (int i = _params.size(); i <= num; i++) {
021: _params.add(i, null);
022: }
023: _params.set(num, new Param(sqlType, value));
024: }
025:
026: public void add(Mapper sqlType, Object value) {
027: _params.add(new Param(sqlType, value));
028: }
029:
030: public void add(Object value) {
031: add(null, value);
032: }
033:
034: public int hashCode() {
035: return _params.hashCode();
036: }
037:
038: public boolean equals(Object obj) {
039: if (obj instanceof QueryParams) {
040: QueryParams query = (QueryParams) obj;
041: return _params.equals(query._params);
042: }
043: return false;
044: }
045:
046: public String toString() {
047: return "Parameters: " + _params.toString();
048: }
049:
050: private class Param {
051: private Mapper _mapper;
052: private final Object _value;
053:
054: public Param(Mapper sqlType, Object value) {
055: _mapper = sqlType;
056: _value = value;
057: }
058:
059: public void setValue(PreparedStatement stmt, int i,
060: MapperRegistry reg) throws SQLException {
061: if (_mapper == null && _value != null)
062: _mapper = reg.get(_value.getClass());
063:
064: if (_mapper == null)
065: throw new SystemException(
066: "Mapper not found for parameter: "
067: + i
068: + " Pass a mapper with the parameter, or add a entry to the mapper registry");
069: Object mappedValue = _mapper.mapToSqlObject(_value);
070: int sqlType = _mapper.getSQLType();
071: if (_value == null)
072: stmt.setNull(i, sqlType);
073: else
074: stmt.setObject(i, mappedValue, sqlType);
075: }
076:
077: public boolean equals(Object obj) {
078: if (obj instanceof Param) {
079: Param param = (Param) obj;
080: return ((_mapper == param._mapper) && ObjectUtils
081: .equals(_value, param._value));
082: }
083: return false;
084: }
085:
086: public int hashCode() {
087: if (_value == null)
088: return _mapper.getSQLType();
089: return _value.hashCode();
090: }
091:
092: public String toString() {
093: return _value == null ? "null" : _value.toString();
094: }
095: }
096:
097: public void setArgs(PreparedStatement stmt, MapperRegistry reg) {
098: try {
099: for (int i = 0; i < _params.size(); i++) {
100: Param p = (Param) _params.get(i);
101: p.setValue(stmt, i + 1, reg);
102: }
103: } catch (SQLException e) {
104: throw new SystemException(e);
105: }
106: }
107:
108: public void add(int num, Object obj) {
109: if (obj == null)
110: throw new IllegalArgumentException(
111: "Argument cannot be null: " + num
112: + " You can pass nulls with mappers");
113: add(num, null, obj);
114: }
115:
116: public void addString(String str) {
117: add(new StringMapper(), str);
118: }
119:
120: public void addLong(Long contactId) {
121: add(new LongMapper(), contactId);
122: }
123:
124: public void addString(int num, String str) {
125: add(num, new StringMapper(), str);
126: }
127:
128: public void addLong(int num, Long l) {
129: add(num, new LongMapper(), l);
130: }
131:
132: public void add(int num, int value) {
133: add(num, new Integer(value));
134: }
135:
136: public void add(int num, long l) {
137: add(num, new Long(l));
138: }
139:
140: public void add(int num, float f) {
141: add(num, new Float(f));
142: }
143:
144: public void add(int num, boolean b) {
145: add(num, new Boolean(b));
146: }
147:
148: public void add(int num, byte b) {
149: add(num, new Byte(b));
150: }
151:
152: public void add(int num, short s) {
153: add(num, new Short(s));
154: }
155:
156: public void add(int num, double d) {
157: add(num, new Double(d));
158: }
159:
160: public void add(int value) {
161: add(new Integer(value));
162: }
163:
164: public void add(long l) {
165: add(new Long(l));
166: }
167:
168: public void add(float f) {
169: add(new Float(f));
170: }
171:
172: public void add(boolean b) {
173: add(new Boolean(b));
174: }
175:
176: public void add(byte b) {
177: add(new Byte(b));
178: }
179:
180: public void add(short s) {
181: add(new Short(s));
182: }
183:
184: public void add(double d) {
185: add(new Double(d));
186: }
187:
188: public Object getValue(int count) {
189: Param p = (Param) _params.get(count);
190: return p._value;
191: }
192:
193: public int size() {
194: return _params.size();
195: }
196: }
|