Skip to content

Commit

Permalink
Add CHECK constraint support
Browse files Browse the repository at this point in the history
Improved encapsulation for CheckConstraint annotations
  • Loading branch information
Muppets committed Apr 18, 2019
1 parent 1e3b018 commit c5a07e0
Show file tree
Hide file tree
Showing 26 changed files with 1,315 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,45 @@ protected virtual void Generate([NotNull] AddUniqueConstraintOperation operation
}
}

/// <summary>
/// Generates code for an <see cref="CreateCheckConstraintOperation" />.
/// </summary>
/// <param name="operation"> The operation. </param>
/// <param name="builder"> The builder code is added to. </param>
protected virtual void Generate([NotNull] CreateCheckConstraintOperation operation, [NotNull] IndentedStringBuilder builder)
{
Check.NotNull(operation, nameof(operation));
Check.NotNull(builder, nameof(builder));

builder.AppendLine(".CreateCheckConstraint(");

using (builder.Indent())
{
builder
.Append("name: ")
.Append(Code.Literal(operation.Name));

if (operation.Schema != null)
{
builder
.AppendLine(",")
.Append("schema: ")
.Append(Code.Literal(operation.Schema));
}

builder
.AppendLine(",")
.Append("table: ")
.Append(Code.Literal(operation.Table))
.AppendLine(",")
.Append("constraintSql: ")
.Append(Code.Literal(operation.ConstraintSql))
.Append(")");

Annotations(operation.GetAnnotations(), builder);
}
}

/// <summary>
/// Generates code for an <see cref="AlterColumnOperation" />.
/// </summary>
Expand Down Expand Up @@ -1039,6 +1078,23 @@ protected virtual void Generate([NotNull] CreateTableOperation operation, [NotNu
builder.AppendLine(";");
}

foreach (var checkConstraints in operation.CheckConstraints)
{
builder
.Append("table.CheckConstraint(")
.Append(Code.Literal(checkConstraints.Name))
.Append(", ")
.Append(Code.Literal(checkConstraints.ConstraintSql))
.Append(")");

using (builder.Indent())
{
Annotations(checkConstraints.GetAnnotations(), builder);
}

builder.AppendLine(";");
}

foreach (var foreignKey in operation.ForeignKeys)
{
builder.AppendLine("table.ForeignKey(");
Expand Down Expand Up @@ -1380,6 +1436,42 @@ protected virtual void Generate([NotNull] DropUniqueConstraintOperation operatio
}
}

/// <summary>
/// Generates code for a <see cref="DropCheckConstraintOperation" />.
/// </summary>
/// <param name="operation"> The operation. </param>
/// <param name="builder"> The builder code is added to. </param>
protected virtual void Generate([NotNull] DropCheckConstraintOperation operation, [NotNull] IndentedStringBuilder builder)
{
Check.NotNull(operation, nameof(operation));
Check.NotNull(builder, nameof(builder));

builder.AppendLine(".DropCheckConstraint(");

using (builder.Indent())
{
builder
.Append("name: ")
.Append(Code.Literal(operation.Name));

if (operation.Schema != null)
{
builder
.AppendLine(",")
.Append("schema: ")
.Append(Code.Literal(operation.Schema));
}

builder
.AppendLine(",")
.Append("table: ")
.Append(Code.Literal(operation.Table))
.Append(")");

Annotations(operation.GetAnnotations(), builder);
}
}

/// <summary>
/// Generates code for a <see cref="RenameColumnOperation" />.
/// </summary>
Expand Down
36 changes: 36 additions & 0 deletions src/EFCore.Relational/Metadata/ICheckConstraint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.EntityFrameworkCore.Metadata
{
/// <summary>
/// Represents a check constraint in the <see cref="IModel" />.
/// </summary>
public interface ICheckConstraint
{
/// <summary>
/// Gets the name of the check constraint in the database.
/// </summary>
string Name { get; }

/// <summary>
/// The database table that contains the check constraint.
/// </summary>
string Table { get; }

/// <summary>
/// The database table schema that contains the check constraint.
/// </summary>
string Schema { get; }

/// <summary>
/// The <see cref="IModel" /> in which this check constraint is defined.
/// </summary>
IModel Model { get; }

/// <summary>
/// Gets the constraint sql used in a check constraint in the database.
/// </summary>
string ConstraintSql { get; }
}
}
17 changes: 17 additions & 0 deletions src/EFCore.Relational/Metadata/IRelationalModelAnnotations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ public interface IRelationalModelAnnotations
/// </returns>
ISequence FindSequence([NotNull] string name, [CanBeNull] string schema = null);

/// <summary>
/// Finds an <see cref="ICheckConstraint" /> with the given name.
/// </summary>
/// <param name="name"> The check constraint name. </param>
/// <param name="table"> The table that contains the check constraint. </param>
/// <param name="schema"> The table schema that contains the check constraint. </param>
/// <returns>
/// The <see cref="ICheckConstraint" /> or <c>null</c> if no check constraint with the given name in
/// the given schema was found.
/// </returns>
ICheckConstraint FindCheckConstraint([NotNull] string name, [NotNull] string table, [CanBeNull] string schema = null);

/// <summary>
/// Finds a <see cref="IDbFunction" /> that is mapped to the method represented by the given <see cref="MethodInfo" />.
/// </summary>
Expand All @@ -36,6 +48,11 @@ public interface IRelationalModelAnnotations
/// </summary>
IReadOnlyList<ISequence> Sequences { get; }

/// <summary>
/// All <see cref="ICheckConstraint" />s contained in the model.
/// </summary>
IReadOnlyList<ICheckConstraint> CheckConstraints { get; }

/// <summary>
/// All <see cref="IDbFunction" />s contained in the model.
/// </summary>
Expand Down
Loading

0 comments on commit c5a07e0

Please sign in to comment.