Skip to content

Commit 7c93dcb

Browse files
committed
2.0.1 (log4j2 and updates to experimental Join)
1 parent 912d7b3 commit 7c93dcb

27 files changed

+324
-165
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.sproket</groupId>
88
<artifactId>persism</artifactId>
9-
<version>2.0.0</version>
9+
<version>2.0.1</version>
1010
<packaging>jar</packaging>
1111

1212
<build>
@@ -297,13 +297,13 @@
297297
<dependency>
298298
<groupId>org.apache.logging.log4j</groupId>
299299
<artifactId>log4j-api</artifactId>
300-
<version>2.14.1</version>
300+
<version>2.15.0</version>
301301
<scope>provided</scope>
302302
</dependency>
303303
<dependency>
304304
<groupId>org.apache.logging.log4j</groupId>
305305
<artifactId>log4j-core</artifactId>
306-
<version>2.14.1</version>
306+
<version>2.15.0</version>
307307
<scope>provided</scope>
308308
</dependency>
309309

src/net/sf/persism/ColumnInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final class ColumnInfo {
1515
// H2 - BIT - comes back NULL
1616
Types columnType;
1717

18-
// Currently just kept for possible future use
18+
// kept for possible future use
1919
int sqlColumnType;
2020
String sqlColumnTypeName;
2121

src/net/sf/persism/Converter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ Object convert(Object value, Class<?> targetType, String columnName) {
196196
returnValue = dval.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
197197

198198
} else if (targetType.isEnum()) {
199-
// If this is an enum do a case insensitive comparison
199+
// If this is an enum do a case-insensitive comparison
200200
Object[] enumConstants = targetType.getEnumConstants();
201201
for (Object element : enumConstants) {
202202
if (("" + value).equalsIgnoreCase(element.toString())) {

src/net/sf/persism/JoinInfo.java

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,37 @@
1010

1111
final class JoinInfo {
1212

13-
String[] parentPropertyNames;
14-
String[] childPropertyNames;
15-
List<PropertyInfo> parentProperties;
16-
List<PropertyInfo> childProperties;
17-
Class<?> parentClass;
18-
Class<?> childClass;
19-
boolean caseSensitive;
20-
boolean parentIsAQuery;
21-
PropertyInfo joinProperty;
13+
private String[] parentPropertyNames;
14+
private String[] childPropertyNames;
15+
private List<PropertyInfo> parentProperties;
16+
private List<PropertyInfo> childProperties;
17+
private Class<?> parentClass;
18+
private Class<?> childClass;
19+
private final PropertyInfo joinProperty;
20+
private final boolean caseSensitive;
21+
private final boolean parentIsAQuery;
22+
private boolean reversed = false;
23+
24+
JoinInfo(JoinInfo other) {
25+
parentPropertyNames = other.parentPropertyNames;
26+
childPropertyNames = other.childPropertyNames;
27+
parentProperties = other.parentProperties;
28+
childProperties = other.childProperties;
29+
parentClass = other.parentClass;
30+
childClass = other.childClass;
31+
joinProperty = other.joinProperty;
32+
caseSensitive = other.caseSensitive;
33+
parentIsAQuery = other.parentIsAQuery;
34+
reversed = other.reversed;
35+
}
2236

2337
// note parent may be a POJO or a list of POJOs
2438
public JoinInfo(Join joinAnnotation, PropertyInfo joinProperty, Object parent, Class<?> parentClass) {
2539
this.joinProperty = joinProperty;
2640
parentPropertyNames = joinAnnotation.onProperties().split(",");
2741
childPropertyNames = joinAnnotation.toProperties().split(",");
2842
if (parentPropertyNames.length != childPropertyNames.length) {
29-
throw new PersismException("how would I match these?");
43+
throw new PersismException("how would I match these?"); // todo add to Messages
3044
}
3145
Util.trimArray(parentPropertyNames);
3246
Util.trimArray(childPropertyNames);
@@ -39,6 +53,7 @@ public JoinInfo(Join joinAnnotation, PropertyInfo joinProperty, Object parent, C
3953

4054
parentIsAQuery = Collection.class.isAssignableFrom(parent.getClass());
4155

56+
// todo if we encapsulate PropertyInfo we MAY NOT need a defensive copy here or with childProperties
4257
parentProperties = new ArrayList<>(MetaData.getPropertyInfo(parentClass)).stream().filter(propertyInfo -> {
4358
boolean found = false;
4459
for (int j = 0; j < parentPropertyNames.length; j++) {
@@ -62,7 +77,65 @@ public JoinInfo(Join joinAnnotation, PropertyInfo joinProperty, Object parent, C
6277
return found;
6378

6479
}).collect(Collectors.toList());;
80+
}
81+
82+
public JoinInfo swapParentAndChild() {
83+
JoinInfo info = new JoinInfo(this);
84+
85+
String[] tmpPropertyNames = info.parentPropertyNames;
86+
info.parentPropertyNames = info.childPropertyNames;
87+
info.childPropertyNames = tmpPropertyNames;
88+
89+
List<PropertyInfo> tmpProperties = info.parentProperties;
90+
info.parentProperties = info.childProperties;
91+
info.childProperties = tmpProperties;
92+
93+
Class<?> tmpClass = info.parentClass;
94+
info.parentClass = info.childClass;
95+
info.childClass = tmpClass;
96+
97+
info.reversed = true;
98+
return info;
99+
}
100+
101+
public String[] parentPropertyNames() {
102+
return parentPropertyNames;
103+
}
104+
105+
public String[] childPropertyNames() {
106+
return childPropertyNames;
107+
}
108+
109+
public List<PropertyInfo> parentProperties() {
110+
return parentProperties;
111+
}
112+
113+
public List<PropertyInfo> childProperties() {
114+
return childProperties;
115+
}
116+
117+
public Class<?> parentClass() {
118+
return parentClass;
119+
}
120+
121+
public Class<?> childClass() {
122+
return childClass;
123+
}
124+
125+
public boolean caseSensitive() {
126+
return caseSensitive;
127+
}
128+
129+
public boolean parentIsAQuery() {
130+
return parentIsAQuery;
131+
}
132+
133+
public PropertyInfo joinProperty() {
134+
return joinProperty;
135+
}
65136

137+
public boolean reversed() {
138+
return reversed;
66139
}
67140

68141
@Override

src/net/sf/persism/Log.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ private Log() {
5555
void warnNoDuplicates(String message) {
5656
String additional = "";
5757

58+
Throwable throwable = new Throwable("");
5859
// This finds the stack element for the user's package name - should be the source of the call to include in the message
59-
Optional<StackTraceElement> opt = Arrays.stream(new Throwable().getStackTrace()).
60+
Optional<StackTraceElement> opt = Arrays.stream(throwable.getStackTrace()).
6061
filter(e -> !e.getClassName().startsWith("net.sf.persism")).findFirst();
6162

6263
if (opt.isPresent()) {

src/net/sf/persism/Messages.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ enum Messages {
4040
PossibleOverflow("Possible overflow column %s - Target type is %s and Value type is %s"),
4141
PropertyShouldBeAnObjectType("Property %s for column %s for class %s should be an Object type to properly detect NULL for defaults (change it from the primitive type to its Boxed version)"),
4242
SettersFoundInReadOnlyObject("Setters found in read only object %s %s"),
43-
UnknownSQLType("Unknown SQL TYPE: %s"),
43+
UnknownSQLType("Convert: Unknown SQL TYPE: %s"),
4444
ConverterValueTypeNotYetSupported("%s not yet supported"),
4545
ConverterDoNotUseClobOrBlobAsAPropertyType("Usually you should not use blob or clob as a property type on a POJO. Blob maps to byteArray, Clob maps to String"),
46-
ColumnTypeNotKnownForSQLType("Column type not known for SQL type %s. Reading as Object"),
46+
ColumnTypeNotKnownForSQLType("Column type not known for SQL type %s. Reading column: %s as Object"),
4747
InappropriateMethodUsedForSQLTypeInstance("%s It seems you're using the %s method with %s. You might prefer to use the %s method instead for better 'Find Usages'"),
4848
UnknownTypeForPrimaryGeneratedKey("Unknown type for primary/generated key: %s. using getObject"),
4949
UnknownTypeInSetParameters("setParameters: Unknown type: %s"),

src/net/sf/persism/MetaData.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ private synchronized <T> Map<String, PropertyInfo> determinePropertyInfo(Class<T
146146
}
147147
PropertyInfo foundProperty = null;
148148
for (PropertyInfo propertyInfo : properties) {
149-
String checkName = propertyInfo.propertyName.toLowerCase().replace("_", "");
149+
String checkName = propertyInfo.propertyName().toLowerCase().replace("_", "");
150150
if (checkName.equalsIgnoreCase(columnName)) {
151151
foundProperty = propertyInfo;
152152
break;
@@ -164,7 +164,7 @@ private synchronized <T> Map<String, PropertyInfo> determinePropertyInfo(Class<T
164164

165165
if (foundProperty != null) {
166166
columns.put(realColumnName, foundProperty);
167-
propertyNames.add(foundProperty.propertyName);
167+
propertyNames.add(foundProperty.propertyName());
168168
} else {
169169
log.warn(Messages.NoPropertyFoundForColumn.message(realColumnName, objectClass));
170170
}
@@ -832,8 +832,8 @@ Map<String, PropertyInfo> getChangedProperties(Persistable<?> persistable, Conne
832832

833833
Object newValue = null;
834834
Object orgValue = null;
835-
newValue = propertyInfo.getter.invoke(persistable);
836-
orgValue = propertyInfo.getter.invoke(original);
835+
newValue = propertyInfo.getValue(persistable);
836+
orgValue = propertyInfo.getValue(original);
837837

838838
if (newValue != null && !newValue.equals(orgValue) || orgValue != null && !orgValue.equals(newValue)) {
839839
changedColumns.put(column, propertyInfo);

src/net/sf/persism/PropertyInfo.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* User: DHoward
1313
* Date: 9/8/11
1414
* Time: 8:09 AM
15+
* todo encapsulate this class better
1516
*/
1617
final class PropertyInfo {
1718

@@ -62,6 +63,20 @@ Object getValue(Object object) {
6263
}
6364
}
6465

66+
void setValue(Object object, Object value) {
67+
try {
68+
if (setter != null) {
69+
setter.invoke(object, value);
70+
} else {
71+
field.setAccessible(true);
72+
field.set(object, value);
73+
field.setAccessible(false);
74+
}
75+
} catch (IllegalAccessException | InvocationTargetException e) {
76+
throw new PersismException(e.getMessage(), e);
77+
}
78+
}
79+
6580

6681
Map<Class<? extends Annotation>, Annotation> annotations() {
6782
return annotations;

src/net/sf/persism/Reader.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,9 @@ <T> T readObject(T object, ResultSet rs) throws IllegalAccessException, SQLExcep
8484

8585
if (value != null) {
8686
try {
87-
if (columnProperty.readOnly) {
88-
// set the value on the field directly
89-
columnProperty.field.setAccessible(true);
90-
columnProperty.field.set(object, value);
91-
columnProperty.field.setAccessible(false);
92-
} else {
93-
columnProperty.setter.invoke(object, value);
94-
}
87+
columnProperty.setValue(object, value);
9588
} catch (IllegalArgumentException e) {
89+
// A IllegalArgumentException (which is a RuntimeException) occurs if we're setting an unmatched ENUM
9690
throw new PersismException(Messages.IllegalArgumentReadingColumn.message(columnProperty.propertyName, objectClass, columnName, returnType, value.getClass(), value), e);
9791
}
9892
}
@@ -367,7 +361,7 @@ <T> T readColumn(ResultSet rs, int column, Class<?> returnType) throws SQLExcept
367361
}
368362

369363
} else {
370-
log.warn(Messages.ColumnTypeNotKnownForSQLType.message(sqlColumnType), new Throwable());
364+
log.warnNoDuplicates(Messages.ColumnTypeNotKnownForSQLType.message(sqlColumnType, columnName));
371365
value = rs.getObject(column);
372366
}
373367

src/net/sf/persism/Session.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public boolean fetch(Object object) throws PersismException {
143143
try {
144144
for (String column : primaryKeys) {
145145
PropertyInfo propertyInfo = properties.get(column);
146-
params.add(propertyInfo.getter.invoke(object));
146+
params.add(propertyInfo.getValue(object));
147147
columnInfos.add(cols.get(column));
148148
}
149149
assert params.size() == columnInfos.size();
@@ -223,7 +223,7 @@ public <T> T fetch(Class<T> objectClass, SQL sql) {
223223
* @param parameters parameters to the query.
224224
* @param <T> Return type
225225
* @return value read from the database of type T or null if not found
226-
* @throws PersismException Well, this is a runtime exception so actually it could be anything really.
226+
* @throws PersismException Well, this is a runtime exception, so it actually could be anything really.
227227
*/
228228
public <T> T fetch(Class<T> objectClass, SQL sql, Parameters parameters) {
229229
// If we know this type it means it's a primitive type. Not a DAO so we use a different rule to read those
@@ -354,12 +354,12 @@ public <T> List<T> query(Class<T> objectClass, Parameters primaryKeyValues) {
354354
String ed = metaData.getConnectionType().getKeywordEndDelimiter();
355355

356356
String andSep = "";
357-
// View should not check for WHERE we don't support @View here anyway RIGHT?
357+
// TODO View should not check for WHERE we don't support @View here anyway RIGHT?
358358
if (objectClass.getAnnotation(View.class) == null) {
359359
int n = query.indexOf(" WHERE");
360360
query = query.substring(0, n + 7);
361361
} else {
362-
query += " WHERE ";
362+
query += " WHERE "; // not covered.....
363363
}
364364

365365
StringBuilder sb = new StringBuilder(query);
@@ -500,14 +500,14 @@ public <T> Result<T> update(T object) throws PersismException {
500500
if (primaryKeys.contains(column)) {
501501
log.debug("Session update: skipping column %s", column);
502502
} else {
503-
Object value = allProperties.get(column).getter.invoke(object);
503+
Object value = allProperties.get(column).getValue(object);
504504
params.add(value);
505505
columnInfos.add(columnInfo);
506506
}
507507
}
508508

509509
for (String column : primaryKeys) {
510-
params.add(allProperties.get(column).getter.invoke(object));
510+
params.add(allProperties.get(column).getValue(object));
511511
columnInfos.add(metaData.getColumns(object.getClass(), connection).get(column));
512512
}
513513
assert params.size() == columnInfos.size();
@@ -594,7 +594,7 @@ public <T> Result<T> insert(T object) throws PersismException {
594594
log.warnNoDuplicates(Messages.PropertyShouldBeAnObjectType.message(propertyInfo.propertyName, columnInfo.columnName, object.getClass()));
595595
}
596596

597-
if (propertyInfo.getter.invoke(object) == null) {
597+
if (propertyInfo.getValue(object) == null) {
598598

599599
if (columnInfo.primary) {
600600
// This is supported with PostgreSQL but otherwise throw this an exception
@@ -608,7 +608,7 @@ public <T> Result<T> insert(T object) throws PersismException {
608608
}
609609
}
610610

611-
Object value = propertyInfo.getter.invoke(object);
611+
Object value = propertyInfo.getValue(object);
612612

613613
params.add(value);
614614
columnInfos.add(columnInfo);
@@ -725,7 +725,7 @@ public <T> Result<T> delete(T object) throws PersismException {
725725
List<Object> params = new ArrayList<>(primaryKeys.size());
726726
List<ColumnInfo> columnInfos = new ArrayList<>(columns.size());
727727
for (String column : primaryKeys) {
728-
params.add(columns.get(column).getter.invoke(object));
728+
params.add(columns.get(column).getValue(object));
729729
columnInfos.add(metaData.getColumns(object.getClass(), connection).get(column));
730730
}
731731

0 commit comments

Comments
 (0)