/
DB.java
291 lines (225 loc) · 7.14 KB
/
DB.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
279
280
281
282
283
284
285
286
287
288
289
290
291
package org.rapidoid.db;
/*
* #%L
* rapidoid-db
* %%
* Copyright (C) 2014 - 2015 Nikolche Mihajlovski
* %%
* 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.
* #L%
*/
import java.io.File;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.config.Conf;
import org.rapidoid.lambda.Callback;
import org.rapidoid.lambda.Operation;
import org.rapidoid.lambda.Predicate;
import org.rapidoid.util.Cls;
import org.rapidoid.util.U;
@Authors("Nikolche Mihajlovski")
@Since("2.0.0")
public class DB {
private static final String IMPL_NAME = "org.rapidoid.db.impl.inmem.DbImpl";
static final Class<Database> DB_IMPL_CLASS;
private static Database db;
static {
String dbImplName = Conf.option("db", IMPL_NAME);
DB_IMPL_CLASS = Cls.getClassIfExists(IMPL_NAME);
U.must(DB_IMPL_CLASS != null, "Cannot find Db implementation (%s)!", dbImplName);
U.must(Database.class.isAssignableFrom(DB_IMPL_CLASS), "%s must implement %s!", dbImplName,
Database.class.getCanonicalName());
start();
}
public static String path() {
String path = Conf.option("db", "");
if (!path.isEmpty() && !path.endsWith(File.separator)) {
path += File.separator;
}
return path;
}
public static void start() {
db = (Database) Cls.customizable(DB.DB_IMPL_CLASS, "default", path() + "default.db");
db.loadAndStart();
}
public static Database db() {
assert U.must(db != null, "Database not initialized!");
return db;
}
public static void setDb(Database db) {
DB.db = db;
}
public static long insert(Object record) {
return db().insert(record);
}
public static void delete(long id) {
db().delete(id);
}
public static void delete(Object record) {
db().delete(record);
}
public static <T> T get(long id) {
return db().get(id);
}
public static <T> T getIfExists(long id) {
return db().getIfExists(id);
}
public static <T> T get(long id, Class<T> clazz) {
return db().get(id, clazz);
}
public static <E> List<E> getAll(Class<E> clazz) {
return db().getAll(clazz);
}
public static <E> List<E> getAll(long... ids) {
return db().getAll(ids);
}
public static <E> List<E> getAll(Collection<Long> ids) {
return db().getAll(ids);
}
public static <E> void refresh(E record) {
db().refresh(record);
}
public static void update(long id, Object record) {
db().update(id, record);
}
public static void update(Object record) {
db().update(record);
}
public static long persist(Object record) {
return db().persist(record);
}
public static long persistedIdOf(Object record) {
return db().persistedIdOf(record);
}
public static <E> E readColumn(long id, String column) {
return db().readColumn(id, column);
}
public static <E> List<E> find(Predicate<E> match) {
return db().find(match);
}
public static <E> List<E> find(Iterable<Long> ids) {
return db().find(ids);
}
public static <E> List<E> find(Class<E> clazz, Predicate<E> match, Comparator<E> orderBy) {
return db().find(clazz, match, orderBy);
}
public static <E> List<E> find(String searchPhrase) {
return db().find(searchPhrase);
}
public static <E> List<E> find(Class<E> clazz, String query, Object... args) {
return db().find(clazz, query, args);
}
public static <E> void each(Operation<E> lambda) {
db().each(lambda);
}
public static void transaction(Runnable transaction, boolean readOnly) {
db().transaction(transaction, readOnly);
}
public static void transaction(Runnable transaction, boolean readOnly, Callback<Void> callback) {
db().transaction(transaction, readOnly, callback);
}
public static void shutdown() {
if (db() != null) {
db().shutdown();
}
}
public static long size() {
return db().size();
}
public static void clear() {
db().clear();
}
public static boolean isActive() {
return db() != null && db().isActive();
}
public static void halt() {
if (db() != null) {
db().halt();
}
}
public static void destroy() {
if (db() != null) {
db().destroy();
}
}
public static long getIdOf(Object record) {
return db().getIdOf(record);
}
public static long getVersionOf(long id) {
return db().getVersionOf(id);
}
public static <E> DbColumn<E> column(Map<String, Object> map, String name, Class<E> type) {
return db().column(map, name, type);
}
public static <E> DbList<E> list(Object holder, String relation) {
return db().list(holder, relation);
}
public static <E> DbSet<E> set(Object holder, String relation) {
return db().set(holder, relation);
}
public static <E> DbRef<E> ref(Object holder, String relation) {
return db().ref(holder, relation);
}
public static DbSchema schema() {
return db().schema();
}
@SuppressWarnings("unchecked")
public static <E> E entity(Class<E> entityType) {
return (E) schema().entity(entityType, Collections.EMPTY_MAP);
}
public static <E> E entity(Class<E> entityType, Map<String, ?> properties) {
return schema().entity(entityType, properties);
}
public static <E> E entity(Class<E> entityType, String prop, Object value) {
return schema().entity(entityType, U.map(prop, value));
}
public static <E> E entity(Class<E> entityType, String prop1, Object value1, String prop2, Object value2) {
return schema().entity(entityType, U.map(prop1, value1, prop2, value2));
}
public static <E> E entity(Class<E> entityType, String prop1, Object value1, String prop2, Object value2,
String prop3, Object value3) {
return schema().entity(entityType, U.map(prop1, value1, prop2, value2, prop3, value3));
}
public static <E> E entity(Class<E> entityType, String prop1, Object value1, String prop2, Object value2,
String prop3, Object value3, String prop4, Object value4) {
return schema().entity(entityType, U.map(prop1, value1, prop2, value2, prop3, value3, prop4, value4));
}
public static <E> E entity(Class<E> entityType, String prop1, Object value1, String prop2, Object value2,
String prop3, Object value3, String prop4, Object value4, String prop5, Object value5) {
return schema().entity(entityType,
U.map(prop1, value1, prop2, value2, prop3, value3, prop4, value4, prop5, value5));
}
public static <E> DbDsl<E> dsl(Class<E> entityType) {
return schema().dsl(entityType);
}
public static Database as(String username) {
return db().as(username);
}
public static Database sudo() {
return db().sudo();
}
public static void init(String data, Object... args) {
db().init(data, args);
}
public static void rql(String rql, Object... args) {
db().rql(rql, args);
}
public static <T> T entity(String data, Object... args) {
return db().entity(data, args);
}
}