@@ -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 );
0 commit comments