1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-18 23:48:35 +02:00
Sonarr/src/NzbDrone.Core/Datastore/Database.cs
2014-02-22 16:22:05 -08:00

58 lines
1.5 KiB
C#

using System;
using System.Data.SQLite;
using Marr.Data;
using NLog;
using NzbDrone.Common.Instrumentation;
namespace NzbDrone.Core.Datastore
{
public interface IDatabase
{
IDataMapper GetDataMapper();
Version Version { get; }
void Vacuum();
}
public class Database : IDatabase
{
private readonly string _connectionString;
private Logger logger = NzbDroneLogger.GetLogger();
public Database(string connectionString)
{
_connectionString = connectionString;
}
public IDataMapper GetDataMapper()
{
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);
}
}
}
}