Repository: xerial/sqlite-jdbc
Pull request #848 (2023-03-06 21:07:04)

Added support for legacy alter table flag
Added support for legacy alter table flag
More info [here](https://www.sqlite.org/pragma.html#pragma_legacy_alter_table) 

gotson: hi, thanks for the PR. Can you run `mvn spotless:apply` and commit the changes ?

Patch:
diff --git a/src/main/java/org/sqlite/SQLiteConfig.java b/src/main/java/org/sqlite/SQLiteConfig.java
index 3fc2301d..fa0a2410 100755
--- a/src/main/java/org/sqlite/SQLiteConfig.java
+++ b/src/main/java/org/sqlite/SQLiteConfig.java
@@ -54,7 +54,7 @@ public class SQLiteConfig {
     private final Properties pragmaTable;
     private int openModeFlag = 0x00;
 
-    private final int busyTimeout;
+    private int busyTimeout;
     private boolean explicitReadOnly;
 
     private final SQLiteConnectionConfig defaultConnectionConfig;
@@ -87,8 +87,8 @@ public class SQLiteConfig {
         // Enable URI filenames
         setOpenMode(SQLiteOpenMode.OPEN_URI);
 
-        this.busyTimeout =
-                Integer.parseInt(pragmaTable.getProperty(Pragma.BUSY_TIMEOUT.pragmaName, "3000"));
+        setBusyTimeout(
+                Integer.parseInt(pragmaTable.getProperty(Pragma.BUSY_TIMEOUT.pragmaName, "3000")));
         this.defaultConnectionConfig = SQLiteConnectionConfig.fromPragmaTable(pragmaTable);
         this.explicitReadOnly =
                 Boolean.parseBoolean(
@@ -425,6 +425,7 @@ public class SQLiteConfig {
                 "journal_size_limit",
                 "Limit the size of rollback-journal and WAL files left in the file-system after transactions or checkpoints",
                 null),
+        LEGACY_ALTER_TABLE("legacy_alter_table", "Use legacy alter table behavior", OnOff),
         LEGACY_FILE_FORMAT("legacy_file_format", "No-op", OnOff),
         LOCKING_MODE(
                 "locking_mode",
@@ -855,6 +856,19 @@ public class SQLiteConfig {
         set(Pragma.LEGACY_FILE_FORMAT, use);
     }
 
+    /**
+     * Sets the value of the legacy_alter_table flag. When this flag is on, the ALTER TABLE RENAME
+     * command (for changing the name of a table) works as it did in SQLite 3.24.0 (2018-06-04) and
+     * earlier.When the flag is off, using the ALTER TABLE RENAME command will mean that all
+     * references to the table anywhere in the schema will be converted to the new name.
+     *
+     * @param flag True to turn on legacy alter table behaviour; false to turn off.
+     * @see <a href="https://www.sqlite.org/pragma.html#pragma_legacy_alter_table</a>
+     */
+    public void setLegacyAlterTable(boolean flag) {
+        set(Pragma.LEGACY_ALTER_TABLE, flag);
+    }
+
     public enum LockingMode implements PragmaValue {
         NORMAL,
         EXCLUSIVE;
@@ -1162,6 +1176,7 @@ public class SQLiteConfig {
     /** @param milliseconds Connect to DB timeout in milliseconds */
     public void setBusyTimeout(int milliseconds) {
         setPragma(Pragma.BUSY_TIMEOUT, Integer.toString(milliseconds));
+        busyTimeout = milliseconds;
     }
 
     public int getBusyTimeout() {
diff --git a/src/main/java/org/sqlite/SQLiteDataSource.java b/src/main/java/org/sqlite/SQLiteDataSource.java
index 2b6f608a..692ccee4 100755
--- a/src/main/java/org/sqlite/SQLiteDataSource.java
+++ b/src/main/java/org/sqlite/SQLiteDataSource.java
@@ -139,6 +139,17 @@ public class SQLiteDataSource implements DataSource {
         config.setReadOnly(readOnly);
     }
 
+    /**
+     * Sets the amount of time that the connection's busy handler will wait when a table is locked.
+     *
+     * @param milliseconds The number of milliseconds to wait.
+     * @see <a
+     *     href="https://www.sqlite.org/pragma.html#pragma_busy_timeout">https://www.sqlite.org/pragma.html#pragma_busy_timeout</a>
+     */
+    public void setBusyTimeout(int milliseconds) {
+        config.setBusyTimeout(milliseconds);
+    }
+
     /**
      * Sets the suggested maximum number of database disk pages that SQLite will hold in memory at
      * once per open database file.
@@ -283,6 +294,19 @@ public class SQLiteDataSource implements DataSource {
         config.useLegacyFileFormat(use);
     }
 
+    /**
+     * Sets the value of the legacy_alter_table flag. When this flag is on, the ALTER TABLE RENAME
+     * command (for changing the name of a table) works as it did in SQLite 3.24.0 (2018-06-04) and
+     * earlier.When the flag is off, using the ALTER TABLE RENAME command will mean that all
+     * references to the table anywhere in the schema will be converted to the new name.
+     *
+     * @param flag True to turn on legacy alter table behaviour; false to turn off.
+     * @see <a href="https://www.sqlite.org/pragma.html#pragma_legacy_alter_table</a>
+     */
+    public void setLegacyAlterTable(boolean flag) {
+        config.setLegacyAlterTable(flag);
+    }
+
     /**
      * Sets the database connection locking-mode.
      *


Test patch:
diff --git a/src/test/java/org/sqlite/SQLiteConfigTest.java b/src/test/java/org/sqlite/SQLiteConfigTest.java
index b0457d43..8980d20a 100644
--- a/src/test/java/org/sqlite/SQLiteConfigTest.java
+++ b/src/test/java/org/sqlite/SQLiteConfigTest.java
@@ -25,4 +25,29 @@ public class SQLiteConfigTest {
         assertThat(properties.getProperty(SQLiteConfig.Pragma.DATE_CLASS.getPragmaName()))
                 .isEqualTo(SQLiteConfig.DateClass.REAL.name());
     }
+
+    @Test
+    public void setBusyTimeout() {
+        SQLiteConfig config = new SQLiteConfig();
+
+        // verify the default is set in the pragma table and the cached value
+        assertThat(config.toProperties().getProperty(SQLiteConfig.Pragma.BUSY_TIMEOUT.pragmaName))
+                .isEqualTo("3000");
+        assertThat(config.getBusyTimeout()).isEqualTo(3000);
+
+        // verify that the default is updated in both places
+        config.setBusyTimeout(1234);
+        assertThat(config.toProperties().getProperty(SQLiteConfig.Pragma.BUSY_TIMEOUT.pragmaName))
+                .isEqualTo("1234");
+        assertThat(config.getBusyTimeout()).isEqualTo(1234);
+
+        Properties properties = new Properties();
+        properties.setProperty(SQLiteConfig.Pragma.BUSY_TIMEOUT.pragmaName, "100");
+        config = new SQLiteConfig(properties);
+
+        // verify that we can set an initial value other than the default
+        assertThat(config.toProperties().getProperty(SQLiteConfig.Pragma.BUSY_TIMEOUT.pragmaName))
+                .isEqualTo("100");
+        assertThat(config.getBusyTimeout()).isEqualTo(100);
+    }
 }
diff --git a/src/test/java/org/sqlite/SQLiteDataSourceTest.java b/src/test/java/org/sqlite/SQLiteDataSourceTest.java
index b3717246..b7674644 100755
--- a/src/test/java/org/sqlite/SQLiteDataSourceTest.java
+++ b/src/test/java/org/sqlite/SQLiteDataSourceTest.java
@@ -80,4 +80,16 @@ public class SQLiteDataSourceTest {
             }
         }
     }
+
+    @Test
+    public void setBusyTimeout() {
+        final SQLiteDataSource ds = new SQLiteDataSource();
+        ds.setBusyTimeout(1234);
+        assertThat(
+                        ds.getConfig()
+                                .toProperties()
+                                .getProperty(SQLiteConfig.Pragma.BUSY_TIMEOUT.pragmaName))
+                .isEqualTo("1234");
+        assertThat(ds.getConfig().getBusyTimeout()).isEqualTo(1234);
+    }
 }