コード例 #1
0
 public string GetQuotedFullTableName(TableChild tableChild)
 {
     if (string.IsNullOrEmpty(tableChild.Owner))
     {
         return(this.GetQuotedString(tableChild.TableName));
     }
     else
     {
         return($"{tableChild.Owner}.{this.GetQuotedString(tableChild.TableName)}");
     }
 }
コード例 #2
0
        public static void ExcludeDbObjects <T>(List <T> sourceObjs, List <T> targetObjs) where T : DatabaseObject
        {
            if (sourceObjs.Count > 0 && targetObjs.Count > 0)
            {
                List <T> excludeObjs = new List <T>();

                foreach (T obj in sourceObjs)
                {
                    bool existed = false;

                    if (obj is TableForeignKey)
                    {
                        TableForeignKey tfk = obj as TableForeignKey;

                        existed = (targetObjs as List <TableForeignKey>).Any(item => item.TableName.ToLower() == tfk.TableName && item.ReferencedTableName.ToLower() == tfk.ReferencedTableName.ToLower() &&
                                                                             IsForeignKeyColumnsEquals(item.Columns, tfk.Columns));
                    }
                    else if (obj is TableColumnChild)
                    {
                        TableColumnChild tk = obj as TableColumnChild;

                        existed = (targetObjs.Cast <TableColumnChild>()).Any(item => item.TableName.ToLower() == tk.TableName && item.ColumnName.ToLower() == tk.ColumnName.ToLower());
                    }
                    else if (obj is TableChild)
                    {
                        TableChild tc = obj as TableChild;

                        existed = (targetObjs.Cast <TableChild>()).Any(item => item.TableName.ToLower() == tc.TableName && item.Name.ToLower() == tc.Name.ToLower());
                    }
                    else
                    {
                        existed = targetObjs.Any(item => item.Name.ToLower() == obj.Name.ToLower());
                    }

                    if (existed)
                    {
                        excludeObjs.Add(obj);
                    }
                }

                sourceObjs.RemoveAll(item => excludeObjs.Any(t => t == item));
            }
        }
コード例 #3
0
        public Script SetTableChildComment(TableChild tableChild, bool isNew)
        {
            string typeName = tableChild.GetType().Name;

            string type = "";

            if (typeName == nameof(TableColumn))
            {
                type = "COLUMN";
            }
            else if (typeName == nameof(TablePrimaryKey) || typeName == nameof(TableForeignKey) || typeName == nameof(TableConstraint))
            {
                type = "CONSTRAINT";
            }
            else if (typeName == nameof(TableIndex))
            {
                type = "INDEX";
            }

            string sql = $"EXEC {(isNew ? "sp_addextendedproperty" : "sp_updateextendedproperty")} N'MS_Description',N'{this.TransferSingleQuotationString(tableChild.Comment)}',N'SCHEMA',N'{tableChild.Owner}',N'table',N'{tableChild.TableName}',N'{type}',N'{tableChild.Name}'";

            return(new ExecuteProcedureScript(sql));
        }
コード例 #4
0
 private void SetTableChildComment(List <Script> scripts, DbScriptGenerator scriptGenerator, TableChild tableChild, bool isNew)
 {
     if (this.dbInterpreter.DatabaseType == DatabaseType.SqlServer)
     {
         scripts.Add((scriptGenerator as SqlServerScriptGenerator).SetTableChildComment(tableChild, isNew));
     }
 }
コード例 #5
0
 private string GetDropConstraintSql(TableChild tableChild)
 {
     return($"ALTER TABLE {this.GetQuotedFullTableName(tableChild)} DROP CONSTRAINT {this.GetQuotedString(tableChild.Name)};");
 }
コード例 #6
0
        public async Task <List <Script> > GenerateTableChildChangedScripts(DbDifference difference)
        {
            List <Script> scripts = new List <Script>();

            Table targetTable = difference.Parent.Target as Table;

            DbDifferenceType diffType = difference.DifferenceType;

            TableChild source = difference.Source as TableChild;
            TableChild target = difference.Target as TableChild;

            if (diffType == DbDifferenceType.Added)
            {
                scripts.Add(this.targetScriptGenerator.Add(this.CloneTableChild(source, difference.DatabaseObjectType, targetTable.Owner)));
            }
            else if (diffType == DbDifferenceType.Deleted)
            {
                scripts.Add(this.targetScriptGenerator.Drop(target));
            }
            else if (diffType == DbDifferenceType.Modified)
            {
                if (difference.DatabaseObjectType == DatabaseObjectType.TableColumn)
                {
                    SchemaInfoFilter filter = new SchemaInfoFilter()
                    {
                        TableNames = new string[] { source.TableName }
                    };
                    List <TableDefaultValueConstraint> defaultValueConstraints = await this.tableManager.GetTableDefaultConstraints(filter);

                    Table table = new Table()
                    {
                        Owner = targetTable.Owner, Name = target.TableName
                    };

                    scripts.AddRange(this.tableManager.GetColumnAlterScripts(table, table, target as TableColumn, source as TableColumn, defaultValueConstraints));
                }
                else
                {
                    var clonedSource = this.CloneTableChild(difference.Source, difference.DatabaseObjectType, targetTable.Owner);

                    if (difference.DatabaseObjectType == DatabaseObjectType.TablePrimaryKey)
                    {
                        scripts.AddRange(this.tableManager.GetPrimaryKeyAlterScripts(target as TablePrimaryKey, clonedSource as TablePrimaryKey, false));
                    }
                    else if (difference.DatabaseObjectType == DatabaseObjectType.TableForeignKey)
                    {
                        scripts.AddRange(this.tableManager.GetForeignKeyAlterScripts(target as TableForeignKey, clonedSource as TableForeignKey));
                    }
                    else if (difference.DatabaseObjectType == DatabaseObjectType.TableIndex)
                    {
                        scripts.AddRange(this.tableManager.GetIndexAlterScripts(target as TableIndex, clonedSource as TableIndex));
                    }
                    else if (difference.DatabaseObjectType == DatabaseObjectType.TableConstraint)
                    {
                        scripts.AddRange(this.tableManager.GetConstraintAlterScripts(target as TableConstraint, clonedSource as TableConstraint));
                    }
                }
            }

            return(scripts);
        }