diff --git a/pom.xml b/pom.xml index 4d28ad4..f85f332 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ cz.foresttech ForestDatabase - 1.0.5 + 1.0.6 17 diff --git a/src/main/java/cz/foresttech/database/DatabaseEntityConvertor.java b/src/main/java/cz/foresttech/database/DatabaseEntityConvertor.java index c77bb93..0cb5d3f 100644 --- a/src/main/java/cz/foresttech/database/DatabaseEntityConvertor.java +++ b/src/main/java/cz/foresttech/database/DatabaseEntityConvertor.java @@ -182,7 +182,7 @@ public String deleteScript(Class clazz, T object) throws IllegalAccessExc return null; } - String condition = processFieldsForScript(clazz, object, true); + String condition = processDeleteConditionScript(clazz, object); return String.format("DELETE FROM %s WHERE (%s);", tableName, condition); } @@ -202,12 +202,10 @@ public String insertOrUpdateScript(Class clazz, T object) throws IllegalA String columns = getColumnsFromField(clazz); String values = getValuesFromField(clazz, object); - String updateSetValues = processFieldsForScript(clazz, object, false); - String conflictPolicy = getConflictPolicy(clazz); - return String.format("INSERT INTO %s (%s) VALUES (%s) ON CONFLICT (%s) DO UPDATE SET (%s);", - tableName, columns, values, conflictPolicy, updateSetValues); + return String.format("INSERT INTO %s (%s) VALUES (%s) ON CONFLICT (%s) DO UPDATE SET (%s) = (%s);", + tableName, columns, values, conflictPolicy, columns, values); } /** @@ -229,14 +227,13 @@ public String generateCreateScript(Class clazz) { } /** - * Processes fields of a class to generate a part of SQL script for insert, update, or delete operations. + * Processes fields of a class to generate a part of SQL script for delete operations. * * @param clazz the class whose fields are to be processed. * @param object the instance of the class. - * @param forDelete flag to indicate if the processing is for DELETE operation. * @return a string representing a part of SQL script. */ - private String processFieldsForScript(Class clazz, T object, boolean forDelete) throws IllegalAccessException { + private String processDeleteConditionScript(Class clazz, T object) throws IllegalAccessException { List keys = new ArrayList<>(); List values = new ArrayList<>(); @@ -244,7 +241,7 @@ private String processFieldsForScript(Class clazz, T object, boolean forD field.setAccessible(true); boolean isPrimaryKey = field.isAnnotationPresent(PrimaryKey.class); - if (forDelete && !isPrimaryKey) continue; + if (!isPrimaryKey) continue; Column column = field.getAnnotation(Column.class); if (column == null) continue; @@ -260,13 +257,7 @@ private String processFieldsForScript(Class clazz, T object, boolean forD if (keys.isEmpty()) return ""; - StringBuilder builder = new StringBuilder(); - for (int i = 0; i < keys.size(); i++) { - builder.append(keys.get(i)).append(" = ").append(values.get(i)).append(", "); - } - - builder.setLength(builder.length() - 2); - return builder.toString(); + return "(" + keys + ") = (" + values + ")"; } private String getValuesFromField(Class clazz, T object) throws IllegalAccessException {