2013-02-21 01:33:05 +00:00
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
using System;
|
|
|
|
using System.Data;
|
|
|
|
using System.Data.Common;
|
|
|
|
using System.Data.SQLite;
|
|
|
|
using System.IO;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
2013-02-24 21:53:54 +00:00
|
|
|
namespace MediaBrowser.Server.Implementations.Sqlite
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Class SqliteRepository
|
|
|
|
/// </summary>
|
|
|
|
public abstract class SqliteRepository : IDisposable
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// The db file name
|
|
|
|
/// </summary>
|
2013-05-21 15:52:59 +00:00
|
|
|
protected string DbFileName;
|
2013-02-21 01:33:05 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The connection
|
|
|
|
/// </summary>
|
2013-05-21 15:52:59 +00:00
|
|
|
protected SQLiteConnection Connection;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2013-02-21 20:26:35 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the logger.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The logger.</value>
|
2013-02-21 01:33:05 +00:00
|
|
|
protected ILogger Logger { get; private set; }
|
|
|
|
|
2013-02-21 20:26:35 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="SqliteRepository" /> class.
|
|
|
|
/// </summary>
|
2013-04-03 02:59:27 +00:00
|
|
|
/// <param name="logManager">The log manager.</param>
|
2013-02-21 20:26:35 +00:00
|
|
|
/// <exception cref="System.ArgumentNullException">logger</exception>
|
2013-04-03 02:59:27 +00:00
|
|
|
protected SqliteRepository(ILogManager logManager)
|
2013-02-21 20:26:35 +00:00
|
|
|
{
|
2013-04-03 02:59:27 +00:00
|
|
|
if (logManager == null)
|
2013-02-21 20:26:35 +00:00
|
|
|
{
|
2013-04-03 02:59:27 +00:00
|
|
|
throw new ArgumentNullException("logManager");
|
2013-02-21 20:26:35 +00:00
|
|
|
}
|
|
|
|
|
2013-04-03 02:59:27 +00:00
|
|
|
Logger = logManager.GetLogger(GetType().Name);
|
2013-02-21 20:26:35 +00:00
|
|
|
}
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Connects to DB.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="dbPath">The db path.</param>
|
|
|
|
/// <returns>Task{System.Boolean}.</returns>
|
2013-02-21 20:26:35 +00:00
|
|
|
/// <exception cref="System.ArgumentNullException">dbPath</exception>
|
2013-06-03 18:15:35 +00:00
|
|
|
protected Task ConnectToDb(string dbPath)
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(dbPath))
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException("dbPath");
|
|
|
|
}
|
|
|
|
|
2013-05-21 15:52:59 +00:00
|
|
|
DbFileName = dbPath;
|
2013-02-21 01:33:05 +00:00
|
|
|
var connectionstr = new SQLiteConnectionStringBuilder
|
|
|
|
{
|
|
|
|
PageSize = 4096,
|
|
|
|
CacheSize = 40960,
|
|
|
|
SyncMode = SynchronizationModes.Off,
|
|
|
|
DataSource = dbPath,
|
2013-06-03 02:46:46 +00:00
|
|
|
JournalMode = SQLiteJournalModeEnum.Wal
|
2013-02-21 01:33:05 +00:00
|
|
|
};
|
|
|
|
|
2013-05-21 15:52:59 +00:00
|
|
|
Connection = new SQLiteConnection(connectionstr.ConnectionString);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2013-06-03 18:15:35 +00:00
|
|
|
return Connection.OpenAsync();
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Runs the queries.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="queries">The queries.</param>
|
|
|
|
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
|
2013-02-21 20:26:35 +00:00
|
|
|
/// <exception cref="System.ArgumentNullException">queries</exception>
|
2013-02-21 01:33:05 +00:00
|
|
|
protected void RunQueries(string[] queries)
|
|
|
|
{
|
|
|
|
if (queries == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException("queries");
|
|
|
|
}
|
|
|
|
|
2013-05-21 15:52:59 +00:00
|
|
|
using (var tran = Connection.BeginTransaction())
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2013-05-21 15:52:59 +00:00
|
|
|
using (var cmd = Connection.CreateCommand())
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
2013-05-08 20:58:52 +00:00
|
|
|
foreach (var query in queries)
|
|
|
|
{
|
|
|
|
cmd.Transaction = tran;
|
|
|
|
cmd.CommandText = query;
|
|
|
|
cmd.ExecuteNonQuery();
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tran.Commit();
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Logger.ErrorException("Error running queries", e);
|
|
|
|
tran.Rollback();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
|
|
|
/// </summary>
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
|
2013-04-22 04:38:03 +00:00
|
|
|
private readonly object _disposeLock = new object();
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Releases unmanaged and - optionally - managed resources.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
|
|
|
protected virtual void Dispose(bool dispose)
|
|
|
|
{
|
|
|
|
if (dispose)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2013-04-22 04:38:03 +00:00
|
|
|
lock (_disposeLock)
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
2013-05-21 15:52:59 +00:00
|
|
|
if (Connection != null)
|
2013-02-27 04:19:05 +00:00
|
|
|
{
|
2013-05-21 15:52:59 +00:00
|
|
|
if (Connection.IsOpen())
|
2013-04-19 20:27:02 +00:00
|
|
|
{
|
2013-05-21 15:52:59 +00:00
|
|
|
Connection.Close();
|
2013-04-19 20:27:02 +00:00
|
|
|
}
|
|
|
|
|
2013-05-21 15:52:59 +00:00
|
|
|
Connection.Dispose();
|
|
|
|
Connection = null;
|
2013-02-27 04:19:05 +00:00
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Logger.ErrorException("Error disposing database", ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a stream from a DataReader at a given ordinal
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="reader">The reader.</param>
|
|
|
|
/// <param name="ordinal">The ordinal.</param>
|
|
|
|
/// <returns>Stream.</returns>
|
2013-02-21 20:26:35 +00:00
|
|
|
/// <exception cref="System.ArgumentNullException">reader</exception>
|
2013-02-21 01:33:05 +00:00
|
|
|
protected static Stream GetStream(IDataReader reader, int ordinal)
|
|
|
|
{
|
|
|
|
if (reader == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException("reader");
|
|
|
|
}
|
2013-06-03 18:15:35 +00:00
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
var memoryStream = new MemoryStream();
|
|
|
|
var num = 0L;
|
|
|
|
var array = new byte[4096];
|
|
|
|
long bytes;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
bytes = reader.GetBytes(ordinal, num, array, 0, array.Length);
|
|
|
|
memoryStream.Write(array, 0, (int)bytes);
|
|
|
|
num += bytes;
|
|
|
|
}
|
|
|
|
while (bytes > 0L);
|
|
|
|
memoryStream.Position = 0;
|
|
|
|
return memoryStream;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|