예제 #1
0
 public async Task <dynamic> InsertAsync <T>(T entity, IDbConnection connection = null, IDbTransaction transaction = null)
     where T : class
 {
     if (connection == null)
     {
         using (connection = await _factory.CreateAsync())
         {
             return(await connection.InsertAsync(entity, transaction : transaction));
         }
     }
     else
     {
         return(await connection.InsertAsync(entity, transaction : transaction));
     }
 }
예제 #2
0
        private async Task InitConnection(CancellationToken cancellationToken)
        {
            mConnection?.Dispose();

            mConnection = await mConnectionFactory.CreateAsync(cancellationToken)
                          .ConfigureAwait(false);
        }
예제 #3
0
        public async Task <T> ExecuteQueryAsync <T>(IQuery query, Func <NpgsqlCommand, Task <T> > functionAsync)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            if (functionAsync == null)
            {
                throw new ArgumentNullException(nameof(functionAsync));
            }

            using (var connection = await connectionFactory.CreateAsync())
            {
                using (var command = connection.CreateCommand())
                {
                    commandPreparer.Prepare(command, query);

                    try
                    {
                        return(await functionAsync(command));
                    }
                    catch (PostgresException pe)
                    {
                        if (pe.SqlState == "23505")
                        {
                            throw new UniqueIndexViolationException(pe);
                        }

                        throw;
                    }
                }
            }
        }
예제 #4
0
 /// <inheritdoc/>
 public async Task <T> GetAsync <T>(string sql, object param = null, IDbConnection connection = null, IDbTransaction transaction = null)
 {
     if (connection == null)
     {
         using (connection = await _factory.CreateAsync())
         {
             return(await connection.QueryFirstOrDefaultAsync <T>(sql, param, commandTimeout : _commandTimeout));
         }
     }
     else
     {
         return(await connection.QueryFirstOrDefaultAsync <T>(sql, param, transaction, commandTimeout : _commandTimeout));
     }
 }
예제 #5
0
 public async Task <IEnumerable <T> > GetAllAsync <T>(ISqlObject obj, IDbConnection connection = null, IDbTransaction transaction = null)
 {
     if (connection == null)
     {
         using (connection = await _factory.CreateAsync())
         {
             return(await connection.QueryAsync <T>(obj.Sql, obj.QueryParams, commandTimeout : _commandTimeout));
         }
     }
     else
     {
         return(await connection.QueryAsync <T>(obj.Sql, obj.QueryParams, transaction, commandTimeout : _commandTimeout));
     }
 }