public virtual void AppendScriptsToFile(string content, GenerateScriptMode generateScriptMode, bool overwrite = false)
        {
            if (generateScriptMode == GenerateScriptMode.Schema)
            {
                content = StringHelper.ToSingleEmptyLine(content);
            }

            string filePath = this.GetScriptOutputFilePath(generateScriptMode);

            string directoryName = Path.GetDirectoryName(filePath);

            if (!Directory.Exists(directoryName))
            {
                Directory.CreateDirectory(directoryName);
            }

            if (!overwrite)
            {
                File.AppendAllText(filePath, content, Encoding.UTF8);
            }
            else
            {
                File.WriteAllText(filePath, content, Encoding.UTF8);
            }
        }
Пример #2
0
        private bool ValidateInputs()
        {
            string name = this.txtName.Text.Trim();

            if (string.IsNullOrEmpty(name))
            {
                MessageBox.Show("Name can't be empty.");
                return(false);
            }

            if (this.rbAnotherDatabase.Checked)
            {
                if (this.targetDbConnectionInfo == null)
                {
                    MessageBox.Show("Please specify target database connection.");
                    return(false);
                }
            }

            GenerateScriptMode scriptMode = this.GetGenerateScriptMode();

            if (scriptMode == GenerateScriptMode.None)
            {
                MessageBox.Show("Please specify the script mode.");
                return(false);
            }

            return(true);
        }
        public string GetScriptOutputFilePath(GenerateScriptMode generateScriptMode)
        {
            string fileName = $"{this.dbInterpreter.ConnectionInfo.Database}_{this.databaseType}_{DateTime.Today.ToString("yyyyMMdd")}_{generateScriptMode.ToString()}.sql";
            string filePath = Path.Combine(this.option.ScriptOutputFolder, fileName);

            return(filePath);
        }
Пример #4
0
        public void GenerateScript(GenerateScriptMode mode)
        {
            try
            {
                if (!string.IsNullOrEmpty(txtAppName.Text) && !string.IsNullOrEmpty(txtPublisher.Text) &&
                    !string.IsNullOrEmpty(txtId.Text) && !string.IsNullOrEmpty(txtVersion.Text) &&
                    !string.IsNullOrEmpty(txtLicense.Text) && !string.IsNullOrEmpty(txtUrl.Text) && txtUrl.Text.IsUrl())
                {
                    var builder = new YamlPackageModel
                    {
                        PackageIdentifier = txtId.Text,
                        PackageVersion    = txtVersion.Text,
                        PackageName       = txtAppName.Text,
                        Publisher         = txtPublisher.Text,
                        License           = txtLicense.Text,
                        LicenseUrl        = txtLicenseUrl.Text,
                        ShortDescription  = txtDescription.Text,
                        PackageUrl        = txtHomePage.Text,
                        ManifestType      = "singleton",
                        ManifestVersion   = "1.0.0",
                        PackageLocale     = "en-US",
                        Installers        = Installers.ToList()
                    };

                    var serializer = new SerializerBuilder().Build();
                    var yaml       = serializer.Serialize(builder);
                    switch (mode)
                    {
                    case GenerateScriptMode.CopyToClipboard:
                        Clipboard.SetText(yaml);
                        Growl.SuccessGlobal("Script Copied to clipboard.");
                        ClearInputs();
                        break;

                    case GenerateScriptMode.SaveToFile:
                        var dialog = new SaveFileDialog();
                        dialog.Title      = "Save Package";
                        dialog.FileName   = $"{txtId.Text}.yaml";
                        dialog.DefaultExt = "yaml";
                        dialog.Filter     = "Yaml File (*.yaml)|*.yaml";
                        if (dialog.ShowDialog() == true)
                        {
                            File.WriteAllText(dialog.FileName, yaml);
                            ClearInputs();
                        }

                        break;
                    }
                }
                else
                {
                    Growl.ErrorGlobal("Required fields must be filled");
                }
            }
            catch (Exception ex)
            {
                Growl.ErrorGlobal(ex.Message);
            }
        }
        public void ClearScriptFile(GenerateScriptMode generateScriptMode)
        {
            string filePath = this.GetScriptOutputFilePath(generateScriptMode);

            if (File.Exists(filePath))
            {
                File.WriteAllText(filePath, "", Encoding.UTF8);
            }
        }
Пример #6
0
        private void SourceScriptBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            if (this.sourceScriptBackgroundWorker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }

            SchemaInfo schemaInfo = this.GetSourceTreeSchemaInfo();

            if (!this.ValidateSource(schemaInfo))
            {
                return;
            }

            DatabaseType sourceDbType = this.GetDatabaseType(this.cboSourceDB.Text);

            int dataBatchSize = SettingManager.Setting.DataBatchSize;
            GenerateScriptOption sourceScriptOption = new GenerateScriptOption()
            {
                ScriptOutputMode = GenerateScriptOutputMode.None, DataBatchSize = dataBatchSize
            };

            this.SetGenerateScriptOption(sourceScriptOption);

            GenerateScriptMode scriptMode = this.GetGenerateScriptMode();

            if (scriptMode == GenerateScriptMode.None)
            {
                MessageBox.Show("Please specify the script mode.");
                return;
            }

            DbInterpreter dbInterpreter = DbInterpreterHelper.GetDbInterpreter(sourceDbType, this.sourceDbConnectionInfo, sourceScriptOption);

            string[] tableNames = schemaInfo.Tables.Select(item => item.Name).ToArray();
            schemaInfo = dbInterpreter.GetSchemaInfo(tableNames);

            dbInterpreter.Subscribe(this);

            if (scriptMode.HasFlag(GenerateScriptMode.Schema))
            {
                dbInterpreter.GenerateSchemaScripts(schemaInfo);
            }

            if (scriptMode.HasFlag(GenerateScriptMode.Data))
            {
                dbInterpreter.GenerateDataScripts(schemaInfo);
            }

            MessageBox.Show(DONE);
        }
Пример #7
0
        private GenerateScriptMode GetGenerateScriptMode()
        {
            GenerateScriptMode scriptMode = GenerateScriptMode.None;

            if (this.chkScriptSchema.Checked)
            {
                scriptMode = scriptMode | GenerateScriptMode.Schema;
            }
            if (this.chkScriptData.Checked)
            {
                scriptMode = scriptMode | GenerateScriptMode.Data;
            }

            return(scriptMode);
        }
Пример #8
0
        public virtual void AppendScriptsToFile(string content, GenerateScriptMode generateScriptMode, bool clearAll = false)
        {
            string fileName = $"{this.ConnectionInfo.Database}_{this.GetType().Name.Replace("Interpreter", "")}_{DateTime.Today.ToString("yyyyMMdd")}_{generateScriptMode.ToString()}.sql";
            string filePath = Path.Combine(Option.ScriptOutputFolder, fileName);

            string directoryName = Path.GetDirectoryName(filePath);

            if (!Directory.Exists(directoryName))
            {
                Directory.CreateDirectory(directoryName);
            }

            if (!clearAll)
            {
                File.AppendAllText(filePath, content, Encoding.UTF8);
            }
            else
            {
                File.WriteAllText(filePath, content, Encoding.UTF8);
            }
        }
Пример #9
0
        public async Task Convert()
        {
            DatabaseType sourceDbType = this.sourceInterpreter.DatabaseType;
            DatabaseType targetDbType = this.targetInterpreter.DatabaseType;

            int dataBatchSize = 500;

            DbInterpreterOption sourceScriptOption = new DbInterpreterOption()
            {
                ScriptOutputMode = GenerateScriptOutputMode.WriteToString
            };
            DbInterpreterOption targetScriptOption = new DbInterpreterOption()
            {
                ScriptOutputMode = (GenerateScriptOutputMode.WriteToFile | GenerateScriptOutputMode.WriteToString)
            };

            this.sourceInterpreter.Option = sourceScriptOption;
            this.targetInterpreter.Option = targetScriptOption;

            GenerateScriptMode scriptMode = GenerateScriptMode.Schema | GenerateScriptMode.Data;

            DbConveterInfo source = new DbConveterInfo()
            {
                DbInterpreter = sourceInterpreter
            };
            DbConveterInfo target = new DbConveterInfo()
            {
                DbInterpreter = targetInterpreter
            };

            try
            {
                using (DbConverter dbConverter = new DbConverter(source, target))
                {
                    dbConverter.Option.GenerateScriptMode = scriptMode;

                    dbConverter.Subscribe(this);

                    if (sourceDbType == DatabaseType.MySql)
                    {
                        source.DbInterpreter.Option.InQueryItemLimitCount = 2000;
                    }

                    if (targetDbType == DatabaseType.SqlServer)
                    {
                        target.DbOwner = "dbo";
                    }
                    else if (targetDbType == DatabaseType.MySql)
                    {
                        target.DbInterpreter.Option.RemoveEmoji = true;
                    }

                    dbConverter.Option.SplitScriptsToExecute = true;

                    FeedbackHelper.EnableLog = true;

                    await dbConverter.Convert();
                }
            }
            catch (Exception ex)
            {
                string msg = ExceptionHelper.GetExceptionDetails(ex);

                this.Feedback(new FeedbackInfo()
                {
                    InfoType = FeedbackInfoType.Error, Message = msg
                });
            }
        }
Пример #10
0
        private async void GenerateScourceDbScripts()
        {
            SchemaInfo schemaInfo = this.GetSourceTreeSchemaInfo();

            if (!this.ValidateSource(schemaInfo))
            {
                return;
            }

            this.btnGenerateSourceScripts.Enabled = false;

            DatabaseType sourceDbType = this.GetDatabaseType(this.cboSourceDB.Text);

            int dataBatchSize = SettingManager.Setting.DataBatchSize;
            DbInterpreterOption sourceScriptOption = new DbInterpreterOption()
            {
                ScriptOutputMode = GenerateScriptOutputMode.WriteToFile, DataBatchSize = dataBatchSize
            };

            this.SetGenerateScriptOption(sourceScriptOption);

            GenerateScriptMode scriptMode = this.GetGenerateScriptMode();

            if (scriptMode == GenerateScriptMode.None)
            {
                MessageBox.Show("Please specify the script mode.");
                return;
            }

            DbInterpreter dbInterpreter = DbInterpreterHelper.GetDbInterpreter(sourceDbType, this.sourceDbConnectionInfo, sourceScriptOption);

            SelectionInfo selectionInfo = new SelectionInfo()
            {
                UserDefinedTypeNames = schemaInfo.UserDefinedTypes.Select(item => item.Name).ToArray(),
                TableNames           = schemaInfo.Tables.Select(item => item.Name).ToArray(),
                ViewNames            = schemaInfo.Views.Select(item => item.Name).ToArray()
            };

            try
            {
                schemaInfo = await dbInterpreter.GetSchemaInfoAsync(selectionInfo);

                dbInterpreter.Subscribe(this);

                GenerateScriptMode mode = GenerateScriptMode.None;

                if (scriptMode.HasFlag(GenerateScriptMode.Schema))
                {
                    mode = GenerateScriptMode.Schema;
                    dbInterpreter.GenerateSchemaScripts(schemaInfo);
                }

                if (scriptMode.HasFlag(GenerateScriptMode.Data))
                {
                    mode = GenerateScriptMode.Data;
                    await dbInterpreter.GenerateDataScriptsAsync(schemaInfo);
                }

                this.OpenInExplorer(dbInterpreter.GetScriptOutputFilePath(mode));

                MessageBox.Show(DONE);
            }
            catch (Exception ex)
            {
                this.HandleException(ex);
            }

            this.btnGenerateSourceScripts.Enabled = true;
        }
Пример #11
0
        private async Task Convert()
        {
            SchemaInfo schemaInfo = this.GetSourceTreeSchemaInfo();

            if (!this.ValidateSource(schemaInfo))
            {
                return;
            }

            if (this.targetDbConnectionInfo == null)
            {
                MessageBox.Show("Target connection info is null.");
                return;
            }

            if (this.sourceDbConnectionInfo.Server == this.targetDbConnectionInfo.Server && this.sourceDbConnectionInfo.Database == this.targetDbConnectionInfo.Database)
            {
                MessageBox.Show("Source database cannot be equal to the target database.");
                return;
            }

            DatabaseType sourceDbType = this.GetDatabaseType(this.cboSourceDB.Text);
            DatabaseType targetDbType = this.GetDatabaseType(this.cboTargetDB.Text);

            int dataBatchSize = SettingManager.Setting.DataBatchSize;
            DbInterpreterOption sourceScriptOption = new DbInterpreterOption()
            {
                ScriptOutputMode = GenerateScriptOutputMode.None, DataBatchSize = dataBatchSize
            };
            DbInterpreterOption targetScriptOption = new DbInterpreterOption()
            {
                ScriptOutputMode = (GenerateScriptOutputMode.WriteToString), DataBatchSize = dataBatchSize
            };

            this.SetGenerateScriptOption(sourceScriptOption, targetScriptOption);

            if (this.chkGenerateSourceScripts.Checked)
            {
                sourceScriptOption.ScriptOutputMode = sourceScriptOption.ScriptOutputMode | GenerateScriptOutputMode.WriteToFile;
            }

            if (this.chkOutputScripts.Checked)
            {
                targetScriptOption.ScriptOutputMode = targetScriptOption.ScriptOutputMode | GenerateScriptOutputMode.WriteToFile;
            }

            targetScriptOption.GenerateIdentity = this.chkGenerateIdentity.Checked;

            GenerateScriptMode scriptMode = this.GetGenerateScriptMode();

            if (scriptMode == GenerateScriptMode.None)
            {
                MessageBox.Show("Please specify the script mode.");
                return;
            }

            DbConvetorInfo source = new DbConvetorInfo()
            {
                DbInterpreter = DbInterpreterHelper.GetDbInterpreter(sourceDbType, this.sourceDbConnectionInfo, sourceScriptOption)
            };
            DbConvetorInfo target = new DbConvetorInfo()
            {
                DbInterpreter = DbInterpreterHelper.GetDbInterpreter(targetDbType, this.targetDbConnectionInfo, targetScriptOption)
            };

            try
            {
                using (dbConvertor = new DbConvertor(source, target))
                {
                    dbConvertor.Option.GenerateScriptMode          = scriptMode;
                    dbConvertor.Option.BulkCopy                    = this.chkBulkCopy.Checked;
                    dbConvertor.Option.ExecuteScriptOnTargetServer = this.chkExecuteOnTarget.Checked;
                    dbConvertor.Option.UseTransaction              = this.chkUseTransaction.Checked;

                    dbConvertor.Subscribe(this);

                    if (sourceDbType == DatabaseType.MySql)
                    {
                        source.DbInterpreter.Option.InQueryItemLimitCount = 2000;
                    }

                    if (targetDbType == DatabaseType.SqlServer)
                    {
                        target.DbOwner = this.txtTargetDbOwner.Text ?? "dbo";
                    }
                    else if (targetDbType == DatabaseType.MySql)
                    {
                        target.DbInterpreter.Option.RemoveEmoji = true;
                    }

                    dbConvertor.Option.SplitScriptsToExecute = true;

                    this.btnExecute.Enabled = false;
                    this.btnCancel.Enabled  = true;

                    await dbConvertor.Convert(schemaInfo);
                }
            }
            catch (Exception ex)
            {
                this.hasError = true;
                this.HandleException(ex);
            }

            if (!this.hasError)
            {
                this.btnExecute.Enabled = true;
                this.btnCancel.Enabled  = false;

                if (!this.dbConvertor.CancelRequested)
                {
                    this.txtMessage.AppendText(Environment.NewLine + DONE);
                    MessageBox.Show(DONE);
                }
                else
                {
                    MessageBox.Show("Task has been canceled.");
                }
            }
        }
Пример #12
0
        static void TestConvertor(DbInterpreter sourceInterpreter, DbInterpreter targetInterpreter)
        {
            DatabaseType sourceDbType = sourceInterpreter.DatabaseType;
            DatabaseType targetDbType = targetInterpreter.DatabaseType;

            int dataBatchSize = 500;

            GenerateScriptOption sourceScriptOption = new GenerateScriptOption()
            {
                ScriptOutputMode = GenerateScriptOutputMode.WriteToString, DataBatchSize = dataBatchSize
            };
            GenerateScriptOption targetScriptOption = new GenerateScriptOption()
            {
                ScriptOutputMode = (GenerateScriptOutputMode.WriteToFile | GenerateScriptOutputMode.WriteToString), DataBatchSize = dataBatchSize
            };

            sourceInterpreter.Option = sourceScriptOption;
            targetInterpreter.Option = targetScriptOption;

            GenerateScriptMode scriptMode = GenerateScriptMode.Schema | GenerateScriptMode.Data;

            DbConvetorInfo source = new DbConvetorInfo()
            {
                DbInterpreter = sourceInterpreter
            };
            DbConvetorInfo target = new DbConvetorInfo()
            {
                DbInterpreter = targetInterpreter
            };

            DbConvertor dbConvertor = new DbConvertor(source, target, null);

            dbConvertor.Option.GenerateScriptMode = scriptMode;

            dbConvertor.OnFeedback += Feedback;

            if (sourceDbType == DatabaseType.MySql)
            {
                source.DbInterpreter.Option.InQueryItemLimitCount = 2000;
            }

            if (targetDbType == DatabaseType.SqlServer)
            {
                target.DbOwner = "dbo";
            }
            else if (targetDbType == DatabaseType.MySql)
            {
                target.DbInterpreter.Option.RemoveEmoji = true;
            }
            else if (targetDbType == DatabaseType.Oracle)
            {
                dbConvertor.Option.SplitScriptsToExecute = true;
                dbConvertor.Option.ScriptSplitChar       = ';';
            }

            try
            {
                dbConvertor.Convert();
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
                if (ex is TableDataTransferException)
                {
                    TableDataTransferException dataException = ex as TableDataTransferException;
                    msg = $"Error occurs when sync data of table {dataException.TargetTableName}:{msg}";
                }

                msg += Environment.NewLine + "StackTrace:" + Environment.NewLine + ex.StackTrace;

                Feedback(new FeedbackInfo()
                {
                    InfoType = FeedbackInfoType.Error, Message = msg
                });
            }
        }
Пример #13
0
        private async void GenerateScripts()
        {
            SchemaInfo schemaInfo = this.tvDbObjects.GetSchemaInfo();

            if (!this.Validate(schemaInfo))
            {
                return;
            }

            this.isBusy = true;
            this.btnGenerate.Enabled = false;

            DatabaseType dbType = this.useConnector ? this.dbConnectionProfile.DatabaseType : this.databaseType;

            DbInterpreterOption option = new DbInterpreterOption()
            {
                ScriptOutputMode       = GenerateScriptOutputMode.WriteToFile,
                SortObjectsByReference = true,
                GetTableAllObjects     = true
            };

            if (this.chkTreatBytesAsNull.Checked)
            {
                option.TreatBytesAsNullForReading   = true;
                option.TreatBytesAsNullForExecuting = true;
            }
            else
            {
                if (dbType == DatabaseType.Oracle)
                {
                    option.TreatBytesAsNullForReading   = true;
                    option.TreatBytesAsNullForExecuting = true;
                }
                else
                {
                    option.TreatBytesAsHexStringForFile = true;
                }
            }

            this.SetGenerateScriptOption(option);

            GenerateScriptMode scriptMode = this.GetGenerateScriptMode();

            if (scriptMode == GenerateScriptMode.None)
            {
                MessageBox.Show("Please specify the script mode.");
                return;
            }

            this.dbInterpreter = DbInterpreterHelper.GetDbInterpreter(dbType, this.connectionInfo, option);

            SchemaInfoFilter filter = new SchemaInfoFilter();

            SchemaInfoHelper.SetSchemaInfoFilterValues(filter, schemaInfo);

            try
            {
                schemaInfo = await this.dbInterpreter.GetSchemaInfoAsync(filter);

                this.dbInterpreter.Subscribe(this);

                GenerateScriptMode mode = GenerateScriptMode.None;

                DbScriptGenerator dbScriptGenerator = DbScriptGeneratorHelper.GetDbScriptGenerator(this.dbInterpreter);

                if (scriptMode.HasFlag(GenerateScriptMode.Schema))
                {
                    mode = GenerateScriptMode.Schema;
                    dbScriptGenerator.GenerateSchemaScripts(schemaInfo);
                }

                if (scriptMode.HasFlag(GenerateScriptMode.Data))
                {
                    mode = GenerateScriptMode.Data;
                    await dbScriptGenerator.GenerateDataScriptsAsync(schemaInfo);
                }

                this.isBusy = false;
                ManagerUtil.OpenInExplorer(dbScriptGenerator.GetScriptOutputFilePath(mode));

                MessageBox.Show(DONE, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                this.HandleException(ex);
            }

            this.btnGenerate.Enabled = true;
        }
Пример #14
0
        private async Task Convert()
        {
            SchemaInfo schemaInfo = this.tvDbObjects.GetSchemaInfo();

            if (!this.ValidateSource(schemaInfo))
            {
                return;
            }

            if (this.targetDbConnectionInfo == null)
            {
                MessageBox.Show("Target connection info is null.");
                return;
            }

            if (this.sourceDbConnectionInfo.Server == this.targetDbConnectionInfo.Server && this.sourceDbConnectionInfo.Database == this.targetDbConnectionInfo.Database)
            {
                MessageBox.Show("Source database cannot be equal to the target database.");
                return;
            }

            DatabaseType sourceDbType = this.useSourceConnector ? this.sourceDbProfile.DatabaseType : this.sourceDatabaseType;
            DatabaseType targetDbType = this.targetDbProfile.DatabaseType;

            DbInterpreterOption sourceScriptOption = new DbInterpreterOption()
            {
                ScriptOutputMode = GenerateScriptOutputMode.None, SortObjectsByReference = true, GetTableAllObjects = true
            };
            DbInterpreterOption targetScriptOption = new DbInterpreterOption()
            {
                ScriptOutputMode = (GenerateScriptOutputMode.WriteToString)
            };

            this.SetGenerateScriptOption(sourceScriptOption, targetScriptOption);

            if (this.chkGenerateSourceScripts.Checked)
            {
                sourceScriptOption.ScriptOutputMode = sourceScriptOption.ScriptOutputMode | GenerateScriptOutputMode.WriteToFile;
            }

            if (this.chkOutputScripts.Checked)
            {
                targetScriptOption.ScriptOutputMode = targetScriptOption.ScriptOutputMode | GenerateScriptOutputMode.WriteToFile;
            }

            if (this.chkTreatBytesAsNull.Checked)
            {
                sourceScriptOption.TreatBytesAsNullForReading   = true;
                targetScriptOption.TreatBytesAsNullForExecuting = true;
            }

            targetScriptOption.TableScriptsGenerateOption.GenerateIdentity = this.chkGenerateIdentity.Checked;

            GenerateScriptMode scriptMode = this.GetGenerateScriptMode();

            if (scriptMode == GenerateScriptMode.None)
            {
                MessageBox.Show("Please specify the script mode.");
                return;
            }

            DbConveterInfo source = new DbConveterInfo()
            {
                DbInterpreter = DbInterpreterHelper.GetDbInterpreter(sourceDbType, this.sourceDbConnectionInfo, sourceScriptOption)
            };
            DbConveterInfo target = new DbConveterInfo()
            {
                DbInterpreter = DbInterpreterHelper.GetDbInterpreter(targetDbType, this.targetDbConnectionInfo, targetScriptOption)
            };

            try
            {
                using (this.dbConverter = new DbConverter(source, target))
                {
                    this.dbConverter.Option.GenerateScriptMode                         = scriptMode;
                    this.dbConverter.Option.BulkCopy                                   = this.chkBulkCopy.Checked;
                    this.dbConverter.Option.ExecuteScriptOnTargetServer                = this.chkExecuteOnTarget.Checked;
                    this.dbConverter.Option.UseTransaction                             = this.chkUseTransaction.Checked;
                    this.dbConverter.Option.SkipScriptError                            = this.chkSkipScriptError.Checked;
                    this.dbConverter.Option.PickupTable                                = this.chkPickup.Checked;
                    this.dbConverter.Option.ConvertComputeColumnExpression             = this.chkComputeColumn.Checked;
                    this.dbConverter.Option.OnlyCommentComputeColumnExpressionInScript = this.chkOnlyCommentComputeExpression.Checked;

                    this.dbConverter.Subscribe(this);

                    if (sourceDbType == DatabaseType.MySql)
                    {
                        source.DbInterpreter.Option.InQueryItemLimitCount = 2000;
                    }

                    if (targetDbType == DatabaseType.SqlServer)
                    {
                        target.DbOwner = this.txtTargetDbOwner.Text ?? "dbo";
                    }
                    else if (targetDbType == DatabaseType.MySql)
                    {
                        target.DbInterpreter.Option.RemoveEmoji = true;
                    }

                    this.dbConverter.Option.SplitScriptsToExecute = true;

                    this.btnExecute.Enabled = false;
                    this.btnCancel.Enabled  = true;

                    await this.dbConverter.Convert(schemaInfo);

                    if (!this.hasError && !this.dbConverter.HasError && !source.DbInterpreter.HasError && !target.DbInterpreter.HasError)
                    {
                        this.btnExecute.Enabled = true;
                        this.btnCancel.Enabled  = false;

                        if (!this.dbConverter.CancelRequested)
                        {
                            this.txtMessage.AppendText(Environment.NewLine + DONE);
                            MessageBox.Show(DONE, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            MessageBox.Show("Task has been canceled.");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.hasError = true;
                this.HandleException(ex);
            }
        }
Пример #15
0
        private async Task ConvertorBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            if (this.convertorBackgroundWorker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }

            SchemaInfo schemaInfo = this.GetSourceTreeSchemaInfo();

            if (!this.ValidateSource(schemaInfo))
            {
                return;
            }

            if (this.targetDbConnectionInfo == null)
            {
                MessageBox.Show("Target connection info is null.");
                return;
            }

            if (this.sourceDbConnectionInfo.Server == this.targetDbConnectionInfo.Server && this.sourceDbConnectionInfo.Database == this.targetDbConnectionInfo.Database)
            {
                MessageBox.Show("Source database cannot be equal to the target database.");
                return;
            }

            DatabaseType sourceDbType = this.GetDatabaseType(this.cboSourceDB.Text);
            DatabaseType targetDbType = this.GetDatabaseType(this.cboTargetDB.Text);

            int dataBatchSize = SettingManager.Setting.DataBatchSize;
            GenerateScriptOption sourceScriptOption = new GenerateScriptOption()
            {
                ScriptOutputMode = GenerateScriptOutputMode.None, DataBatchSize = dataBatchSize
            };
            GenerateScriptOption targetScriptOption = new GenerateScriptOption()
            {
                ScriptOutputMode = (GenerateScriptOutputMode.WriteToString), DataBatchSize = dataBatchSize
            };

            this.SetGenerateScriptOption(sourceScriptOption, targetScriptOption);

            targetScriptOption.GenerateIdentity = this.chkGenerateIdentity.Checked;

            GenerateScriptMode scriptMode = this.GetGenerateScriptMode();

            if (scriptMode == GenerateScriptMode.None)
            {
                MessageBox.Show("Please specify the script mode.");
                return;
            }

            DbConvetorInfo source = new DbConvetorInfo()
            {
                DbInterpreter = DbInterpreterHelper.GetDbInterpreter(sourceDbType, this.sourceDbConnectionInfo, sourceScriptOption)
            };
            DbConvetorInfo target = new DbConvetorInfo()
            {
                DbInterpreter = DbInterpreterHelper.GetDbInterpreter(targetDbType, this.targetDbConnectionInfo, targetScriptOption)
            };

            DbConvertor dbConvertor = new DbConvertor(source, target, null);

            dbConvertor.Option.GenerateScriptMode = scriptMode;

            dbConvertor.OnFeedback += Feedback;

            if (sourceDbType == DatabaseType.MySql)
            {
                source.DbInterpreter.Option.InQueryItemLimitCount = 2000;
            }

            if (targetDbType == DatabaseType.SqlServer)
            {
                target.DbOwner = this.txtTargetDbOwner.Text ?? "dbo";
            }
            else if (targetDbType == DatabaseType.MySql)
            {
                target.DbInterpreter.Option.RemoveEmoji = true;
            }
            else if (targetDbType == DatabaseType.Oracle)
            {
                dbConvertor.Option.SplitScriptsToExecute = true;
                dbConvertor.Option.ScriptSplitChar       = ';';
            }

            DataTransferErrorProfile dataErrorProfile = null;

            if (this.chkPickup.Checked && scriptMode.HasFlag(GenerateScriptMode.Data))
            {
                dataErrorProfile = DataTransferErrorProfileManager.GetProfile(this.sourceDbConnectionInfo, this.targetDbConnectionInfo);
                if (dataErrorProfile != null)
                {
                    dbConvertor.Option.PickupTable = new Table()
                    {
                        Owner = schemaInfo.Tables.FirstOrDefault()?.Owner, Name = dataErrorProfile.SourceTableName
                    };
                }
            }

            this.btnExecute.Enabled = false;
            this.btnCancel.Enabled  = true;

            bool success = false;

            try
            {
                await dbConvertor.ConvertAsync(schemaInfo, false);

                success = true;

                if (dataErrorProfile != null)
                {
                    DataTransferErrorProfileManager.Remove(dataErrorProfile);
                }
            }
            catch (Exception ex)
            {
                string errMsg = ex.Message;

                sbFeedback.AppendLine("Error:" + ex.Message);
                if (ex.InnerException != null)
                {
                    sbFeedback.AppendLine("Innser Exception:" + ex.InnerException.Message);
                }

                if (!string.IsNullOrEmpty(ex.StackTrace))
                {
                    sbFeedback.AppendLine(ex.StackTrace);
                }

                this.AppendErrorMessage(errMsg);

                this.txtMessage.SelectionStart = this.txtMessage.TextLength;
                this.txtMessage.ScrollToCaret();

                this.btnExecute.Enabled = true;
                this.btnCancel.Enabled  = false;

                if (ex is TableDataTransferException dataException)
                {
                    DataTransferErrorProfileManager.Save(new DataTransferErrorProfile
                    {
                        SourceServer    = dataException.SourceServer,
                        SourceDatabase  = dataException.SourceDatabase,
                        SourceTableName = dataException.SourceTableName,
                        TargetServer    = dataException.TargetServer,
                        TargetDatabase  = dataException.TargetDatabase,
                        TargetTableName = dataException.TargetTableName
                    });
                }

                MessageBox.Show(ex.Message);
            }

            LogHelper.Log(sbFeedback.ToString());
            sbFeedback.Clear();

            if (success)
            {
                this.btnExecute.Enabled = true;
                this.btnCancel.Enabled  = false;

                this.txtMessage.AppendText(Environment.NewLine + DONE);
                MessageBox.Show(DONE);
            }
        }
Пример #16
0
        private async Task CopyTable()
        {
            string name = this.txtName.Text.Trim();

            if (!this.ValidateInputs())
            {
                return;
            }

            try
            {
                GenerateScriptMode scriptMode = this.GetGenerateScriptMode();

                bool isTableExisted = false;

                if (scriptMode.HasFlag(GenerateScriptMode.Schema))
                {
                    isTableExisted = await this.IsNameExisted();
                }

                if (isTableExisted)
                {
                    name = name + "_copy";
                }

                SchemaInfo schemaInfo = new SchemaInfo();
                schemaInfo.Tables.Add(this.Table);

                DatabaseType   targetDatabaseType   = this.rbAnotherDatabase.Checked ? this.ucConnection.DatabaseType : this.DatabaseType;
                ConnectionInfo targetConnectionInfo = this.rbAnotherDatabase.Checked ? this.targetDbConnectionInfo : this.ConnectionInfo;

                DbInterpreterOption sourceOption = new DbInterpreterOption();
                DbInterpreterOption targetOption = new DbInterpreterOption();

                targetOption.TableScriptsGenerateOption.GenerateIdentity = this.chkGenerateIdentity.Checked;

                DbConveterInfo source = new DbConveterInfo()
                {
                    DbInterpreter = DbInterpreterHelper.GetDbInterpreter(this.DatabaseType, this.ConnectionInfo, sourceOption)
                };
                DbConveterInfo target = new DbConveterInfo()
                {
                    DbInterpreter = DbInterpreterHelper.GetDbInterpreter(targetDatabaseType, targetConnectionInfo, targetOption)
                };

                source.TableNameMappings.Add(this.Table.Name, name);

                this.btnExecute.Enabled = false;

                using (this.dbConverter = new DbConverter(source, target))
                {
                    this.dbConverter.Option.RenameTableChildren            = isTableExisted || this.rbSameDatabase.Checked;
                    this.dbConverter.Option.GenerateScriptMode             = scriptMode;
                    this.dbConverter.Option.BulkCopy                       = true;
                    this.dbConverter.Option.UseTransaction                 = true;
                    this.dbConverter.Option.ConvertComputeColumnExpression = true;
                    this.dbConverter.Option.IgnoreNotSelfForeignKey        = true;

                    this.dbConverter.Subscribe(this);

                    if (this.DatabaseType == DatabaseType.MySql)
                    {
                        source.DbInterpreter.Option.InQueryItemLimitCount = 2000;
                    }

                    if (this.DatabaseType != targetDatabaseType)
                    {
                        if (targetDatabaseType == DatabaseType.SqlServer)
                        {
                            target.DbOwner = "dbo";
                        }
                        else if (targetDatabaseType == DatabaseType.MySql)
                        {
                            target.DbInterpreter.Option.RemoveEmoji = true;
                        }
                    }
                    else
                    {
                        target.DbOwner = this.Table.Owner;
                    }

                    this.dbConverter.Option.SplitScriptsToExecute = true;

                    await this.dbConverter.Convert(schemaInfo);

                    if (!this.hasError && !this.dbConverter.HasError && !source.DbInterpreter.HasError && !target.DbInterpreter.HasError)
                    {
                        if (!this.dbConverter.CancelRequested)
                        {
                            MessageBox.Show("Copy finished", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            MessageBox.Show("Task has been canceled.");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.hasError = true;
                this.HandleException(ex);
            }
            finally
            {
                this.btnExecute.Enabled = true;
            }
        }