migrate database providers to core framework
This commit is contained in:
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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user