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); }
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) { LogMessage message = new LogMessage("Executing batched statements canceled by observer!"); LogMessage verbose = new LogMessage("Sql: {0}", e.Sql); this.Context.LogManager.Warn(this, message, verbose); // 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 } }
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); }
//public virtual IDataReader ExecuteReader(string sql, IDataSource dataSource, IDbConnection connection, IList parameters) public virtual IDataReader ExecuteReader(string sql, IDataSource dataSource, IList parameters) { LogMessage message = new LogMessage("Executing sql query and returning data reader"); LogMessage verbose = new LogMessage("Sql: {0}", sql); this.Context.LogManager.Info(this, message, verbose); // 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) { message = new LogMessage("Executing sql query and returning data reader canceled by observer!"); verbose = new LogMessage("Sql: {0}", sql); this.Context.LogManager.Info(this, message, verbose); // 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)); }
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); }
public virtual object ExecuteScalar(string sql, IDataSource dataSource, IList parameters) { LogMessage message = new LogMessage("Executing scalar sql query"); LogMessage verbose = new LogMessage("Sql: {0}", sql); this.Context.LogManager.Info(this, message, verbose); // 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) { message = new LogMessage("Executing scalar sql query canceled by observer!"); verbose = new LogMessage("Sql: {0}", sql); this.Context.LogManager.Warn(this, message, verbose); // 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); }
public virtual void OnExecutedSql(object sender, SqlExecutorEventArgs e) { }