示例#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)));
        }
        public CreateProcedureStatement GenerateInsertStoredProcedure(Table table, QuoteType quoteType = QuoteType.NotQuoted)
        {
            Database database            = table.Parent;
            string   storedProcedureName = string.Concat("Insert", table.Name);

            List <ColumnReferenceExpression> columns           = new List <ColumnReferenceExpression>();
            List <ScalarExpression>          scalarExpressions = new List <ScalarExpression>();
            List <RowValue> rowValues = new List <RowValue>();
            RowValue        rowValue  = ScriptFactory.RowValue();

            rowValues.Add(rowValue);

            List <ProcedureParameter> parameters = new List <ProcedureParameter>();

            foreach (var column in table.Columns)
            {
                if (column.IsPrimaryKey())
                {
                    continue;
                }

                scalarExpressions.Add(
                    ScriptFactory.IdentifierLiteral(
                        CreateProcedureParameterVariableName(column), QuoteType.NotQuoted));

                columns.Add(
                    ScriptFactory.ColumnReferenceExpression(
                        ScriptFactory.MultiPartIdentifier(
                            ScriptFactory.Identifier(column.Name, quoteType))));

                parameters.Add(
                    ScriptFactory.ProcedureParameter(CreateProcedureParameterVariableName(column), false,
                                                     GenerateDataTypeReference(SqlDataType.Parse(column.DataType))));
            }
            rowValue.ColumnValues.AddRange(scalarExpressions);

            StatementList statements = ScriptFactory.List(
                ScriptFactory.PredicateSet(true, SetOptions.NoCount),
                ScriptFactory.Insert(
                    ScriptFactory.InsertSpecification(
                        ScriptFactory.ValuesInsertSource(false, rowValues),
                        ScriptFactory.NamedTableReference(
                            GenerateSchemaObjectName(table, quoteType)),
                        null, null, null, InsertOption.Into, columns)),
                ScriptFactory.Return(ScriptFactory.IntegerLiteral("1")));

            return(ScriptFactory.CreateProcedure(false,
                                                 ScriptFactory.ProcedureReference(
                                                     GenerateStoredProcedureName(storedProcedureName, table, quoteType)),
                                                 ScriptFactory.List(
                                                     ScriptFactory.BeginEndBlock(statements)), null, null, parameters));
        }