Skip to content

Commit e70bfd7

Browse files
committed
Update convertor script for inserting and updating values
1 parent d834e4b commit e70bfd7

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>cz.foresttech</groupId>
88
<artifactId>ForestDatabase</artifactId>
9-
<version>1.0.2</version>
9+
<version>1.0.3</version>
1010

1111
<properties>
1212
<maven.compiler.source>17</maven.compiler.source>

src/main/java/cz/foresttech/database/DatabaseEntityConvertor.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,15 @@ public <T> String insertOrUpdateScript(Class<T> clazz, T object) throws IllegalA
198198
return null;
199199
}
200200

201-
String columns = processFieldsForScript(clazz, object, false);
201+
String columns = getColumnsFromField(clazz);
202+
String values = getValuesFromField(clazz, object);
203+
204+
String updateSetValues = processFieldsForScript(clazz, object, false);
205+
202206
String conflictPolicy = getConflictPolicy(clazz);
203207

204208
return String.format("INSERT INTO %s (%s) VALUES (%s) ON CONFLICT (%s) DO UPDATE SET (%s);",
205-
tableName, columns, columns, conflictPolicy, columns);
209+
tableName, columns, values, conflictPolicy, updateSetValues);
206210
}
207211

208212
/**
@@ -260,6 +264,42 @@ private <T> String processFieldsForScript(Class<T> clazz, T object, boolean forD
260264
return keys + " = " + values;
261265
}
262266

267+
private <T> String getValuesFromField(Class<T> clazz, T object) throws IllegalAccessException {
268+
StringBuilder values = new StringBuilder();
269+
270+
for (Field field : clazz.getDeclaredFields()) {
271+
field.setAccessible(true);
272+
Column column = field.getAnnotation(Column.class);
273+
if (column == null) continue;
274+
String dbName = getDbName(field, column);
275+
Object fieldValue = field.get(object);
276+
DatabaseValueProcessor valueProcessor = databaseAPI.getProcessor(field.getType());
277+
278+
String processedValue = processFieldValue(fieldValue, valueProcessor);
279+
values.append(processedValue).append(",");
280+
}
281+
282+
if (!values.isEmpty()) values.setLength(values.length() - 1);
283+
284+
return values.toString();
285+
}
286+
287+
private <T> String getColumnsFromField(Class<T> clazz) {
288+
StringBuilder keys = new StringBuilder();
289+
290+
for (Field field : clazz.getDeclaredFields()) {
291+
field.setAccessible(true);
292+
Column column = field.getAnnotation(Column.class);
293+
if (column == null) continue;
294+
String dbName = getDbName(field, column);
295+
keys.append(dbName).append(",");
296+
}
297+
298+
if (!keys.isEmpty()) keys.setLength(keys.length() - 1);
299+
300+
return keys.toString();
301+
}
302+
263303
/**
264304
* Processes the value of a field for inclusion in an SQL script.
265305
*

0 commit comments

Comments
 (0)