jellyfin-server/MediaBrowser.Server.Implementations/Persistence/SqliteExtensions.cs

54 lines
1.6 KiB
C#
Raw Normal View History

2016-06-01 05:50:00 +00:00
using System;
using System.Data;
using System.Data.SQLite;
using System.Threading.Tasks;
using MediaBrowser.Model.Logging;
namespace MediaBrowser.Server.Implementations.Persistence
{
/// <summary>
/// Class SQLiteExtensions
/// </summary>
public static class SqliteExtensions
{
/// <summary>
/// Connects to db.
/// </summary>
2016-06-11 15:55:05 +00:00
public static async Task<IDbConnection> ConnectToDb(string dbPath, bool isReadOnly, bool enablePooling, int? cacheSize, ILogger logger)
2016-06-01 05:50:00 +00:00
{
if (string.IsNullOrEmpty(dbPath))
{
throw new ArgumentNullException("dbPath");
}
2016-06-15 18:56:37 +00:00
SQLiteConnection.SetMemoryStatus(false);
2016-06-01 05:50:00 +00:00
var connectionstr = new SQLiteConnectionStringBuilder
{
PageSize = 4096,
2016-06-10 16:45:04 +00:00
CacheSize = cacheSize ?? 2000,
2016-06-01 05:50:00 +00:00
SyncMode = SynchronizationModes.Normal,
DataSource = dbPath,
2016-06-11 15:55:05 +00:00
JournalMode = SQLiteJournalModeEnum.Wal,
2016-06-14 04:15:13 +00:00
// This is causing crashing under linux
2016-06-14 19:21:26 +00:00
Pooling = enablePooling && Environment.OSVersion.Platform == PlatformID.Win32NT,
2016-06-11 15:55:05 +00:00
ReadOnly = isReadOnly
2016-06-01 05:50:00 +00:00
};
2016-06-11 20:12:01 +00:00
var connectionString = connectionstr.ConnectionString;
2016-06-15 18:56:37 +00:00
if (!enablePooling)
{
logger.Info("Sqlite {0} opening {1}", SQLiteConnection.SQLiteVersion, connectionString);
}
2016-06-11 20:12:01 +00:00
var connection = new SQLiteConnection(connectionString);
2016-06-01 05:50:00 +00:00
await connection.OpenAsync().ConfigureAwait(false);
return connection;
}
}
}