Пример #1
0
        private object SqlExecute(SqlResultHandler handler)
        {
            object result = null;

            try
            {
                sqlconnection.Open();
                result = handler.Invoke();
            }
            catch (SqlException sqlex)
            {
                ThrowException(sqlex);
            }
            catch (Exception ex)
            {
                ThrowException(ex);
            }
            finally
            {
                if (sqlconnection.State != ConnectionState.Closed)
                {
                    sqlconnection.Close();
                }
            }
            return(result);
        }
Пример #2
0
        // -------------------------------------------------
        //                                       SQL Logging
        //                                       -----------
        protected virtual void LogSql(Object[] args, Type[] argTypes)
        {
            // [SqlLogHandler]
            SqlLogHandler sqlLogHandler       = GetSqlLogHander();
            bool          existsSqlLogHandler = sqlLogHandler != null;

            // [SqlResultHandler]
            SqlResultHandler sqlResultHandler       = GetSqlResultHander();
            bool             existsSqlResultHandler = sqlResultHandler != null;

            // [SqlLogRegistry]
            InternalSqlLogRegistry sqlLogRegistry = InternalSqlLogRegistryLocator.Instance;
            bool existsSqlLogRegistry             = sqlLogRegistry != null;

            if (IsLogEnabled() || existsSqlLogHandler || existsSqlResultHandler || existsSqlLogRegistry)
            {
                String displaySql = GetCompleteSql(args);
                if (IsLogEnabled())
                {
                    Log((IsContainsLineSeparatorInSql() ? GetLineSeparator() : "") + displaySql);
                }
                if (existsSqlLogHandler)   // DBFlute provides
                {
                    sqlLogHandler.Invoke(this.Sql, displaySql, args, argTypes);
                }
                if (existsSqlLogRegistry)   // S2Container provides (But Actually DBFlute provides at CSharp)
                {
                    sqlLogRegistry.Add(CreateInternalSqlLog(displaySql, args, argTypes));
                }
                PutObjectToMapContext("df:DisplaySql", displaySql);
            }
        }
Пример #3
0
        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);
        }