示例#1
0
        public void ToStringTest()
        {
            var command = new ExecuteCommand("select * from users where id=@userid and name=@name", "@userid", 1,"@name","张三");
            Assert.Equal(@"select * from users where id=@userid and name=@name
{
    @userid = 1
    @name = '张三'
}", command.ToString());
        }
示例#2
0
 /// <summary>
 /// 初始化一个 <see cref="Aoite.Data.DefaultDbExecutor"/> 类的新实例。
 /// </summary>
 /// <param name="engine">数据源查询与交互引擎的实例。</param>
 /// <param name="command">执行的命令。</param>
 public DefaultDbExecutor(IDbEngine engine, ExecuteCommand command)
 {
     if (engine == null)
     {
         throw new ArgumentNullException("engine");
     }
     this._Engine         = engine;
     this._Command        = command;
     this._owner          = engine.Owner;
     this._connection     = engine.CreateConnection();
     this._transaction    = engine.CreateTransaction();
     this._engineInjector = this._owner.Injector;
 }
示例#3
0
        /// <summary>
        /// 表示 <see cref="Aoite.Data.DbEngine.Executing"/> 事件的处理方法。
        /// </summary>
        /// <param name="engine">数据源查询与交互引擎的实例。</param>
        /// <param name="type">执行的类型。</param>
        /// <param name="command">执行的命令。</param>
        protected virtual void OnExecuting(IDbEngine engine, ExecuteType type, ExecuteCommand command)
        {
            if (this._Manager != null)
            {
                this._Manager.InternalOnExecuting(engine, type, command);
            }
            var handler = this.Executing;

            if (handler != null)
            {
                handler(engine, command.GetEventArgs(type, null));
            }
        }
示例#4
0
        /// <summary>
        /// 表示 <see cref="Aoite.Data.DbEngine.Executed"/> 事件的处理方法。
        /// </summary>
        /// <param name="engine">数据源查询与交互引擎的实例。</param>
        /// <param name="type">执行的类型。</param>
        /// <param name="result">操作的返回值。</param>
        /// <param name="command">执行的命令。</param>
        protected virtual void OnExecuted(IDbEngine engine, ExecuteType type, ExecuteCommand command, IDbResult result)
        {
            if (this._Manager != null)
            {
                this._Manager.InternalOnExecuted(engine, type, command, result);
            }
            var handler = this.Executed;

            if (handler != null)
            {
                handler(engine, command.GetEventArgs(type, result));
            }
        }
示例#5
0
 /// <summary>
 /// 初始化一个 <see cref="DbExecutor"/> 类的新实例。
 /// </summary>
 /// <param name="engine">数据源查询与交互引擎的实例。</param>
 /// <param name="command">执行的命令。</param>
 /// <param name="connection">数据源的连接。可以为 null,表示一个新的连接。</param>
 /// <param name="transaction">数据源的事务上下文。可以为 null,表示当前交互行为不存在事务。</param>
 /// <param name="closeAfterFinally">指示当执行命令以后是否关闭连接。</param>
 public DbExecutor(IDbEngine engine, ExecuteCommand command, DbConnection connection, DbTransaction transaction, bool closeAfterFinally)
 {
     if (engine == null)
     {
         throw new ArgumentNullException(nameof(engine));
     }
     if (command == null)
     {
         throw new ArgumentNullException(nameof(command));
     }
     this.Engine             = engine;
     this.Command            = command;
     this._connection        = connection ?? engine.Provider.CreateConnection();
     this._transaction       = transaction;
     this._closeAfterFinally = closeAfterFinally;
 }
示例#6
0
        /// <summary>
        /// 指定执行命令,创建一个关联当前执行器的 <see cref="DbCommand"/> 的实例。
        /// </summary>
        /// <param name="command">执行的命令。</param>
        /// <returns>关联当前执行器的 <see cref="DbCommand"/> 的实例。</returns>
        protected virtual DbCommand CreateDbCommand(ExecuteCommand command)
        {
            var dbCommand = this.Engine.Provider.DbFactory.CreateCommand();

            dbCommand.Connection  = this._connection;
            dbCommand.Transaction = this._transaction;

            var commandText = command.Text;

            if (commandText[0] == '>')//- 存储过程
            {
                commandText           = commandText.Remove(0, 1);
                dbCommand.CommandType = CommandType.StoredProcedure;
            }

            dbCommand.CommandText = commandText;
            if (command.Count > 0)
            {
                this.FillParameters(dbCommand, command.Parameters);
            }
            return(dbCommand);
        }
示例#7
0
        /// <summary>
        /// 创建并返回一个与给定数据源关联的命令对象。
        /// </summary>
        /// <param name="engine">数据源查询与交互引擎的实例。</param>
        /// <param name="command">查询命令。</param>
        /// <param name="conn">指定数据源。</param>
        /// <returns>返回一个命令对象。</returns>
        public DbCommand CreateDbCommand(IDbEngine engine, ExecuteCommand command, DbConnection conn = null)
        {
            if (engine == null)
            {
                throw new ArgumentNullException("engine");
            }
            if (command == null)
            {
                throw new ArgumentNullException("exeCommand");
            }
            if (conn == null)
            {
                conn = this.CreateConnection(engine);
            }

            var dbCommand = this.CreateDbCommand(engine, command.CommandText);

            dbCommand.Connection = conn;

            if (command.CommandType.HasValue)
            {
                dbCommand.CommandType = command.CommandType.Value;
            }
            if (command.Timeout.HasValue)
            {
                dbCommand.CommandTimeout = command.Timeout.Value;
            }
            if (command.Count > 0)
            {
                this.FillParameters(engine, dbCommand, command.Parameters);
            }

            command.SetRuntimeObject(engine, dbCommand);

            return(dbCommand);
        }
示例#8
0
 internal void InternalOnExecuted(IDbEngine engine, ExecuteType type, ExecuteCommand command, IDbResult result)
 {
     this.OnExecuted(engine, type, command, result);
 }
示例#9
0
 /// <summary>
 /// 执行指定的命令。
 /// </summary>
 /// <param name="command">执行的命令。</param>
 /// <returns>数据源查询与交互的执行器。</returns>
 public IDbExecutor Execute(ExecuteCommand command)
 {
     if(command == null) throw new ArgumentNullException(nameof(command));
     return new DbExecutor(this, command, null, null, true);
 }
示例#10
0
 internal void InternalOnExecuting(IDbEngine engine, ExecuteType type, ExecuteCommand command)
 {
     this.OnExecuting(engine, type, command);
 }
示例#11
0
 /// <summary>
 /// 执行指定的命令。
 /// </summary>
 /// <param name="command">执行的命令。</param>
 /// <returns>数据源查询与交互的执行器。</returns>
 public IDbExecutor Execute(ExecuteCommand command)
 {
     this.ThrowIfDisposed();
     if(command == null) throw new ArgumentNullException(nameof(command));
     return new DbExecutor(this, command, this._lazyConnection.Value, this._transaction, false);
 }
示例#12
0
 internal ExecutedEventArgs(ExecuteCommand command, DbCommand dbCommand) : base(command, dbCommand)
 {
 }
示例#13
0
 public ExecuteCommand GetCommand(TypeMapper mapper, ExecuteCommand command)
 {
     return(command);
 }
示例#14
0
 internal ExecutedEventArgs(ExecuteCommand command, DbCommand dbCommand) : base(command, dbCommand) { }
示例#15
0
 internal ExecutingEventArgs(ExecuteCommand command)
 {
     this._Command = command;
 }
示例#16
0
 /// <summary>
 /// 表示 <see cref="Executing"/> 事件的处理方法。
 /// </summary>
 /// <param name="engine">数据源查询与交互引擎的实例。</param>
 /// <param name="type">执行的类型。</param>
 /// <param name="command">执行的命令。</param>
 /// <param name="dbCommand">执行的 <see cref="DbCommand"/>。</param>
 internal protected virtual void OnExecuting(IDbEngine engine, ExecuteType type, ExecuteCommand command, DbCommand dbCommand)
 => this.Executing?.Invoke(engine, command.GetEventArgs(type, dbCommand, null));
示例#17
0
 /// <summary>
 /// 表示 <see cref="Executed"/> 事件的处理方法。
 /// </summary>
 /// <param name="engine">数据源查询与交互引擎的实例。</param>
 /// <param name="type">执行的类型。</param>
 /// <param name="result">操作的返回值。</param>
 /// <param name="command">执行的命令。</param>
 /// <param name="dbCommand">执行的 <see cref="DbCommand"/>。</param>
 internal protected virtual void OnExecuted(IDbEngine engine, ExecuteType type, ExecuteCommand command, DbCommand dbCommand, object result)
     => this.Executed?.Invoke(engine, command.GetEventArgs(type, dbCommand, result));
示例#18
0
 /// <summary>
 /// 表示 <see cref="Executing"/> 事件的处理方法。
 /// </summary>
 /// <param name="engine">数据源查询与交互引擎的实例。</param>
 /// <param name="type">执行的类型。</param>
 /// <param name="command">执行的命令。</param>
 /// <param name="dbCommand">执行的 <see cref="DbCommand"/>。</param>
 internal protected virtual void OnExecuting(IDbEngine engine, ExecuteType type, ExecuteCommand command, DbCommand dbCommand)
     => this.Executing?.Invoke(engine, command.GetEventArgs(type, dbCommand, null));
示例#19
0
 /// <summary>
 /// 表示 <see cref="Executed"/> 事件的处理方法。
 /// </summary>
 /// <param name="engine">数据源查询与交互引擎的实例。</param>
 /// <param name="type">执行的类型。</param>
 /// <param name="result">操作的返回值。</param>
 /// <param name="command">执行的命令。</param>
 /// <param name="dbCommand">执行的 <see cref="DbCommand"/>。</param>
 internal protected virtual void OnExecuted(IDbEngine engine, ExecuteType type, ExecuteCommand command, DbCommand dbCommand, object result)
 => this.Executed?.Invoke(engine, command.GetEventArgs(type, dbCommand, result));
示例#20
0
 public ExecuteCommand GetCommand(TypeMapper mapper, ExecuteCommand command)
 {
     return command;
 }
示例#21
0
 internal ExecutingEventArgs(ExecuteCommand command, DbCommand dbCommand)
 {
     this.Command = command;
     this.DbCommand = dbCommand;
 }
示例#22
0
 /// <summary>
 /// 创建一个数据源查询与交互的执行器。
 /// </summary>
 /// <param name="engine">数据源查询与交互引擎的实例。</param>
 /// <param name="command">执行查询的命令。</param>
 /// <returns>返回一个执行器。</returns>
 public virtual IDbExecutor CreateExecutor(IDbEngine engine, ExecuteCommand command)
 {
     return(new DefaultDbExecutor(engine, command));
 }
示例#23
0
 public ExecuteCommand GetCommand(TypeMapper mapper, ExecuteCommand command) => command;
示例#24
0
 internal ExecutingEventArgs(ExecuteCommand command, DbCommand dbCommand)
 {
     this.Command   = command;
     this.DbCommand = dbCommand;
 }