Ejemplo n.º 1
0
        /// <summary>
        /// For every table that has the same schema in both databases - compare its
        /// data and update the _result object if necessary.
        /// </summary>
        /// <param name="leftdb">The leftdb.</param>
        /// <param name="rightdb">The rightdb.</param>
        /// <param name="changes">The list of schema changes to check</param>
        private void CompareTables(
            string leftdb, string rightdb,
            Dictionary <SchemaObject, List <SchemaComparisonItem> > changes, bool allowBlobComparison)
        {
            // Go over all tables and select for comparison only those tables that have identical
            // schema.
            List <SchemaComparisonItem> clist = changes[SchemaObject.Table];
            int total  = clist.Count;
            int offset = 0;
            int prev   = -1;

            foreach (SchemaComparisonItem item in clist)
            {
                if (item.Result == ComparisonResult.Same || item.Result == ComparisonResult.DifferentSchema)
                {
                    offset++;
                    double progress = 50.0 + 50.0 * offset / total;
                    if ((int)progress > prev)
                    {
                        prev = (int)progress;
                        NotifyPrimaryProgress(false, prev, "Comparing data for table [" + item.LeftDdlStatement.ObjectName.ToString() + "]..");
                    }

                    IWorker tableComparer =
                        new TableCompareWorker((SQLiteCreateTableStatement)item.LeftDdlStatement,
                                               (SQLiteCreateTableStatement)item.RightDdlStatement, leftdb, rightdb, allowBlobComparison);

                    _tableComparer = new SyncWorker(tableComparer);
                    EventHandler <ProgressEventArgs> eh = new EventHandler <ProgressEventArgs>(delegate(object s, ProgressEventArgs e)
                    {
                        NotifySecondaryProgress(e.IsDone, e.Progress, e.Message);
                    });

                    try
                    {
                        _tableComparer.ProgressChanged += eh;

                        _tableComparer.BeginWork();

                        TableChanges tableChanges = (TableChanges)_tableComparer.Result;
                        item.TableChanges = tableChanges;
                    }
                    catch (UserCancellationException uce)
                    {
                        // Ignore
                    }
                    catch (Exception ex)
                    {
                        // The tables data cannot be compared so ignore.
                        item.ErrorMessage = ex.Message;
                    }
                    finally
                    {
                        _tableComparer.ProgressChanged -= eh;
                        _tableComparer = null;
                    }

                    if (_cancelled)
                    {
                        throw new UserCancellationException();
                    }
                }
            } // foreach
        }
        private void btnCompareData_Click(object sender, EventArgs e)
        {
            // Before comparing data we have to check if there are any BLOB columns
            // in the any common columns of the tables. If there are any - we have to
            // ask the user if he wants to compare BLOB fields or not.
            SQLiteCreateTableStatement   leftTable  = _item.LeftDdlStatement as SQLiteCreateTableStatement;
            SQLiteCreateTableStatement   rightTable = _item.RightDdlStatement as SQLiteCreateTableStatement;
            List <SQLiteColumnStatement> common     = Utils.GetCommonColumns(leftTable, rightTable);
            bool allowBlobComparison = false;

            if (Utils.ContainsBlobColumn(common))
            {
                DialogResult res = MessageBox.Show(this,
                                                   "At least one column that will be compared is a BLOB.\r\nComparing BLOB fields can potentially take " +
                                                   "a lot of time to perform.\r\nDo you want to disable BLOB content comparison in order\r\nto make " +
                                                   "the comparison go faster?",
                                                   "Disable BLOB Contents Comparison?",
                                                   MessageBoxButtons.YesNo,
                                                   MessageBoxIcon.Warning);
                if (res == DialogResult.No)
                {
                    allowBlobComparison = true;
                }
            }

            string errmsg;

            if (!Utils.IsTableComparisonAllowed(leftTable, rightTable,
                                                out errmsg, allowBlobComparison))
            {
                MessageBox.Show(this, errmsg, "Data comparison is not allowed",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            TableCompareWorker worker = new TableCompareWorker(
                leftTable, rightTable, _leftdb, _rightdb, allowBlobComparison);
            ProgressDialog dlg = new ProgressDialog();

            dlg.Start(this, worker);
            if (dlg.Error != null)
            {
                if (dlg.Error.GetType() != typeof(UserCancellationException))
                {
                    _item.ErrorMessage = dlg.Error.Message;
                }
                return;
            }
            _tableChanges = (TableChanges)dlg.Result;
            if (!tbcViews.TabPages.Contains(tbpData))
            {
                tbcViews.TabPages.Add(tbpData);
            }

            // Update the schema comparison item
            panel1.Visible     = false;
            _item.ErrorMessage = null;
            _item.TableChanges = _tableChanges;
            if (SchemaChanged != null)
            {
                SchemaChanged(this, EventArgs.Empty);
            }

            // Set the table changes object into the table diff control
            UpdateDataTab();

            tbcViews.SelectedTab = tbpData;
        }