Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: format code with dotnet-format #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 28 additions & 27 deletions src/Migrations/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Type, string> 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<Type, string> ClrTypeToSqlTypeMap = new Dictionary<
Type,
string
>
{
{ typeof(string), NVarCharMax.ShortName },
{ typeof(int), Int.ShortName },
Expand Down
55 changes: 28 additions & 27 deletions src/Migrations/CreateFunctionOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
{ }
}
49 changes: 25 additions & 24 deletions src/Migrations/CreateProcedureOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
{ }
}
10 changes: 5 additions & 5 deletions src/Migrations/CreateViewOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
74 changes: 37 additions & 37 deletions src/Migrations/CustomSqlMigrationsGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
12 changes: 6 additions & 6 deletions src/Migrations/DropOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
84 changes: 42 additions & 42 deletions src/Models/Slug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,48 @@ namespace Dgmjr.EntityFrameworkCore;
public readonly partial struct Slug(string? Value = default) : IComparable<Slug>, IEquatable<Slug>, 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<Slug, string>
Expand Down
Loading