using System;
using System.Data;
using System.Data.SQLite;
using System.Threading.Tasks;
using MediaBrowser.Model.Logging;
using MediaBrowser.Server.Implementations.Persistence;
namespace MediaBrowser.ServerApplication.Native
{
///
/// Class SQLiteExtensions
///
static class SqliteExtensions
{
///
/// Connects to db.
///
/// The db path.
/// The logger.
/// Task{IDbConnection}.
/// dbPath
public static async Task 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.Full,
DataSource = dbPath,
JournalMode = SQLiteJournalModeEnum.Wal
};
var connection = new SQLiteConnection(connectionstr.ConnectionString);
await connection.OpenAsync().ConfigureAwait(false);
return connection;
}
}
public class DbConnector : IDbConnector
{
private readonly ILogger _logger;
public DbConnector(ILogger logger)
{
_logger = logger;
}
public async Task Connect(string dbPath)
{
try
{
return await SqliteExtensions.ConnectToDb(dbPath, _logger).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.ErrorException("Error opening database {0}", ex, dbPath);
throw;
}
}
}
}