2016-06-01 05:50:00 +00: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 19:32:15 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
2016-06-01 05:50:00 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.Persistence
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class SQLiteExtensions
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class SqliteExtensions
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Connects to db.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dbPath">The db path.</param>
|
|
|
|
|
/// <param name="logger">The logger.</param>
|
|
|
|
|
/// <returns>Task{IDbConnection}.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">dbPath</exception>
|
|
|
|
|
public static async Task<IDbConnection> ConnectToDb(string dbPath, ILogger logger)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(dbPath))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("dbPath");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.Info("Sqlite {0} opening {1}", SQLiteConnection.SQLiteVersion, dbPath);
|
|
|
|
|
|
|
|
|
|
var connectionstr = new SQLiteConnectionStringBuilder
|
|
|
|
|
{
|
|
|
|
|
PageSize = 4096,
|
|
|
|
|
CacheSize = 2000,
|
|
|
|
|
SyncMode = SynchronizationModes.Normal,
|
|
|
|
|
DataSource = dbPath,
|
|
|
|
|
JournalMode = SQLiteJournalModeEnum.Wal
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var connection = new SQLiteConnection(connectionstr.ConnectionString);
|
|
|
|
|
|
|
|
|
|
await connection.OpenAsync().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return connection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void BindFunction(this SQLiteConnection connection, SQLiteFunction function)
|
|
|
|
|
{
|
|
|
|
|
var attributes = function.GetType().GetCustomAttributes(typeof(SQLiteFunctionAttribute), true).Cast<SQLiteFunctionAttribute>().ToArray();
|
|
|
|
|
if (attributes.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("SQLiteFunction doesn't have SQLiteFunctionAttribute");
|
|
|
|
|
}
|
|
|
|
|
connection.BindFunction(attributes[0], function);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|