Skip to content

Commit f1579b9

Browse files
committed
replace SQLite Statement getGeneratedKeys usage with RETURNING clause
1 parent 5c80cc5 commit f1579b9

2 files changed

Lines changed: 59 additions & 15 deletions

File tree

src/main/java/mil/nga/geopackage/db/SQLUtils.java

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

src/main/java/mil/nga/geopackage/user/UserDao.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public int update(ContentValues values, String whereClause,
157157
@Override
158158
public long insert(TRow row) {
159159
long id = SQLUtils.insertOrThrow(connection, getTableName(),
160-
row.toContentValues(false));
160+
getPkColumnName(), row.toContentValues(false));
161161
if (row.hasIdColumn()) {
162162
row.setId(id, true);
163163
}
@@ -172,7 +172,8 @@ public long insert(TRow row) {
172172
* @return row id, -1 on error
173173
*/
174174
public long insert(ContentValues values) {
175-
return SQLUtils.insert(connection, getTableName(), values);
175+
return SQLUtils.insert(connection, getTableName(), getPkColumnName(),
176+
values);
176177
}
177178

178179
/**
@@ -183,7 +184,8 @@ public long insert(ContentValues values) {
183184
* @return row id
184185
*/
185186
public long insertOrThrow(ContentValues values) {
186-
return SQLUtils.insertOrThrow(connection, getTableName(), values);
187+
return SQLUtils.insertOrThrow(connection, getTableName(),
188+
getPkColumnName(), values);
187189
}
188190

189191
}

0 commit comments

Comments
 (0)