internal static void AddDbParams(this IDbCommand cmd, DbParams param) { foreach (var p in param) { var cmdParam = cmd.CreateParameter(); cmdParam.ParameterName = p.Key; cmdParam.Value = p.Value ?? DBNull.Value; cmd.Parameters.Add(cmdParam); } }
private T Do <T>(string sql, DbParams param, Func <IDbCommand, T> func) { try { var cmd = _connection.NewCommand(_transaction, sql, param); return(func(cmd)); } catch (FailedExecutionException) { Rollback(); throw; } }
private async Task <T> DoAsync <T>(string sql, DbParams param, Func <DbCommand, Task <T> > func) { try { var cmd = _connection.NewCommand(_transaction, sql, param) as DbCommand; return(await func(cmd)); } catch (FailedExecutionException) { Rollback(); throw; } }
/// <summary> /// Asynchronously execute parameterized query and return single-value. /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public Task <object> ScalarAsync(string sql, DbParams param = null) => BatchAsync(b => b.ScalarAsync(sql, param));
/// <summary> /// Asynchronously execute parameterized query and return rows affected. /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public Task <int> ExecAsync(string sql, DbParams param = null) => BatchAsync(b => b.ExecAsync(sql, param));
/// <summary> /// Execute paramterized query, read only first record and apply mapping. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql"></param> /// <param name="map"></param> /// <param name="param"></param> /// <returns></returns> public T QuerySingle <T>(string sql, DbParams param, Func <IDataReader, T> map) => Batch(b => b.QuerySingle(sql, param, map));
/// <summary> /// Execute paramterized query and manually cursor IDataReader. /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public IDataReader Read(string sql, DbParams param = null) => Do(sql, param, cmd => cmd.Read());
/// <summary> /// Execute parameterized query, enumerate all records and apply mapping. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql"></param> /// <param name="map"></param> /// <param name="param"></param> /// <returns></returns> public IEnumerable <T> Query <T>(string sql, DbParams param, Func <IDataReader, T> map) => Batch(b => b.Query(sql, param, map));
/// <summary> /// Execute parameterized query and return rows affected. /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public int Exec(string sql, DbParams param = null) => Batch(b => b.Exec(sql, param));
/// <summary> /// Asynchronously execute paramterized query, read only first record and apply mapping. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql"></param> /// <param name="map"></param> /// <param name="param"></param> /// <returns></returns> public Task <T> QuerySingleAsync <T>(string sql, DbParams param, Func <IDataReader, T> map) => DoAsync(sql, param, cmd => cmd.QuerySingleAsync(map));
/// <summary> /// Execute paramterized query and manually cursor IDataReader. /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public Task <IDataReader> ReadAsync(string sql, DbParams param = null) => DoAsync(sql, param, cmd => cmd.ReadAsync());
/// <summary> /// Asynchronously execute parameterized query, enumerate all records and apply mapping. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql"></param> /// <param name="map"></param> /// <param name="param"></param> /// <returns></returns> public Task <IEnumerable <T> > QueryAsync <T>(string sql, DbParams param, Func <IDataReader, T> map) => DoAsync(sql, param, cmd => cmd.QueryAsync(map));
/// <summary> /// Asynchronously execute parameterized query and return single-value. /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public Task <object> ScalarAsync(string sql, DbParams param = null) => DoAsync(sql, param, cmd => cmd.ScalarAsync());
/// <summary> /// Asynchronously execute parameterized query and return rows affected. /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public Task <int> ExecAsync(string sql, DbParams param = null) => DoAsync(sql, param, cmd => cmd.ExecAsync());
/// <summary> /// Asynchronously execute parameterized query, enumerate all records and apply mapping. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql"></param> /// <param name="map"></param> /// <param name="param"></param> /// <returns></returns> public Task <IEnumerable <T> > QueryAsync <T>(string sql, DbParams param, Func <IDataReader, T> map) => BatchAsync(b => b.QueryAsync(sql, param, map));
/// <summary> /// Execute parameterized query and return rows affected. /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public int Exec(string sql, DbParams param = null) => Do(sql, param, cmd => cmd.Exec());
/// <summary> /// Asynchronously execute paramterized query, read only first record and apply mapping. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql"></param> /// <param name="map"></param> /// <param name="param"></param> /// <returns></returns> public Task <T> QuerySingleAsync <T>(string sql, DbParams param, Func <IDataReader, T> map) => BatchAsync(b => b.QuerySingleAsync(sql, param, map));
/// <summary> /// Execute parameterized query and return single-value. /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public object Scalar(string sql, DbParams param = null) => Do(sql, param, cmd => cmd.Scalar());
/// <summary> /// Execute parameterized query and return single-value. /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public object Scalar(string sql, DbParams param = null) => Batch(b => b.Scalar(sql, param));
/// <summary> /// Execute parameterized query, enumerate all records and apply mapping. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql"></param> /// <param name="map"></param> /// <param name="param"></param> /// <returns></returns> public IEnumerable <T> Query <T>(string sql, DbParams param, Func <IDataReader, T> map) => Do(sql, param, cmd => cmd.Query(map));
/// <summary> /// Create a new IDbCommand. /// </summary> /// <param name="conn"></param> /// <param name="trans"></param> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> internal static IDbCommand NewCommand(this IDbConnection conn, IDbTransaction trans, string sql, DbParams param = null) { var cmd = conn.CreateCommand(); cmd.Transaction = trans; cmd.CommandType = CommandType.Text; cmd.CommandText = sql; if (param != null && param.Count > 0) { cmd.AddDbParams(param); } return(cmd); }
/// <summary> /// Execute paramterized query, read only first record and apply mapping. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql"></param> /// <param name="map"></param> /// <param name="param"></param> /// <returns></returns> public T QuerySingle <T>(string sql, DbParams param, Func <IDataReader, T> map) => Do(sql, param, cmd => cmd.QuerySingle(map));