Skip to content

Commit

Permalink
Improve insert/update script
Browse files Browse the repository at this point in the history
  • Loading branch information
apik007 committed Mar 26, 2024
1 parent afc2027 commit ecfb5dd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>cz.foresttech</groupId>
<artifactId>ForestDatabase</artifactId>
<version>1.0.5</version>
<version>1.0.6</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand Down
23 changes: 7 additions & 16 deletions src/main/java/cz/foresttech/database/DatabaseEntityConvertor.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public <T> String deleteScript(Class<T> 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);
}

Expand All @@ -202,12 +202,10 @@ public <T> String insertOrUpdateScript(Class<T> 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);
}

/**
Expand All @@ -229,22 +227,21 @@ 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 <T> String processFieldsForScript(Class<T> clazz, T object, boolean forDelete) throws IllegalAccessException {
private <T> String processDeleteConditionScript(Class<T> clazz, T object) throws IllegalAccessException {
List<String> keys = new ArrayList<>();
List<String> values = new ArrayList<>();

for (Field field : clazz.getDeclaredFields()) {
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;
Expand All @@ -260,13 +257,7 @@ private <T> String processFieldsForScript(Class<T> 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 <T> String getValuesFromField(Class<T> clazz, T object) throws IllegalAccessException {
Expand Down

0 comments on commit ecfb5dd

Please sign in to comment.