Пример #1
0
		public virtual object ExecuteScalar(string sql, IDataSource dataSource, IList parameters)
		{
			this.Context.LogManager.Info(this, "Executing scalar sql query", "Sql: " + sql); // do not localize
			IDbConnection connection;
			IDbCommand cmd;
			object result;
			ITransaction transaction;
			SqlExecutorCancelEventArgs e = new SqlExecutorCancelEventArgs(sql, dataSource, parameters);
			this.Context.EventManager.OnExecutingSql(this, e);
			if (e.Cancel)
			{
				this.Context.LogManager.Warn(this, "Executing scalar sql query canceled by observer!", "Sql: " + sql); // do not localize
				return 0;
			}
			sql = e.Sql;
			dataSource = e.DataSource;
			parameters = e.Parameters;
			connection = dataSource.GetConnection();
			cmd = connection.CreateCommand();
			cmd.CommandType = CommandType.Text;
			cmd.CommandText = sql;
			AddParametersToCommand(cmd, parameters);
			transaction = this.Context.GetTransaction(connection);
			if (transaction != null)
			{
				cmd.Transaction = transaction.DbTransaction;
			}
			result = cmd.ExecuteScalar();
			dataSource.ReturnConnection();
			
			SqlExecutorEventArgs e2 = new SqlExecutorEventArgs(sql, dataSource, parameters);

			this.Context.EventManager.OnExecutedSql(this, e2);
			return result;
		}
Пример #2
0
		public DataTable ExecuteDataTable(string sql, IDataSource dataSource, IList parameters)
		{
			this.Context.LogManager.Info(this, "Executing sql query and returning data table", "Sql: " + sql); // do not localize
			IDbConnection connection;
			IDbCommand cmd;
			IDataReader dr;
			DataTable result = null;
			ITransaction transaction;
			SqlExecutorCancelEventArgs e = new SqlExecutorCancelEventArgs(sql, dataSource, parameters);
			this.Context.EventManager.OnExecutingSql(this, e);
			if (e.Cancel)
			{
				this.Context.LogManager.Warn(this, "Executing sql query and returning data table canceled by observer!", "Sql: " + sql); // do not localize
				return null;
			}
			sql = e.Sql;
			parameters = e.Parameters;
			if (!(m_ExecutionMode == ExecutionMode.NoExecution))
			{
				ExecuteBatchedStatements(dataSource);
				connection = dataSource.GetConnection();
				cmd = connection.CreateCommand();
				cmd.CommandType = CommandType.Text;
				cmd.CommandText = sql;
				AddParametersToCommand(cmd, parameters);
				transaction = this.Context.GetTransaction(connection);
				if (transaction != null)
				{
					cmd.Transaction = transaction.DbTransaction;
				}
				dr = cmd.ExecuteReader();
				result = ReaderToDataTable(dr);
				dr.Close();
				dataSource.ReturnConnection();
			}

			SqlExecutorEventArgs e2 = new SqlExecutorEventArgs(sql, dataSource, parameters);

			this.Context.EventManager.OnExecutedSql(this, e2);
			return result;
		}
Пример #3
0
		public virtual void ExecuteBatchedStatements(IDataSource dataSource)
		{
			if (!(m_BatchStatements.ContainsKey(dataSource)))
			{
				return;
			}

			IDbConnection connection;
			IDbCommand cmd;
			int result;
			string sqlBatch = "";
			long matchResult = 0;
			IList parameters = new ArrayList() ;
			foreach (BatchedSqlStatement sql in ((ArrayList) (m_BatchStatements[dataSource])))
			{
				sqlBatch += sql.Sql + ";";
				matchResult += 1;
				foreach (QueryParameter parameter in sql.Parameters)
					parameters.Add(parameter);
			}

			SqlExecutorCancelEventArgs e = new SqlExecutorCancelEventArgs(sqlBatch, dataSource, parameters);
			this.Context.EventManager.OnExecutingSql(this, e);
			if (e.Cancel)
			{
				this.Context.LogManager.Warn(this, "Executing batched statements canceled by observer!", "Sql: " + e.Sql); // do not localize
			}
			sqlBatch = e.Sql;
			dataSource = e.DataSource;
			parameters = e.Parameters;

			connection = dataSource.GetConnection();
			cmd = connection.CreateCommand();
			cmd.CommandText = sqlBatch;
			AddParametersToCommand(cmd, parameters);
			result = cmd.ExecuteNonQuery();
			cmd.Dispose();
			dataSource.ReturnConnection();
			
			SqlExecutorEventArgs e2 = new SqlExecutorEventArgs(sqlBatch, dataSource, parameters);

			this.Context.EventManager.OnExecutedSql(this, e2);

			if (!(result == matchResult))
			{
				throw new OptimisticConcurrencyException("An optimistic concurrency exception occurred when executing a batch statement!", null); // do not localize
			}
		}
Пример #4
0
		//public virtual IDataReader ExecuteReader(string sql, IDataSource dataSource, IDbConnection connection, IList parameters)
		public virtual IDataReader ExecuteReader(string sql, IDataSource dataSource, IList parameters)
		{
			this.Context.LogManager.Info(this, "Executing sql query and returning data reader", "Sql: " + sql); // do not localize
			IDbConnection connection;
			IDbCommand cmd;
			IDataReader dr;
			ITransaction transaction;
			SqlExecutorCancelEventArgs e = new SqlExecutorCancelEventArgs(sql, dataSource, parameters);
			this.Context.EventManager.OnExecutingSql(this, e);
			if (e.Cancel)
			{
				this.Context.LogManager.Warn(this, "Executing sql query and returning data reader canceled by observer!", "Sql: " + sql); // do not localize
				return null;
			}
			sql = e.Sql;
			dataSource = e.DataSource;
			parameters = e.Parameters;
			if (m_ExecutionMode == ExecutionMode.NoExecution)
			{
				return null;
			}
			ExecuteBatchedStatements(dataSource);
			connection = dataSource.GetConnection();
			cmd = connection.CreateCommand();
			cmd.CommandType = CommandType.Text;
			cmd.CommandText = sql;
			AddParametersToCommand(cmd, parameters);
			transaction = this.Context.GetTransaction(connection);
			if (transaction != null)
			{
				cmd.Transaction = transaction.DbTransaction;
			}
			
			dr = cmd.ExecuteReader();

			SqlExecutorEventArgs e2 = new SqlExecutorEventArgs(sql, dataSource, parameters);

			this.Context.EventManager.OnExecutedSql(this, e2);

			//clone data and return the offline data to the rest of NP
			return new OfflineDataReader(dr);
		}
Пример #5
0
		public virtual int ExecuteNonQuery(string sql, IDataSource dataSource, IList parameters)
		{
            if (dataSource == null)
                throw new ArgumentNullException("dataSource");

			this.Context.LogManager.Info(this, "Executing non query", "Sql: " + sql); // do not localize
			IDbConnection connection;
			IDbCommand cmd;
			int result = 0;
			ITransaction transaction;
			bool postPoned = false;
			if (m_ExecutionMode == ExecutionMode.BatchExecution)
				postPoned = true;

			SqlExecutorCancelEventArgs e = new SqlExecutorCancelEventArgs(sql, dataSource, parameters, postPoned);
			this.Context.EventManager.OnExecutingSql(this, e);
			if (e.Cancel)
			{
				this.Context.LogManager.Warn(this, "Executing non query canceled by observer!", "Sql: " + sql); // do not localize
				return 0;
			}
			sql = e.Sql;
			dataSource = e.DataSource;
			parameters = e.Parameters;
			if (m_ExecutionMode == ExecutionMode.NoExecution || m_ExecutionMode == ExecutionMode.NoWriteExecution)
			{
				result = 1;
			}
			else if (m_ExecutionMode == ExecutionMode.BatchExecution)
			{
				result = 1;
				BatchStatement(sql, dataSource, parameters);
			}
			else if (m_ExecutionMode == ExecutionMode.DirectExecution)
			{
				connection = dataSource.GetConnection();
				cmd = connection.CreateCommand();
				cmd.CommandType = CommandType.Text;
				AddParametersToCommand(cmd, parameters);
				cmd.CommandText = sql;
				transaction = this.Context.GetTransaction(connection);
				if (transaction != null)
				{
					cmd.Transaction = transaction.DbTransaction;
				}
				result = cmd.ExecuteNonQuery();
				dataSource.ReturnConnection();
			}

			SqlExecutorEventArgs e2 = new SqlExecutorEventArgs(sql, dataSource, parameters, postPoned);

			this.Context.EventManager.OnExecutedSql(this, e2);
			return result;
		}
Пример #6
0
 public virtual void OnExecutedSql(object sender, SqlExecutorEventArgs e)
 {
 }
Пример #7
0
		public virtual void OnExecutedSql(object sender, SqlExecutorEventArgs e)
		{
			if (!(m_RaiseEvents))
			{
				return;
			}
			if (!(m_RaiseAfterEvents))
			{
				return;
			}
			if (!(m_RaiseSqlExecutorEvents))
			{
				return;
			}
			foreach (IObserver observer in m_Observers)
			{
				observer.OnExecutedSql(sender, e);
			}
			foreach (IObserver observer in m_ContextObservers)
			{
				observer.OnExecutedSql(sender, e);
			}
			this.Observer.OnExecutedSql(sender, e);
		}
Пример #8
0
        public virtual object ExecuteArray(string sql, IDataSource dataSource, IList parameters)
        {
            LogMessage message = new LogMessage("Executing sql query and returning array");
            LogMessage verbose = new LogMessage("Sql: {0}", sql);
            this.Context.LogManager.Info(this, message, verbose); // do not localize

            IDbConnection connection;
            IDbCommand cmd;
            IDataReader dr;
            object result = null;
            ITransaction transaction;
            SqlExecutorCancelEventArgs e = new SqlExecutorCancelEventArgs(sql, dataSource, parameters);
            this.Context.EventManager.OnExecutingSql(this, e);
            if (e.Cancel)
            {
                message = new LogMessage("Executing sql query and returning array canceled by observer!");
                verbose = new LogMessage("Sql: {0}", sql);
                this.Context.LogManager.Info(this, message, verbose); // do not localize
                return null;
            }
            sql = e.Sql;
            parameters = e.Parameters;
            if (!(m_ExecutionMode == ExecutionMode.NoExecution))
            {
                ExecuteBatchedStatements(dataSource);
                connection = dataSource.GetConnection();
                cmd = connection.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sql;
                AddParametersToCommand(cmd, parameters);
                transaction = this.Context.GetTransaction(connection);
                if (transaction != null)
                {
                    cmd.Transaction = transaction.DbTransaction;
                }
                dr = cmd.ExecuteReader();
                result = ReaderToArray(dr);
                dr.Close();
                dataSource.ReturnConnection();
            }

            SqlExecutorEventArgs e2 = new SqlExecutorEventArgs(sql, dataSource, parameters);

            this.Context.EventManager.OnExecutedSql(this, e2);
            return result;
        }