Ejemplo n.º 1
0
        private StringBuilder getColumnCreates()
        {
            _createdColumns = new List <string>();

            var  sb      = new StringBuilder();
            bool firstIn = true;

            foreach (var col in DatabaseObject.Columns.OrderBy(c => c.OrdinalPosition))
            {
                _createdColumns.Add(col.ColumnName);
                if (!string.IsNullOrEmpty(col.Formula))
                {
                    sb.AppendLine("\t" + (firstIn ? string.Empty : ",") + string.Format("[{0}] AS {1}",
                                                                                        col.ColumnName,
                                                                                        col.Formula));

                    firstIn = false;
                    continue;
                }

                string part2 = TargetDatabase.DataSource.GetColumnPostPart(col);
                string def   = new ColumnSynchronization(TargetDatabase, col).GetDefaultScript();

                sb.AppendLine("\t" + (firstIn ? string.Empty : ",") + string.Format("{0} {1}{2} {3} {4}",
                                                                                    TargetDatabase.DataSource.GetConvertedObjectName(col.ColumnName),
                                                                                    TargetDatabase.DataSource.GetConvertedColumnType(col, true),
                                                                                    part2,
                                                                                    col.IsNullable ? "NULL" : "NOT NULL",
                                                                                    def));
                firstIn = false;
            }
            return(sb);
        }
Ejemplo n.º 2
0
        public override List <SynchronizationItem> GetAlterItems(DatabaseObjectBase target, bool ignoreCase, bool condensed)
        {
            var targetTable = target as Table;

            var items = new List <SynchronizationItem>();


            bool recreate = false;

            foreach (var fc in DatabaseObject.Columns)
            {
                var tc = targetTable.Columns.FirstOrDefault(c => string.Compare(c.ColumnName, fc.ColumnName, ignoreCase) == 0);
                if (tc != null && tc.IsIdentity != fc.IsIdentity)
                {
                    var diff = getDifference(DifferenceType.Create, fc, tc, "IsIdentity", fc.IsIdentity.ToString(), tc.IsIdentity.ToString());
                    if (diff != null)
                    {
                        var item = getRecreateTableItem(targetTable, ignoreCase);
                        item.Differences.Add(diff);
                        items.Add(item);
                        recreate    = true;
                        targetTable = null;
                        break;
                    }
                }

                if (tc != null && tc.ColumnType.DataType == DataType.RowVersion)
                {
                    var diff = new ColumnSynchronization(TargetDatabase, fc).GetPropertyDifferences(tc, ignoreCase);
                    if (diff.Any())
                    {
                        var item = getRecreateTableItem(targetTable, ignoreCase);
                        item.Differences.AddRange(diff);
                        items.Add(item);
                        recreate    = true;
                        targetTable = null;
                        break;
                    }
                }
            }

            if (!recreate)
            {
                foreach (var tk in targetTable.ForeignKeys)
                {
                    if (!DatabaseObject.Database.DataSource.NamedConstraints || !targetTable.Database.DataSource.NamedConstraints)
                    {
                        if (!DatabaseObject.ForeignKeys.Any(k =>
                                                            string.Compare(k.ChildTable.ObjectName, tk.ChildTable.ObjectName, ignoreCase) == 0 &&
                                                            string.Compare(k.ParentTable.ObjectName, tk.ParentTable.ObjectName, ignoreCase) == 0 &&
                                                            k.Columns.All(x => tk.Columns.Any(c => string.Compare(c.ChildColumn.ObjectName, x.ChildColumn.ObjectName, ignoreCase) == 0 &&
                                                                                              string.Compare(c.ParentColumn.ObjectName, x.ParentColumn.ObjectName, ignoreCase) == 0))))
                        {
                            items.AddRange(new ForeignKeySynchronization(TargetDatabase, tk).GetDropItems(DatabaseObject));
                        }
                    }
                    else if (!DatabaseObject.ForeignKeys.Any(k => string.Compare(k.ForeignKeyName, tk.ForeignKeyName, ignoreCase) == 0))
                    {
                        if (targetTable.Database.DataSource.ForeignKeyDropsWithColumns)
                        {
                            // column being dropped
                            if (tk.Columns.Any(c => !DatabaseObject.Columns.Any(x => string.Compare(x.ColumnName, c.ChildColumn.ColumnName, ignoreCase) == 0)))
                            {
                                continue;
                            }
                        }

                        items.AddRange(new ForeignKeySynchronization(TargetDatabase, tk).GetDropItems(DatabaseObject));
                    }
                }

                foreach (var fk in DatabaseObject.ForeignKeys)
                {
                    var tk = targetTable.ForeignKeys.FirstOrDefault(x => string.Compare(x.ObjectName, fk.ObjectName, ignoreCase) == 0);
                    if (tk == null)
                    {
                        if (!TargetDatabase.DataSource.NamedConstraints || !DatabaseObject.Database.DataSource.NamedConstraints)
                        {
                            tk = targetTable.ForeignKeys.FirstOrDefault(x =>
                                                                        string.Compare(x.ParentTable.TableName, fk.ParentTable.TableName, ignoreCase) == 0 &&
                                                                        string.Compare(x.ChildTable.TableName, fk.ChildTable.TableName, ignoreCase) == 0 &&
                                                                        x.Columns.All(y =>
                                                                                      fk.Columns.Any(z =>
                                                                                                     string.Compare(z.ChildColumn.ColumnName, y.ChildColumn.ColumnName, ignoreCase) == 0 &&
                                                                                                     string.Compare(z.ParentColumn.ColumnName, y.ParentColumn.ColumnName, ignoreCase) == 0)));
                        }
                    }
                    if (tk != null)
                    {
                        items.AddRange(new ForeignKeySynchronization(TargetDatabase, fk).GetSynchronizationItems(tk, ignoreCase, condensed));
                    }
                    else
                    {
                        items.AddRange(new ForeignKeySynchronization(TargetDatabase, fk).GetCreateItems());
                    }
                }

                foreach (var tc in targetTable.Columns)
                {
                    if (!DatabaseObject.Columns.Any(c => string.Compare(c.ColumnName, tc.ColumnName, ignoreCase) == 0))
                    {
                        items.AddRange(new ColumnSynchronization(TargetDatabase, tc).GetDropItems(DatabaseObject));
                    }
                }

                _createdColumns = new List <string>();
                _alteredColumns = new List <string>();

                foreach (var fc in DatabaseObject.Columns)
                {
                    var tc = targetTable.Columns.FirstOrDefault(c => string.Compare(c.ColumnName, fc.ColumnName, ignoreCase) == 0);
                    if (tc == null)
                    {
                        items.AddRange(new ColumnSynchronization(TargetDatabase, fc).GetAddAlterItems(null, ignoreCase, false));
                        _createdColumns.Add(fc.ColumnName);
                    }
                    else
                    {
                        var alteredItems = new ColumnSynchronization(TargetDatabase, fc).GetSynchronizationItems(tc, ignoreCase, condensed);
                        if (alteredItems.Any())
                        {
                            items.AddRange(alteredItems);
                            _alteredColumns.Add(fc.ColumnName);
                        }
                    }
                }
            }

            if (!recreate)
            {
                items.AddRange(getKeyConstraintUpdateItems(targetTable, ignoreCase, condensed));
            }

            if (!condensed)
            {
                items.AddRange(getIndexCreateUpdateItems(targetTable, ignoreCase));
                items.AddRange(getDefaultConstraintCreateUpdateItems(targetTable, ignoreCase));
                items.AddRange(getTriggerUpdateItems(targetTable, ignoreCase));
                foreach (var column in DatabaseObject.Columns)
                {
                    items.AddRange(ExtendedPropertySynchronization.GetExtendedProperties(TargetDatabase, column, targetTable == null ? null : targetTable.Columns.FirstOrDefault(c => c.ColumnName == column.ColumnName)));
                }
            }
            TargetDatabase.DataSource.CheckUnnecessaryItems(items);
            return(items);
        }