Example #1
0
        public async Task <TReturn> Execute <TReturn>(ISqlCommand <TReturn> sqlCommand, SqlExecutionContext context = null)
        {
            if (context != null)
            {
                return(await sqlCommand.Execute(context.Connection, context.Transaction));
            }

            return(await _executionStrategy.Value.ExecuteAsync(
                       async() =>
            {
                using (var connection = _connectionFactory.Value.Invoke())
                {
                    await connection.Value.OpenAsync();

                    var dbTransaction = connection.Value.BeginTransaction();

                    try
                    {
                        var result = await sqlCommand.Execute(connection.Value, dbTransaction);

                        dbTransaction.Commit();

                        return result;
                    }
                    finally
                    {
                        dbTransaction.Dispose();
                    }
                }
            }));
        }
        public static bool TryExecute([NotNull] this ISqlCommand command, [NotNull] ISqlConnection connection)
        {
            try
            {
                command.Execute(connection);
            }
            catch (Exception)
            {
                return(false);
            }

            return(true);
        }
Example #3
0
 public void ExecuteCommand(ISqlCommand sqlCommand)
 {
     QueryCount++;
     sqlCommand.Execute(CreateCommand(null));
     sqlConnection.Close();
 }
        protected virtual object DispatchInvoking(IMethodInvocation invocation)
        {
            MethodBase method = invocation.Method;

            if (!method.IsAbstract)
            {
                return(invocation.Proceed());
            }
            bool logEnabled = IsLogEnabled();

            // - - - - - - - - - - - - -
            // Initialize DAO meta data
            // - - - - - - - - - - - - -
            if (method.Name.Equals("InitializeDaoMetaData"))
            {
                InitializeSqlCommand(invocation);
                return(null); // The end! (Initilization Only)
            }

            // - - - - - - - - - - -
            // Preprocess outsideSql
            // - - - - - - - - - - -
            PreprocessOutsideSql(invocation);

            // - - - - - - - - - - - - -
            // Preprocess conditionBean
            // - - - - - - - - - - - - -
            ConditionBean cb = PreprocessConditionBean(invocation);

            // - - - - - - - - -
            // Set up sqlCommand
            // - - - - - - - - -
            ISqlCommand cmd = null;

            try {
                DateTime?beforeCmd = null;
                if (logEnabled)
                {
                    beforeCmd = DateTime.Now;
                }
                cmd = FindSqlCommand(invocation);
                if (logEnabled)
                {
                    DateTime afterCmd = DateTime.Now;
                    if (!afterCmd.Equals(beforeCmd.Value))
                    {
                        LogSqlCommand(invocation, cmd, beforeCmd.Value, afterCmd);
                    }
                }
            } finally {
                if (IsLogEnabled())
                {
                    LogInvocation(invocation);
                }
            }

            SqlResultHandler sqlResultHandler       = GetSqlResultHander();
            bool             existsSqlResultHandler = sqlResultHandler != null;
            DateTime?        before = null;

            if (logEnabled || existsSqlResultHandler)
            {
                before = DateTime.Now; // for performance view
            }

            // - - - - - - - - - -
            // Execute sqlCommand!
            // - - - - - - - - - -
            object ret = null;

            try {
                ret = cmd.Execute(invocation.Arguments);
            } catch (Exception e) {
                if (e.GetType().Equals(typeof(NotSingleRowUpdatedRuntimeException)))
                {
                    throw new EntityAlreadyUpdatedException((NotSingleRowUpdatedRuntimeException)e);
                }
                throw;
            } finally {
                PostprocessConditionBean(invocation, cb);
            }

            // - - - - - - - - - -
            // Convert and Return!
            // - - - - - - - - - -
            Type retType = ((MethodInfo)method).ReturnType;

            ret = Seasar.Framework.Util.ConversionUtil.ConvertTargetType(ret, retType);

            if (logEnabled || existsSqlResultHandler)
            {
                DateTime after = DateTime.Now; // for performance view
                if (logEnabled)
                {
                    LogReturn(invocation, retType, ret, before.Value, after);
                }
                if (existsSqlResultHandler)
                {
                    SqlResultInfo info = new SqlResultInfo();
                    info.Result         = ret;
                    info.CommandName    = method.Name;
                    info.DisplaySql     = (String)InternalMapContext.GetObject("df:DisplaySql");
                    info.BeforeDateTime = before.Value;
                    info.AfterDateTime  = after;
                    sqlResultHandler.Invoke(info);
                }
            }
            return(ret);
        }