public static string GenerateProcedure(
            Catalog catalog,
            Table rootTable,
            string procedureName           = null,
            string primaryKeyParameterName = null,
            IReadOnlyDictionary <Column, Parameter> updateParameters = null,
            string primaryKeyOutputParameterName   = null,
            IReadOnlyList <Column> excludedColumns = null,
            IReadOnlyList <Table> excludedTables   = null,
            ReferenceGraph referenceGraph          = null)
        {
            procedureName           = procedureName ?? $"Copy{rootTable.SingularSpacelessName}";
            primaryKeyParameterName = primaryKeyParameterName ?? rootTable.DefaultPrimaryKeyParameterName;
            var parameters = (updateParameters ?? new Dictionary <Column, Parameter>())
                             .Select(kvp => kvp.Value)
                             .Prepend(new Parameter(primaryKeyParameterName, "INT"));

            parameters = primaryKeyOutputParameterName == null ? parameters
                : parameters.Append(new Parameter(primaryKeyOutputParameterName, "INT = NULL OUTPUT"));
            var updateParameterNames = updateParameters?.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Name);

            return
                ($@"CREATE PROCEDURE [{rootTable.Schema.Name}].[{procedureName}]
    {string.Join(Separators.Cnlw4, parameters.Select(p => $"{p.Name} {p.DataTypeDescription}"))}
AS
BEGIN
    SET NOCOUNT ON;
    SET XACT_ABORT ON;
    BEGIN TRAN;
{GenerateProcedureBody(catalog, rootTable, primaryKeyParameterName, updateParameterNames, primaryKeyOutputParameterName, excludedColumns, excludedTables, referenceGraph)}
    COMMIT TRAN;
END;");
        }
        protected RootCopyGenerator(
            Catalog catalog,
            Table rootTable,
            string primaryKeyParameterName,
            IReadOnlyDictionary <Column, string> updateParameterNames = null,
            string primaryKeyOutputParameterName   = null,
            IReadOnlyList <Column> excludedColumns = null,
            IReadOnlyList <Table> excludedTables   = null,
            ReferenceGraph referenceGraph          = null)
        {
            Catalog   = catalog;
            RootTable = rootTable;
            PrimaryKeyParameterName       = Parameter.ValidateName(primaryKeyParameterName ?? RootTable.DefaultPrimaryKeyParameterName);
            UpdateParameterNames          = updateParameterNames?.ToDictionary(kvp => kvp.Key, kvp => Parameter.ValidateName(kvp.Value)) ?? new Dictionary <Column, string>();
            PrimaryKeyOutputParameterName = Parameter.ValidateName(primaryKeyOutputParameterName);
            ExcludedColumns = excludedColumns ?? new Column[0];
            ReferenceGraph  = referenceGraph ?? new ReferenceGraph(catalog, rootTable, excludedTables);

            GenerateTableVariables();
            GenerateRootTableCopy();
            foreach (var vertex in ReferenceGraph.Skip(1))
            {
                GenerateDependentTableCopy(vertex);
            }
            foreach (var vertex in ReferenceGraph
                     .Where(v => v.NonDependentReferences.Any()))
            {
                GenerateNonDependentReferenceUpdates(vertex);
            }
        }
 public static string GenerateProcedureBody(
     Catalog catalog,
     Table rootTable,
     string primaryKeyParameterName,
     IReadOnlyDictionary <Column, string> updateParameterNames = null,
     string primaryKeyOutputParameterName   = null,
     IReadOnlyList <Column> excludedColumns = null,
     IReadOnlyList <Table> excludedTables   = null,
     ReferenceGraph referenceGraph          = null)
 => new RootCopyGenerator(catalog, rootTable, primaryKeyParameterName, updateParameterNames, primaryKeyOutputParameterName, excludedColumns, excludedTables, referenceGraph)
 .ProcedureBody
 .ToString();
Exemple #4
0
 protected DeepCopyGenerator(
     Catalog catalog,
     Table rootTable,
     string primaryKeyParameterName,
     IReadOnlyDictionary <Column, string> updateParameterNames = null,
     string primaryKeyOutputParameterName   = null,
     IReadOnlyList <Column> excludedColumns = null,
     IReadOnlyList <Table> excludedTables   = null,
     ReferenceGraph referenceGraph          = null)
     : base(catalog, rootTable, primaryKeyParameterName, updateParameterNames, primaryKeyOutputParameterName, excludedColumns, excludedTables, referenceGraph)
 {
 }
Exemple #5
0
 protected internal Vertex(ReferenceGraph referenceGraph, Table table)
 {
     ReferenceGraph = referenceGraph;
     Table          = table;
 }
Exemple #6
0
 public virtual bool IsReferenced()
 => ReferenceGraph.Any(v =>
                       v.DependentReferences.Any(r => r.ReferencedTable == Table) ||
                       v.NonDependentReferences.Any(r => r.ReferencedTable == Table));