Repository navigation
Expand file tree
/
Copy pathORM.java
More file actions
49 lines (36 loc) · 1.48 KB
/
Copy pathORM.java
File metadata and controls
49 lines (36 loc) · 1.48 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
package org.javawebstack.orm;
import org.javawebstack.orm.exception.ORMConfigurationException;
import org.javawebstack.orm.migration.AutoMigrator;
import org.javawebstack.orm.wrapper.SQL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
public class ORM {
public static final Logger LOGGER = Logger.getLogger("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, SQL sql, ORMConfig config) throws ORMConfigurationException {
Repo<T> repo = new Repo<>(model, sql, config);
repositories.put(model, repo);
return repo;
}
public static <T extends Model> Repo<T> register(Class<T> model, SQL sql) throws ORMConfigurationException {
return register(model, sql, new ORMConfig());
}
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 void autoMigrate(){
AutoMigrator.migrate(repositories.values().toArray(new Repo<?>[0]));
}
}