sqlite-jdbc icon indicating copy to clipboard operation
sqlite-jdbc copied to clipboard

Default values are preferred over pragmas stated in filename

Open TuomasKiviaho opened this issue 10 years ago • 2 comments

SQLiteConfig class sets default values such as dateStringFormat already in place when toProperties is called. This happens prematurely because extractPragmasFromFilename of CoreConnection thinks that these values are user defined because they are already stated in the given properties.

As a fix I propose hiding of the still public SQLiteConfig properties and moving the default login to getters where default value would be returned only when property hasn't yet been declared.

org/sqlite/SQLiteConfig.java b/src/org/sqlite/SQLiteConfig.java

@@ -43,16 +43,16 @@
 public class SQLiteConfig
 {
     private final Properties pragmaTable;
-    private int openModeFlag = 0x00;
+    private Integer openModeFlag;
     private TransactionMode transactionMode;
-    public final int busyTimeout;
+    private Integer busyTimeout;

     /* Date storage class*/
     public final static String DEFAULT_DATE_STRING_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
-    public DateClass dateClass;
-    public DatePrecision datePrecision;
-    public long dateMultiplier;
-    public String dateStringFormat;
+    private DateClass dateClass;
+    private DatePrecision datePrecision;
+    private Long dateMultiplier;
+    private String dateStringFormat;

     /**
      * Default constructor.
@@ -68,28 +68,6 @@
      */
     public SQLiteConfig(Properties prop) {
         this.pragmaTable = prop;
-
-        String openMode = pragmaTable.getProperty(Pragma.OPEN_MODE.pragmaName);
-        if (openMode != null) {
-            openModeFlag = Integer.parseInt(openMode);
-        }
-        else {
-            // set the default open mode of SQLite3
-            setOpenMode(SQLiteOpenMode.READWRITE);
-            setOpenMode(SQLiteOpenMode.CREATE);
-        }
-        openMode = pragmaTable.getProperty(Pragma.SHARED_CACHE.pragmaName);
-        setOpenMode(SQLiteOpenMode.OPEN_URI); // Enable URI filenames
-
-        transactionMode = TransactionMode.getMode(
-                pragmaTable.getProperty(Pragma.TRANSACTION_MODE.pragmaName, TransactionMode.DEFFERED.name()));
-
-        dateClass = DateClass.getDateClass(pragmaTable.getProperty(Pragma.DATE_CLASS.pragmaName, DateClass.INTEGER.name()));
-        datePrecision = DatePrecision.getPrecision(pragmaTable.getProperty(Pragma.DATE_PRECISION.pragmaName, DatePrecision.MILLISECONDS.name()));
-        dateMultiplier = (datePrecision == DatePrecision.MILLISECONDS) ? 1L : 1000L;
-        dateStringFormat = pragmaTable.getProperty(Pragma.DATE_STRING_FORMAT.pragmaName, DEFAULT_DATE_STRING_FORMAT);
-
-        busyTimeout = Integer.parseInt(pragmaTable.getProperty(Pragma.BUSY_TIMEOUT.pragmaName, "3000"));
     }

     /**
@@ -190,7 +168,62 @@
      * @return The open mode flags.
      */
     public int getOpenModeFlags() {
+        Integer openModeFlag = this.openModeFlag;
+        if (openModeFlag == null) {
+            String openMode = pragmaTable.getProperty(Pragma.OPEN_MODE.pragmaName);
+            if (openMode != null) {
+                openModeFlag = Integer.parseInt(openMode);
+            }
+            else {
+                openModeFlag = 0x00;
+                // set the default open mode of SQLite3
+                openModeFlag |= SQLiteOpenMode.READWRITE.flag;
+                openModeFlag |= SQLiteOpenMode.CREATE.flag;
+            }
+            openMode = pragmaTable.getProperty(Pragma.SHARED_CACHE.pragmaName);
+            openModeFlag |= SQLiteOpenMode.OPEN_URI.flag; // Enable URI filenames
+        }
         return openModeFlag;
+    }
+    
+    public int getBusyTimeout() {
+        Integer busyTimeout = this.busyTimeout;
+        if (busyTimeout == null) {
+            busyTimeout = Integer.parseInt(pragmaTable.getProperty(Pragma.BUSY_TIMEOUT.pragmaName, "3000"));
+        }
+        return busyTimeout;
+    }
+    
+    public DateClass getDateClass() {
+        DateClass dateClass = this.dateClass;
+        if (dateClass == null) {
+            dateClass = DateClass.getDateClass(pragmaTable.getProperty(Pragma.DATE_CLASS.pragmaName, DateClass.INTEGER.name()));
+        }
+        return dateClass;
+    }
+    
+    public DatePrecision getDatePrecision() {
+        DatePrecision datePrecision = this.datePrecision;
+        if (datePrecision == null) {
+            datePrecision = DatePrecision.getPrecision(pragmaTable.getProperty(Pragma.DATE_PRECISION.pragmaName, DatePrecision.MILLISECONDS.name()));
+        }
+        return datePrecision;
+    }
+    
+    public long getDateMultiplier() {
+        Long dateMultiplier = this.dateMultiplier;
+        if (dateMultiplier == null) {
+            dateMultiplier = (datePrecision == DatePrecision.MILLISECONDS) ? 1L : 1000L;
+        }
+        return dateMultiplier;
+    }
+    
+    public String getDateStringFormat() {
+        String dateStringFormat = this.dateStringFormat;
+        if (dateStringFormat == null) {
+            dateStringFormat = pragmaTable.getProperty(Pragma.DATE_STRING_FORMAT.pragmaName, DEFAULT_DATE_STRING_FORMAT);
+        }
+        return dateStringFormat;
     }

     /**
@@ -208,11 +241,21 @@
      * @return The property object.
      */
     public Properties toProperties() {
-        pragmaTable.setProperty(Pragma.OPEN_MODE.pragmaName, Integer.toString(openModeFlag));
-        pragmaTable.setProperty(Pragma.TRANSACTION_MODE.pragmaName, transactionMode.getValue());
-        pragmaTable.setProperty(Pragma.DATE_CLASS.pragmaName, dateClass.getValue());
-        pragmaTable.setProperty(Pragma.DATE_PRECISION.pragmaName, datePrecision.getValue());
-        pragmaTable.setProperty(Pragma.DATE_STRING_FORMAT.pragmaName, dateStringFormat);
+        if (openModeFlag != null) {
+            pragmaTable.setProperty(Pragma.OPEN_MODE.pragmaName, Integer.toString(openModeFlag));
+        }
+        if (transactionMode != null) {
+            pragmaTable.setProperty(Pragma.TRANSACTION_MODE.pragmaName, transactionMode.getValue());
+        }
+        if (dateClass != null) {
+            pragmaTable.setProperty(Pragma.DATE_CLASS.pragmaName, dateClass.getValue());
+        }
+        if (datePrecision != null) {
+            pragmaTable.setProperty(Pragma.DATE_PRECISION.pragmaName, datePrecision.getValue());
+        }
+        if (dateStringFormat != null) {
+            pragmaTable.setProperty(Pragma.DATE_STRING_FORMAT.pragmaName, dateStringFormat);
+        }

         return pragmaTable;
     }
@@ -743,6 +786,11 @@
      * @return The transaction mode.
      */
     public TransactionMode getTransactionMode() {
+        TransactionMode transactionMode = this.transactionMode;
+        if (transactionMode == null) {
+            transactionMode = TransactionMode.getMode(
+                pragmaTable.getProperty(Pragma.TRANSACTION_MODE.pragmaName, TransactionMode.DEFFERED.name()));
+        }
         return transactionMode;
     }

org/sqlite/core/CoreConnection.java

@@ -65,15 +65,15 @@
         this.fileName = extractPragmasFromFilename(fileName, prop);

         SQLiteConfig config = new SQLiteConfig(prop);
-        this.dateClass = config.dateClass;
-        this.dateMultiplier = config.dateMultiplier;
-        this.dateFormat = FastDateFormat.getInstance(config.dateStringFormat);
-        this.dateStringFormat = config.dateStringFormat;
-        this.datePrecision = config.datePrecision;
+        this.dateClass = config.getDateClass();
+        this.dateMultiplier = config.getDateMultiplier();
+        this.dateFormat = FastDateFormat.getInstance(config.getDateStringFormat());
+        this.dateStringFormat = config.getDateStringFormat();
+        this.datePrecision = config.getDatePrecision();
         this.transactionMode = config.getTransactionMode();
         this.openModeFlags = config.getOpenModeFlags();

-        open(openModeFlags, config.busyTimeout);
+        open(openModeFlags, config.getBusyTimeout());

         if (fileName.startsWith("file:") && !fileName.contains("cache="))
         {   // URI cache overrides flags

TuomasKiviaho avatar Nov 10 '15 10:11 TuomasKiviaho

https://github.com/xerial/sqlite-jdbc/issues/24 would like to add separate dateStringFormat. These would suffer from the same problem.

TuomasKiviaho avatar Nov 14 '15 18:11 TuomasKiviaho

Is this still happening on the latest version?

gotson avatar Jul 29 '22 06:07 gotson