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

59 lines
1.8 KiB
C#
Raw Normal View History

2016-05-31 22:50:00 -07:00
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2016-06-02 12:32:15 -07:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.Entities;
2016-05-31 22:50:00 -07:00
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 08:55:05 -07:00
public static async Task<IDbConnection> ConnectToDb(string dbPath, bool isReadOnly, bool enablePooling, int? cacheSize, ILogger logger)
2016-05-31 22:50:00 -07:00
{
if (string.IsNullOrEmpty(dbPath))
{
throw new ArgumentNullException("dbPath");
}
2016-06-15 11:56:37 -07:00
SQLiteConnection.SetMemoryStatus(false);
2016-05-31 22:50:00 -07:00
var connectionstr = new SQLiteConnectionStringBuilder
{
PageSize = 4096,
2016-06-10 09:45:04 -07:00
CacheSize = cacheSize ?? 2000,
2016-05-31 22:50:00 -07:00
SyncMode = SynchronizationModes.Normal,
DataSource = dbPath,
2016-06-11 08:55:05 -07:00
JournalMode = SQLiteJournalModeEnum.Wal,
2016-06-13 21:15:13 -07:00
// This is causing crashing under linux
2016-06-14 12:21:26 -07:00
Pooling = enablePooling && Environment.OSVersion.Platform == PlatformID.Win32NT,
2016-06-11 08:55:05 -07:00
ReadOnly = isReadOnly
2016-05-31 22:50:00 -07:00
};
2016-06-11 13:12:01 -07:00
var connectionString = connectionstr.ConnectionString;
2016-06-15 11:56:37 -07:00
if (!enablePooling)
{
logger.Info("Sqlite {0} opening {1}", SQLiteConnection.SQLiteVersion, connectionString);
}
2016-06-11 13:12:01 -07:00
var connection = new SQLiteConnection(connectionString);
2016-05-31 22:50:00 -07:00
await connection.OpenAsync().ConfigureAwait(false);
return connection;
}
}
}