Exemplo n.º 1
0
        public void GenerateDataDiffScript(object sender, ExecutedRoutedEventArgs e)
        {
            try
            {
                var menuInfo = ValidateMenuInfo(sender);
                var package = ParentWindow.Package as SqlCeToolboxPackage;

                if (menuInfo == null) return;

                Dictionary<string, DatabaseInfo> databaseList = Helpers.DataConnectionHelper.GetDataConnections(package, true, false);
                foreach (KeyValuePair<string, DatabaseInfo> info in Helpers.DataConnectionHelper.GetOwnDataConnections())
                {
                    if (!databaseList.ContainsKey(info.Key))
                        databaseList.Add(info.Key, info.Value);
                }
                foreach (KeyValuePair<string, DatabaseInfo> info in databaseList)
                {
                    string sourceType = string.Empty;
                    switch (info.Value.DatabaseType)
                    {
                        case DatabaseType.SQLCE35:
                            sourceType = "3.5";
                            break;
                        case DatabaseType.SQLCE40:
                            sourceType = "4.0";
                            break;
                        case DatabaseType.SQLServer:
                            sourceType = "Server";
                            break;
                    }
                    info.Value.Caption = string.Format("{0} ({1})", info.Value.Caption, sourceType);
                }

                CompareDialog cd = new CompareDialog(menuInfo.DatabaseInfo.Caption, databaseList, menuInfo.Name);

                bool? result = cd.ShowModal();
                if (result.HasValue && result.Value == true && (cd.TargetDatabase.Key != null))
                {
                    var target = cd.TargetDatabase;
                    var source = new KeyValuePair<string, DatabaseInfo>(menuInfo.DatabaseInfo.ConnectionString, menuInfo.DatabaseInfo);
                    var editorTarget = target;
                    if (editorTarget.Value.DatabaseType == DatabaseType.SQLServer)
                    {
                        editorTarget = source;
                    }

                    using (IRepository sourceRepository = Helpers.DataConnectionHelper.CreateRepository(source.Value))
                    {
                        using (IRepository targetRepository = Helpers.DataConnectionHelper.CreateRepository(target.Value))
                        {
                            var generator = Helpers.DataConnectionHelper.CreateGenerator(targetRepository, target.Value.DatabaseType);
                            try
                            {
                                var script = SqlCeDiff.CreateDataDiffScript(sourceRepository, menuInfo.Name, targetRepository, menuInfo.Name, generator);

                                var sqlEditorWindow = package.CreateWindow<SqlEditorWindow>();
                                var editorControl = sqlEditorWindow.Content as SqlEditorControl;
                                editorControl.ExplorerControl = ParentWindow.Content as ExplorerControl;
                                Debug.Assert(editorControl != null);
                                editorControl.DatabaseInfo = editorTarget.Value;
                                editorControl.SqlText = script;
                                Helpers.DataConnectionHelper.LogUsage("TableScriptDataDiff");
                            }
                            catch (Exception ex)
                            {
                                Helpers.DataConnectionHelper.SendError(ex, source.Value.DatabaseType, false);
                            }

                        }
                    }
                }
            }
            catch (ArgumentException ae)
            {
                EnvDTEHelper.ShowError(ae.Message);
            }
            catch (Exception ex)
            {
                Helpers.DataConnectionHelper.SendError(ex, DatabaseType.SQLCE35, false);
            }
        }
        public void GenerateDiffScript(object sender, ExecutedRoutedEventArgs e)
        {
            try
            {
                var databaseInfo = ValidateMenuInfo(sender);
                if (databaseInfo == null) return;

                Dictionary<string, DatabaseInfo> databaseList = DataConnectionHelper.GetDataConnections(package, true, false);
                foreach (KeyValuePair<string, DatabaseInfo> info in DataConnectionHelper.GetOwnDataConnections())
                {
                    if (!databaseList.ContainsKey(info.Key))
                        databaseList.Add(info.Key, info.Value);
                }
                foreach (KeyValuePair<string, DatabaseInfo> info in databaseList)
                {
                    string sourceType = string.Empty;
                    switch (info.Value.DatabaseType)
                    {
                        case DatabaseType.SQLCE35:
                            sourceType = "3.5";
                            break;
                        case DatabaseType.SQLCE40:
                            sourceType = "4.0";
                            break;
                        case DatabaseType.SQLServer:
                            sourceType = "Server";
                            break;
                    }
                    info.Value.Caption = string.Format("{0} ({1})", info.Value.Caption, sourceType);
                }

                CompareDialog cd = new CompareDialog(databaseInfo.DatabaseInfo.Caption, databaseList);

                bool? result = cd.ShowModal();
                if (result.HasValue && result.Value == true && (cd.TargetDatabase.Key != null))
                {
                    var target = cd.TargetDatabase;
                    var swap = cd.SwapTarget;

                    var source = new KeyValuePair<string, DatabaseInfo>(databaseInfo.DatabaseInfo.ConnectionString, databaseInfo.DatabaseInfo);
                    if (swap)
                    {
                        source = target;
                        target = new KeyValuePair<string, DatabaseInfo>(databaseInfo.DatabaseInfo.ConnectionString, databaseInfo.DatabaseInfo);
                    }

                    var editorTarget = target;
                    if (editorTarget.Value.DatabaseType == DatabaseType.SQLServer)
                    {
                        editorTarget = source;
                    }

                    using (IRepository sourceRepository = Helpers.DataConnectionHelper.CreateRepository(source.Value))
                    {
                        var generator = Helpers.DataConnectionHelper.CreateGenerator(sourceRepository, databaseInfo.DatabaseInfo.DatabaseType);
                        using (IRepository targetRepository = Helpers.DataConnectionHelper.CreateRepository(target.Value))
                        {
                            try
                            {
                                SqlCeDiff.CreateDiffScript(sourceRepository, targetRepository, generator, Properties.Settings.Default.DropTargetTables);

                                var sqlEditorWindow = package.CreateWindow<SqlEditorWindow>();
                                var editorControl = sqlEditorWindow.Content as SqlEditorControl;
                                editorControl.ExplorerControl = _parentWindow.Content as ExplorerControl;
                                Debug.Assert(editorControl != null);
                                editorControl.DatabaseInfo = editorTarget.Value;

                                string explain = @"-- This database diff script contains the following objects:
-- - Tables:  Any that are not in the destination
-- -          (tables that are only in the destination are NOT dropped, unless that option is set)
-- - Columns: Any added, deleted, changed columns for existing tables
-- - Indexes: Any added, deleted indexes for existing tables
-- - Foreign keys: Any added, deleted foreign keys for existing tables
-- ** Make sure to test against a production version of the destination database! ** " + Environment.NewLine + Environment.NewLine;

                                if (swap)
                                {
                                    explain += "-- ** Please note that the soruce and target have been swapped! ** " + Environment.NewLine + Environment.NewLine;
                                }

                                editorControl.SqlText = explain + generator.GeneratedScript;
                                Helpers.DataConnectionHelper.LogUsage("DatabaseScriptDiff");
                            }
                            catch (Exception ex)
                            {
                                Helpers.DataConnectionHelper.SendError(ex, DatabaseType.SQLCE35);
                            }

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Helpers.DataConnectionHelper.SendError(ex, DatabaseType.SQLCE35);
            }

        }