Exemplo n.º 1
0
        private void ExecuteSqlInContext(Action <ReferenceConnection, Database, string> dbAction, bool clearSqlAfterExecute)
        {
            SqlContextItem sqlContext = this.GetSqlContext();

            DoSafeConnectionOp(this.Name, this.GetSqlContextReferenceConnections(), refConnection =>
            {
                string sql = sqlContext.GetSqlInContext();

                if (sql.IsNotEmpty() && dbAction != null)
                {
                    sqlContext.BeforeActions.DoActions();

                    Database db = DatabaseFactory.Create(this);

                    dbAction(refConnection, db, sql);

                    sqlContext.AfterActions.DoActions();

                    if (clearSqlAfterExecute)
                    {
                        sqlContext.ClearSqlInContext();
                    }
                }
            });
        }
Exemplo n.º 2
0
        ///// <summary>
        ///// 在上下文中添加待执行的SQL语句,自动添加语句分隔符
        ///// </summary>
        ///// <param name="sqlBuilder"></param>
        ///// <param name="format"></param>
        ///// <param name="args"></param>
        //public override void AppendSqlWithSperatorInContext(SqlBuilderBase sqlBuilder, string format, params object[] args)
        //{
        //    this.GetSqlContext().AppendSqlInContext(sqlBuilder, true, format, args);
        //    //DoSafeConnectionOp(this.Name, GetSqlContextReferenceConnections(), refConnection => refConnection.AppendSqlInContext(sqlBuilder, true, format, args));
        //}

        ///// <summary>
        ///// 在上下文中添加待执行的SQL语句
        ///// </summary>
        ///// <param name="sqlBuilder"></param>
        ///// <param name="format"></param>
        ///// <param name="args"></param>
        //public override void AppendSqlInContext(SqlBuilderBase sqlBuilder, string format, params object[] args)
        //{
        //    DoSafeConnectionOp(this.Name, GetSqlContextReferenceConnections(), refConnection => refConnection.AppendSqlInContext(sqlBuilder, false, format, args));
        //}

        ///// <summary>
        ///// 在上下文中注册查询返回的集中表的结果操作
        ///// </summary>
        ///// <param name="tableName"></param>
        ///// <param name="action"></param>
        //public override void RegisterTableAction(string tableName, Action<DataTable> action)
        //{
        //    DoSafeConnectionOp(this.Name, GetSqlContextReferenceConnections(), refConnection => refConnection.RegisterTableAction(tableName, action));
        //}

        ///// <summary>
        ///// 清除上下文中的SQL语句
        ///// </summary>
        //public override void ClearSqlInContext()
        //{
        //    DoSafeConnectionOp(this.Name, GetSqlContextReferenceConnections(), refConnection => refConnection.ClearSqlInContext());
        //}

        ///// <summary>
        ///// 得到上下文中的SQL语句
        ///// </summary>
        //public override string GetSqlInContext()
        //{
        //    string result = string.Empty;

        //    DoSafeConnectionOp(this.Name, GetSqlContextReferenceConnections(), refConnection => result = refConnection.GetSqlInContext());

        //    return result;
        //}

        /// <summary>
        /// 执行保存在上下文中的SQL语句,返回DataSet
        /// </summary>
        /// <param name="clearSqlAfterExecute">执行完后是否清除上下文中的SQL,默认是true</param>
        /// <param name="tableNames">可选参数,DataSet中的表名</param>
        /// <returns></returns>
        public override DataSet ExecuteDataSetSqlInContext(bool clearSqlAfterExecute = true, params string[] tableNames)
        {
            DataSet result = null;

            SqlContextItem sqlContext = this.GetSqlContext();

            this.ExecuteSqlInContext((refConnection, db, sql) =>
            {
                if (tableNames == null || tableNames.Length == 0)
                {
                    tableNames = sqlContext.TableActions.ToTableNames();
                }

                result = db.ExecuteDataSet(CommandType.Text, sql, tableNames);

                //根据Table回调到每个事件
                for (int i = 0; i < result.Tables.Count; i++)
                {
                    if (i < sqlContext.TableActions.Count)
                    {
                        sqlContext.TableActions[i].DoAction(result.Tables[i]);
                    }
                }
            },
                                     clearSqlAfterExecute);

            return(result);
        }