2013-03-25 06:51:32 +03:00
|
|
|
using System;
|
2013-11-28 10:11:05 +03:00
|
|
|
using System.Data.SQLite;
|
2013-03-25 06:51:32 +03:00
|
|
|
using Marr.Data;
|
2013-11-28 10:11:05 +03:00
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common.Instrumentation;
|
2013-03-25 06:51:32 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Datastore
|
|
|
|
{
|
|
|
|
public interface IDatabase
|
|
|
|
{
|
2013-07-16 03:45:49 +03:00
|
|
|
IDataMapper GetDataMapper();
|
2013-11-28 10:11:05 +03:00
|
|
|
Version Version { get; }
|
|
|
|
void Vacuum();
|
2013-03-25 06:51:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public class Database : IDatabase
|
|
|
|
{
|
2013-11-28 10:11:05 +03:00
|
|
|
private readonly string _connectionString;
|
2013-03-25 06:51:32 +03:00
|
|
|
|
2013-11-28 10:11:05 +03:00
|
|
|
private Logger logger = NzbDroneLogger.GetLogger();
|
|
|
|
|
|
|
|
public Database(string connectionString)
|
2013-03-25 06:51:32 +03:00
|
|
|
{
|
2013-11-28 10:11:05 +03:00
|
|
|
_connectionString = connectionString;
|
2013-03-25 06:51:32 +03:00
|
|
|
}
|
|
|
|
|
2013-07-16 03:45:49 +03:00
|
|
|
public IDataMapper GetDataMapper()
|
2013-05-11 23:06:57 +03:00
|
|
|
{
|
2013-11-28 10:11:05 +03:00
|
|
|
return new DataMapper(SQLiteFactory.Instance, _connectionString)
|
|
|
|
{
|
|
|
|
SqlMode = SqlModes.Text,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public Version Version
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
var version = GetDataMapper().ExecuteScalar("SELECT sqlite_version()").ToString();
|
|
|
|
return new Version(version);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Vacuum()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
logger.Info("Vacuuming database " + _connectionString);
|
|
|
|
GetDataMapper().ExecuteNonQuery("Vacuum;");
|
|
|
|
logger.Info("Database Compressed " + _connectionString);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
logger.Error("An Error occurred while vacuuming database. " + _connectionString, e);
|
|
|
|
}
|
2013-05-11 23:06:57 +03:00
|
|
|
}
|
2013-03-25 06:51:32 +03:00
|
|
|
}
|
|
|
|
}
|