Exemplo n.º 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());
        }
Exemplo n.º 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;
 }
Exemplo n.º 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));
            }
        }
Exemplo n.º 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));
            }
        }
Exemplo n.º 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;
 }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 8
0
 internal void InternalOnExecuted(IDbEngine engine, ExecuteType type, ExecuteCommand command, IDbResult result)
 {
     this.OnExecuted(engine, type, command, result);
 }
Exemplo n.º 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);
 }
Exemplo n.º 10
0
 internal void InternalOnExecuting(IDbEngine engine, ExecuteType type, ExecuteCommand command)
 {
     this.OnExecuting(engine, type, command);
 }
Exemplo n.º 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);
 }
Exemplo n.º 12
0
 internal ExecutedEventArgs(ExecuteCommand command, DbCommand dbCommand) : base(command, dbCommand)
 {
 }
Exemplo n.º 13
0
 public ExecuteCommand GetCommand(TypeMapper mapper, ExecuteCommand command)
 {
     return(command);
 }
Exemplo n.º 14
0
 internal ExecutedEventArgs(ExecuteCommand command, DbCommand dbCommand) : base(command, dbCommand) { }
Exemplo n.º 15
0
 internal ExecutingEventArgs(ExecuteCommand command)
 {
     this._Command = command;
 }
Exemplo n.º 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));
Exemplo n.º 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));
Exemplo n.º 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));
Exemplo n.º 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));
Exemplo n.º 20
0
 public ExecuteCommand GetCommand(TypeMapper mapper, ExecuteCommand command)
 {
     return command;
 }
Exemplo n.º 21
0
 internal ExecutingEventArgs(ExecuteCommand command, DbCommand dbCommand)
 {
     this.Command = command;
     this.DbCommand = dbCommand;
 }
Exemplo n.º 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));
 }
Exemplo n.º 23
0
 public ExecuteCommand GetCommand(TypeMapper mapper, ExecuteCommand command) => command;
Exemplo n.º 24
0
 internal ExecutingEventArgs(ExecuteCommand command, DbCommand dbCommand)
 {
     this.Command   = command;
     this.DbCommand = dbCommand;
 }