Skip to content

Commit

Permalink
Remove redundant pragmas
Browse files Browse the repository at this point in the history
  • Loading branch information
jjxtra committed Oct 3, 2019
1 parent a520304 commit f00337f
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions Core/SqliteDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace DigitalRuby.IPBan
{
public abstract class SqliteDB : IDisposable
{
private readonly string additionalPragmas;

/// <summary>
/// Wraps a transaction
/// </summary>
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}
}

Expand All @@ -296,8 +287,7 @@ protected void CloseConnection(SqliteConnection conn)
/// </summary>
protected virtual void OnInitialize()
{
ExecuteNonQuery("PRAGMA auto_vacuum = INCREMENTAL;");
ExecuteNonQuery("PRAGMA journal_mode = WAL;");

}

/// <summary>
Expand All @@ -324,20 +314,22 @@ static SqliteDB()
/// <summary>
/// Constructor
/// </summary>
/// <param name="dbPath">Database full path or just the file name. Can use </param>
public SqliteDB(string dbPath)
/// <param name="dbPath">Database full path or just the file name. Can use SqliteDB.DbPathInMemory for in memory db.</param>
/// <param name="additionalPragmas">Pragma statements for new connection (semi-colon delimited), or null for none</param>
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();
}

Expand Down

0 comments on commit f00337f

Please sign in to comment.