diff --git a/src/Migrations/Constants.cs b/src/Migrations/Constants.cs index 99c0773..84cad55 100644 --- a/src/Migrations/Constants.cs +++ b/src/Migrations/Constants.cs @@ -12,38 +12,39 @@ public static class Constants { public const string CreateOrAlterFunctionPattern = """ CREATE OR ALTER FUNCTION {0}.{1}({2}) - RETURNS {3} - AS - BEGIN - {4} - END + RETURNS {3} +AS +BEGIN +{4} +END """; public const string CreateOrAlterViewPattern = """ - CREATE OR ALTER VIEW {0}.{1} - AS - {2} - """; + CREATE OR ALTER VIEW {0}.{ 1} +AS +{ 2} +"""; public const string CreateOrAlterProcedurePattern = """ - CREATE OR ALTER PROCEDURE {0}.{1} {2} - AS - BEGIN - {3} - END + CREATE OR ALTER PROCEDURE {0}.{ 1} +{ 2} +AS +BEGIN +{3} +END """; - public const string Function = "FUNCTION"; - public const string Procedure = "PROCEDURE"; - public const string View = "VIEW"; - - public const string DropFunctionIfExistsPattern = "DROP FUNCTION IF EXISTS {0}.{1}"; - public const string DropProcedureIfExistsPattern = "DROP PROCEDURE IF EXISTS {0}.{1}"; - public const string DropViewIfExistsPattern = "DROP VIEW IF EXISTS {0}.{1}"; - public const string DropSomethingIfExistsPattern = "DROP {0} IF EXISTS {1}.{2}"; - - public static readonly IReadOnlyDictionary ClrTypeToSqlTypeMap = new Dictionary< - Type, - string - > + public const string Function = "FUNCTION"; +public const string Procedure = "PROCEDURE"; +public const string View = "VIEW"; + +public const string DropFunctionIfExistsPattern = "DROP FUNCTION IF EXISTS {0}.{1}"; +public const string DropProcedureIfExistsPattern = "DROP PROCEDURE IF EXISTS {0}.{1}"; +public const string DropViewIfExistsPattern = "DROP VIEW IF EXISTS {0}.{1}"; +public const string DropSomethingIfExistsPattern = "DROP {0} IF EXISTS {1}.{2}"; + +public static readonly IReadOnlyDictionary ClrTypeToSqlTypeMap = new Dictionary< + Type, + string +> { { typeof(string), NVarCharMax.ShortName }, { typeof(int), Int.ShortName }, diff --git a/src/Migrations/CreateFunctionOperation.cs b/src/Migrations/CreateFunctionOperation.cs index 7f9e5c1..4548f00 100644 --- a/src/Migrations/CreateFunctionOperation.cs +++ b/src/Migrations/CreateFunctionOperation.cs @@ -5,31 +5,32 @@ namespace Dgmjr.EntityFrameworkCore.Migrations; public class CreateFunctionOperation(string schema, string name, SqlArgument[] arguments, string returnType, string body) : SqlOperation { - public string Schema { get; set; } = schema; - public string Name { get; set; } = name; - public SqlArgument[] Arguments { get; set; } = arguments; - public string Body { get; set; } = body; - public string ReturnType { get; set; } = returnType; - - public override string Sql => - Format(CreateOrAlterFunctionPattern, Schema, Name, Arguments.Join(", "), ReturnType, Body); - - public CreateFunctionOperation( - MethodInfo mi, - string body, - string schema = DboSchema.ShortName, - string? name = default, - SqlArgument[]? arguments = default - ) - : this( - schema, - name ?? mi.Name, - arguments - ?? mi.GetParameters() - // .Select(p => $"@{p.Name} {ClrTypeToSqlTypeMap[p.ParameterType]}").ToArray(), - .Select(arg => (SqlArgument)arg) - .ToArray(), - ClrTypeToSqlTypeMap[mi.ReturnType], - body - ) { } + public string Schema { get; set; } = schema; +public string Name { get; set; } = name; +public SqlArgument[] Arguments { get; set; } = arguments; +public string Body { get; set; } = body; +public string ReturnType { get; set; } = returnType; + +public override string Sql => + Format(CreateOrAlterFunctionPattern, Schema, Name, Arguments.Join(", "), ReturnType, Body); + +public CreateFunctionOperation( + MethodInfo mi, + string body, + string schema = DboSchema.ShortName, + string? name = default, + SqlArgument[]? arguments = default +) + : this( + schema, + name ?? mi.Name, + arguments + ?? mi.GetParameters() + // .Select(p => $"@{p.Name} {ClrTypeToSqlTypeMap[p.ParameterType]}").ToArray(), + .Select(arg => (SqlArgument)arg) + .ToArray(), + ClrTypeToSqlTypeMap[mi.ReturnType], + body + ) +{ } } diff --git a/src/Migrations/CreateProcedureOperation.cs b/src/Migrations/CreateProcedureOperation.cs index cd94493..ec47755 100644 --- a/src/Migrations/CreateProcedureOperation.cs +++ b/src/Migrations/CreateProcedureOperation.cs @@ -5,28 +5,29 @@ namespace Dgmjr.EntityFrameworkCore.Migrations; public class CreateProcedureOperation(string schema, string name, SqlArgument[] arguments, string body) : SqlOperation { - public string Schema { get; set; } = schema; - public string Name { get; set; } = name; - public SqlArgument[] Arguments { get; set; } = arguments; - public string Body { get; set; } = body; - - public override string Sql => - Format(CreateOrAlterProcedurePattern, Schema, Name, Arguments.Join(", "), Body); - - public CreateProcedureOperation( - MethodInfo mi, - string body, - string schema = DboSchema.ShortName, - string? name = default, - SqlArgument[]? arguments = default - ) - : this( - schema, - name ?? mi.Name, - arguments - ?? mi.GetParameters() - .Select(arg => (SqlArgument)arg) - .ToArray(), - body - ) { } + public string Schema { get; set; } = schema; +public string Name { get; set; } = name; +public SqlArgument[] Arguments { get; set; } = arguments; +public string Body { get; set; } = body; + +public override string Sql => + Format(CreateOrAlterProcedurePattern, Schema, Name, Arguments.Join(", "), Body); + +public CreateProcedureOperation( + MethodInfo mi, + string body, + string schema = DboSchema.ShortName, + string? name = default, + SqlArgument[]? arguments = default +) + : this( + schema, + name ?? mi.Name, + arguments + ?? mi.GetParameters() + .Select(arg => (SqlArgument)arg) + .ToArray(), + body + ) +{ } } diff --git a/src/Migrations/CreateViewOperation.cs b/src/Migrations/CreateViewOperation.cs index 2e5c9d8..75687e1 100644 --- a/src/Migrations/CreateViewOperation.cs +++ b/src/Migrations/CreateViewOperation.cs @@ -2,9 +2,9 @@ namespace Dgmjr.EntityFrameworkCore.Migrations; public class CreateViewOperation(string schema, string name, string selectStatement) : SqlOperation { - public string Schema { get; set; } = schema; - public string Name { get; set; } = name; - public string SelectStatement { get; set; } = selectStatement; - - public override string Sql => Format(CreateOrAlterViewPattern, Schema, Name, SelectStatement); + public string Schema { get; set; } = schema; +public string Name { get; set; } = name; +public string SelectStatement { get; set; } = selectStatement; + +public override string Sql => Format(CreateOrAlterViewPattern, Schema, Name, SelectStatement); } diff --git a/src/Migrations/CustomSqlMigrationsGenerator.cs b/src/Migrations/CustomSqlMigrationsGenerator.cs index 9083d5f..a27cf95 100644 --- a/src/Migrations/CustomSqlMigrationsGenerator.cs +++ b/src/Migrations/CustomSqlMigrationsGenerator.cs @@ -25,41 +25,41 @@ ICommandBatchPreparer commandBatchPreparer #error "Unsupported target framework" #endif { - private ISqlGenerationHelper SqlHelper => Dependencies.SqlGenerationHelper; - - protected override void Generate( - MigrationOperation operation, - IModel? model, - MigrationCommandListBuilder builder - ) - { - if (operation is CreateFunctionOperation cfo) - { - builder.Append(cfo.Sql).Append(SqlHelper.StatementTerminator).EndCommand(); - } - else if (operation is DropFunctionOperation dfo) - { - builder.Append(dfo.Sql).Append(SqlHelper.StatementTerminator).EndCommand(); - } - else if (operation is DropViewOperation dvo) - { - builder.Append(dvo.Sql).Append(SqlHelper.StatementTerminator).EndCommand(); - } - else if (operation is CreateViewOperation cvo) - { - builder.Append(cvo.Sql).Append(SqlHelper.StatementTerminator).EndCommand(); - } - else if (operation is DropOperation dropop) - { - builder.Append(dropop.Sql).Append(SqlHelper.StatementTerminator).EndCommand(); - } - else if (operation is CreateProcedureOperation cpop) - { - builder.Append(cpop.Sql).Append(SqlHelper.StatementTerminator).EndCommand(); - } - else - { - base.Generate(operation, model, builder); - } - } + private ISqlGenerationHelper SqlHelper => Dependencies.SqlGenerationHelper; + +protected override void Generate( + MigrationOperation operation, + IModel? model, + MigrationCommandListBuilder builder +) +{ + if (operation is CreateFunctionOperation cfo) + { + builder.Append(cfo.Sql).Append(SqlHelper.StatementTerminator).EndCommand(); + } + else if (operation is DropFunctionOperation dfo) + { + builder.Append(dfo.Sql).Append(SqlHelper.StatementTerminator).EndCommand(); + } + else if (operation is DropViewOperation dvo) + { + builder.Append(dvo.Sql).Append(SqlHelper.StatementTerminator).EndCommand(); + } + else if (operation is CreateViewOperation cvo) + { + builder.Append(cvo.Sql).Append(SqlHelper.StatementTerminator).EndCommand(); + } + else if (operation is DropOperation dropop) + { + builder.Append(dropop.Sql).Append(SqlHelper.StatementTerminator).EndCommand(); + } + else if (operation is CreateProcedureOperation cpop) + { + builder.Append(cpop.Sql).Append(SqlHelper.StatementTerminator).EndCommand(); + } + else + { + base.Generate(operation, model, builder); + } +} } diff --git a/src/Migrations/DropOperation.cs b/src/Migrations/DropOperation.cs index 13630f3..54a0be9 100644 --- a/src/Migrations/DropOperation.cs +++ b/src/Migrations/DropOperation.cs @@ -2,10 +2,10 @@ namespace Dgmjr.EntityFrameworkCore.Migrations; public class DropOperation(string thingKind, string schema, string name) : SqlOperation { - public string ThingKind { get; set; } = thingKind; - public string Schema { get; set; } = schema; - public string Name { get; set; } = name; - public override bool IsDestructiveChange => true; - - public override string Sql => Format(DropSomethingIfExistsPattern, ThingKind, Schema, Name); + public string ThingKind { get; set; } = thingKind; +public string Schema { get; set; } = schema; +public string Name { get; set; } = name; +public override bool IsDestructiveChange => true; + +public override string Sql => Format(DropSomethingIfExistsPattern, ThingKind, Schema, Name); } diff --git a/src/Models/Slug.cs b/src/Models/Slug.cs index 30a157e..98ba3e2 100644 --- a/src/Models/Slug.cs +++ b/src/Models/Slug.cs @@ -6,48 +6,48 @@ namespace Dgmjr.EntityFrameworkCore; public readonly partial struct Slug(string? Value = default) : IComparable, IEquatable, IComparable { public Slug() - : this(NewSlug()) { } - - public string Value { get; init; } = Value ?? NewSlug(); - - public int CompareTo(Slug other) - => Compare(Value, other.Value, OrdinalIgnoreCase); - - public int CompareTo(object obj) - => obj is Slug other ? CompareTo(other) : 1; - - public static string NewSlug() - => guid.NewGuid().ToString("N").Substring(0, 6).ToLowerInvariant(); - - public override string ToString() - => Value; - - public bool Equals(Slug other) - => CompareTo(other) == 0; - - public override bool Equals(object? obj) - => obj is Slug other && Equals(other); - - public override int GetHashCode() - => Value.GetHashCode(); - - public static bool operator==(Slug left, Slug right) - => left.Equals(right); - - public static bool operator!=(Slug left, Slug right) - => !left.Equals(right); - - public static bool operator<(Slug left, Slug right) - => left.CompareTo(right) < 0; - - public static bool operator<=(Slug left, Slug right) - => left.CompareTo(right) <= 0; - - public static bool operator>(Slug left, Slug right) - => left.CompareTo(right) > 0; - - public static bool operator>=(Slug left, Slug right) - => left.CompareTo(right) >= 0; + : this(NewSlug()) { } + +public string Value { get; init; } = Value ?? NewSlug(); + +public int CompareTo(Slug other) + => Compare(Value, other.Value, OrdinalIgnoreCase); + +public int CompareTo(object obj) + => obj is Slug other ? CompareTo(other) : 1; + +public static string NewSlug() + => guid.NewGuid().ToString("N").Substring(0, 6).ToLowerInvariant(); + +public override string ToString() + => Value; + +public bool Equals(Slug other) + => CompareTo(other) == 0; + +public override bool Equals(object? obj) + => obj is Slug other && Equals(other); + +public override int GetHashCode() + => Value.GetHashCode(); + +public static bool operator ==(Slug left, Slug right) + => left.Equals(right); + +public static bool operator !=(Slug left, Slug right) + => !left.Equals(right); + +public static bool operator <(Slug left, Slug right) + => left.CompareTo(right) < 0; + +public static bool operator <=(Slug left, Slug right) + => left.CompareTo(right) <= 0; + +public static bool operator >(Slug left, Slug right) + => left.CompareTo(right) > 0; + +public static bool operator >=(Slug left, Slug right) + => left.CompareTo(right) >= 0; } public class SlugEfCoreConverter : ValueConverter