diff options
| author | Luke Pulverenti <luke.pulverenti@gmail.com> | 2016-06-16 14:10:58 -0400 |
|---|---|---|
| committer | Luke Pulverenti <luke.pulverenti@gmail.com> | 2016-06-16 14:10:58 -0400 |
| commit | 4f791d6ee1712439c8485260221a8c0aa64760f2 (patch) | |
| tree | 88bd1f7e13bfda15644a054cd205f64753d25275 /MediaBrowser.Server.Implementations/Persistence/SqliteExtensions.cs | |
| parent | d9406d48ca0231bc096aeadc595c30f0596c8dda (diff) | |
| parent | 7498b7b5b7e2f7ddf380df1f47421d26c8171418 (diff) | |
Merge branch 'dev'
Diffstat (limited to 'MediaBrowser.Server.Implementations/Persistence/SqliteExtensions.cs')
| -rw-r--r-- | MediaBrowser.Server.Implementations/Persistence/SqliteExtensions.cs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/MediaBrowser.Server.Implementations/Persistence/SqliteExtensions.cs b/MediaBrowser.Server.Implementations/Persistence/SqliteExtensions.cs new file mode 100644 index 000000000..d5b582da5 --- /dev/null +++ b/MediaBrowser.Server.Implementations/Persistence/SqliteExtensions.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.SQLite; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Logging; + +namespace MediaBrowser.Server.Implementations.Persistence +{ + /// <summary> + /// Class SQLiteExtensions + /// </summary> + public static class SqliteExtensions + { + /// <summary> + /// Connects to db. + /// </summary> + public static async Task<IDbConnection> ConnectToDb(string dbPath, bool isReadOnly, bool enablePooling, int? cacheSize, ILogger logger) + { + if (string.IsNullOrEmpty(dbPath)) + { + throw new ArgumentNullException("dbPath"); + } + + SQLiteConnection.SetMemoryStatus(false); + + var connectionstr = new SQLiteConnectionStringBuilder + { + PageSize = 4096, + CacheSize = cacheSize ?? 2000, + SyncMode = SynchronizationModes.Normal, + DataSource = dbPath, + JournalMode = SQLiteJournalModeEnum.Wal, + + // This is causing crashing under linux + Pooling = enablePooling && Environment.OSVersion.Platform == PlatformID.Win32NT, + ReadOnly = isReadOnly + }; + + var connectionString = connectionstr.ConnectionString; + + if (!enablePooling) + { + logger.Info("Sqlite {0} opening {1}", SQLiteConnection.SQLiteVersion, connectionString); + } + + var connection = new SQLiteConnection(connectionString); + + await connection.OpenAsync().ConfigureAwait(false); + + return connection; + } + } +} |
