001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.backend.list.helper;
016:
017: import java.io.Serializable;
018: import java.sql.PreparedStatement;
019: import java.sql.SQLException;
020: import java.util.ArrayList;
021: import java.util.List;
022:
023: /**
024: * @author <a href="mailto:rein@araneaframework.org">Rein Raudjärv</a>
025: */
026: public class SqlStatement implements Serializable, Cloneable {
027:
028: private static final long serialVersionUID = 1L;
029:
030: protected String query;
031: protected List parameters;
032:
033: public SqlStatement(String query, List parameters) {
034: this .query = query;
035: this .parameters = parameters;
036: }
037:
038: public SqlStatement(String query) {
039: this (query, new ArrayList());
040: }
041:
042: public SqlStatement() {
043: this (null);
044: }
045:
046: public List getParams() {
047: return this .parameters;
048: }
049:
050: public void setParams(List params) {
051: if (params == null) {
052: throw new RuntimeException(
053: "Parameters list can not be NULL, use an empty list instead");
054: }
055: this .parameters = params;
056: }
057:
058: public String getQuery() {
059: return this .query;
060: }
061:
062: public void setQuery(String query) {
063: this .query = query;
064: }
065:
066: /**
067: * Adds a parameter at the specified position.
068: *
069: * @param index
070: * index at witch the specified parameter will be inserted.
071: * @param param
072: * a parameter.
073: */
074: public void addParam(int index, Object param) {
075: this .parameters.add(index, param);
076: }
077:
078: /**
079: * Adds a <code>NULL</code> parameter at the specified position.
080: *
081: * @param index
082: * index at witch the specified parameter will be inserted.
083: * @param valueType
084: * the type of the NULL value.
085: */
086: public void addNullParam(int index, int valueType) {
087: this .addParam(index, new NullValue(valueType));
088: }
089:
090: /**
091: * Adds parameters at the specified position.
092: *
093: * @param index
094: * index at witch the specified parameters will be inserted.
095: * @param params
096: * parameters.
097: */
098: public void addAllParams(int index, List params) {
099: this .parameters.addAll(index, params);
100: }
101:
102: /**
103: * Counts all parameters.
104: *
105: * @return paramaetrs count.
106: */
107: public int countParams() {
108: return this .parameters.size();
109: }
110:
111: /**
112: * Adds a parameter.
113: *
114: * @param param
115: * a parameter.
116: */
117: public void addParam(Object param) {
118: addParam(countParams(), param);
119: }
120:
121: /**
122: * Adds a <code>NULL</code> parameter.
123: *
124: * @param valueType
125: * the type of the NULL value.
126: */
127: public void addNullParam(int valueType) {
128: addNullParam(countParams(), valueType);
129: }
130:
131: /**
132: * Adds parameters.
133: *
134: * @param params
135: * parameters.
136: */
137: public void addAllParams(List params) {
138: addAllParams(countParams(), params);
139: }
140:
141: /**
142: * Clears all parameters.
143: */
144: public void clearParams() {
145: this .parameters.clear();
146: }
147:
148: /**
149: * Constructs a new <code>SqlStatement</code> with the same
150: * <code>Query</code> and <code>Parameters</code>.
151: *
152: * @see java.lang.Object#clone()
153: */
154: public Object clone() {
155: return new SqlStatement(this .query, new ArrayList(
156: this .parameters));
157: }
158:
159: // *********************************************************************
160: // * PREPARED STATEMENT PROPAGATION
161: // *********************************************************************
162:
163: /**
164: * Helper method that sets the parameters to the
165: * <code>PreparedStatement</code>.
166: *
167: * @param pstmt <code>PreparedStatement</code> which parameters will be set.
168: * @throws SQLException
169: */
170: protected void propagateStatementWithParams(PreparedStatement pstmt)
171: throws SQLException {
172: for (int i = 1; i <= this .parameters.size(); i++) {
173: Object parameter = this .parameters.get(i - 1);
174: if (parameter instanceof NullValue) {
175: pstmt.setNull(i, ((NullValue) parameter).getType());
176: } else {
177: // converting java.util.Date into java.sql.Date (java.sql.Timestamp is not changed)
178: if (parameter != null
179: && parameter.getClass().equals(
180: java.util.Date.class)) {
181: parameter = new java.sql.Date(
182: ((java.util.Date) parameter).getTime());
183: }
184: pstmt.setObject(i, parameter);
185: }
186: }
187: }
188: }
|