Skip to content

Commit ecfb5dd

Browse files
committed
Improve insert/update script
1 parent afc2027 commit ecfb5dd

File tree

2 files changed

+8
-17
lines changed

2 files changed

+8
-17
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.5</version>
9+
<version>1.0.6</version>
1010

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

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

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public <T> String deleteScript(Class<T> clazz, T object) throws IllegalAccessExc
182182
return null;
183183
}
184184

185-
String condition = processFieldsForScript(clazz, object, true);
185+
String condition = processDeleteConditionScript(clazz, object);
186186
return String.format("DELETE FROM %s WHERE (%s);", tableName, condition);
187187
}
188188

@@ -202,12 +202,10 @@ public <T> String insertOrUpdateScript(Class<T> clazz, T object) throws IllegalA
202202
String columns = getColumnsFromField(clazz);
203203
String values = getValuesFromField(clazz, object);
204204

205-
String updateSetValues = processFieldsForScript(clazz, object, false);
206-
207205
String conflictPolicy = getConflictPolicy(clazz);
208206

209-
return String.format("INSERT INTO %s (%s) VALUES (%s) ON CONFLICT (%s) DO UPDATE SET (%s);",
210-
tableName, columns, values, conflictPolicy, updateSetValues);
207+
return String.format("INSERT INTO %s (%s) VALUES (%s) ON CONFLICT (%s) DO UPDATE SET (%s) = (%s);",
208+
tableName, columns, values, conflictPolicy, columns, values);
211209
}
212210

213211
/**
@@ -229,22 +227,21 @@ public String generateCreateScript(Class<?> clazz) {
229227
}
230228

231229
/**
232-
* Processes fields of a class to generate a part of SQL script for insert, update, or delete operations.
230+
* Processes fields of a class to generate a part of SQL script for delete operations.
233231
*
234232
* @param clazz the class whose fields are to be processed.
235233
* @param object the instance of the class.
236-
* @param forDelete flag to indicate if the processing is for DELETE operation.
237234
* @return a string representing a part of SQL script.
238235
*/
239-
private <T> String processFieldsForScript(Class<T> clazz, T object, boolean forDelete) throws IllegalAccessException {
236+
private <T> String processDeleteConditionScript(Class<T> clazz, T object) throws IllegalAccessException {
240237
List<String> keys = new ArrayList<>();
241238
List<String> values = new ArrayList<>();
242239

243240
for (Field field : clazz.getDeclaredFields()) {
244241
field.setAccessible(true);
245242

246243
boolean isPrimaryKey = field.isAnnotationPresent(PrimaryKey.class);
247-
if (forDelete && !isPrimaryKey) continue;
244+
if (!isPrimaryKey) continue;
248245

249246
Column column = field.getAnnotation(Column.class);
250247
if (column == null) continue;
@@ -260,13 +257,7 @@ private <T> String processFieldsForScript(Class<T> clazz, T object, boolean forD
260257

261258
if (keys.isEmpty()) return "";
262259

263-
StringBuilder builder = new StringBuilder();
264-
for (int i = 0; i < keys.size(); i++) {
265-
builder.append(keys.get(i)).append(" = ").append(values.get(i)).append(", ");
266-
}
267-
268-
builder.setLength(builder.length() - 2);
269-
return builder.toString();
260+
return "(" + keys + ") = (" + values + ")";
270261
}
271262

272263
private <T> String getValuesFromField(Class<T> clazz, T object) throws IllegalAccessException {

0 commit comments

Comments
 (0)