migrate database providers to core framework
This commit is contained in:
114
Oqtane.Server/Databases/MySQL/MySQLDatabase.cs
Normal file
114
Oqtane.Server/Databases/MySQL/MySQLDatabase.cs
Normal file
@ -0,0 +1,114 @@
|
||||
using System.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations.Operations;
|
||||
using Microsoft.EntityFrameworkCore.Migrations.Operations.Builders;
|
||||
using MySql.Data.MySqlClient;
|
||||
using Oqtane.Databases;
|
||||
|
||||
namespace Oqtane.Database.MySQL
|
||||
{
|
||||
public class MySQLDatabase : DatabaseBase
|
||||
{
|
||||
private static string _friendlyName => "MySQL";
|
||||
|
||||
private static string _name => "MySQL";
|
||||
|
||||
static MySQLDatabase()
|
||||
{
|
||||
Initialize(typeof(MySQLDatabase));
|
||||
}
|
||||
|
||||
public MySQLDatabase() :base(_name, _friendlyName) { }
|
||||
|
||||
public override string Provider => "Pomelo.EntityFrameworkCore.MySql";
|
||||
|
||||
public override OperationBuilder<AddColumnOperation> AddAutoIncrementColumn(ColumnsBuilder table, string name)
|
||||
{
|
||||
return table.Column<int>(name: name, nullable: false).Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn);
|
||||
}
|
||||
|
||||
public override string ConcatenateSql(params string[] values)
|
||||
{
|
||||
var returnValue = "CONCAT(";
|
||||
for (var i = 0; i < values.Length; i++)
|
||||
{
|
||||
if (i > 0)
|
||||
{
|
||||
returnValue += ",";
|
||||
}
|
||||
returnValue += values[i];
|
||||
}
|
||||
|
||||
returnValue += ")";
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
public override int ExecuteNonQuery(string connectionString, string query)
|
||||
{
|
||||
var conn = new MySqlConnection(connectionString);
|
||||
var cmd = conn.CreateCommand();
|
||||
using (conn)
|
||||
{
|
||||
PrepareCommand(conn, cmd, query);
|
||||
var val = -1;
|
||||
try
|
||||
{
|
||||
val = cmd.ExecuteNonQuery();
|
||||
}
|
||||
catch
|
||||
{
|
||||
// an error occurred executing the query
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override IDataReader ExecuteReader(string connectionString, string query)
|
||||
{
|
||||
var conn = new MySqlConnection(connectionString);
|
||||
var cmd = conn.CreateCommand();
|
||||
PrepareCommand(conn, cmd, query);
|
||||
var dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
|
||||
return dr;
|
||||
}
|
||||
|
||||
public override string DelimitName(string name)
|
||||
{
|
||||
return $"`{name}`";
|
||||
}
|
||||
|
||||
public override string RewriteValue(object value)
|
||||
{
|
||||
var type = value.GetType().Name;
|
||||
if (type == "DateTime")
|
||||
{
|
||||
return $"'{value}'";
|
||||
}
|
||||
if (type == "Boolean")
|
||||
{
|
||||
return (bool)value ? "1" : "0"; // MySQL uses 1/0 for boolean values
|
||||
}
|
||||
return value.ToString();
|
||||
}
|
||||
|
||||
public override DbContextOptionsBuilder UseDatabase(DbContextOptionsBuilder optionsBuilder, string connectionString)
|
||||
{
|
||||
return optionsBuilder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
|
||||
}
|
||||
|
||||
private void PrepareCommand(MySqlConnection conn, MySqlCommand cmd, string query)
|
||||
{
|
||||
if (conn.State != ConnectionState.Open)
|
||||
{
|
||||
conn.Open();
|
||||
}
|
||||
|
||||
cmd.Connection = conn;
|
||||
cmd.CommandText = query;
|
||||
cmd.CommandType = CommandType.Text;
|
||||
}
|
||||
}
|
||||
}
|
49
Oqtane.Server/Databases/PostgreSQL/HistoryRepository.cs
Normal file
49
Oqtane.Server/Databases/PostgreSQL/HistoryRepository.cs
Normal file
@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Migrations.Internal;
|
||||
using Oqtane.Migrations.Framework;
|
||||
using Oqtane.Models;
|
||||
|
||||
// ReSharper disable ClassNeverInstantiated.Global
|
||||
|
||||
namespace Oqtane.Database.PostgreSQL
|
||||
{
|
||||
public class HistoryRepository : NpgsqlHistoryRepository
|
||||
{
|
||||
private string _appliedDateColumnName = "applied_date";
|
||||
private string _appliedVersionColumnName = "applied_version";
|
||||
private MigrationHistoryTable _migrationHistoryTable;
|
||||
|
||||
public HistoryRepository(HistoryRepositoryDependencies dependencies) : base(dependencies)
|
||||
{
|
||||
_migrationHistoryTable = new MigrationHistoryTable
|
||||
{
|
||||
TableName = TableName,
|
||||
TableSchema = TableSchema,
|
||||
MigrationIdColumnName = MigrationIdColumnName,
|
||||
ProductVersionColumnName = ProductVersionColumnName,
|
||||
AppliedVersionColumnName = _appliedVersionColumnName,
|
||||
AppliedDateColumnName = _appliedDateColumnName
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
protected override void ConfigureTable(EntityTypeBuilder<HistoryRow> history)
|
||||
{
|
||||
base.ConfigureTable(history);
|
||||
history.Property<string>(_appliedVersionColumnName).HasMaxLength(10);
|
||||
history.Property<DateTime>(_appliedDateColumnName);
|
||||
}
|
||||
|
||||
public override string GetInsertScript(HistoryRow row)
|
||||
{
|
||||
if (row == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(row));
|
||||
}
|
||||
|
||||
return MigrationUtils.BuildInsertScript(row, Dependencies, _migrationHistoryTable);
|
||||
}
|
||||
}
|
||||
}
|
165
Oqtane.Server/Databases/PostgreSQL/PostgreSQLDatabase.cs
Normal file
165
Oqtane.Server/Databases/PostgreSQL/PostgreSQLDatabase.cs
Normal file
@ -0,0 +1,165 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Globalization;
|
||||
using EFCore.NamingConventions.Internal;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Migrations.Operations;
|
||||
using Microsoft.EntityFrameworkCore.Migrations.Operations.Builders;
|
||||
using Npgsql;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
using Oqtane.Databases;
|
||||
|
||||
namespace Oqtane.Database.PostgreSQL
|
||||
{
|
||||
public class PostgreSQLDatabase : DatabaseBase
|
||||
{
|
||||
private static string _friendlyName => "PostgreSQL";
|
||||
|
||||
private static string _name => "PostgreSQL";
|
||||
|
||||
private readonly INameRewriter _rewriter;
|
||||
|
||||
static PostgreSQLDatabase()
|
||||
{
|
||||
Initialize(typeof(PostgreSQLDatabase));
|
||||
}
|
||||
|
||||
public PostgreSQLDatabase() : base(_name, _friendlyName)
|
||||
{
|
||||
_rewriter = new SnakeCaseNameRewriter(CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
public override string Provider => "Npgsql.EntityFrameworkCore.PostgreSQL";
|
||||
|
||||
public override OperationBuilder<AddColumnOperation> AddAutoIncrementColumn(ColumnsBuilder table, string name)
|
||||
{
|
||||
return table.Column<int>(name: name, nullable: false).Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityAlwaysColumn);
|
||||
}
|
||||
|
||||
public override string ConcatenateSql(params string[] values)
|
||||
{
|
||||
var returnValue = String.Empty;
|
||||
for (var i = 0; i < values.Length; i++)
|
||||
{
|
||||
if (i > 0)
|
||||
{
|
||||
returnValue += " || ";
|
||||
}
|
||||
returnValue += values[i];
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
public override int ExecuteNonQuery(string connectionString, string query)
|
||||
{
|
||||
var conn = new NpgsqlConnection(connectionString);
|
||||
var cmd = conn.CreateCommand();
|
||||
using (conn)
|
||||
{
|
||||
PrepareCommand(conn, cmd, query);
|
||||
var val = -1;
|
||||
try
|
||||
{
|
||||
val = cmd.ExecuteNonQuery();
|
||||
}
|
||||
catch
|
||||
{
|
||||
// an error occurred executing the query
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override IDataReader ExecuteReader(string connectionString, string query)
|
||||
{
|
||||
var conn = new NpgsqlConnection(connectionString);
|
||||
var cmd = conn.CreateCommand();
|
||||
PrepareCommand(conn, cmd, query);
|
||||
var dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
|
||||
return dr;
|
||||
}
|
||||
|
||||
public override string RewriteName(string name)
|
||||
{
|
||||
return _rewriter.RewriteName(name);
|
||||
}
|
||||
|
||||
public override string DelimitName(string name)
|
||||
{
|
||||
return $"\"{name}\"";
|
||||
}
|
||||
|
||||
public override string RewriteValue(object value)
|
||||
{
|
||||
var type = value.GetType().Name;
|
||||
if (type == "DateTime")
|
||||
{
|
||||
return $"'{value}'";
|
||||
}
|
||||
if (type == "Boolean")
|
||||
{
|
||||
return (bool)value ? "true" : "false"; // PostgreSQL uses true/false for boolean values
|
||||
}
|
||||
return value.ToString();
|
||||
}
|
||||
|
||||
public override void UpdateIdentityStoreTableNames(ModelBuilder builder)
|
||||
{
|
||||
foreach(var entity in builder.Model.GetEntityTypes())
|
||||
{
|
||||
var tableName = entity.GetTableName();
|
||||
if (tableName.StartsWith("AspNetUser"))
|
||||
{
|
||||
// replace table name
|
||||
entity.SetTableName(RewriteName(entity.GetTableName()));
|
||||
|
||||
// replace column names
|
||||
foreach(var property in entity.GetProperties())
|
||||
{
|
||||
property.SetColumnName(RewriteName(property.Name));
|
||||
}
|
||||
|
||||
// replace key names
|
||||
foreach(var key in entity.GetKeys())
|
||||
{
|
||||
key.SetName(RewriteName(key.GetName()));
|
||||
}
|
||||
|
||||
// replace foreign key names
|
||||
foreach (var key in entity.GetForeignKeys())
|
||||
{
|
||||
key.PrincipalKey.SetName(RewriteName(key.PrincipalKey.GetName()));
|
||||
}
|
||||
|
||||
// replace index names
|
||||
foreach (var index in entity.GetIndexes())
|
||||
{
|
||||
index.SetDatabaseName(RewriteName(index.GetDatabaseName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override DbContextOptionsBuilder UseDatabase(DbContextOptionsBuilder optionsBuilder, string connectionString)
|
||||
{
|
||||
return optionsBuilder.UseNpgsql(connectionString)
|
||||
.UseSnakeCaseNamingConvention()
|
||||
.ReplaceService<IHistoryRepository, HistoryRepository>();
|
||||
}
|
||||
|
||||
private void PrepareCommand(NpgsqlConnection conn, NpgsqlCommand cmd, string query)
|
||||
{
|
||||
if (conn.State != ConnectionState.Open)
|
||||
{
|
||||
conn.Open();
|
||||
}
|
||||
|
||||
cmd.Connection = conn;
|
||||
cmd.CommandText = query;
|
||||
cmd.CommandType = CommandType.Text;
|
||||
}
|
||||
}
|
||||
}
|
49
Oqtane.Server/Databases/SqlServer/HistoryRepository.cs
Normal file
49
Oqtane.Server/Databases/SqlServer/HistoryRepository.cs
Normal file
@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.SqlServer.Migrations.Internal;
|
||||
using Oqtane.Migrations.Framework;
|
||||
using Oqtane.Models;
|
||||
|
||||
// ReSharper disable ClassNeverInstantiated.Global
|
||||
|
||||
namespace Oqtane.Database.SqlServer
|
||||
{
|
||||
public class HistoryRepository : SqlServerHistoryRepository
|
||||
{
|
||||
private string _appliedDateColumnName = "AppliedDate";
|
||||
private string _appliedVersionColumnName = "AppliedVersion";
|
||||
private MigrationHistoryTable _migrationHistoryTable;
|
||||
|
||||
public HistoryRepository(HistoryRepositoryDependencies dependencies) : base(dependencies)
|
||||
{
|
||||
_migrationHistoryTable = new MigrationHistoryTable
|
||||
{
|
||||
TableName = TableName,
|
||||
TableSchema = TableSchema,
|
||||
MigrationIdColumnName = MigrationIdColumnName,
|
||||
ProductVersionColumnName = ProductVersionColumnName,
|
||||
AppliedVersionColumnName = _appliedVersionColumnName,
|
||||
AppliedDateColumnName = _appliedDateColumnName
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
protected override void ConfigureTable(EntityTypeBuilder<HistoryRow> history)
|
||||
{
|
||||
base.ConfigureTable(history);
|
||||
history.Property<string>(_appliedVersionColumnName).HasMaxLength(10);
|
||||
history.Property<DateTime>(_appliedDateColumnName);
|
||||
}
|
||||
|
||||
public override string GetInsertScript(HistoryRow row)
|
||||
{
|
||||
if (row == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(row));
|
||||
}
|
||||
|
||||
return MigrationUtils.BuildInsertScript(row, Dependencies, _migrationHistoryTable);
|
||||
}
|
||||
}
|
||||
}
|
120
Oqtane.Server/Databases/SqlServer/SqlServerDatabase.cs
Normal file
120
Oqtane.Server/Databases/SqlServer/SqlServerDatabase.cs
Normal file
@ -0,0 +1,120 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Migrations.Operations;
|
||||
using Microsoft.EntityFrameworkCore.Migrations.Operations.Builders;
|
||||
using Oqtane.Databases;
|
||||
|
||||
namespace Oqtane.Database.SqlServer
|
||||
{
|
||||
public class SqlServerDatabase : DatabaseBase
|
||||
{
|
||||
private static string _friendlyName => "SQL Server";
|
||||
|
||||
private static string _name => "SqlServer";
|
||||
|
||||
static SqlServerDatabase()
|
||||
{
|
||||
Initialize(typeof(SqlServerDatabase));
|
||||
}
|
||||
|
||||
public SqlServerDatabase() : base(_name, _friendlyName)
|
||||
{
|
||||
}
|
||||
|
||||
public override string Provider => "Microsoft.EntityFrameworkCore.SqlServer";
|
||||
|
||||
public override OperationBuilder<AddColumnOperation> AddAutoIncrementColumn(ColumnsBuilder table, string name)
|
||||
{
|
||||
return table.Column<int>(name: name, nullable: false).Annotation("SqlServer:Identity", "1, 1");
|
||||
}
|
||||
|
||||
public override void AlterStringColumn(MigrationBuilder builder, string name, string table, int length, bool nullable, bool unicode, string index)
|
||||
{
|
||||
var elements = index.Split(':', StringSplitOptions.RemoveEmptyEntries);
|
||||
if (elements.Length != 0)
|
||||
{
|
||||
builder.DropIndex(elements[0], table);
|
||||
}
|
||||
builder.AlterColumn<string>(name, table, maxLength: length, nullable: nullable, unicode: unicode);
|
||||
if (elements.Length != 0)
|
||||
{
|
||||
var columns = elements[1].Split(',');
|
||||
builder.CreateIndex(elements[0], table, columns, null, bool.Parse(elements[2]), null);
|
||||
}
|
||||
}
|
||||
|
||||
public override string DelimitName(string name)
|
||||
{
|
||||
return $"[{name}]";
|
||||
}
|
||||
|
||||
public override string RewriteValue(object value)
|
||||
{
|
||||
var type = value.GetType().Name;
|
||||
if (type == "DateTime")
|
||||
{
|
||||
return $"'{value}'";
|
||||
}
|
||||
if (type == "Boolean")
|
||||
{
|
||||
return (bool)value ? "1" : "0"; // SQL Server uses 1/0 for boolean values
|
||||
}
|
||||
return value.ToString();
|
||||
}
|
||||
|
||||
public override int ExecuteNonQuery(string connectionString, string query)
|
||||
{
|
||||
var conn = new SqlConnection(FormatConnectionString(connectionString));
|
||||
var cmd = conn.CreateCommand();
|
||||
using (conn)
|
||||
{
|
||||
PrepareCommand(conn, cmd, query);
|
||||
var val = -1;
|
||||
try
|
||||
{
|
||||
val = cmd.ExecuteNonQuery();
|
||||
}
|
||||
catch
|
||||
{
|
||||
// an error occurred executing the query
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override IDataReader ExecuteReader(string connectionString, string query)
|
||||
{
|
||||
var conn = new SqlConnection(FormatConnectionString(connectionString));
|
||||
var cmd = conn.CreateCommand();
|
||||
PrepareCommand(conn, cmd, query);
|
||||
var dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
|
||||
return dr;
|
||||
}
|
||||
|
||||
public override DbContextOptionsBuilder UseDatabase(DbContextOptionsBuilder optionsBuilder, string connectionString)
|
||||
{
|
||||
return optionsBuilder.UseSqlServer(connectionString)
|
||||
.ReplaceService<IHistoryRepository, HistoryRepository>();
|
||||
}
|
||||
|
||||
private string FormatConnectionString(string connectionString)
|
||||
{
|
||||
return connectionString.Replace("|DataDirectory|", AppDomain.CurrentDomain.GetData("DataDirectory").ToString());
|
||||
}
|
||||
|
||||
private void PrepareCommand(SqlConnection conn, SqlCommand cmd, string query)
|
||||
{
|
||||
if (conn.State != ConnectionState.Open)
|
||||
{
|
||||
conn.Open();
|
||||
}
|
||||
cmd.Connection = conn;
|
||||
cmd.CommandText = query;
|
||||
cmd.CommandType = CommandType.Text;
|
||||
}
|
||||
}
|
||||
}
|
49
Oqtane.Server/Databases/Sqlite/HistoryRepository.cs
Normal file
49
Oqtane.Server/Databases/Sqlite/HistoryRepository.cs
Normal file
@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Sqlite.Migrations.Internal;
|
||||
using Oqtane.Migrations.Framework;
|
||||
using Oqtane.Models;
|
||||
|
||||
// ReSharper disable ClassNeverInstantiated.Global
|
||||
|
||||
namespace Oqtane.Database.Sqlite
|
||||
{
|
||||
public class HistoryRepository : SqliteHistoryRepository
|
||||
{
|
||||
private string _appliedDateColumnName = "AppliedDate";
|
||||
private string _appliedVersionColumnName = "AppliedVersion";
|
||||
private MigrationHistoryTable _migrationHistoryTable;
|
||||
|
||||
public HistoryRepository(HistoryRepositoryDependencies dependencies) : base(dependencies)
|
||||
{
|
||||
_migrationHistoryTable = new MigrationHistoryTable
|
||||
{
|
||||
TableName = TableName,
|
||||
TableSchema = TableSchema,
|
||||
MigrationIdColumnName = MigrationIdColumnName,
|
||||
ProductVersionColumnName = ProductVersionColumnName,
|
||||
AppliedVersionColumnName = _appliedVersionColumnName,
|
||||
AppliedDateColumnName = _appliedDateColumnName
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
protected override void ConfigureTable(EntityTypeBuilder<HistoryRow> history)
|
||||
{
|
||||
base.ConfigureTable(history);
|
||||
history.Property<string>(_appliedVersionColumnName).HasMaxLength(10);
|
||||
history.Property<DateTime>(_appliedDateColumnName);
|
||||
}
|
||||
|
||||
public override string GetInsertScript(HistoryRow row)
|
||||
{
|
||||
if (row == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(row));
|
||||
}
|
||||
|
||||
return MigrationUtils.BuildInsertScript(row, Dependencies, _migrationHistoryTable);
|
||||
}
|
||||
}
|
||||
}
|
123
Oqtane.Server/Databases/Sqlite/SqliteDatabase.cs
Normal file
123
Oqtane.Server/Databases/Sqlite/SqliteDatabase.cs
Normal file
@ -0,0 +1,123 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Migrations.Operations;
|
||||
using Microsoft.EntityFrameworkCore.Migrations.Operations.Builders;
|
||||
using Oqtane.Databases;
|
||||
|
||||
namespace Oqtane.Database.Sqlite
|
||||
{
|
||||
public class SqliteDatabase : DatabaseBase
|
||||
{
|
||||
private static string _friendlyName => "SQLite";
|
||||
|
||||
private static string _name => "Sqlite";
|
||||
|
||||
static SqliteDatabase()
|
||||
{
|
||||
Initialize(typeof(SqliteDatabase));
|
||||
}
|
||||
|
||||
public SqliteDatabase() :base(_name, _friendlyName) { }
|
||||
|
||||
public override string Provider => "Microsoft.EntityFrameworkCore.Sqlite";
|
||||
|
||||
public override OperationBuilder<AddColumnOperation> AddAutoIncrementColumn(ColumnsBuilder table, string name)
|
||||
{
|
||||
return table.Column<int>(name: name, nullable: false).Annotation("Sqlite:Autoincrement", true);
|
||||
}
|
||||
|
||||
public override void DropColumn(MigrationBuilder builder, string name, string table)
|
||||
{
|
||||
// not implemented as SQLite does not support dropping columns
|
||||
}
|
||||
|
||||
public override void AlterStringColumn(MigrationBuilder builder, string name, string table, int length, bool nullable, bool unicode, string index)
|
||||
{
|
||||
// not implemented as SQLite does not support altering columns
|
||||
}
|
||||
|
||||
public override string ConcatenateSql(params string[] values)
|
||||
{
|
||||
var returnValue = String.Empty;
|
||||
for (var i = 0; i < values.Length; i++)
|
||||
{
|
||||
if (i > 0)
|
||||
{
|
||||
returnValue += " || ";
|
||||
}
|
||||
returnValue += values[i];
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
public override int ExecuteNonQuery(string connectionString, string query)
|
||||
{
|
||||
var conn = new SqliteConnection(connectionString);
|
||||
var cmd = conn.CreateCommand();
|
||||
using (conn)
|
||||
{
|
||||
PrepareCommand(conn, cmd, query);
|
||||
var val = -1;
|
||||
try
|
||||
{
|
||||
val = cmd.ExecuteNonQuery();
|
||||
}
|
||||
catch
|
||||
{
|
||||
// an error occurred executing the query
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override IDataReader ExecuteReader(string connectionString, string query)
|
||||
{
|
||||
var conn = new SqliteConnection(connectionString);
|
||||
var cmd = conn.CreateCommand();
|
||||
PrepareCommand(conn, cmd, query);
|
||||
var dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
|
||||
return dr;
|
||||
}
|
||||
|
||||
public override string DelimitName(string name)
|
||||
{
|
||||
return $"\"{name}\"";
|
||||
}
|
||||
|
||||
public override string RewriteValue(object value)
|
||||
{
|
||||
var type = value.GetType().Name;
|
||||
if (type == "DateTime")
|
||||
{
|
||||
return $"'{value}'";
|
||||
}
|
||||
if (type == "Boolean")
|
||||
{
|
||||
return (bool)value ? "1" : "0"; // SQLite uses 1/0 for boolean values
|
||||
}
|
||||
return value.ToString();
|
||||
}
|
||||
|
||||
public override DbContextOptionsBuilder UseDatabase(DbContextOptionsBuilder optionsBuilder, string connectionString)
|
||||
{
|
||||
return optionsBuilder.UseSqlite(connectionString)
|
||||
.ReplaceService<IHistoryRepository, HistoryRepository>();
|
||||
}
|
||||
|
||||
private void PrepareCommand(SqliteConnection conn, SqliteCommand cmd, string query)
|
||||
{
|
||||
if (conn.State != ConnectionState.Open)
|
||||
{
|
||||
conn.Open();
|
||||
}
|
||||
cmd.Connection = conn;
|
||||
cmd.CommandText = query;
|
||||
cmd.CommandType = CommandType.Text;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user