-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathORMapping.java
139 lines (116 loc) · 3.97 KB
/
ORMapping.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
package org.jminiorm.mapping;
import org.jminiorm.exception.DBException;
import org.jminiorm.utils.CaseInsensitiveMap;
import java.util.ArrayList;
import java.util.List;
/**
* Represents the mapping between a Java class and a database table.
*/
public class ORMapping {
private Class<?> javaClass;
private String schema;
private String table;
private List<Index> indexes;
private List<ColumnMapping> columnMappings;
private CaseInsensitiveMap<ColumnMapping> columnMappingsIndexedByProperty;
private CaseInsensitiveMap<ColumnMapping> columnMappingsIndexedByColumn;
private List<ColumnMapping> idColumnMappings;
private Boolean hasId;
public ORMapping() {
}
public Class<?> getJavaClass() {
return javaClass;
}
public void setJavaClass(Class<?> javaClass) {
this.javaClass = javaClass;
}
public String getSchema() {
return schema;
}
public void setSchema(String schema) {
this.schema = schema;
}
public String getTable() {
return table;
}
public void setTable(String table) {
this.table = table;
}
public List<Index> getIndexes() {
return indexes;
}
public void setIndexes(List<Index> indexes) {
this.indexes = indexes;
}
public List<ColumnMapping> getColumnMappings() {
return columnMappings;
}
public void setColumnMappings(List<ColumnMapping> columnMappings) {
this.columnMappings = columnMappings;
}
/**
* Returns the column mapping for the given property. Case doesn't matter.
*
* @param property
* @return
*/
public ColumnMapping getColumnMappingByProperty(String property) {
if (columnMappingsIndexedByProperty == null) {
columnMappingsIndexedByProperty = createColumnMappingsIndexedByProperty();
}
return columnMappingsIndexedByProperty.get(property);
}
protected CaseInsensitiveMap<ColumnMapping> createColumnMappingsIndexedByProperty() {
CaseInsensitiveMap<ColumnMapping> mappings = new CaseInsensitiveMap<>();
for (ColumnMapping mapping : getColumnMappings()) {
mappings.put(mapping.getPropertyDescriptor().getName(), mapping);
}
return mappings;
}
/**
* Returns the column mapping for the given column. Case doesn't matter.
*
* @param column
* @return
*/
public ColumnMapping getColumnMappingByColumn(String column) {
if (columnMappingsIndexedByColumn == null) {
columnMappingsIndexedByColumn = createColumnMappingsIndexedByColumn();
}
return columnMappingsIndexedByColumn.get(column);
}
protected CaseInsensitiveMap<ColumnMapping> createColumnMappingsIndexedByColumn() {
CaseInsensitiveMap<ColumnMapping> mappings = new CaseInsensitiveMap<>();
for (ColumnMapping mapping : getColumnMappings()) {
mappings.put(mapping.getColumn(), mapping);
}
return mappings;
}
/**
* Returns the column mappings for the properties marked as id.
*/
public List<ColumnMapping> getIdColumnMappings() {
if (idColumnMappings == null) {
idColumnMappings = new ArrayList<>();
for (ColumnMapping mapping : getColumnMappings()) {
if (mapping.isId()) {
idColumnMappings.add( mapping);
break;
}
}
if (idColumnMappings.isEmpty())
throw new RuntimeException("No ID column defined in class " + getJavaClass().getName());
}
return idColumnMappings;
}
public ColumnMapping getIdColumnMapping() {
if (getIdColumnMappings().size() > 1) throw new DBException("More than one ID column.");
return getIdColumnMappings().get(0);
}
public Boolean hasId() {
if (hasId == null) {
hasId = getColumnMappings().stream().anyMatch(ColumnMapping::isId);
}
return hasId;
}
}