Exemplo n.º 1
0
        private void compareFieldExistence()
        {
            TableFieldDifferences = new ObservableCollection <a7DbTableFieldDifferences>();
            _log("Checking columns of Db " + DbAName);
            var alreadyWrittenTableFieldDifferences = new Dictionary <string, a7DbTableFieldDifferences>();

            foreach (var tb in DbA.Tables)
            {
                var table    = tb as Table;
                var tableOnB = DbB.Tables[table.Name];
                if (tableOnB == null)
                {
                    continue;
                }
                var tableFieldDiffOnlyInA = new a7DbTableFieldDifferences(table.Name, this);
                var someFieldDifferent    = false;
                var pos = 0;
                foreach (var fld in table.Columns)
                {
                    var col = fld as Column;
                    _log(string.Format("Field in '{2}' in Table '{0}' in db '{1}'", table.Name, DbA.Name, col.Name));
                    var colOnB = tableOnB.Columns[col.Name];
                    if (colOnB == null)
                    {
                        someFieldDifferent = true;
                        var copyToItem =
                            new a7DbTableFieldCopyTo(_dbName, col, pos, table, tableOnB, DbA, DbB, this, tableFieldDiffOnlyInA);
                        tableFieldDiffOnlyInA.TableFieldsOnlyInA.Add(copyToItem);
                    }
                    else
                    {
                        if (col.DataType.MaximumLength != colOnB.DataType.MaximumLength ||
                            col.DataType.SqlDataType != colOnB.DataType.SqlDataType)
                        {
                            someFieldDifferent = true;
                            tableFieldDiffOnlyInA.TableFieldsDifferentType.Add(new a7DbTableFieldDifferent(col, colOnB, table, tableOnB, DbA, DbB, this, tableFieldDiffOnlyInA));
                            //a7DbTableFieldDifferences tableFieldDifferent;
                            //if (alreadyWrittenTableFieldDifferences.ContainsKey(table.Name))
                            //{
                            //    tableFieldDifferent = alreadyWrittenTableFieldDifferences[table.Name];
                            //}
                            //else
                            //{
                            //    tableFieldDifferent = new a7DbTableFieldDifferences(table.Name, this);
                            //    alreadyWrittenTableFieldDifferences[table.Name] = tableFieldDifferent;
                            //}
                            //tableFieldDifferent.TableFieldsOnlyInA.Add(new a7DbTableFieldDifferent(col, colOnB, table, tableOnB, DbA, DbB, this));
                        }
                    }
                    pos++;
                }
                if (someFieldDifferent)
                {
                    TableFieldDifferences.Add(tableFieldDiffOnlyInA);
                }
            }

            //foreach (var tableDifferent in alreadyWrittenTableFieldDifferences.Values)
            //    this.TableFieldDifferences.Add(tableDifferent);

            _log("Checking columns of Db " + DbBName);
            foreach (var tb in DbB.Tables)
            {
                var table    = tb as Table;
                var tableOnA = DbA.Tables[table.Name];
                if (tableOnA == null)
                {
                    continue;
                }

                var tableFieldDiffOnlyInA = TableFieldDifferences.FirstOrDefault((s) => s.TableName == table.Name);
                var addA = false;
                if (tableFieldDiffOnlyInA == null)
                {
                    addA = true;
                    tableFieldDiffOnlyInA = new a7DbTableFieldDifferences(table.Name, this);
                }
                var someFieldDifferent = false;
                var pos = 0;
                foreach (var fld in table.Columns)
                {
                    var col = fld as Column;
                    _log(string.Format("Field in '{2}' in Table '{0}' in db '{1}'", table.Name, DbB.Name, col.Name));
                    var colOnA = tableOnA.Columns[col.Name];
                    if (colOnA == null)
                    {
                        someFieldDifferent = true;
                        var copyTo =
                            new a7DbTableFieldCopyTo(_dbName, col, pos, table, tableOnA, DbB, DbA, this, tableFieldDiffOnlyInA);
                        tableFieldDiffOnlyInA.TableFieldsOnlyInB.Add(copyTo);
                    }
                    pos++;
                }
                if (someFieldDifferent && addA)
                {
                    TableFieldDifferences.Add(tableFieldDiffOnlyInA);
                }
            }
        }
Exemplo n.º 2
0
 public a7DbTableFieldDifferent(Column colA, Column colB, Table tableA, Table tableB, Database dbA, Database dbB, a7DbStructureComparer comparer, a7DbTableFieldDifferences differences)
 {
     DbAName = comparer.DbAName;
     DbBName = comparer.DbBName;
     TypeInA = colA.DataType.ToString();
     if (colA.DataType.SqlDataType == SqlDataType.VarChar || colA.DataType.SqlDataType == SqlDataType.NVarChar ||
         colA.DataType.SqlDataType == SqlDataType.Char || colA.DataType.SqlDataType == SqlDataType.NChar)
     {
         TypeInA += "(" + colA.DataType.MaximumLength + ")";
     }
     TypeInB = colB.DataType.ToString();
     if (colB.DataType.SqlDataType == SqlDataType.VarChar || colB.DataType.SqlDataType == SqlDataType.NVarChar ||
         colB.DataType.SqlDataType == SqlDataType.Char || colB.DataType.SqlDataType == SqlDataType.NChar)
     {
         TypeInB += "(" + colB.DataType.MaximumLength + ")";
     }
     Text        = colA.Name;
     CopyTypeToA = new a7LambdaCommand((o) =>
     {
         comparer.IsBusy = true;
         Task.Factory.StartNew(() =>
         {
             if ((colA.DataType.SqlDataType == SqlDataType.VarChar &&
                  colB.DataType.SqlDataType == SqlDataType.VarChar) ||
                 (colA.DataType.SqlDataType == SqlDataType.NVarChar &&
                  colB.DataType.SqlDataType == SqlDataType.NVarChar))
             {
                 colA.DataType.MaximumLength = colB.DataType.MaximumLength;
                 tableA.Alter();
                 Application.Current.Dispatcher.Invoke(() =>
                 {
                     differences.TableFieldsDifferentType.Remove(this);
                 });
             }
             else
             {
                 Application.Current.Dispatcher.Invoke(() =>
                 {
                     MessageBox.Show("Not supported for non-varchar types");
                 });
             }
             Application.Current.Dispatcher.Invoke(() =>
             {
                 comparer.IsBusy = false;
             });
         }).ContinueWith((t) =>
         {
             if (t.Exception != null)
             {
                 throw t.Exception;
             }
         });
     }
                                       );
     CopyTypeToB = new a7LambdaCommand((o) =>
     {
         comparer.IsBusy = true;
         if ((colA.DataType.SqlDataType == SqlDataType.VarChar &&
              colB.DataType.SqlDataType == SqlDataType.VarChar) ||
             (colA.DataType.SqlDataType == SqlDataType.NVarChar &&
              colB.DataType.SqlDataType == SqlDataType.NVarChar))
         {
             colB.DataType.MaximumLength = colA.DataType.MaximumLength;
             tableB.Alter();
             Application.Current.Dispatcher.Invoke(() =>
             {
                 differences.TableFieldsDifferentType.Remove(this);
             });
         }
         else
         {
             Application.Current.Dispatcher.Invoke(() =>
             {
                 MessageBox.Show("Not supported for non-varchar types");
             });
         }
         Application.Current.Dispatcher.Invoke(() =>
         {
             comparer.IsBusy = false;
         });
     }
                                       );
 }
        public a7DbTableFieldCopyTo(string dbName, Column column, int colPos, Table tableExists, Table tableNotExists, Database isInDb, Database isNotIntDb, a7DbStructureComparer comparer, a7DbTableFieldDifferences tableFieldDifferencesExists)
        {
            Text = column.Name;

            AddToOtherDbName  = isNotIntDb.Name;
            AddToOtherCommand = new a7LambdaCommand((o) =>
            {
                comparer.IsBusy = true;
                Task.Factory.StartNew(() =>
                {
                    a7DbTableUtils.CopyColumn(column, tableExists, tableNotExists, new Server(dbName), colPos);
                    tableNotExists.Alter();
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        tableFieldDifferencesExists.TableFieldsOnlyInA.Remove(this);
                        tableFieldDifferencesExists.TableFieldsOnlyInB.Remove(this);
                        comparer.IsBusy = false;
                    });
                }).ContinueWith((t) =>
                {
                    if (t.Exception != null)
                    {
                        throw t.Exception;
                    }
                });
            }
                                                    );
            RemoveFromThisDbName  = isInDb.Name;
            RemoveFromThisCommand = new a7LambdaCommand((o) =>
            {
                comparer.IsBusy = true;
                Task.Factory.StartNew(() =>
                {
                    column.Drop();
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        tableFieldDifferencesExists.TableFieldsOnlyInA.Remove(this);
                        tableFieldDifferencesExists.TableFieldsOnlyInB.Remove(this);
                        comparer.IsBusy = false;
                    });
                });
            }
                                                        );
        }