Esempio n. 1
0
        private void AddCreateUniqueConstraintBatch(Index index, QuoteType quoteType)
        {
            List <ColumnWithSortOrder> columns = new List <ColumnWithSortOrder>();

            foreach (var member in index.Members)
            {
                Column column = index.Parent.Columns.FirstOrDefault(c => c.Name == member.Column);
                columns.Add(
                    ScriptFactory.ColumnWithSortOrder(
                        ScriptFactory.ColumnReferenceExpression(
                            ScriptFactory.MultiPartIdentifier(
                                ScriptFactory.Identifier(member.Column, quoteType)), ColumnType.Regular), SortOrder.Ascending));
            }
            UniqueConstraintDefinition uniqueConstraint = ScriptFactory.UniqueConstraintDefinition(
                ScriptFactory.Identifier(index.Name, quoteType),
                false,
                index.IsClustered,
                null,
                null,
                null,
                columns
                );

            AddBatch(ScriptFactory.AlterTableAddTableElement(
                         ScriptFactory.TableDefinition(
                             null,
                             null,
                             new List <ConstraintDefinition> {
                uniqueConstraint
            },
                             null),
                         Generator.GenerateSchemaObjectName(index.Parent, quoteType)));
        }
Esempio n. 2
0
        public void AddCreateForeignKeyBatch(ForeignKey key, QuoteType quoteType = QuoteType.NotQuoted)
        {
            var first = key.ForeignKeyColumns.FirstOrDefault();

            var pkTable = key.Parent.Parent.Tables.FirstOrDefault(t => t.Name == key.PkTable);

            ForeignKeyConstraintDefinition uniqueConstraint = ScriptFactory.ForeignKeyConstraintDefinition(
                ScriptFactory.Identifier(key.Name, quoteType),
                false,
                Generator.GenerateSchemaObjectName(pkTable, quoteType),
                DeleteUpdateAction.NotSpecified,
                DeleteUpdateAction.NotSpecified,
                key.ForeignKeyColumns.Select(fkc => ScriptFactory.Identifier(fkc.FkColumn, quoteType)),
                key.ForeignKeyColumns.Select(fkc => ScriptFactory.Identifier(fkc.PkColumn, quoteType))
                );

            AddBatch(ScriptFactory.AlterTableAddTableElement(
                         ScriptFactory.TableDefinition(
                             null,
                             null,
                             new List <ConstraintDefinition> {
                uniqueConstraint
            },
                             null),
                         Generator.GenerateSchemaObjectName(key.Parent, quoteType)));
        }
 public void AddDefaultConstraints(List <TableDefaultConstraint> definitions, QuoteType quoteType = QuoteType.NotQuoted)
 {
     for (int i = 0; i < definitions.Count; i++)
     {
         var definition = definitions[i];
         var table      = definition.Table;
         var constraint = definition.Constraint;
         AddBatch(Generator.GeneratePrintStatement($"[{i + 1} - {definitions.Count}] Creating default constraint [{constraint.ConstraintIdentifier.Value}]"));
         AddBatch(ScriptFactory.AlterTableAddTableElement(
                      ScriptFactory.TableDefinition(
                          null,
                          null,
                          new List <ConstraintDefinition> {
             constraint
         },
                          null),
                      Generator.GenerateSchemaObjectName(table, quoteType)));
     }
 }