Skip to content

Commit

Permalink
Add sproc support to relational model
Browse files Browse the repository at this point in the history
Add sproc support for Output and InputOutput parameters
Add sproc support to runtime and compiled models
Fixed property overrides not taking the default schema into account
Changed EntityType.ShortName() to not include the generic argument

Part of #245
  • Loading branch information
AndriySvyryd authored Jul 25, 2022
1 parent 5c6372a commit 41bd788
Show file tree
Hide file tree
Showing 76 changed files with 4,176 additions and 450 deletions.
3 changes: 1 addition & 2 deletions src/EFCore.Design/Design/Internal/CSharpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1358,8 +1358,7 @@ private static bool IsIdentifierPartCharacter(char ch)
return ch < 'A'
? ch >= '0'
&& ch <= '9'
: ch <= 'Z'
|| ch == '_';
: ch <= 'Z';
}

if (ch <= 'z')
Expand Down
3 changes: 2 additions & 1 deletion src/EFCore.Relational/Design/AnnotationCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public class AnnotationCodeGenerator : IAnnotationCodeGenerator
RelationalAnnotationNames.InsertStoredProcedure,
RelationalAnnotationNames.UpdateStoredProcedure,
RelationalAnnotationNames.MappingFragments,
RelationalAnnotationNames.RelationalOverrides
RelationalAnnotationNames.RelationalOverrides,
RelationalAnnotationNames.ParameterDirection
};

#region MethodInfos
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ public override void Generate(IEntityType entityType, CSharpRuntimeAnnotationCod
annotations.Remove(RelationalAnnotationNames.ViewMappings);
annotations.Remove(RelationalAnnotationNames.SqlQueryMappings);
annotations.Remove(RelationalAnnotationNames.FunctionMappings);
annotations.Remove(RelationalAnnotationNames.InsertStoredProcedureMappings);
annotations.Remove(RelationalAnnotationNames.DeleteStoredProcedureMappings);
annotations.Remove(RelationalAnnotationNames.UpdateStoredProcedureMappings);
annotations.Remove(RelationalAnnotationNames.DefaultMappings);
}
else
Expand Down Expand Up @@ -360,6 +363,42 @@ public override void Generate(IEntityType entityType, CSharpRuntimeAnnotationCod

GenerateSimpleAnnotation(RelationalAnnotationNames.Triggers, triggersVariable, parameters);
}

if (annotations.TryGetAndRemove(
RelationalAnnotationNames.InsertStoredProcedure,
out StoredProcedure insertStoredProcedure))
{
var sprocVariable = Dependencies.CSharpHelper.Identifier("insertSproc", parameters.ScopeVariables, capitalize: false);

Create(insertStoredProcedure, sprocVariable, parameters);

GenerateSimpleAnnotation(RelationalAnnotationNames.InsertStoredProcedure, sprocVariable, parameters);
parameters.MainBuilder.AppendLine();
}

if (annotations.TryGetAndRemove(
RelationalAnnotationNames.DeleteStoredProcedure,
out StoredProcedure deleteStoredProcedure))
{
var sprocVariable = Dependencies.CSharpHelper.Identifier("deleteSproc", parameters.ScopeVariables, capitalize: false);

Create(deleteStoredProcedure, sprocVariable, parameters);

GenerateSimpleAnnotation(RelationalAnnotationNames.DeleteStoredProcedure, sprocVariable, parameters);
parameters.MainBuilder.AppendLine();
}

if (annotations.TryGetAndRemove(
RelationalAnnotationNames.UpdateStoredProcedure,
out StoredProcedure updateStoredProcedure))
{
var sprocVariable = Dependencies.CSharpHelper.Identifier("updateSproc", parameters.ScopeVariables, capitalize: false);

Create(updateStoredProcedure, sprocVariable, parameters);

GenerateSimpleAnnotation(RelationalAnnotationNames.UpdateStoredProcedure, sprocVariable, parameters);
parameters.MainBuilder.AppendLine();
}
}

base.Generate(entityType, parameters);
Expand Down Expand Up @@ -439,6 +478,49 @@ private void Create(ITrigger trigger, string triggersVariable, CSharpRuntimeAnno
public virtual void Generate(ITrigger trigger, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
=> GenerateSimpleAnnotations(parameters);

private void Create(IStoredProcedure storedProcedure, string sprocVariable, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
{
AddNamespace(typeof(RuntimeStoredProcedure), parameters.Namespaces);
var code = Dependencies.CSharpHelper;
var mainBuilder = parameters.MainBuilder;
mainBuilder
.Append("var ").Append(sprocVariable).AppendLine(" = new RuntimeStoredProcedure(").IncrementIndent()
.Append(parameters.TargetName).AppendLine(",")
.Append(code.Literal(storedProcedure.Name)).AppendLine(",")
.Append(code.Literal(storedProcedure.Schema)).AppendLine(",")
.Append(code.Literal(storedProcedure.AreTransactionsSuppressed))
.AppendLine(");")
.DecrementIndent()
.AppendLine();

foreach (var parameter in storedProcedure.Parameters)
{
mainBuilder.Append(sprocVariable).Append(".AddParameter(")
.Append(code.Literal(parameter))
.AppendLine(");");
}

foreach (var resultColumn in storedProcedure.ResultColumns)
{
mainBuilder.Append(sprocVariable).Append(".AddResultColumn(")
.Append(code.Literal(resultColumn))
.AppendLine(");");
}

CreateAnnotations(
storedProcedure,
Generate,
parameters with { TargetName = sprocVariable });
}

/// <summary>
/// Generates code to create the given annotations.
/// </summary>
/// <param name="storedProcedure">The stored procedure to which the annotations are applied.</param>
/// <param name="parameters">Additional parameters used during code generation.</param>
public virtual void Generate(IStoredProcedure storedProcedure, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
=> GenerateSimpleAnnotations(parameters);

/// <summary>
/// Generates code to create the given annotations.
/// </summary>
Expand All @@ -457,6 +539,11 @@ public override void Generate(IProperty property, CSharpRuntimeAnnotationCodeGen
annotations.Remove(RelationalAnnotationNames.ViewColumnMappings);
annotations.Remove(RelationalAnnotationNames.SqlQueryColumnMappings);
annotations.Remove(RelationalAnnotationNames.FunctionColumnMappings);
annotations.Remove(RelationalAnnotationNames.InsertStoredProcedureParameterMappings);
annotations.Remove(RelationalAnnotationNames.InsertStoredProcedureResultColumnMappings);
annotations.Remove(RelationalAnnotationNames.DeleteStoredProcedureParameterMappings);
annotations.Remove(RelationalAnnotationNames.UpdateStoredProcedureParameterMappings);
annotations.Remove(RelationalAnnotationNames.UpdateStoredProcedureResultColumnMappings);
annotations.Remove(RelationalAnnotationNames.DefaultColumnMappings);
}
else
Expand All @@ -472,7 +559,7 @@ public override void Generate(IProperty property, CSharpRuntimeAnnotationCodeGen
AddNamespace(typeof(StoreObjectDictionary<RuntimeRelationalPropertyOverrides>), parameters.Namespaces);
AddNamespace(typeof(StoreObjectIdentifier), parameters.Namespaces);
var overridesVariable = Dependencies.CSharpHelper.Identifier("overrides", parameters.ScopeVariables, capitalize: false);
parameters.MainBuilder
parameters.MainBuilder.AppendLine()
.Append("var ").Append(overridesVariable).AppendLine(" = new StoreObjectDictionary<RuntimeRelationalPropertyOverrides>();");

foreach (var overrides in tableOverrides.GetValues())
Expand All @@ -481,6 +568,7 @@ public override void Generate(IProperty property, CSharpRuntimeAnnotationCodeGen
}

GenerateSimpleAnnotation(RelationalAnnotationNames.RelationalOverrides, overridesVariable, parameters);
parameters.MainBuilder.AppendLine();
}
}

Expand Down Expand Up @@ -617,6 +705,21 @@ private static void AppendLiteral(StoreObjectIdentifier storeObject, IndentedStr
builder
.Append("DbFunction(").Append(code.Literal(storeObject.Name)).Append(")");
break;
case StoreObjectType.InsertStoredProcedure:
builder
.Append("InsertStoredProcedure(").Append(code.Literal(storeObject.Name))
.Append(", ").Append(code.Literal(storeObject.Schema)).Append(")");
break;
case StoreObjectType.DeleteStoredProcedure:
builder
.Append("DeleteStoredProcedure(").Append(code.Literal(storeObject.Name))
.Append(", ").Append(code.Literal(storeObject.Schema)).Append(")");
break;
case StoreObjectType.UpdateStoredProcedure:
builder
.Append("UpdateStoredProcedure(").Append(code.Literal(storeObject.Name))
.Append(", ").Append(code.Literal(storeObject.Schema)).Append(")");
break;
default:
Check.DebugFail("Unexpected StoreObjectType: " + storeObject.StoreObjectType);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,7 @@ public static EntityTypeBuilder<TEntity> ToTable<TEntity>(
string name,
Action<TableBuilder<TEntity>> buildAction)
where TEntity : class
{
Check.NotNull(name, nameof(name));
Check.NotNull(buildAction, nameof(buildAction));

entityTypeBuilder.Metadata.SetTableName(name);
entityTypeBuilder.Metadata.SetSchema(null);
buildAction(new (StoreObjectIdentifier.Table(name, null), entityTypeBuilder));

return entityTypeBuilder;
}
=> ToTable(entityTypeBuilder, name, null, buildAction);

/// <summary>
/// Configures the table that the entity type maps to when targeting a relational database.
Expand Down Expand Up @@ -195,7 +186,7 @@ public static EntityTypeBuilder ToTable(

entityTypeBuilder.Metadata.SetTableName(name);
entityTypeBuilder.Metadata.SetSchema(schema);
buildAction(new (StoreObjectIdentifier.Table(name, schema), entityTypeBuilder));
buildAction(new(StoreObjectIdentifier.Table(name, entityTypeBuilder.Metadata.GetSchema()), entityTypeBuilder));

return entityTypeBuilder;
}
Expand Down Expand Up @@ -243,7 +234,7 @@ public static EntityTypeBuilder<TEntity> ToTable<TEntity>(

entityTypeBuilder.Metadata.SetTableName(name);
entityTypeBuilder.Metadata.SetSchema(schema);
buildAction(new (StoreObjectIdentifier.Table(name, schema), entityTypeBuilder));
buildAction(new(StoreObjectIdentifier.Table(name, entityTypeBuilder.Metadata.GetSchema()), entityTypeBuilder));

return entityTypeBuilder;
}
Expand Down Expand Up @@ -374,16 +365,7 @@ public static OwnedNavigationBuilder<TOwnerEntity, TDependentEntity> ToTable<TOw
Action<OwnedNavigationTableBuilder<TOwnerEntity, TDependentEntity>> buildAction)
where TOwnerEntity : class
where TDependentEntity : class
{
Check.NotNull(name, nameof(name));
Check.NotNull(buildAction, nameof(buildAction));

ownedNavigationBuilder.OwnedEntityType.SetTableName(name);
ownedNavigationBuilder.OwnedEntityType.SetSchema(null);
buildAction(new (StoreObjectIdentifier.Table(name, null), ownedNavigationBuilder));

return ownedNavigationBuilder;
}
=> ToTable(ownedNavigationBuilder, name, null, buildAction);

/// <summary>
/// Configures the table that the entity type maps to when targeting a relational database.
Expand Down Expand Up @@ -432,7 +414,7 @@ public static OwnedNavigationBuilder ToTable(

ownedNavigationBuilder.OwnedEntityType.SetTableName(name);
ownedNavigationBuilder.OwnedEntityType.SetSchema(schema);
buildAction(new (StoreObjectIdentifier.Table(name, schema), ownedNavigationBuilder));
buildAction(new(StoreObjectIdentifier.Table(name, ownedNavigationBuilder.OwnedEntityType.GetSchema()), ownedNavigationBuilder));

return ownedNavigationBuilder;
}
Expand Down Expand Up @@ -485,7 +467,7 @@ public static OwnedNavigationBuilder<TOwnerEntity, TDependentEntity> ToTable<TOw

ownedNavigationBuilder.OwnedEntityType.SetTableName(name);
ownedNavigationBuilder.OwnedEntityType.SetSchema(schema);
buildAction(new (StoreObjectIdentifier.Table(name, schema), ownedNavigationBuilder));
buildAction(new(StoreObjectIdentifier.Table(name, ownedNavigationBuilder.OwnedEntityType.GetSchema()), ownedNavigationBuilder));

return ownedNavigationBuilder;
}
Expand Down Expand Up @@ -548,7 +530,7 @@ public static EntityTypeBuilder SplitToTable(
Check.NullButNotEmpty(schema, nameof(schema));
Check.NotNull(buildAction, nameof(buildAction));

buildAction(new(StoreObjectIdentifier.Table(name, schema), entityTypeBuilder));
buildAction(new(StoreObjectIdentifier.Table(name, schema ?? entityTypeBuilder.Metadata.GetDefaultSchema()), entityTypeBuilder));

return entityTypeBuilder;
}
Expand Down Expand Up @@ -577,7 +559,7 @@ public static EntityTypeBuilder<TEntity> SplitToTable<TEntity>(
Check.NullButNotEmpty(schema, nameof(schema));
Check.NotNull(buildAction, nameof(buildAction));

buildAction(new(StoreObjectIdentifier.Table(name, schema), entityTypeBuilder));
buildAction(new(StoreObjectIdentifier.Table(name, schema ?? entityTypeBuilder.Metadata.GetDefaultSchema()), entityTypeBuilder));

return entityTypeBuilder;
}
Expand Down Expand Up @@ -642,7 +624,7 @@ public static OwnedNavigationBuilder SplitToTable(
Check.NullButNotEmpty(schema, nameof(schema));
Check.NotNull(buildAction, nameof(buildAction));

buildAction(new (StoreObjectIdentifier.Table(name, schema), ownedNavigationBuilder));
buildAction(new(StoreObjectIdentifier.Table(name, schema ?? ownedNavigationBuilder.OwnedEntityType.GetDefaultSchema()), ownedNavigationBuilder));

return ownedNavigationBuilder;
}
Expand Down Expand Up @@ -673,7 +655,7 @@ public static OwnedNavigationBuilder<TOwnerEntity, TDependentEntity> SplitToTabl
Check.NullButNotEmpty(schema, nameof(schema));
Check.NotNull(buildAction, nameof(buildAction));

buildAction(new (StoreObjectIdentifier.Table(name, schema), ownedNavigationBuilder));
buildAction(new(StoreObjectIdentifier.Table(name, schema ?? ownedNavigationBuilder.OwnedEntityType.GetDefaultSchema()), ownedNavigationBuilder));

return ownedNavigationBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public static EntityTypeBuilder ToView(
entityTypeBuilder.Metadata.SetViewName(name);
entityTypeBuilder.Metadata.SetViewSchema(schema);
entityTypeBuilder.Metadata.SetAnnotation(RelationalAnnotationNames.ViewDefinitionSql, null);
buildAction(new(StoreObjectIdentifier.View(name, schema), entityTypeBuilder));
buildAction(new(StoreObjectIdentifier.View(name, entityTypeBuilder.Metadata.GetViewSchema()), entityTypeBuilder));

return entityTypeBuilder;
}
Expand Down Expand Up @@ -175,7 +175,7 @@ public static EntityTypeBuilder<TEntity> ToView<TEntity>(
entityTypeBuilder.Metadata.SetViewName(name);
entityTypeBuilder.Metadata.SetViewSchema(schema);
entityTypeBuilder.Metadata.SetAnnotation(RelationalAnnotationNames.ViewDefinitionSql, null);
buildAction(new(StoreObjectIdentifier.View(name, schema), entityTypeBuilder));
buildAction(new(StoreObjectIdentifier.View(name, entityTypeBuilder.Metadata.GetViewSchema()), entityTypeBuilder));

return entityTypeBuilder;
}
Expand Down Expand Up @@ -317,7 +317,7 @@ public static OwnedNavigationBuilder ToView(
ownedNavigationBuilder.OwnedEntityType.SetViewName(name);
ownedNavigationBuilder.OwnedEntityType.SetViewSchema(schema);
ownedNavigationBuilder.OwnedEntityType.SetAnnotation(RelationalAnnotationNames.ViewDefinitionSql, null);
buildAction(new(StoreObjectIdentifier.View(name, schema), ownedNavigationBuilder));
buildAction(new(StoreObjectIdentifier.View(name, ownedNavigationBuilder.OwnedEntityType.GetViewSchema()), ownedNavigationBuilder));

return ownedNavigationBuilder;
}
Expand Down Expand Up @@ -349,7 +349,7 @@ public static OwnedNavigationBuilder<TOwnerEntity, TDependentEntity> ToView<TOwn
ownedNavigationBuilder.OwnedEntityType.SetViewName(name);
ownedNavigationBuilder.OwnedEntityType.SetViewSchema(schema);
ownedNavigationBuilder.OwnedEntityType.SetAnnotation(RelationalAnnotationNames.ViewDefinitionSql, null);
buildAction(new(StoreObjectIdentifier.View(name, schema), ownedNavigationBuilder));
buildAction(new(StoreObjectIdentifier.View(name, ownedNavigationBuilder.OwnedEntityType.GetViewSchema()), ownedNavigationBuilder));

return ownedNavigationBuilder;
}
Expand Down Expand Up @@ -397,13 +397,13 @@ public static EntityTypeBuilder<TEntity> SplitToView<TEntity>(
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and examples.
/// </remarks>
/// <param name="ownedNavigationBuilder">The builder for the entity type being configured.</param>
/// <param name="entityTypeBuilder">The builder for the entity type being configured.</param>
/// <param name="name">The name of the view.</param>
/// <param name="schema">The schema of the view.</param>
/// <param name="buildAction">An action that performs configuration of the view.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static EntityTypeBuilder SplitToView(
this EntityTypeBuilder ownedNavigationBuilder,
this EntityTypeBuilder entityTypeBuilder,
string name,
string? schema,
Action<SplitViewBuilder> buildAction)
Expand All @@ -412,9 +412,9 @@ public static EntityTypeBuilder SplitToView(
Check.NullButNotEmpty(schema, nameof(schema));
Check.NotNull(buildAction, nameof(buildAction));

buildAction(new(StoreObjectIdentifier.View(name, schema), ownedNavigationBuilder));
buildAction(new(StoreObjectIdentifier.View(name, schema ?? entityTypeBuilder.Metadata.GetDefaultViewSchema()), entityTypeBuilder));

return ownedNavigationBuilder;
return entityTypeBuilder;
}

/// <summary>
Expand All @@ -441,7 +441,7 @@ public static EntityTypeBuilder<TEntity> SplitToView<TEntity>(
Check.NullButNotEmpty(schema, nameof(schema));
Check.NotNull(buildAction, nameof(buildAction));

buildAction(new(StoreObjectIdentifier.View(name, schema), entityTypeBuilder));
buildAction(new(StoreObjectIdentifier.View(name, schema ?? entityTypeBuilder.Metadata.GetDefaultViewSchema()), entityTypeBuilder));

return entityTypeBuilder;
}
Expand Down Expand Up @@ -506,7 +506,7 @@ public static OwnedNavigationBuilder SplitToView(
Check.NullButNotEmpty(schema, nameof(schema));
Check.NotNull(buildAction, nameof(buildAction));

buildAction(new (StoreObjectIdentifier.View(name, schema), ownedNavigationBuilder));
buildAction(new(StoreObjectIdentifier.View(name, schema ?? ownedNavigationBuilder.OwnedEntityType.GetDefaultViewSchema()), ownedNavigationBuilder));

return ownedNavigationBuilder;
}
Expand Down Expand Up @@ -537,7 +537,7 @@ public static OwnedNavigationBuilder<TOwnerEntity, TDependentEntity> SplitToView
Check.NullButNotEmpty(schema, nameof(schema));
Check.NotNull(buildAction, nameof(buildAction));

buildAction(new (StoreObjectIdentifier.View(name, schema), ownedNavigationBuilder));
buildAction(new(StoreObjectIdentifier.View(name, schema ?? ownedNavigationBuilder.OwnedEntityType.GetDefaultViewSchema()), ownedNavigationBuilder));

return ownedNavigationBuilder;
}
Expand Down
Loading

0 comments on commit 41bd788

Please sign in to comment.