Skip to content

Commit 52b8468

Browse files
committed
feat(repository): support custom generated IDs and improve SQL parameter handling
Add support for returning user-provided IDs when field property "generate"=true across MySQL, SQLite, SQLServer, and Redis Unify appendAndGetId behavior: fall back to DB-generated keys only when no custom ID is provided Improve setParameters to support LONGTEXT and BOOLEAN types Fix parameter binding to no longer skip non-auto-increment fields incorrectly Improve SQL formatting, spacing, and Javadoc wrapping for readability Minor refactors and code style cleanups (isEmpty checks, spacing, line breaks) Keep backward compatibility for existing auto-increment ID behavior
1 parent 6ccf8c2 commit 52b8468

4 files changed

Lines changed: 131 additions & 71 deletions

File tree

src/main/java/org/tinystruct/data/repository/MySQLServer.java

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public boolean append(Field ready_fields, String table) throws ApplicationExcept
7272

7373
String SQL = "INSERT INTO " + table + " (" + expressions + ") VALUES(" + values + ")";
7474
try (DatabaseOperator operator = new DatabaseOperator()) {
75-
PreparedStatement ps = operator.preparedStatement(SQL, new Object[]{});
75+
PreparedStatement ps = operator.preparedStatement(SQL, new Object[] {});
7676
setParameters(ps, fields);
7777

7878
return ps.executeUpdate() > 0;
@@ -83,6 +83,10 @@ public boolean append(Field ready_fields, String table) throws ApplicationExcept
8383

8484
/**
8585
* Append a new record to the database and return the generated ID.
86+
* <p>
87+
* If a field's "generate" property is set to true, its value will be used as
88+
* the returned ID.
89+
* </p>
8690
*
8791
* @param ready_fields The fields ready for insertion.
8892
* @param table The table name.
@@ -92,22 +96,27 @@ public boolean append(Field ready_fields, String table) throws ApplicationExcept
9296
@Override
9397
public Object appendAndGetId(Field ready_fields, String table) throws ApplicationException {
9498
int i = 0, cols = ready_fields.size();
95-
String[] columns = new String[cols];
9699
FieldInfo[] fields = new FieldInfo[cols];
97100

101+
Object Id = null;
98102
String key;
99103
StringBuilder expressions = new StringBuilder(), values = new StringBuilder();
100104
Enumeration<String> keys = ready_fields.keys();
101105
while (keys.hasMoreElements()) {
102106
key = keys.nextElement();
103-
if (!ready_fields.get(key).autoIncrement()) {
104-
columns[i] = ready_fields.get(key).getColumnName();
105-
fields[i] = ready_fields.get(key);
107+
FieldInfo currentField = ready_fields.get(key);
108+
if (!currentField.autoIncrement()) {
109+
fields[i] = currentField;
110+
111+
if (Id == null && currentField.get("generate") != null
112+
&& Boolean.parseBoolean(currentField.get("generate").toString())) {
113+
Id = currentField.value();
114+
}
106115

107116
if (expressions.length() == 0)
108-
expressions.append("`").append(columns[i]).append("`");
117+
expressions.append("`").append(currentField.getColumnName()).append("`");
109118
else
110-
expressions.append(COMMA).append("`").append(columns[i]).append("`");
119+
expressions.append(COMMA).append("`").append(currentField.getColumnName()).append("`");
111120

112121
if (values.length() == 0)
113122
values.append('?');
@@ -124,8 +133,13 @@ public Object appendAndGetId(Field ready_fields, String table) throws Applicatio
124133
PreparedStatement ps = operator.createPreparedStatement(SQL, false, true);
125134
setParameters(ps, fields);
126135

127-
// Execute the update and get the generated ID
128-
return operator.executeUpdateAndGetGeneratedId(ps);
136+
if (Id == null) {
137+
// Execute the update and get the generated ID
138+
return operator.executeUpdateAndGetGeneratedId(ps);
139+
} else {
140+
operator.executeUpdate(ps);
141+
return Id;
142+
}
129143
} catch (SQLException e) {
130144
throw new ApplicationException("Error appending record: " + e.getMessage(), e);
131145
}
@@ -170,7 +184,7 @@ public boolean update(Field ready_fields, String table) throws ApplicationExcept
170184

171185
String SQL = "UPDATE " + table + " SET " + expressions + " WHERE " + id + "=?";
172186
try (DatabaseOperator operator = new DatabaseOperator()) {
173-
PreparedStatement ps = operator.preparedStatement(SQL, new Object[]{});
187+
PreparedStatement ps = operator.preparedStatement(SQL, new Object[] {});
174188
setParameters(ps, values);
175189
ps.setObject(ready_fields.size(), Id); // Set Id parameter
176190

@@ -191,7 +205,8 @@ public Type getType() {
191205
}
192206

193207
/**
194-
* Retrieve records from the MySQL database table based on the provided SQL query.
208+
* Retrieve records from the MySQL database table based on the provided SQL
209+
* query.
195210
*
196211
* @param SQL The SQL query to retrieve records.
197212
* @param parameters The parameters to be used in the SQL query.
@@ -236,7 +251,7 @@ public Table find(String SQL, Object[] parameters) throws ApplicationException {
236251
fieldValue = resultSet.getInt(i + 1);
237252
}
238253
} else if (type.equals("DECIMAL") || type.equals("NUMERIC") ||
239-
type.contains("FLOAT") || type.contains("DOUBLE")) {
254+
type.contains("FLOAT") || type.contains("DOUBLE")) {
240255
// Handle all floating-point types
241256
if (type.contains("FLOAT")) {
242257
fieldValue = resultSet.getFloat(i + 1);
@@ -256,8 +271,8 @@ public Table find(String SQL, Object[] parameters) throws ApplicationException {
256271
fieldValue = resultSet.getTimestamp(i + 1);
257272
}
258273
} else if (type.equals("BLOB") || type.equals("BINARY") ||
259-
type.equals("VARBINARY") || type.equals("TINYBLOB") ||
260-
type.equals("MEDIUMBLOB") || type.equals("LONGBLOB")) {
274+
type.equals("VARBINARY") || type.equals("TINYBLOB") ||
275+
type.equals("MEDIUMBLOB") || type.equals("LONGBLOB")) {
261276
// Handle binary data
262277
fieldValue = resultSet.getBytes(i + 1);
263278
} else {
@@ -288,7 +303,8 @@ public Table find(String SQL, Object[] parameters) throws ApplicationException {
288303
}
289304

290305
/**
291-
* Retrieve a single record from the MySQL database table based on the provided SQL query.
306+
* Retrieve a single record from the MySQL database table based on the provided
307+
* SQL query.
292308
*
293309
* @param SQL The SQL query to retrieve the record.
294310
* @param parameters The parameters to be used in the SQL query.
@@ -330,7 +346,7 @@ public Row findOne(String SQL, Object[] parameters) throws ApplicationException
330346
fieldValue = resultSet.getInt(i + 1);
331347
}
332348
} else if (type.equals("DECIMAL") || type.equals("NUMERIC") ||
333-
type.contains("FLOAT") || type.contains("DOUBLE")) {
349+
type.contains("FLOAT") || type.contains("DOUBLE")) {
334350
// Handle all floating-point types
335351
if (type.contains("FLOAT")) {
336352
fieldValue = resultSet.getFloat(i + 1);
@@ -350,8 +366,8 @@ public Row findOne(String SQL, Object[] parameters) throws ApplicationException
350366
fieldValue = resultSet.getTimestamp(i + 1);
351367
}
352368
} else if (type.equals("BLOB") || type.equals("BINARY") ||
353-
type.equals("VARBINARY") || type.equals("TINYBLOB") ||
354-
type.equals("MEDIUMBLOB") || type.equals("LONGBLOB")) {
369+
type.equals("VARBINARY") || type.equals("TINYBLOB") ||
370+
type.equals("MEDIUMBLOB") || type.equals("LONGBLOB")) {
355371
// Handle binary data
356372
fieldValue = resultSet.getBytes(i + 1);
357373
} else {
@@ -397,18 +413,18 @@ private String generateInsertSQL(String[] columns, String table) {
397413
return "INSERT INTO " + table + " (" + _columns + ") VALUES (" + values + ")";
398414
}
399415

400-
private void setParameters(PreparedStatement ps, FieldInfo[] values) throws SQLException {
416+
private void setParameters(PreparedStatement ps, FieldInfo[] fields) throws SQLException {
401417
int i = 1;
402-
for (FieldInfo fieldInfo : values) {
403-
if (fieldInfo != null && !fieldInfo.autoIncrement()) {
418+
for (FieldInfo fieldInfo : fields) {
419+
if (fieldInfo != null) {
404420
Object value = fieldInfo.value();
405421
if ("int".equalsIgnoreCase(fieldInfo.getType().getRealType())) {
406422
ps.setInt(i++, fieldInfo.intValue());
407-
} else if (fieldInfo.getType() == FieldType.TEXT) {
423+
} else if (fieldInfo.getType() == FieldType.TEXT || fieldInfo.getType() == FieldType.LONGTEXT) {
408424
ps.setString(i++, fieldInfo.stringValue());
409425
} else if (fieldInfo.getType() == FieldType.DATE || fieldInfo.getType() == FieldType.DATETIME) {
410426
ps.setDate(i++, new Date(fieldInfo.dateValue().getTime()));
411-
} else if (fieldInfo.getType() == FieldType.BIT) {
427+
} else if (fieldInfo.getType() == FieldType.BIT || fieldInfo.getType() == FieldType.BOOLEAN) {
412428
ps.setBoolean(i++, fieldInfo.booleanValue());
413429
} else {
414430
ps.setObject(i++, value);

src/main/java/org/tinystruct/data/repository/RedisServer.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public Type getType() {
4646
* Append a new record to the Redis database.
4747
*
4848
* @param ready_fields the fields ready for insertion.
49-
* @param table the table to append the record to (not applicable for Redis).
49+
* @param table the table to append the record to (not applicable for
50+
* Redis).
5051
* @return true if the operation is successful, false otherwise.
5152
* @throws ApplicationException if an application-specific error occurs.
5253
*/
@@ -58,9 +59,14 @@ public boolean append(Field ready_fields, String table) throws ApplicationExcept
5859

5960
/**
6061
* Append a new record to the Redis database and return the generated ID.
62+
* <p>
63+
* If a field's "generate" property is set to true, its value will be used as
64+
* the returned ID.
65+
* </p>
6166
*
6267
* @param ready_fields the fields ready for insertion.
63-
* @param table the table to append the record to (not applicable for Redis).
68+
* @param table the table to append the record to (not applicable for
69+
* Redis).
6470
* @return the generated ID if the operation is successful, null otherwise.
6571
* @throws ApplicationException if an application-specific error occurs.
6672
*/
@@ -96,7 +102,8 @@ public Object appendAndGetId(Field ready_fields, String table) throws Applicatio
96102
* Update an existing record in the Redis database.
97103
*
98104
* @param ready_fields the fields ready for update.
99-
* @param table the table to update the record in (not applicable for Redis).
105+
* @param table the table to update the record in (not applicable for
106+
* Redis).
100107
* @return true if the operation is successful, false otherwise.
101108
* @throws ApplicationException if an application-specific error occurs.
102109
*/
@@ -135,7 +142,8 @@ public boolean delete(Object Id, String table) throws ApplicationException {
135142
}
136143

137144
/**
138-
* Find records in the Redis database based on the given SQL query and parameters.
145+
* Find records in the Redis database based on the given SQL query and
146+
* parameters.
139147
*
140148
* @param SQL the SQL query (not applicable for Redis).
141149
* @param parameters the parameters for the query (not applicable for Redis).
@@ -153,7 +161,8 @@ public Table find(String SQL, Object[] parameters) throws ApplicationException {
153161
}
154162

155163
/**
156-
* Find a single record in the Redis database based on the given SQL query and parameters.
164+
* Find a single record in the Redis database based on the given SQL query and
165+
* parameters.
157166
*
158167
* @param SQL the SQL query (not applicable for Redis).
159168
* @param parameters the parameters for the query (not applicable for Redis).

0 commit comments

Comments
 (0)