Esempio n. 1
0
        protected override Expression VisitCreateTable(SqlCreateTableExpression createTableExpression)
        {
            this.Write("CREATE TABLE ");
            this.Visit(createTableExpression.Table);
            this.WriteLine();
            this.Write("(");

            using (this.AcquireIndentationContext())
            {
                this.WriteDeliminatedListOfItems(createTableExpression.ColumnDefinitionExpressions, c => this.Visit(c), () => this.WriteLine(","));

                if (createTableExpression.ColumnDefinitionExpressions.Count > 0 && createTableExpression.TableConstraints.Count > 0)
                {
                    this.Write(",");
                }

                this.WriteLine();
                this.WriteDeliminatedListOfItems(createTableExpression.TableConstraints, c => this.Visit(c), () => this.WriteLine(","));
            }

            this.WriteLine();
            this.WriteLine(");");

            return(createTableExpression);
        }
        protected override Expression VisitCreateTable(SqlCreateTableExpression createTableExpression)
        {
            var autoIncrementColumn = createTableExpression
                                      .ColumnDefinitionExpressions
                                      .SingleOrDefault(c => c.ConstraintExpressions.Any(d => (d.ConstraintType & ConstraintType.AutoIncrement) != 0));

            if (autoIncrementColumn != null)
            {
                var primaryKeyConstraint = createTableExpression
                                           .TableConstraints
                                           .SingleOrDefault(c => (c.ConstraintType & ConstraintType.PrimaryKey) != 0);

                if (primaryKeyConstraint != null)
                {
                    if (!primaryKeyConstraint.ColumnNames.Contains(autoIncrementColumn.ColumnName))
                    {
                        var newPrimaryKeyConstraint = new SqlConstraintExpression(ConstraintType.PrimaryKey, /* TODO: name */ null, new [] { autoIncrementColumn.ColumnName }.ToReadOnlyCollection());
                        var newUniqueConstraint     = new SqlConstraintExpression(ConstraintType.Unique, /* TODO: name */ null, primaryKeyConstraint.ColumnNames.Concat(autoIncrementColumn.ColumnName).ToReadOnlyCollection());

                        return(createTableExpression.ChangeConstraints(createTableExpression.TableConstraints.Where(c => c != primaryKeyConstraint).Concat(newPrimaryKeyConstraint).Concat(newUniqueConstraint).ToReadOnlyCollection()));
                    }
                }
                else
                {
                    var newPrimaryKeyConstraint = new SqlConstraintExpression(ConstraintType.PrimaryKey, /* TODO: name */ null, new[] { autoIncrementColumn.ColumnName }.ToReadOnlyCollection());

                    return(createTableExpression.ChangeConstraints(new [] { newPrimaryKeyConstraint }.ToReadOnlyCollection()));
                }
            }

            return(base.VisitCreateTable(createTableExpression));
        }
Esempio n. 3
0
        protected override Expression VisitCreateTable(SqlCreateTableExpression createTableExpression)
        {
            var autoIncrementColumn = createTableExpression.ColumnDefinitionExpressions.SingleOrDefault(c => c.ConstraintExpressions.OfType <SqlSimpleConstraintExpression>().Any(d => d.Constraint == SqlSimpleConstraint.AutoIncrement));

            if (autoIncrementColumn != null)
            {
                var primaryKeyConstraint = createTableExpression.TableConstraints.OfType <SqlSimpleConstraintExpression>().SingleOrDefault(c => c.Constraint == SqlSimpleConstraint.PrimaryKey);

                if (primaryKeyConstraint != null)
                {
                    if (!primaryKeyConstraint.ColumnNames.Contains(autoIncrementColumn.ColumnName))
                    {
                        var newPrimaryKeyConstraint = new SqlSimpleConstraintExpression(SqlSimpleConstraint.PrimaryKey, new [] { autoIncrementColumn.ColumnName });
                        var newUniqueConstraint     = new SqlSimpleConstraintExpression(SqlSimpleConstraint.Unique, primaryKeyConstraint.ColumnNames.Concat(autoIncrementColumn.ColumnName).ToArray());

                        return(createTableExpression.UpdateConstraints(createTableExpression.TableConstraints.Where(c => c != primaryKeyConstraint).Concat(newPrimaryKeyConstraint).Concat(newUniqueConstraint).ToReadOnlyCollection()));
                    }
                }
                else
                {
                    var newPrimaryKeyConstraint = new SqlSimpleConstraintExpression(SqlSimpleConstraint.PrimaryKey, new [] { autoIncrementColumn.ColumnName });

                    return(createTableExpression.UpdateConstraints(new [] { newPrimaryKeyConstraint }.ToReadOnlyCollection()));
                }
            }

            return(base.VisitCreateTable(createTableExpression));
        }
        protected override Expression VisitCreateTable(SqlCreateTableExpression createTableExpression)
        {
            var primaryKeyConstraint = createTableExpression
                                       .TableConstraints
                                       .OfType <SqlSimpleConstraintExpression>()
                                       .SingleOrDefault(c => c.Constraint == SqlSimpleConstraint.PrimaryKey);

            if (primaryKeyConstraint != null)
            {
                var autoIncrementColumns = createTableExpression
                                           .ColumnDefinitionExpressions
                                           .SelectMany(c => c.ConstraintExpressions.OfType <SqlSimpleConstraintExpression>().Select(d => new { Constraint = d, ColumnDefinition = c }))
                                           .Where(c => c.Constraint.Constraint == SqlSimpleConstraint.AutoIncrement)
                                           .ToList();

                if (autoIncrementColumns.Count > 1)
                {
                    throw new UnsupportedDataAccessModelDefinitionException();
                }

                if (autoIncrementColumns.Count > 0)
                {
                    var autoIncrementColumn = autoIncrementColumns.Single();

                    var newTableConstraints = createTableExpression
                                              .TableConstraints
                                              .Where(c => c != primaryKeyConstraint);

                    if (primaryKeyConstraint.ColumnNames.Length > 1)
                    {
                        var uniqueConstraint = new SqlSimpleConstraintExpression(SqlSimpleConstraint.Unique, primaryKeyConstraint.ColumnNames);

                        newTableConstraints = newTableConstraints.Concat(uniqueConstraint);
                    }

                    primaryKeyNameByTablesWithReducedPrimaryKeyName[createTableExpression.Table.Name] = autoIncrementColumn.ColumnDefinition.ColumnName;

                    columnsToMakeNotNull.Clear();

                    primaryKeyConstraint
                    .ColumnNames
                    .Where(c => c != autoIncrementColumn.ColumnDefinition.ColumnName)
                    .ForEach(c => columnsToMakeNotNull.Add(c));

                    createTableExpression = new SqlCreateTableExpression
                                            (
                        createTableExpression.Table,
                        createTableExpression.IfNotExist,
                        this.VisitExpressionList(createTableExpression.ColumnDefinitionExpressions),
                        newTableConstraints
                                            );

                    columnsToMakeNotNull.Clear();

                    return(createTableExpression);
                }
            }

            return(base.VisitCreateTable(createTableExpression));
        }
Esempio n. 5
0
        protected override Expression VisitCreateTable(SqlCreateTableExpression createTableExpression)
        {
            this.currentTable = createTableExpression;

            var retval = base.VisitCreateTable(createTableExpression);

            this.currentTable = null;

            return(retval);
        }
        protected override Expression VisitCreateTable(SqlCreateTableExpression createTableExpression)
        {
            var count = 0;

            foreach (var columnDefinition in createTableExpression
                     .ColumnDefinitionExpressions
                     .Where(columnDefinition => columnDefinition
                            .ConstraintExpressions
                            .OfType <SqlSimpleConstraintExpression>()
                            .Any(simpleConstraint => simpleConstraint.Constraint == SqlSimpleConstraint.PrimaryKey)))
            {
                count++;

                if (count >= 2)
                {
                    break;
                }
            }

            if (count < 2)
            {
                return(base.VisitCreateTable(createTableExpression));
            }

            var newColumnExpressions          = new List <SqlColumnDefinitionExpression>();
            var newTableConstraintExpressions = new List <Expression>(createTableExpression.TableConstraints);

            foreach (var columnDefinition in createTableExpression.ColumnDefinitionExpressions)
            {
                var newConstraints = columnDefinition.ConstraintExpressions.Where(delegate(Expression constraint)
                {
                    var simpleConstraint = constraint as SqlSimpleConstraintExpression;

                    if (simpleConstraint == null)
                    {
                        return(true);
                    }

                    return(simpleConstraint.Constraint != SqlSimpleConstraint.PrimaryKey);
                });

                if (ReferenceEquals(newConstraints, columnDefinition.ConstraintExpressions))
                {
                    newColumnExpressions.Add(columnDefinition);
                }
                else
                {
                    newColumnExpressions.Add(new SqlColumnDefinitionExpression(columnDefinition.ColumnName, columnDefinition.ColumnType, newConstraints));
                }
            }

            return(new SqlCreateTableExpression(createTableExpression.Table, false, newColumnExpressions, newTableConstraintExpressions, Enumerable.Empty <SqlTableOption>()));
        }
        protected override Expression VisitCreateTable(SqlCreateTableExpression createTableExpression)
        {
            var previousCreateTableExpression = this.currentCreateTableExpression;

            this.currentCreateTableExpression = createTableExpression;

            var retval = base.VisitCreateTable(createTableExpression);

            this.currentCreateTableExpression = previousCreateTableExpression;

            return(retval);
        }
Esempio n. 8
0
        protected virtual Expression VisitCreateTable(SqlCreateTableExpression createTableExpression)
        {
            var newTable          = (SqlTableExpression)this.Visit(createTableExpression.Table);
            var constraints       = this.VisitExpressionList(createTableExpression.TableConstraints);
            var columnDefinitions = this.VisitExpressionList(createTableExpression.ColumnDefinitionExpressions);

            if (newTable != createTableExpression.Table || createTableExpression.TableConstraints != constraints || createTableExpression.ColumnDefinitionExpressions != columnDefinitions)
            {
                return(new SqlCreateTableExpression(newTable, false, columnDefinitions, constraints, Enumerable.Empty <SqlTableOption>()));
            }
            else
            {
                return(createTableExpression);
            }
        }
        protected virtual Expression VisitCreateTable(SqlCreateTableExpression createTableExpression)
        {
            var newTable          = (SqlTableExpression)Visit(createTableExpression.Table);
            var constraints       = VisitExpressionList(createTableExpression.TableConstraints);
            var columnDefinitions = VisitExpressionList(createTableExpression.ColumnDefinitionExpressions);
            var organizationIndex = (SqlOrganizationIndexExpression)Visit(createTableExpression.OrganizationIndex);

            if (newTable != createTableExpression.Table || createTableExpression.TableConstraints != constraints || createTableExpression.ColumnDefinitionExpressions != columnDefinitions || createTableExpression.OrganizationIndex != organizationIndex)
            {
                return(new SqlCreateTableExpression(newTable, false, columnDefinitions, constraints, organizationIndex, createTableExpression.TableOptions));
            }
            else
            {
                return(createTableExpression);
            }
        }
Esempio n. 10
0
        public void Test_Format_Create_Table_With_Table_Constraints()
        {
            var columnDefinitions = new []
            {
                new SqlColumnDefinitionExpression("Column1", new SqlTypeExpression("INTEGER"), new List <Expression> {
                    new SqlSimpleConstraintExpression(SqlSimpleConstraint.Unique), new SqlReferencesColumnExpression(new SqlTableExpression("Table2"), SqlColumnReferenceDeferrability.InitiallyDeferred, new [] { "Id" }, SqlColumnReferenceAction.NoAction, SqlColumnReferenceAction.SetNull)
                })
            };

            var constraints = new Expression[]
            {
                new SqlSimpleConstraintExpression(SqlSimpleConstraint.Unique, new[] { "Column1" }),
                new SqlForeignKeyConstraintExpression("fkc", new [] { "Column1" }, new SqlReferencesColumnExpression(new SqlTableExpression("Table2"), SqlColumnReferenceDeferrability.InitiallyDeferred, new [] { "Id" }, SqlColumnReferenceAction.NoAction, SqlColumnReferenceAction.NoAction))
            };

            var createTableExpression = new SqlCreateTableExpression(new SqlTableExpression("Table1"), false, columnDefinitions, constraints);

            var formatter = new Sql92QueryFormatter();

            Console.WriteLine(formatter.Format(createTableExpression).CommandText);
        }
Esempio n. 11
0
        public void Test_Format_Create_Table_With_Table_Constraints()
        {
            var columnDefinitions = new []
            {
                new SqlColumnDefinitionExpression("Column1", new SqlTypeExpression("INTEGER"), new List <SqlConstraintExpression> {
                    new SqlConstraintExpression(ConstraintType.Unique), new SqlConstraintExpression(new SqlReferencesExpression(new SqlTableExpression("Table2"), SqlColumnReferenceDeferrability.InitiallyDeferred, new [] { "Id" }, SqlColumnReferenceAction.NoAction, SqlColumnReferenceAction.SetNull))
                })
            };

            var constraints = new []
            {
                new SqlConstraintExpression(ConstraintType.Unique),
                new SqlConstraintExpression(new SqlReferencesExpression(new SqlTableExpression("Table2"), SqlColumnReferenceDeferrability.InitiallyDeferred, new [] { "Id" }, SqlColumnReferenceAction.NoAction, SqlColumnReferenceAction.NoAction), columnNames: new [] { "Column1" }, constraintName: "fck")
            };

            var createTableExpression = new SqlCreateTableExpression(new SqlTableExpression("Table1"), false, columnDefinitions.ToReadOnlyCollection(), constraints.ToReadOnlyCollection(), null);

            var formatter = new Sql92QueryFormatter();

            Console.WriteLine(formatter.Format(createTableExpression).CommandText);
        }
        protected override Expression VisitCreateTable(SqlCreateTableExpression createTableExpression)
        {
            var retval            = createTableExpression;
            var organizationIndex = createTableExpression.OrganizationIndex;

            if (organizationIndex == null)
            {
                return(createTableExpression);
            }

            // No organization index - remove from primary key

            if (organizationIndex.Columns == null)
            {
                try
                {
                    this.makeUnclustered = true;

                    retval = ((SqlCreateTableExpression)base.VisitCreateTable(createTableExpression))
                             .ChangeOrganizationIndex(null);

                    return(retval);
                }
                finally
                {
                    this.makeUnclustered = false;
                }
            }

            // Organization index the same as primary key so just leave with SQL server default

            if (createTableExpression
                .TableConstraints
                .Where(c => c.PrimaryKey)
                .SelectMany(c => c.ColumnNames)
                .OrderBy(c => c)
                .SequenceEqual(organizationIndex.Columns.Select(c => c.Column.Name).OrderBy(c => c)))
            {
                retval = ((SqlCreateTableExpression)base.VisitCreateTable(createTableExpression))
                         .ChangeOrganizationIndex(null);

                return(retval);
            }

            // Custom organization index

            try
            {
                this.makeUnclustered = true;

                retval = ((SqlCreateTableExpression)base.VisitCreateTable(createTableExpression))
                         .ChangeOrganizationIndex(null);
            }
            finally
            {
                this.makeUnclustered = false;
            }

            var indexExpression = new SqlCreateIndexExpression(organizationIndex.IndexName, createTableExpression.Table, false, IndexType.Default, false, organizationIndex.Columns, null, null, true);

            if (this.additionalStatements == null)
            {
                this.additionalStatements = new List <Expression>();
            }

            this.additionalStatements.Add(indexExpression);

            return(retval);
        }