KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > sps > jdbc > DataStoreSql


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.syncclient.sps.jdbc;
20
21 import sync4j.syncclient.sps.common.*;
22
23
24 /**
25  * This class provide
26  * static methods about create sql statements
27  *
28  * @author Fabio Maggi @ Funambol
29  * $Id: DataStoreSql.java,v 1.2 2005/01/19 11:18:37 fabius Exp $
30  */

31
32 public class DataStoreSql {
33
34     //----------------------------------------------------------- Public methods
35

36     /**
37      * build sql query to find record about key
38      *
39      * @param dataStore
40      * @param record with set key
41      * @return sql query
42      **/

43     public static String JavaDoc selectQuery(DataStore dataStore, Record record) {
44
45         String JavaDoc queryFields = null;
46         String JavaDoc query = null;
47
48         String JavaDoc function = null;
49         String JavaDoc nameFieldKey = null;
50
51         Object JavaDoc params = null;
52
53         for (int i=0; i < dataStore.getRecordMetadata().getFieldMetadata().length; i++) {
54
55             function = (String JavaDoc) dataStore.getRecordMetadata().getFieldMetadata()[i].getAttributes().get("function");
56
57             if (function.equals("key")) {
58                 nameFieldKey = dataStore.getRecordMetadata().getFieldMetadata()[i].getName();
59             }
60
61         }
62
63         FieldMetadata[] fmd = dataStore.getRecordMetadata().getFieldMetadata();
64
65         for(int i = 0; i < fmd.length; i++) {
66
67             if( queryFields == null) {
68                 queryFields = fmd[i].getName();
69             } else {
70                 queryFields = queryFields + "," + " " + fmd[i].getName();
71             }
72
73         }
74
75         query = "SELECT" +" " + queryFields + " " + "FROM" + " " + dataStore.getDataStoreName() + " " +
76                 "WHERE " + nameFieldKey + " = ?";
77
78         return query;
79
80     }
81
82
83
84     /**
85      * build sql query to insert record
86      *
87      * @param dataStore
88      * @param record to insert
89      * @return sql query
90      **/

91     public static String JavaDoc insertQuery(DataStore dataStore, Record record) {
92
93         String JavaDoc fieldValue = null;
94         String JavaDoc fieldsValue = null;
95         String JavaDoc fieldTmp = null;
96
97         String JavaDoc query = null;
98
99         String JavaDoc nameFieldsQuery = null;
100
101         FieldMetadata[] fmd = dataStore.getRecordMetadata().getFieldMetadata();
102
103
104         for(int i = 0; i < fmd.length; i++) {
105
106             fieldValue = "?";
107
108             if(fieldsValue == null) {
109                 fieldsValue = fieldValue;
110             } else {
111                 fieldsValue = fieldsValue + "," + " " + fieldValue;
112             }
113
114
115             if(nameFieldsQuery == null) {
116                 nameFieldsQuery = fmd[i].getName();
117             } else {
118                 nameFieldsQuery = nameFieldsQuery + "," + " " + fmd[i].getName();
119             }
120
121         }
122
123
124         query = "INSERT INTO" + " " + dataStore.getDataStoreName() + " " + "(" + nameFieldsQuery + ")" + " " + "VALUES("+ fieldsValue +")";
125
126         return query;
127
128     }
129
130
131
132     /**
133      * build sql query to update record
134      *
135      * @param dataStore
136      * @param record to update
137      * @return sql query
138      **/

139     public static String JavaDoc updateQuery(DataStore dataStore, Record record) {
140
141         String JavaDoc fieldValue = null;
142         String JavaDoc fieldsValue = null;
143
144         String JavaDoc queryFields = null;
145         String JavaDoc query = null;
146
147         String JavaDoc function = null;
148         String JavaDoc nameFieldKey = null;
149
150         for (int i=0; i < dataStore.getRecordMetadata().getFieldMetadata().length; i++) {
151
152             function = (String JavaDoc) dataStore.getRecordMetadata().getFieldMetadata()[i].getAttributes().get("function");
153
154             if (function.equals("key")) {
155                 nameFieldKey = dataStore.getRecordMetadata().getFieldMetadata()[i].getName();
156             }
157
158         }
159
160         FieldMetadata[] fmd = dataStore.getRecordMetadata().getFieldMetadata();
161
162         for(int i = 1; i < fmd.length; i++) {
163
164
165             fieldValue = fmd[i].getName() + "=?";
166
167             if(fieldsValue == null) {
168                 fieldsValue = fieldValue;
169             } else {
170                 fieldsValue = fieldsValue + "," + fieldValue;
171             }
172
173         }
174
175         query = "UPDATE" + " " + dataStore.getDataStoreName() + " SET " + fieldsValue +
176                 " WHERE " + nameFieldKey + " = ?";
177
178         return query;
179
180     }
181
182
183
184     /**
185      * build sql query to delete record
186      *
187      * @param dataStore
188      * @param record to delete
189      * @return sql query
190      **/

191     public static String JavaDoc deleteQuery(DataStore dataStore, Record record) {
192
193         String JavaDoc fieldsValue = null;
194
195         String JavaDoc queryFields = null;
196         String JavaDoc query = null;
197
198         String JavaDoc function = null;
199         String JavaDoc nameFieldKey = null;
200
201         for (int i=0; i < dataStore.getRecordMetadata().getFieldMetadata().length; i++) {
202
203             function = (String JavaDoc) dataStore.getRecordMetadata().getFieldMetadata()[i].getAttributes().get("function");
204
205             if (function.equals("key")) {
206                 nameFieldKey = dataStore.getRecordMetadata().getFieldMetadata()[i].getName();
207             }
208
209         }
210
211         query = "DELETE FROM " + dataStore.getDataStoreName() +
212                 " WHERE " + nameFieldKey +" = ?";
213
214         return query;
215
216     }
217
218
219
220     /**
221      * build sql query to find all record in datastore
222      *
223      * @param dataStore
224      * @return sql query
225      **/

226     public static String JavaDoc allRecordsQuery(DataStore dataStore) {
227
228         String JavaDoc queryFields = null;
229         String JavaDoc query = null;
230
231         FieldMetadata[] fmd = dataStore.getRecordMetadata().getFieldMetadata();
232
233         for(int i = 0; i < fmd.length; i++) {
234             if( queryFields == null) {
235                 queryFields = fmd[i].getName();
236             } else {
237                 queryFields = queryFields + "," + " " + fmd[i].getName();
238             }
239
240         }
241
242         query = "SELECT" + " " + queryFields + " " + "FROM" + " " + dataStore.getDataStoreName();
243
244         return query;
245
246     }
247
248
249
250     /**
251      * build sql query to find all record in datastore
252      *
253      * @param dataStore
254      * @return sql query
255      **/

256     public static String JavaDoc RecordsByStateByDateQuery(DataStore dataStore, String JavaDoc nameFieldState, String JavaDoc nameFieldTimestamp) {
257
258         String JavaDoc queryFields = null;
259         String JavaDoc query = null;
260
261         String JavaDoc where = null;
262
263         FieldMetadata[] fmd = dataStore.getRecordMetadata().getFieldMetadata();
264
265         for(int i = 0; i < fmd.length; i++) {
266             if( queryFields == null) {
267                 queryFields = fmd[i].getName();
268             } else {
269                 queryFields = queryFields + "," + " " + fmd[i].getName();
270             }
271
272         }
273
274
275         if (nameFieldState != null && nameFieldTimestamp != null) {
276             where = " " + "where " + nameFieldState + " = ? and " + nameFieldTimestamp + " > ?";
277         } else if (nameFieldState != null && nameFieldTimestamp == null) {
278             where = " " + "where " + nameFieldState + " = ?";
279         } else if (nameFieldState == null && nameFieldTimestamp != null) {
280             where = " " + "where " + nameFieldTimestamp + " > ?";
281         }
282
283         query = "SELECT" + " " + queryFields + " " + "FROM" + " " + dataStore.getDataStoreName() + where;
284
285         return query;
286
287     }
288
289
290
291     /**
292      * build sql query to find next key in datastore
293      *
294      * @param dataStore
295      * @return sql query
296      **/

297     public static String JavaDoc getNextKeyQuery(DataStore dataStore, String JavaDoc nameFieldKey) {
298
299         String JavaDoc query = null;
300
301         query = "SELECT MAX(" + nameFieldKey + ") + 1 FROM" + " " + dataStore.getDataStoreName();
302
303         return query;
304
305     }
306
307 }
Popular Tags