示例#1
0
        public async Task <QueryResult> Run(DatabaseType dbType, ConnectionInfo connectionInfo, string script)
        {
            this.cancelRequested = false;
            this.isBusy          = false;

            QueryResult result = new QueryResult();

            DbInterpreterOption option = new DbInterpreterOption()
            {
                RequireInfoMessage = true
            };

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

            dbInterpreter.Subscribe(this.observer);

            try
            {
                ScriptParser scriptParser = new ScriptParser(dbInterpreter, script);

                string cleanScript = scriptParser.CleanScript;

                if (string.IsNullOrEmpty(cleanScript))
                {
                    result.DoNothing = true;
                    return(result);
                }

                using (DbConnection dbConnection = dbInterpreter.CreateConnection())
                {
                    if (scriptParser.IsSelect())
                    {
                        this.isBusy       = true;
                        result.ResultType = QueryResultType.Grid;

                        if (!scriptParser.IsCreateOrAlterScript() && dbInterpreter.ScriptsDelimiter.Length == 1)
                        {
                            cleanScript = script.TrimEnd(dbInterpreter.ScriptsDelimiter[0]);
                        }

                        DataTable dataTable = await dbInterpreter.GetDataTableAsync(dbConnection, script, this.LimitCount);

                        result.Result = dataTable;
                    }
                    else
                    {
                        this.isBusy       = true;
                        result.ResultType = QueryResultType.Text;

                        dbConnection.Open();

                        this.transaction = dbConnection.BeginTransaction();

                        IEnumerable <string> commands = Enumerable.Empty <string>();

                        if (scriptParser.IsCreateOrAlterScript())
                        {
                            commands = new string[] { script };
                        }
                        else
                        {
                            string delimiter = dbInterpreter.ScriptsDelimiter;

                            commands = script.Split(new string[] { delimiter, delimiter.Replace("\r", "\n") }, StringSplitOptions.RemoveEmptyEntries);
                        }

                        int affectedRows = 0;

                        foreach (string command in commands)
                        {
                            if (string.IsNullOrEmpty(command.Trim()))
                            {
                                continue;
                            }

                            CommandInfo commandInfo = new CommandInfo()
                            {
                                CommandType       = CommandType.Text,
                                CommandText       = command,
                                Transaction       = transaction,
                                CancellationToken = this.CancellationTokenSource.Token
                            };

                            int res = await dbInterpreter.ExecuteNonQueryAsync(dbConnection, commandInfo);

                            affectedRows += (res == -1 ? 0 : res);
                        }

                        result.Result = affectedRows;

                        if (!dbInterpreter.HasError && !this.cancelRequested)
                        {
                            this.transaction.Commit();
                        }
                    }

                    this.isBusy = false;
                }
            }
            catch (Exception ex)
            {
                this.Rollback();

                result.ResultType = QueryResultType.Text;
                result.HasError   = true;
                result.Result     = ex.Message;

                this.HandleError(ex);
            }

            return(result);
        }