/// <summary> /// Returns the total number of records of the specified entity filtered by the specified filter. /// </summary> /// <param name="entity">An Entity</param> /// <param name="filter">The filter information</param> /// <exception cref="System.ArgumentNullException">Thrown when entity is null, entity TableName is not specified.</exception> /// <exception cref="System.Exception">Thrown when a problem occur during the execution of the statement.</exception> /// <returns>The total number of filtered records</returns> public virtual int GetFilteredTotalRecords(Entity entity, FilterInfo filter) { if (entity == null || string.IsNullOrEmpty(entity.GetTableName())) { LoggerHelper.Warning("Entity is null or entity TableName is not specified."); throw new ArgumentNullException("Entity is null or entity TableName is not specified."); } LoggerHelper.Info("Start"); int total = 0; try { StatementWrapper stmtWrapper = this.GetQueryBuilder().BuildFilteredTotalRecordsStatement(entity, filter); LoggerHelper.Debug(stmtWrapper.Query.ToString()); total = GetQueryRunner().ExecuteScalar(GetConnection(), stmtWrapper); } catch (Exception e) { LoggerHelper.Error(e); throw new Exception("Unable to get " + entity.GetTableName() + " filtered total records.", e); } finally { LoggerHelper.Info("End"); } return(total); }
private bool HasOutputParams(StatementWrapper stmtWrapper, out DBParam param) { IList <DBParam> stmParams = stmtWrapper.DBParams; param = ((List <DBParam>)stmParams).Find(x => x.Direction == ParameterDirection.Output); if (param == null) { return(false); } return(true); }
/// <summary> /// Execute an SQL INSERT, UPDATE, or DELETE statement, using the specified Connection /// </summary> /// <param name="conn">The SQL connection.</param> /// <param name="stmtWrapper">The statement wrapper containing the query to execute and the statement params.</param> public void ExecuteNonQuery(DbConnection conn, StatementWrapper stmtWrapper) { CheckNulls(conn, stmtWrapper.Query); DbCommand cmd = null; try { cmd = conn.CreateCommand(); cmd.CommandText = stmtWrapper.Query.ToString(); SetParameters(cmd, stmtWrapper.DBParams); cmd.ExecuteNonQuery(); } finally { Close(conn, cmd, null); } }
/// <summary> /// Executes the specified statement using the specified connection, and returns the first column of the first row in the result set returned by the query. /// <para>Additional columns or rows are ignored.</para> /// </summary> /// <param name="conn">The SQL connection.</param> /// <param name="stmtWrapper">The statement wrapper containing the query to execute and the statement params.</param> /// <returns>The first column of the first row in the result set.</returns> public int ExecuteScalar(DbConnection conn, StatementWrapper stmtWrapper) { CheckNulls(conn, stmtWrapper.Query); int newId; DbCommand cmd = null; try { cmd = conn.CreateCommand(); cmd.CommandText = stmtWrapper.Query.ToString(); SetParameters(cmd, stmtWrapper.DBParams); //This check id for the Oracle Implementation //Because the only way to get the new id is has an output param and //executing a NonQuery operation DBParam idparam; if (HasOutputParams(stmtWrapper, out idparam)) { DbParameter idOutputParam = cmd.CreateParameter(); idOutputParam.ParameterName = idparam.ParamName; idOutputParam.DbType = idparam.ParamType; idOutputParam.Direction = ParameterDirection.Output; cmd.Parameters.Add(idOutputParam); cmd.ExecuteNonQuery(); newId = Convert.ToInt32(idOutputParam.Value); } else { newId = int.Parse(cmd.ExecuteScalar().ToString()); } } finally { Close(conn, cmd, null); } return(newId); }
/// <summary> /// Executes the specified statement using the specified connection. /// </summary> /// <typeparam name="T">The type of object that the handler returns</typeparam> /// <param name="conn">The connection to use for the query call.</param> /// <param name="stmtWrapper">The statement wrapper containing the query to execute and the statement params.</param> /// <param name="rsh">The handler used to create the result object from the ResultSet.</param> /// <returns>An object generated by the handler.</returns> public T Query <T>(DbConnection conn, StatementWrapper stmtWrapper, ResultSetHandler <T> rsh) { CheckNulls(conn, stmtWrapper.Query); T result = default(T); DbCommand cmd = null; DbDataReader rd = null; try { cmd = conn.CreateCommand(); cmd.CommandText = stmtWrapper.Query.ToString(); SetParameters(cmd, stmtWrapper.DBParams); rd = cmd.ExecuteReader(); result = rsh.Handle(rd); } finally { Close(conn, cmd, rd); } return(result); }