Esempio n. 1
0
        public override IEnumerable <TResult> Convert(DbCommandResult commandResult)
        {
            if (returningClause.ConvertExpression == null)
            {
                returningClause.Build(ServiceProvider.GetRequiredService <DbCommandBuilder>(), ServiceProvider);
            }

            return(returningClause.ConvertExpression.Convert(commandResult, false));
        }
Esempio n. 2
0
        public override IEnumerable <TResult> Convert(DbCommandResult commandResult)
        {
            if (SqlServerOutputClause.ConvertExpression == null)
            {
                SqlServerOutputClause.Build(ServiceProvider.GetRequiredService <DbCommandBuilder>(), ServiceProvider);
            }

            return(SqlServerOutputClause.ConvertExpression.Convert(commandResult, true));
        }
Esempio n. 3
0
        public override T Convert(DbCommandResult commandResult)
        {
            if (returningClause.ConvertExpression == null)
            {
                returningClause.Build(ServiceProvider.GetRequiredService <DbCommandBuilder>(), ServiceProvider);
            }

            if (returningClause.ConvertExpression == null)
            {
                return(default);
        public override IEnumerable <TResult> Convert(DbCommandResult commandResult)
        {
            if (genericSelectClause == null)
            {
                throw new InvalidOperationException();
            }

            var environmentOption = ServiceProvider.GetRequiredService <EnvironmentOption>();

            if (genericSelectClause.ConvertExpression == null)
            {
                genericSelectClause.Build(ServiceProvider.GetRequiredService <DbCommandBuilder>(), ServiceProvider);
            }

            return(genericSelectClause.ConvertExpression.Convert(commandResult, !environmentOption.IsSubQuery));
        }
Esempio n. 5
0
        private Tuple <bool, Exception?> CommitOrRollBackIfNecessary(DbCommandResult result)
        {
            if (result.IsSuccess)
            {
                try
                {
                    this.EnsureCommit();
                    return(new Tuple <bool, Exception?>(true, null));
                }
                catch (Exception ex)
                {
                    this.EnsureRollBack();
                    return(new Tuple <bool, Exception?>(false, ex));
                }
            }

            // if the result is not success, return false and the underlying exception
            return(new Tuple <bool, Exception?>(false, result.Exception));
        }
Esempio n. 6
0
        /// <inheritdoc />
        public async Task <TReturn> ExecuteAsync <TReturn>(string sql, Func <TReturn> customValueProviderToExecute, Action?dispatcherPostExecution = null, bool throwException = false)
        {
            DbCommandResult result = new DbCommandResult();

            if (string.IsNullOrWhiteSpace(sql))
            {
                throw new ArgumentNullException(nameof(sql));
            }

            this.EnsureDbConnection();

            Func <Task <DbCommandResult> > execution = async() =>
            {
                try
                {
                    this.EnsureDbConnectionIsOpened();
                    result.Result = await this.dbConnection.ExecuteAsync(sql, transaction : this.Transaction);

                    result.IsSuccess = true;

                    dispatcherPostExecution?.Invoke();
                }
                catch (Exception ex)
                {
                    result.Exception = ex;
                    result.Result    = -1;
                    result.IsSuccess = false;

                    dispatcherPostExecution?.Invoke();

                    if (throwException)
                    {
                        throw;
                    }
                }

                return(result);
            };

            await FluentDbCommandBase.SafeExecuteDbCommandAction(execution, this.RetryPolicy, throwException);

            return(customValueProviderToExecute());
        }
Esempio n. 7
0
        // DbObjectCommand execute
        private async Task <DbCommandResult> ExecuteDbObjectCommand <T>(DbObjectCommand <T> command, Action?dispatcherPostExecution = null, bool throwException = false)
            where T : class, new()
        {
            DbCommandResult result = new DbCommandResult();

            try
            {
                this.CheickDbObjectCommand(command);
                bool operation = command.Operation switch
                {
                    DbOperation.Delete => await this.dbConnection.DeleteAsync <T>(command.ObjectParameter !, transaction : this.Transaction),
                    DbOperation.Insert => await this.dbConnection.InsertAsync <T>(command.ObjectParameter !, transaction : this.Transaction) > 0,
                    DbOperation.Update => await this.dbConnection.UpdateAsync <T>(command.ObjectParameter !, transaction : this.Transaction),
                    DbOperation.ExecuteSql => await this.ExecuteScriptAsync <T>(command.ScriptSql !, command.ObjectParameter !),
                    _ => false
                };

                result.Result    = operation ? 1 : 0;
                result.IsSuccess = operation;

                dispatcherPostExecution?.Invoke();
            }
            catch (Exception ex)
            {
                result.Exception = ex;
                result.Result    = -1;
                result.IsSuccess = false;

                dispatcherPostExecution?.Invoke();

                this.ThrowExceptionIfNecessary(throwException, ex);
            }

            if (!result.IsSuccess && result.Exception == null)
            {
                result.Exception = new FluentDbExecutionException("Data was not found");
            }

            return(result);
        }
Esempio n. 8
0
        /// <inheritdoc />
        public async Task <DbCommandResult> ExecuteAsync(Action?dispatcherPostExecution = null, bool throwException = false)
        {
            this.EnsureDbConnection();
            this.EnsureDbConnectionIsOpened();

            Func <Task <DbCommandResult> > execution = async() =>
            {
                bool commitException = false;

                DbCommandResultCollection results = new DbCommandResultCollection();

                // wait all tasks in the queue
                while (this.ActionsToExecute.Any())
                {
                    Func <Task <DbCommandResult> > dbTask = this.ActionsToExecute.Dequeue();
                    results.Add(await dbTask());
                }

                DbCommandResult mergeResult = results.MergeResults();

                // We should not commit if there is any exception that occured
                var commit = this.CommitOrRollBackIfNecessary(mergeResult);
                commitException = !commit.Item1;

                dispatcherPostExecution?.Invoke();

                // Get the eventuals commit exception
                mergeResult.Exception = commit.Item2;
                if (throwException && (!mergeResult.IsSuccess || commitException))
                {
                    // throw the inner exception (Exception of the merge result or the commit exception)
                    throw commit.Item2 !;
                }

                return(mergeResult);
            };

            return(await FluentDbCommandBase.SafeExecuteDbCommandAction(execution, this.RetryPolicy, throwException));
        }
Esempio n. 9
0
        /// <inheritdoc />
        public async Task <DbCommandResult> ExecuteAsync <T>(IEnumerable <DbObjectCommand <T> > commands, Action?dispatcherPostExecution = null, bool throwException = false)
            where T : class, new()
        {
            this.EnsureDbConnection();
            this.EnsureDbConnectionIsOpened();
            this.CheickDbObjectCommand(commands);

            Func <Task <DbCommandResult> > execution = async() =>
            {
                bool commitException = false;

                DbCommandResultCollection results = new DbCommandResultCollection();
                foreach (DbObjectCommand <T> command in commands)
                {
                    results.Add(await this.ExecuteDbObjectCommand <T>(command));
                }

                DbCommandResult mergeResult = results.MergeResults();

                // We should not commit if there is any exception that occured
                var commit = this.CommitOrRollBackIfNecessary(mergeResult);
                commitException = !commit.Item1;

                dispatcherPostExecution?.Invoke();

                // Get the eventuals commit exception
                mergeResult.Exception = commit.Item2;
                if (throwException && (!mergeResult.IsSuccess || commitException))
                {
                    // throw the inner exception (Exception of the merge result or the commit exception)
                    throw commit.Item2 !;
                }

                return(mergeResult);
            };

            return(await this.SafeExecute(execution, throwException));
        }
Esempio n. 10
0
        /// <inheritdoc />
        public async Task <DbCommandResult> ExecuteAsync(string sql, Action?dispatcherPostExecution = null, bool throwException = false)
        {
            if (string.IsNullOrWhiteSpace(sql))
            {
                throw new ArgumentNullException(nameof(sql));
            }

            this.EnsureDbConnection();

            Func <Task <DbCommandResult> > toExecute = async() =>
            {
                DbCommandResult result = new DbCommandResult();
                try
                {
                    this.EnsureDbConnectionIsOpened();
                    result.Result = await this.dbConnection.ExecuteAsync(sql, transaction : this.Transaction);

                    result.IsSuccess = result.Result > 0;

                    dispatcherPostExecution?.Invoke();
                }
                catch (Exception ex)
                {
                    result.Exception = ex;
                    result.Result    = -1;
                    result.IsSuccess = false;

                    dispatcherPostExecution?.Invoke();

                    this.ThrowExceptionIfNecessary(throwException, ex);
                }

                return(result);
            };

            // Execute the command
            return(await FluentDbCommandBase.SafeExecuteDbCommandAction(toExecute, this.RetryPolicy, throwException));
        }
Esempio n. 11
0
        // DbObjectCommand execute
        private async Task <DbCommandResult> ExecuteDbObjectCommand(DbObjectCommand command, Action?dispatcherPostExecution = null, bool throwException = false)
        {
            DbCommandResult result = new DbCommandResult();

            try
            {
                this.CheickDbObjectCommand(command);

                if (command.Parameters != null && command.Parameters.Any())
                {
                    result.Result = await this.dbConnection.ExecuteAsync(
                        command.ScriptSql,
                        command.Parameters,
                        transaction : this.Transaction);
                }
                else
                {
                    result.Result = await this.dbConnection.ExecuteAsync(command.ScriptSql, transaction : this.Transaction);
                }

                result.IsSuccess = result.Result > 0;
                dispatcherPostExecution?.Invoke();
            }
            catch (Exception ex)
            {
                result.Exception = ex;
                result.Result    = -1;
                result.IsSuccess = false;

                dispatcherPostExecution?.Invoke();

                this.ThrowExceptionIfNecessary(throwException, ex);
            }

            return(result);
        }
Esempio n. 12
0
 public abstract TResult Convert(DbCommandResult commandResult);
Esempio n. 13
0
 public override IEnumerable <IDictionary <string, object> > Convert(DbCommandResult commandResult) => commandResult;
Esempio n. 14
0
 public override DbCommandResult Convert(DbCommandResult commandResult) => commandResult;
 public static IEnumerable <T> Convert <T>(this Expression <Func <JToken, T> > expression, DbCommandResult commandResult, bool isJson)
 {
     if (expression == null)
     {
         return(default);