using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using Kyoo.Models;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Newtonsoft.Json;
namespace Kyoo.SqLite
{
///
/// A sqlite implementation of .
///
public class SqLiteContext : DatabaseContext
{
///
/// The connection string to use.
///
private readonly string _connection;
///
/// Is this instance in debug mode?
///
private readonly bool _debugMode;
///
/// Should the configure step be skipped? This is used when the database is created via DbContextOptions.
///
private readonly bool _skipConfigure;
///
/// A basic constructor that set default values (query tracker behaviors, mapping enums...)
///
public SqLiteContext()
{ }
///
/// Create a new using specific options
///
/// The options to use.
public SqLiteContext(DbContextOptions options)
: base(options)
{
_skipConfigure = true;
}
///
/// A basic constructor that set default values (query tracker behaviors, mapping enums...)
///
/// The connection string to use
/// Is this instance in debug mode?
public SqLiteContext(string connection, bool debugMode)
{
_connection = connection;
_debugMode = debugMode;
}
///
/// Set connection information for this database context
///
/// An option builder to fill.
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!_skipConfigure)
{
if (_connection != null)
optionsBuilder.UseSqlite(_connection);
else
optionsBuilder.UseSqlite();
if (_debugMode)
optionsBuilder.EnableDetailedErrors().EnableSensitiveDataLogging();
}
base.OnConfiguring(optionsBuilder);
}
///
/// Set database parameters to support every types of Kyoo.
///
/// The database's model builder.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
ValueConverter arrayConvertor = new(
x => string.Join(";", x),
x => x.Split(';', StringSplitOptions.None));
modelBuilder.Entity()
.Property(x => x.Paths)
.HasConversion(arrayConvertor);
modelBuilder.Entity()
.Property(x => x.Aliases)
.HasConversion(arrayConvertor);
modelBuilder.Entity()
.Property(x => x.Permissions)
.HasConversion(arrayConvertor);
modelBuilder.Entity()
.Property(x => x.Status)
.HasConversion();
modelBuilder.Entity