Skip to content

Commit

Permalink
feat(jdbc): add setGetGeneratedKeys on SQLiteDataSource and SQLiteCon…
Browse files Browse the repository at this point in the history
…fig for proper cascading

Refs: #1135
  • Loading branch information
gotson committed Sep 26, 2024
1 parent 3b73529 commit d463ef7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
3 changes: 2 additions & 1 deletion USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ SQLite has limited support to retrieve generated keys, using [last_insert_rowid]

By default the driver will eagerly retrieve the generated keys after each statement, which may impact performances.

You can disable the retrieval of generated keys in 2 ways:
You can disable the retrieval of generated keys in 3 ways:
- via `SQLiteDataSource#setGetGeneratedKeys(false)`
- via `SQLiteConnectionConfig#setGetGeneratedKeys(false)`:
- using the pragma `jdbc.get_generated_keys`:
```java
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/sqlite/SQLiteConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -1189,4 +1189,12 @@ public void setBusyTimeout(int milliseconds) {
public int getBusyTimeout() {
return busyTimeout;
}

public boolean isGetGeneratedKeys() {
return this.defaultConnectionConfig.isGetGeneratedKeys();
}

public void setGetGeneratedKeys(boolean generatedKeys) {
this.defaultConnectionConfig.setGetGeneratedKeys(generatedKeys);
}
}
9 changes: 9 additions & 0 deletions src/main/java/org/sqlite/SQLiteDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,15 @@ public void setTransactionMode(String transactionMode) {
config.setTransactionMode(transactionMode);
}

/**
* Configure where generated keys will be retrieved for this database.
*
* @param generatedKeys true to retrieve generated keys
*/
public void setGetGeneratedKeys(boolean generatedKeys) {
config.setGetGeneratedKeys(generatedKeys);
}

/**
* Sets the value of the user-version. It is a big-endian 32-bit signed integer stored in the
* database header at offset 60.
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/org/sqlite/SQLiteConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public void toProperties() {
config.setDateStringFormat("yyyy/mm/dd");
config.setDatePrecision("seconds");
config.setDateClass("real");
config.setGetGeneratedKeys(false);

Properties properties = config.toProperties();

Expand All @@ -27,6 +28,8 @@ public void toProperties() {
.isEqualTo(SQLiteConfig.DatePrecision.SECONDS.name());
assertThat(properties.getProperty(SQLiteConfig.Pragma.DATE_CLASS.getPragmaName()))
.isEqualTo(SQLiteConfig.DateClass.REAL.name());
assertThat(properties.getProperty(Pragma.JDBC_GET_GENERATED_KEYS.getPragmaName()))
.isEqualTo("false");
}

@Test
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/org/sqlite/SQLiteDataSourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.nio.ByteOrder;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -92,4 +93,36 @@ public void setBusyTimeout() {
.isEqualTo("1234");
assertThat(ds.getConfig().getBusyTimeout()).isEqualTo(1234);
}

@Test
public void setGetGeneratedKeys() throws SQLException {
final SQLiteDataSource ds = new SQLiteDataSource();
ds.setGetGeneratedKeys(false);
assertThat(
ds.getConfig()
.toProperties()
.getProperty(
SQLiteConfig.Pragma.JDBC_GET_GENERATED_KEYS.pragmaName))
.isEqualTo("false");
assertThat(ds.getConfig().isGetGeneratedKeys()).isEqualTo(false);
assertThat(
((SQLiteConnection) ds.getConnection())
.getConnectionConfig()
.isGetGeneratedKeys())
.isFalse();

ds.setGetGeneratedKeys(true);
assertThat(
ds.getConfig()
.toProperties()
.getProperty(
SQLiteConfig.Pragma.JDBC_GET_GENERATED_KEYS.pragmaName))
.isEqualTo("true");
assertThat(ds.getConfig().isGetGeneratedKeys()).isEqualTo(true);
assertThat(
((SQLiteConnection) ds.getConnection())
.getConnectionConfig()
.isGetGeneratedKeys())
.isTrue();
}
}

0 comments on commit d463ef7

Please sign in to comment.