コード例 #1
0
        private async void GeneateChangeScripts()
        {
            this.EndControlsEdit();

            SchemaDesignerInfo schemaDesignerInfo = this.GetSchemaDesingerInfo();

            TableManager tableManager = this.GetTableManager();

            this.Feedback("Generating changed scripts...");

            ContentSaveResult result = await tableManager.GenerateChangeScripts(schemaDesignerInfo, this.displayInfo.IsNew);

            this.Feedback("End generate changed scripts.");

            if (!result.IsOK)
            {
                MessageBox.Show(result.Message);
            }
            else
            {
                TableDesignerGenerateScriptsData scriptsData = result.ResultData as TableDesignerGenerateScriptsData;

                string scripts = string.Join(Environment.NewLine, scriptsData.Scripts.Select(item => item.Content));

                frmScriptsViewer scriptsViewer = new frmScriptsViewer()
                {
                    DatabaseType = this.displayInfo.DatabaseType
                };
                scriptsViewer.LoadScripts(StringHelper.ToSingleEmptyLine(scripts).Trim());

                scriptsViewer.ShowDialog();
            }
        }
コード例 #2
0
        public ContentSaveResult Save(ContentSaveInfo info)
        {
            this.EndControlsEdit();

            ContentSaveResult result = Task.Run(() => this.SaveTable()).Result;

            if (!result.IsOK)
            {
                MessageBox.Show(result.Message);
            }
            else
            {
                this.Feedback("Table saved.");

                Table table = result.ResultData as Table;

                this.displayInfo.DatabaseObject = table;
                this.ucColumns.OnSaved();
                this.ucIndexes.OnSaved();
                this.ucForeignKeys.OnSaved();
                this.ucConstraints.OnSaved();

                if (this.displayInfo.IsNew || table.Name != this.displayInfo.Name)
                {
                    if (FormEventCenter.OnRefreshNavigatorFolder != null)
                    {
                        FormEventCenter.OnRefreshNavigatorFolder();
                    }
                }
            }

            return(result);
        }
コード例 #3
0
        public async Task<ContentSaveResult> Save(SchemaDesignerInfo schemaDesignerInfo, bool isNew)
        {
            Table table = null;

            try
            {
                ContentSaveResult result = await this.GenerateChangeScripts(schemaDesignerInfo, isNew);

                if (!result.IsOK)
                {
                    return result;
                }

                TableDesignerGenerateScriptsData scriptsData = result.ResultData as TableDesignerGenerateScriptsData;

                List<Script> scripts = scriptsData.Scripts;

                table = scriptsData.Table;

                if (scripts == null || scripts.Count == 0)
                {
                    return this.GetFaultSaveResult("No changes need to save.");
                }

                ScriptRunner scriptRunner = new ScriptRunner();

                await scriptRunner.Run(this.dbInterpreter, scripts);
            }
            catch (Exception ex)
            {
                string errMsg = ExceptionHelper.GetExceptionDetails(ex);

                this.FeedbackError(errMsg);

                return this.GetFaultSaveResult(errMsg);
            }

            return new ContentSaveResult() { IsOK = true, ResultData = table };
        }
コード例 #4
0
        private void Save()
        {
            if (this.tabControl1.TabCount < 1)
            {
                return;
            }

            TabPage tabPage = this.tabControl1.SelectedTab;

            if (tabPage == null)
            {
                return;
            }

            DatabaseObjectDisplayInfo displayInfo = tabPage.Tag as DatabaseObjectDisplayInfo;

            if (displayInfo == null)
            {
                return;
            }

            DatabaseObjectDisplayType displayType = displayInfo.DisplayType;

            if (displayType == DatabaseObjectDisplayType.Script || displayType == DatabaseObjectDisplayType.Data)
            {
                if (File.Exists(displayInfo.FilePath))
                {
                    this.SaveToFile(tabPage, displayInfo.FilePath);
                    return;
                }

                if (this.dlgSave == null)
                {
                    this.dlgSave = new SaveFileDialog();
                }

                this.dlgSave.FileName = tabPage.Text.Trim();

                if (displayType == DatabaseObjectDisplayType.Script)
                {
                    this.dlgSave.Filter = "sql file|*.sql|txt file|*.txt";
                }
                else if (displayType == DatabaseObjectDisplayType.Data)
                {
                    this.dlgSave.Filter = "csv file|*.csv|txt file|*.txt";
                }

                DialogResult result = this.dlgSave.ShowDialog();

                if (result == DialogResult.OK)
                {
                    string filePath = this.dlgSave.FileName;

                    this.SaveToFile(tabPage, filePath);

                    displayInfo.FilePath = filePath;

                    string name = Path.GetFileNameWithoutExtension(filePath);

                    displayInfo.IsNew = false;
                    displayInfo.Name  = name;

                    tabPage.Text = this.GetFormatTabHeaderText(name);

                    this.SetTabPageTooltip(tabPage);
                }
            }
            else if (displayType == DatabaseObjectDisplayType.TableDesigner)
            {
                UC_TableDesigner  tableDesigner = this.GetUcControl <UC_TableDesigner>(tabPage);
                ContentSaveResult result        = tableDesigner.Save(new ContentSaveInfo()
                {
                });

                if (result.IsOK)
                {
                    Table table = result.ResultData as Table;

                    displayInfo.IsNew = false;
                    displayInfo.Name  = table.Name;

                    tabPage.Text = this.GetFormatTabHeaderText(displayInfo.Name);

                    this.SetTabPageTooltip(tabPage);
                }
            }
        }
コード例 #5
0
        public async Task <ContentSaveResult> Save(SchemaDesignerInfo schemaDesignerInfo, bool isNew)
        {
            Table table = null;

            try
            {
                using (DbConnection dbConnection = this.dbInterpreter.CreateConnection())
                {
                    dbConnection.Open();

                    DbTransaction transaction = dbConnection.BeginTransaction();

                    ContentSaveResult result = await this.GenerateChangeScripts(schemaDesignerInfo, isNew);

                    if (!result.IsOK)
                    {
                        return(result);
                    }

                    TableDesignerGenerateScriptsData scriptsData = result.ResultData as TableDesignerGenerateScriptsData;

                    List <Script> scripts = scriptsData.Scripts;

                    table = scriptsData.Table;

                    if (scripts == null || scripts.Count == 0)
                    {
                        return(this.GetFaultSaveResult("No change need to save."));
                    }

                    Func <Script, bool> isValidScript = (s) =>
                    {
                        return(!(s is NewLineSript || s is SpliterScript || string.IsNullOrEmpty(s.Content) || s.Content == this.dbInterpreter.ScriptsDelimiter));
                    };

                    int count = scripts.Where(item => isValidScript(item)).Count();
                    int i     = 0;

                    foreach (Script s in scripts)
                    {
                        if (!isValidScript(s))
                        {
                            continue;
                        }

                        string sql = s.Content?.Trim();

                        if (!string.IsNullOrEmpty(sql) && sql != this.dbInterpreter.ScriptsDelimiter)
                        {
                            i++;

                            if (this.dbInterpreter.ScriptsDelimiter.Length == 1 && sql.EndsWith(this.dbInterpreter.ScriptsDelimiter))
                            {
                                sql = sql.TrimEnd(this.dbInterpreter.ScriptsDelimiter.ToArray());
                            }

                            if (!this.dbInterpreter.HasError)
                            {
                                CommandInfo commandInfo = new CommandInfo()
                                {
                                    CommandType = CommandType.Text,
                                    CommandText = sql,
                                    Transaction = transaction
                                };

                                await this.dbInterpreter.ExecuteNonQueryAsync(dbConnection, commandInfo);
                            }
                        }
                    }

                    transaction.Commit();
                }
            }
            catch (Exception ex)
            {
                string errMsg = ExceptionHelper.GetExceptionDetails(ex);

                this.FeedbackError(errMsg);

                return(this.GetFaultSaveResult(errMsg));
            }

            return(new ContentSaveResult()
            {
                IsOK = true, ResultData = table
            });
        }
コード例 #6
0
        private async Task GenerateOrSync(bool isSync, DbDifference difference = null)
        {
            if (this.sourceInterpreter == null || this.targetInterpreter == null || this.differences == null)
            {
                MessageBox.Show("Please compare first.");
                return;
            }

            try
            {
                DbSynchro dbSynchro = new DbSynchro(this.sourceInterpreter, this.targetInterpreter);

                if (!isSync)
                {
                    List <Script> scripts = null;

                    string targetDbOwner = this.GetTargetDbOwner();

                    if (difference == null)
                    {
                        scripts = await dbSynchro.GenerateChangedScripts(this.sourceSchemaInfo, targetDbOwner, this.differences);
                    }
                    else if (difference.Source is ScriptDbObject || difference.Target is ScriptDbObject)
                    {
                        scripts = dbSynchro.GenereateUserDefinedTypeChangedScripts(difference, targetDbOwner);
                    }
                    else if (difference.DatabaseObjectType == DatabaseObjectType.Table)
                    {
                        scripts = await dbSynchro.GenerateTableChangedScripts(this.sourceSchemaInfo, difference, targetDbOwner);
                    }
                    else if (difference.Source is TableChild || difference.Target is TableChild)
                    {
                        scripts = await dbSynchro.GenerateTableChildChangedScripts(difference);
                    }
                    else if (difference.Source is UserDefinedType || difference.Target is UserDefinedType)
                    {
                        scripts = dbSynchro.GenereateUserDefinedTypeChangedScripts(difference, targetDbOwner);
                    }

                    if (scripts != null)
                    {
                        string strScripts = string.Join(Environment.NewLine, scripts.Select(item => item.Content));

                        frmScriptsViewer scriptsViewer = new frmScriptsViewer()
                        {
                            DatabaseType = this.targetInterpreter.DatabaseType
                        };
                        scriptsViewer.LoadScripts(StringHelper.ToSingleEmptyLine(strScripts).Trim());

                        scriptsViewer.ShowDialog();
                    }
                }
                else
                {
                    if (MessageBox.Show("Are you sure to sync changes to target database?", "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        ContentSaveResult result = await dbSynchro.Sync(this.sourceSchemaInfo, this.GetTargetDbOwner(), this.differences);

                        if (result.IsOK)
                        {
                            MessageBox.Show("sync successfully.");
                        }
                        else
                        {
                            MessageBox.Show(result.Message);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.HandleException(ex);
            }
        }