From f00337f9223c76dc90ed6fa62d3fbcc7a2700817 Mon Sep 17 00:00:00 2001 From: Jeff Johnson Date: Thu, 3 Oct 2019 17:53:54 -0600 Subject: [PATCH] Remove redundant pragmas --- Core/SqliteDB.cs | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/Core/SqliteDB.cs b/Core/SqliteDB.cs index db1a3877..9a80ac57 100644 --- a/Core/SqliteDB.cs +++ b/Core/SqliteDB.cs @@ -9,6 +9,8 @@ namespace DigitalRuby.IPBan { public abstract class SqliteDB : IDisposable { + private readonly string additionalPragmas; + /// /// Wraps a transaction /// @@ -20,16 +22,6 @@ public SqliteDBTransaction(SqliteConnection conn, bool disposeConnection) { DBConnection = conn; this.disposeConnection = disposeConnection; - using (SqliteCommand command = DBConnection.CreateCommand()) - { - command.CommandText = "PRAGMA auto_vacuum = INCREMENTAL;"; - command.ExecuteNonQuery(); - } - using (SqliteCommand command = DBConnection.CreateCommand()) - { - command.CommandText = "PRAGMA journal_mode = WAL;"; - command.ExecuteNonQuery(); - } DBTransaction = DBConnection.BeginTransaction(TransactionLevel); } @@ -274,8 +266,7 @@ protected void OpenConnection(SqliteConnection conn) if (conn != InMemoryConnection) { conn.Open(); - ExecuteNonQuery("PRAGMA auto_vacuum = INCREMENTAL;", conn, null); - ExecuteNonQuery("PRAGMA journal_mode = WAL;", conn, null); + ExecuteNonQuery($"PRAGMA auto_vacuum = INCREMENTAL; PRAGMA journal_mode = WAL; PRAGMA busy_timeout = 30000; PRAGMA synchronous = NORMAL; PRAGMA foreign_keys = ON; PRAGMA temp_store = MEMORY; {additionalPragmas}", conn, (SqliteTransaction)null); } } @@ -296,8 +287,7 @@ protected void CloseConnection(SqliteConnection conn) /// protected virtual void OnInitialize() { - ExecuteNonQuery("PRAGMA auto_vacuum = INCREMENTAL;"); - ExecuteNonQuery("PRAGMA journal_mode = WAL;"); + } /// @@ -324,20 +314,22 @@ static SqliteDB() /// /// Constructor /// - /// Database full path or just the file name. Can use - public SqliteDB(string dbPath) + /// Database full path or just the file name. Can use SqliteDB.DbPathInMemory for in memory db. + /// Pragma statements for new connection (semi-colon delimited), or null for none + public SqliteDB(string dbPath, string additionalPragmas = null) { if (dbPath == DbPathInMemory) { - InMemoryConnection = new SqliteConnection(ConnectionString = ("Data Source=" + dbPath)); + InMemoryConnection = new SqliteConnection(ConnectionString = ($"Data Source={dbPath}")); InMemoryConnection.Open(); } else { dbPath.ThrowIfNullOrEmpty(nameof(dbPath)); dbPath = (Path.IsPathRooted(dbPath) ? dbPath : Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dbPath)); - ConnectionString = "Data Source=" + dbPath; + ConnectionString = $"Data Source={dbPath}"; } + this.additionalPragmas = (additionalPragmas ?? string.Empty).Trim(); OnInitialize(); }