@@ -418,8 +418,43 @@ public static int update(Connection connection, String table,
418418 */
419419 public static long insert (Connection connection , String table ,
420420 ContentValues values ) {
421+ return insert (connection , table , null , values );
422+ }
423+
424+ /**
425+ * Insert a new row
426+ *
427+ * @param connection
428+ * connection
429+ * @param table
430+ * table name
431+ * @param values
432+ * content values
433+ * @return row id
434+ */
435+ public static long insertOrThrow (Connection connection , String table ,
436+ ContentValues values ) {
437+ return insertOrThrow (connection , table , null , values );
438+ }
439+
440+ /**
441+ * Insert a new row
442+ *
443+ * @param connection
444+ * connection
445+ * @param table
446+ * table name
447+ * @param pkColumn
448+ * primary key id column
449+ * @param values
450+ * content values
451+ * @return row id or -1 on an exception
452+ * @since 6.6.2
453+ */
454+ public static long insert (Connection connection , String table ,
455+ String pkColumn , ContentValues values ) {
421456 try {
422- return insertOrThrow (connection , table , values );
457+ return insertOrThrow (connection , table , pkColumn , values );
423458 } catch (Exception e ) {
424459 log .log (Level .WARNING , "Error inserting into table: " + table
425460 + ", Values: " + values , e );
@@ -434,12 +469,15 @@ public static long insert(Connection connection, String table,
434469 * connection
435470 * @param table
436471 * table name
472+ * @param pkColumn
473+ * primary key id column
437474 * @param values
438475 * content values
439476 * @return row id
477+ * @since 6.6.2
440478 */
441479 public static long insertOrThrow (Connection connection , String table ,
442- ContentValues values ) {
480+ String pkColumn , ContentValues values ) {
443481
444482 StringBuilder insert = new StringBuilder ();
445483 insert .append ("insert into " ).append (CoreSQLUtils .quoteWrap (table ))
@@ -462,6 +500,12 @@ public static long insertOrThrow(Connection connection, String table,
462500 }
463501 insert .append (')' );
464502
503+ if (pkColumn == null ) {
504+ pkColumn = CoreSQLUtils .ROWID_COLUMN ;
505+ }
506+ insert .append (" returning " );
507+ insert .append (CoreSQLUtils .quoteWrap (pkColumn ));
508+
465509 String sql = insert .toString ();
466510
467511 PreparedStatement statement = null ;
@@ -470,23 +514,21 @@ public static long insertOrThrow(Connection connection, String table,
470514 try {
471515 statement = connection .prepareStatement (sql );
472516 setArguments (statement , args );
473- int count = statement .executeUpdate ();
517+ boolean result = statement .execute ();
474518
519+ while (result ) {
520+ ResultSet resultSet = statement .getResultSet ();
521+ id = resultSet .getLong (pkColumn );
522+ result = statement .getMoreResults ();
523+ }
524+
525+ int count = statement .getUpdateCount ();
475526 if (count == 0 ) {
476527 throw new GeoPackageException (
477528 "Failed to execute SQL insert statement: " + sql
478529 + ". No rows added from execution." );
479530 }
480531
481- try (ResultSet generatedKeys = statement .getGeneratedKeys ()) {
482- if (generatedKeys .next ()) {
483- id = generatedKeys .getLong (1 );
484- } else {
485- throw new GeoPackageException (
486- "Failed to execute SQL insert statement: " + sql
487- + ". No row id was found." );
488- }
489- }
490532 } catch (SQLException e ) {
491533 throw new GeoPackageException (
492534 "Failed to execute SQL insert statement: " + sql , e );
0 commit comments