-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathORMConfig.java
More file actions
115 lines (93 loc) · 3.23 KB
/
Copy pathORMConfig.java
File metadata and controls
115 lines (93 loc) · 3.23 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package org.javawebstack.orm;
import org.javawebstack.orm.exception.ORMConfigurationException;
import org.javawebstack.orm.mapper.DefaultMapper;
import org.javawebstack.orm.mapper.TypeMapper;
import java.util.ArrayList;
import java.util.List;
public class ORMConfig {
private String tablePrefix = "";
private boolean camelToSnakeCase = true;
private int defaultSize = 0;
private boolean idPrimaryKey = true;
private boolean idAutoIncrement = true;
private final List<TypeMapper> typeMappers = new ArrayList<>();
private boolean preventUnnecessaryUpdates = true;
public ORMConfig() {
typeMappers.add(new DefaultMapper());
}
public ORMConfig setTablePrefix(String tablePrefix) {
this.tablePrefix = tablePrefix;
return this;
}
public ORMConfig setCamelToSnakeCase(boolean camelToSnakeCase) {
this.camelToSnakeCase = camelToSnakeCase;
return this;
}
public ORMConfig setDefaultSize(int defaultSize) throws ORMConfigurationException {
if(defaultSize <= 0)
throw new ORMConfigurationException("Default size must be positive and non-zero. If this exceptions occurs unexpectedly make sure no overflow is occurring.");
this.defaultSize = defaultSize;
return this;
}
public ORMConfig addTypeMapper(TypeMapper typeMapper) {
typeMappers.add(typeMapper);
return this;
}
public ORMConfig setIdPrimaryKey(boolean idPrimaryKey) {
this.idPrimaryKey = idPrimaryKey;
return this;
}
public ORMConfig setIdAutoIncrement(boolean idAutoIncrement) {
this.idAutoIncrement = idAutoIncrement;
return this;
}
public boolean isCamelToSnakeCase() {
return camelToSnakeCase;
}
public String getTablePrefix() {
return tablePrefix;
}
public int getDefaultSize() {
return defaultSize;
}
public List<TypeMapper> getTypeMappers() {
return typeMappers;
}
public boolean isIdPrimaryKey() {
return idPrimaryKey;
}
public boolean isIdAutoIncrement() {
return idAutoIncrement;
}
public TypeMapper getTypeMapper(Class<?> type, int size) {
for (TypeMapper mapper : getTypeMappers()) {
SQLType sqlType = mapper.getType(type, size);
if (sqlType != null)
return mapper;
}
return null;
}
public SQLType getType(Class<?> type, int size) {
for (TypeMapper mapper : getTypeMappers()) {
SQLType sqlType = mapper.getType(type, size);
if (sqlType != null)
return sqlType;
}
return SQLType.TEXT;
}
public String getTypeParameters(Class<?> type, int size) {
for (TypeMapper mapper : getTypeMappers()) {
SQLType sqlType = mapper.getType(type, size);
if (sqlType != null)
return mapper.getTypeParameters(type, size);
}
return null;
}
public boolean shouldPreventUnnecessaryUpdates() {
return preventUnnecessaryUpdates;
}
public ORMConfig setPreventUnnecessaryUpdates(boolean preventUnnecessaryUpdates) {
this.preventUnnecessaryUpdates = preventUnnecessaryUpdates;
return this;
}
}