-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathORM.java
More file actions
97 lines (75 loc) · 3.17 KB
/
ORM.java
File metadata and controls
97 lines (75 loc) · 3.17 KB
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
package org.javawebstack.orm;
import org.javawebstack.orm.connection.SQL;
import org.javawebstack.orm.connection.pool.SQLPool;
import org.javawebstack.orm.connection.pool.SingletonPool;
import org.javawebstack.orm.exception.ORMConfigurationException;
import org.javawebstack.orm.migration.AutoMigrator;
import org.reflections.Reflections;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ORM {
private static final Map<Class<? extends Model>, Repo<?>> repositories = new HashMap<>();
public static <T extends Model> Repo<T> repo(Class<T> model) {
return (Repo<T>) repositories.get(model);
}
public static <T extends Model> Repo<T> register(Class<T> model, SQLPool pool, ORMConfig config) throws ORMConfigurationException {
Repo<T> repo = new Repo<>(model, pool, config);
repositories.put(model, repo);
return repo;
}
public static <T extends Model> Repo<T> register(Class<T> model, SQLPool pool) throws ORMConfigurationException {
return register(model, pool, new ORMConfig());
}
public static void register(Package p, SQLPool pool, ORMConfig config) throws ORMConfigurationException {
for (Class<? extends Model> model : new Reflections(p.getName()).getSubTypesOf(Model.class)) {
if (!Modifier.isAbstract(model.getModifiers()))
ORM.register(model, pool, config);
}
}
public static void register(Package p, SQLPool pool) throws ORMConfigurationException {
register(p, pool, new ORMConfig());
}
@Deprecated
public static <T extends Model> Repo<T> register(Class<T> model, SQL sql, ORMConfig config) throws ORMConfigurationException {
return register(model, new SingletonPool(sql), config);
}
@Deprecated
public static <T extends Model> Repo<T> register(Class<T> model, SQL sql) throws ORMConfigurationException {
return register(model, new SingletonPool(sql));
}
@Deprecated
public static void register(Package p, SQL sql, ORMConfig config) throws ORMConfigurationException {
register(p, new SingletonPool(sql), config);
}
@Deprecated
public static void register(Package p, SQL sql) throws ORMConfigurationException {
register(p, new SingletonPool(sql));
}
public static void unregister(Class<? extends Model> model) {
repositories.remove(model);
}
public static void unregister(Repo<?> model) {
repositories.remove(model.getInfo().getModelClass());
}
public static List<Class<? extends Model>> getModels() {
return new ArrayList<>(repositories.keySet());
}
public static List<Repo<?>> getRepos() {
return new ArrayList<>(repositories.values());
}
public static void autoDrop() {
AutoMigrator.drop(repositories.values().toArray(new Repo<?>[0]));
}
public static void autoMigrate() {
autoMigrate(false);
}
public static void autoMigrate(boolean fresh) {
AutoMigrator.migrate(fresh, repositories.values().toArray(new Repo<?>[0]));
}
public static void reset() {
repositories.clear();
}
}