KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > jfccontrols > Query


1 /**
2  * com.mckoi.jfccontrols.Query 23 Aug 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.jfccontrols;
26
27 import java.math.BigDecimal JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Date JavaDoc;
30
31 import com.mckoi.util.TimeFrame;
32
33 /**
34  * Encapsulates the information in a query to the database. This object is
35  * used in QueryAgent.
36  *
37  * @author Tobias Downer
38  */

39
40 public class Query implements Cloneable JavaDoc {
41
42   /**
43    * The string to query.
44    */

45   private String JavaDoc query_string;
46
47   /**
48    * The parameters of the query (if any).
49    */

50   private ArrayList JavaDoc parameters;
51
52   /**
53    * Constructs the query.
54    */

55   public Query(String JavaDoc query) {
56     this.query_string = query;
57   }
58
59   /**
60    * Sets a parameter.
61    */

62   private void internalSet(int index, Object JavaDoc ob) {
63     if (parameters == null) {
64       parameters = new ArrayList JavaDoc();
65     }
66     for (int i = parameters.size(); i < index; ++i) {
67       parameters.add(null);
68     }
69     Object JavaDoc old = parameters.set(index - 1, ob);
70     if (old != null) {
71 // Debug.write(Debug.WARNING, this,
72
// "Setting over a previously set parameter.");
73
}
74   }
75
76   /**
77    * Returns the query string.
78    */

79   public String JavaDoc getString() {
80     return query_string;
81   }
82
83   /**
84    * Returns the number of parameters.
85    */

86   public int parameterCount() {
87     if (parameters == null) {
88       return 0;
89     }
90     return parameters.size();
91   }
92
93   /**
94    * Returns parameters number 'n' where 0 is the first parameters, etc.
95    */

96   public Object JavaDoc getParameter(int index) {
97     return parameters.get(index);
98   }
99
100   /**
101    * Returns a copy of this Query object but with a different query string.
102    */

103   public Query changeSQL(String JavaDoc sql) {
104     try {
105       Query query = (Query) clone();
106       query.query_string = sql;
107       return query;
108     }
109     catch (CloneNotSupportedException JavaDoc e) {
110       throw new Error JavaDoc(e.getMessage());
111     }
112   }
113
114   /**
115    * For debugging.
116    */

117   public String JavaDoc toString() {
118     StringBuffer JavaDoc str = new StringBuffer JavaDoc();
119     str.append("Query: " + query_string + "\n");
120     str.append("Parameters: ");
121     for (int i = 0; i < parameterCount(); ++i) {
122       str.append(getParameter(i));
123       str.append(", ");
124     }
125     return new String JavaDoc(str);
126   }
127
128
129
130   // ---------- Methods for adding different types of parameters ----------
131
// NOTE: For all these methods, para_index = 1 is the first parameters,
132
// 2 is the second, etc.
133

134   public void setString(int para_index, String JavaDoc str) {
135     internalSet(para_index, str);
136   }
137
138   public void setBoolean(int para_index, boolean val) {
139     internalSet(para_index, new Boolean JavaDoc(val));
140   }
141
142   public void setBigDecimal(int para_index, BigDecimal JavaDoc val) {
143     internalSet(para_index, val);
144   }
145
146   public void setInt(int para_index, int val) {
147     internalSet(para_index, new BigDecimal JavaDoc(val));
148   }
149
150   public void setLong(int para_index, long val) {
151     internalSet(para_index, new BigDecimal JavaDoc(val));
152   }
153
154   public void setDouble(int para_index, double val) {
155     internalSet(para_index, new BigDecimal JavaDoc(val));
156   }
157
158   public void setDate(int para_index, Date JavaDoc val) {
159     internalSet(para_index, val);
160   }
161
162   public void setTimeFrame(int para_index, TimeFrame val) {
163     internalSet(para_index, val.getPeriod());
164   }
165
166   public void setObject(int para_index, Object JavaDoc val) {
167     if (val == null ||
168         val instanceof BigDecimal JavaDoc ||
169         val instanceof String JavaDoc ||
170         val instanceof Date JavaDoc ||
171         val instanceof Boolean JavaDoc) {
172       internalSet(para_index, val);
173     }
174     else if (val instanceof TimeFrame) {
175       setTimeFrame(para_index, (TimeFrame) val);
176     }
177     else if (val instanceof Integer JavaDoc) {
178       internalSet(para_index, new BigDecimal JavaDoc(((Integer JavaDoc) val).intValue()));
179     }
180     else if (val instanceof Long JavaDoc) {
181       internalSet(para_index, new BigDecimal JavaDoc(((Long JavaDoc) val).longValue()));
182     }
183     else if (val instanceof Double JavaDoc) {
184       internalSet(para_index, new BigDecimal JavaDoc(((Double JavaDoc) val).doubleValue()));
185     }
186     // Default behaviour for unknown objects is to cast as a String
187
// parameter.
188
else {
189       setString(para_index, val.toString());
190     }
191   }
192
193 }
194
Popular Tags