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

Support for CHECK constraints #14673

Merged
merged 1 commit into from
Apr 18, 2019
Merged
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
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