sonarr-repo-only/NzbDrone.Core/Datastore/RepositoryProvider.cs

99 lines
3.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using Migrator.Providers;
using Migrator.Providers.SQLite;
using NzbDrone.Core.Repository;
using SubSonic.DataProviders;
using SubSonic.Extensions;
using SubSonic.Schema;
using Migrator.Framework;
namespace NzbDrone.Core.Datastore
{
public class RepositoryProvider
{
public static readonly ITable EpisodesSchema = new RepositoryProvider().GetSchemaFromType(typeof(Episode));
public static readonly ITable SeriesSchema = new RepositoryProvider().GetSchemaFromType(typeof(Series));
public static readonly ITable EpisodeFilesSchema = new RepositoryProvider().GetSchemaFromType(typeof(EpisodeFile));
public static readonly ITable JobsSchema = new RepositoryProvider().GetSchemaFromType(typeof(JobSetting));
public virtual IList<Type> GetRepositoryTypes()
{
var coreAssembly = Assembly.GetExecutingAssembly();
var repoTypes = coreAssembly.GetTypes().Where(t => !String.IsNullOrWhiteSpace(t.Namespace) && t.Namespace.StartsWith("NzbDrone.Core.Repository"));
repoTypes = repoTypes.Where(r => !r.IsEnum);
return repoTypes.ToList();
}
public virtual ITable GetSchemaFromType(Type type)
{
return type.ToSchemaTable(Connection.GetDataProvider(Connection.MainConnectionString));
}
public virtual Column[] GetColumnsFromDatabase(ITransformationProvider database, string tableName)
{
return database.GetColumns(tableName);
}
public virtual List<Column> GetDeletedColumns(ITable typeSchema, Column[] dbColumns)
{
var deleteColumns = new List<Column>();
foreach (var dbColumn in dbColumns)
{
if (!typeSchema.Columns.ToList().Exists(c => c.Name == dbColumn.Name.Trim('[', ']')))
{
deleteColumns.Add(dbColumn);
}
}
return deleteColumns;
}
public virtual List<Column> GetNewColumns(ITable typeSchema, Column[] dbColumns)
{
var newColumns = new List<Column>();
foreach (var typeColumn in typeSchema.Columns)
{
if (!dbColumns.ToList().Exists(c => c.Name.Trim('[', ']') == typeColumn.Name))
{
newColumns.Add(ConvertToMigratorColumn(typeColumn));
}
}
return newColumns;
}
public virtual Column ConvertToMigratorColumn(SubSonic.Schema.IColumn subsonicColumns)
{
var migColumn = new Column(subsonicColumns.Name, subsonicColumns.DataType);
if (subsonicColumns.IsPrimaryKey)
{
migColumn.ColumnProperty = ColumnProperty.PrimaryKey;
}
if (subsonicColumns.IsNullable)
{
migColumn.ColumnProperty = migColumn.ColumnProperty | ColumnProperty.Null;
}
else
{
migColumn.ColumnProperty = migColumn.ColumnProperty | ColumnProperty.NotNull;
migColumn.DefaultValue = false;
}
return migColumn;
}
}
}