-
Notifications
You must be signed in to change notification settings - Fork 20
/
SqlAdapter.java
278 lines (253 loc) · 12.4 KB
/
SqlAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Copyright 2013 CodeSlap
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.codeslap.persistence;
import java.util.List;
public interface SqlAdapter {
/**
* Persist an object in the database
*
* @param object the object to insert into the database
* @param <T> object type. Must be already registered using {@link DatabaseSpec#match(Class[])}
* @return an object containing the ID of the inserted object
*/
<T> Object store(T object);
/**
* Persist an object in the database attached to another object
*
* @param bean the object to insert into the database
* @param attachedTo the object that bean is attached to
* @param <T> object type. Must be already registered using {@link DatabaseSpec#match(Class[])}
* @param <G> attached type. Must be already registered using {@link DatabaseSpec#match(Class[])}
* @return an object containing the ID of the inserted object
*/
<T, G> Object store(T bean, G attachedTo);
/**
* Persist a collection of objects into the database
*
* @param collection a collection with objects to insert
* @param listener callback to notify progress. Can be null.
*/
<T> void storeCollection(List<T> collection, ProgressListener listener);
/**
* Persist a collection of objects into the database
*
* @param collection a collection with objects to insert
* @param listener callback to notify progress. Can be null.
* @param attachedTo the object that each bean of this collection is attached to
*/
<T, G> void storeCollection(List<T> collection, G attachedTo, ProgressListener listener);
/**
* Persist a collection of objects into the database. It will delete the objects from the
* database that are not in the collection.
*
* @param collection a collection with objects to insert
* @param listener callback to notify progress. Can be null.
*/
<T> void storeUniqueCollection(List<T> collection, ProgressListener listener);
/**
* Updates one of more records in the database
* <p/>
* <b>Note:</b> You must clean the variable <code>where</code>, for instance: <code>entry = 'don't'</code> should be <code>'dont''t'</code>
*
* @param object the object to insert into the database
* @param where the sample object
* @param <T> object type. Must be already registered using {@link DatabaseSpec#match(Class[])}
* @return how many items were updated
*/
<T> int update(T object, T where);
/**
* Updates one of more records in the database
* <p/>
* <b>Note:</b> You must clean the variables <code>where</code> and <code>whereArgs</code>, for instance: <code>entry = 'don't'</code> should be <code>'dont''t'</code>
*
* @param object the object to insert into the database
* @param where a SQL query. It is recommended to use wildcards like: <code>something = ? AND another = ?</code>
* @param whereArgs the list of values used in the wildcards
* @param <T> object type. Must be already registered using {@link DatabaseSpec#match(Class[])}
* @return how many items were updated
*/
<T> int update(T object, String where, String[] whereArgs);
/**
* Retrieves an object from the database
* <p/>
* <b>Note:</b> You must clean the variable <code>where</code>, for instance: <code>entry = 'don't'</code> should be <code>'dont''t'</code>
*
* @param where sample object
* @param <T> object type. Must be already registered using {@link DatabaseSpec#match(Class[])}
* @return the first found element using the passed sample
*/
<T> T findFirst(T where);
/**
* Retrieves an object from the database
* <p/>
* <b>Note:</b> You must clean the variables <code>where</code> and <code>whereArgs</code>, for instance: <code>entry = 'don't'</code> should be <code>'dont''t'</code>
*
* @param theClass the type of the object to retrieve
* @param where a SQL query. It is recommended to use wildcards like: <code>something = ? AND another = ?</code>
* @param whereArgs the list of values used in the wildcards
* @param <T> object type. Must be already registered using {@link DatabaseSpec#match(Class[])}
* @return the first found element using the raw query parameters
*/
<T> T findFirst(Class<T> theClass, String where, String[] whereArgs);
/**
* Retrieves all elements from the database of the specified type
*
* @param theClass the class of the object that we want to retrieve
* @param <T> object type. Must be already registered using {@link DatabaseSpec#match(Class[])}
* @return a list of T objects
*/
<T> List<T> findAll(Class<T> theClass);
/**
* Retrieves all elements from the database that matches the specified sample
* <p/>
* <b>Note:</b> You must clean the variable <code>where</code>, for instance: <code>entry = 'don't'</code> should be <code>'dont''t'</code>
*
* @param where sample object
* @param <T> object type. Must be already registered using {@link DatabaseSpec#match(Class[])}
* @return a list of T objects
*/
<T> List<T> findAll(T where);
/**
* Retrieves all elements from the database that matches the specified sample. And follows the specified constraint.
* <p/>
* <b>Note:</b> You must clean the variable <code>where</code>, for instance: <code>entry = 'don't'</code> should be <code>'dont''t'</code>
*
* @param where sample object
* @param constraint constrains for this query
* @param <T> object type. Must be already registered using {@link DatabaseSpec#match(Class[])}
* @return a list of T objects
*/
<T> List<T> findAll(T where, Constraint constraint);
/**
* Retrieves all elements from the database that are attached to the specified object
* <p/>
* <b>Note:</b> You must clean the variable <code>where</code>, for instance: <code>entry = 'don't'</code> should be <code>'dont''t'</code>
*
* @param where the sample object
* @param attachedTo the object that is attached to the sample object
* @param <T> object type. Must be already registered using {@link DatabaseSpec#match(Class[])}
* @return a list of T objects
*/
<T, G> List<T> findAll(T where, G attachedTo);
/**
* Retrieves all elements from the database that matches the specified sample
* <p/>
* <b>Note:</b> You must clean the variables <code>where</code> and <code>whereArgs</code>, for instance: <code>entry = 'don't'</code> should be <code>'dont''t'</code>
*
* @param theClass the class to find all items
* @param where a SQL query. It is recommended to use wildcards like: <code>something = ? AND another = ?</code>
* @param whereArgs the list of values used in the wildcards
* @param <T> object type. Must be already registered using {@link DatabaseSpec#match(Class[])}
* @return a list of T objects
*/
<T> List<T> findAll(Class<T> theClass, String where, String[] whereArgs);
/**
* Deletes one or more elements from the database
* <p/>
* <b>Note:</b> You must clean the variable <code>where</code>, for instance: <code>entry = 'don't'</code> should be <code>'dont''t'</code>
*
* @param where sample object
* @param <T> object type. Must be already registered using {@link DatabaseSpec#match(Class[])}
* @return how many items were deleted
*/
<T> int delete(T where);
/**
* Deletes one or more elements from the database
* <p/>
* <p/>
* <b>Note:</b> You must clean the variable <code>where</code>, for instance: <code>entry = 'don't'</code> should be <code>'dont''t'</code>
*
* @param where sample object
* @param onCascade true if it must delete relations on cascade
* @param <T> object type. Must be already registered using {@link DatabaseSpec#match(Class[])}
* @return how many items were deleted
*/
<T> int delete(T where, boolean onCascade);
/**
* Deletes one or more elements from the database
* <p/>
* <b>Note:</b> You must clean the variables <code>where</code> and <code>whereArgs</code>, for instance: <code>entry = 'don't'</code> should be <code>'dont''t'</code>
*
* @param theClass the type of the object to delete
* @param where a SQL query. It is recommended to use wildcards like: <code>something = ? AND another = ?</code>
* @param whereArgs the list of values used in the wildcards
* @param <T> object type. Must be already registered using {@link DatabaseSpec#match(Class[])}
* @return how many items were deleted
*/
<T> int delete(Class<T> theClass, String where, String[] whereArgs);
/**
* Deletes one or more elements from the database
* <p/>
* <b>Note:</b> You must clean the variables <code>where</code> and <code>whereArgs</code>, for instance: <code>entry = 'don't'</code> should be <code>'dont''t'</code>
*
* @param theClass the type of the object to delete
* @param where a SQL query. It is recommended to use wildcards like: <code>something = ? AND another = ?</code>
* @param onCascade true if it must delete relations on cascade
* @param whereArgs the list of values used in the wildcards
* @param <T> object type. Must be already registered using {@link DatabaseSpec#match(Class[])}
* @return how many items were deleted
*/
<T> int delete(Class<T> theClass, String where, String[] whereArgs, boolean onCascade);
/**
* Truncates a table. This will also remove the autoincrement counters
*
* @param classes the type of the object to delete
*/
void truncate(Class<?>... classes);
/**
* Counts how many items there are in the database and match the specified condition
* <p/>
* <b>Note:</b> You must clean the variables <code>where</code>, for instance: <code>entry = 'don't'</code> should be <code>'dont''t'</code>
*
* @param where the sample object
* @param <T> object type. Must be already registered using {@link DatabaseSpec#match(Class[])}
* @return number of elements in the table of the specified object
*/
<T> int count(T where);
/**
* Counts how many items there are in the database and match the specified condition
* <p/>
* <b>Note:</b> You must clean the variables <code>where</code> and <code>whereArgs</code>, for instance: <code>entry = 'don't'</code> should be <code>'dont''t'</code>
*
* @param theClass the class of the object that we want to count
* @param where a SQL query. It is recommended to use wildcards like: <code>something = ? AND another = ?</code>
* @param whereArgs the list of values used in the wildcards
* @param <T> object type. Must be already registered using {@link DatabaseSpec#match(Class[])}
* @return number of elements in the table of the specified object
*/
<T> int count(Class<T> theClass, String where, String[] whereArgs);
/**
* Counts how many items there are in the database
*
* @param theClass the class of the object that we want to count
* @param <T> object type. Must be already registered using {@link DatabaseSpec#match(Class[])}
* @return number of elements in the table of the specified object
*/
<T> int count(Class<T> theClass);
/**
* Callback used when storing a collection to notify the progress.
* Note: when doing a bulk insert, we use the BEGIN TRANSACTION; ...; COMMIT; technique. So, if you are inserting
* 99 records, each record will consume 1% and the COMMIT phase another 1%.
*/
interface ProgressListener {
/**
* Called each time the progress percentage changes
*
* @param percentage the current progress
*/
void onProgressChange(int percentage);
}
}