Example #1
0
 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);
     }
 }
Example #2
0
 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;
     }
 }
Example #3
0
 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;
     }
 }
Example #4
0
 /// <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));
Example #5
0
 /// <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));
Example #6
0
 /// <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));
Example #7
0
 /// <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());
Example #8
0
 /// <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));
Example #9
0
 /// <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));
Example #10
0
 /// <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));
Example #11
0
 /// <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());
Example #12
0
 /// <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));
Example #13
0
 /// <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());
Example #14
0
 /// <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());
Example #15
0
 /// <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));
Example #16
0
 /// <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());
Example #17
0
 /// <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));
Example #18
0
 /// <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());
Example #19
0
 /// <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));
Example #20
0
 /// <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);
        }
Example #22
0
 /// <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));