Ejemplo n.º 1
0
        /// <summary>
        ///     Execute Join query
        /// </summary>
        protected virtual IEnumerable <TEntity> ExecuteJoinQuery <TChild1, TChild2, TChild3, TChild4, TChild5, TChild6>(
            SqlQuery sqlQuery,
            IDbTransaction?transaction,
            params Expression <Func <TEntity, object> >[] includes)
        {
            if (!SqlGenerator.KeySqlProperties.Any())
            {
                throw new NotSupportedException("Join doesn't support without [Key] attribute");
            }

            var type = typeof(TEntity);

            var childProperties    = new List <PropertyInfo>();
            var childKeyProperties = new List <PropertyInfo>();
            var keyProperties      = SqlGenerator.KeySqlProperties.Select(q => q.PropertyInfo).ToArray();

            foreach (var s in includes)
            {
                var prop      = ExpressionHelper.GetPropertyName(s);
                var childProp = type.GetProperty(prop);

                if (childProp == null)
                {
                    continue;
                }

                childProperties.Add(childProp);
                var childType  = childProp.PropertyType.IsGenericType ? childProp.PropertyType.GenericTypeArguments[0] : childProp.PropertyType;
                var properties = childType.FindClassPrimitiveProperties();

                childKeyProperties.AddRange(properties.Where(p => p.GetCustomAttributes <KeyAttribute>().Any()));
            }

            if (!childKeyProperties.Any())
            {
                throw new NotSupportedException("Join doesn't support without [Key] attribute");
            }

            var        lookup   = new Dictionary <object, TEntity>();
            const bool buffered = true;

            var spiltOn = string.Join(",", childKeyProperties.Select(q => q.Name));

            switch (includes.Length)
            {
            case 1:
                Connection.Query <TEntity, TChild1, TEntity>(sqlQuery.GetSql(), (entity, child1) =>
                                                             EntityJoinMapping <TChild1, TChild2, TChild3, TChild4, TChild5, TChild6>(lookup, keyProperties, childKeyProperties, childProperties, entity, child1),
                                                             sqlQuery.Param, transaction, buffered, spiltOn);
                break;

            case 2:
                Connection.Query <TEntity, TChild1, TChild2, TEntity>(sqlQuery.GetSql(), (entity, child1, child2) =>
                                                                      EntityJoinMapping <TChild1, TChild2, TChild3, TChild4, TChild5, TChild6>(lookup, keyProperties, childKeyProperties, childProperties, entity, child1,
                                                                                                                                               child2),
                                                                      sqlQuery.Param, transaction, buffered, spiltOn);
                break;

            case 3:
                Connection.Query <TEntity, TChild1, TChild2, TChild3, TEntity>(sqlQuery.GetSql(), (entity, child1, child2, child3) =>
                                                                               EntityJoinMapping <TChild1, TChild2, TChild3, TChild4, TChild5, TChild6>(lookup, keyProperties, childKeyProperties, childProperties, entity, child1,
                                                                                                                                                        child2, child3),
                                                                               sqlQuery.Param, transaction, buffered, spiltOn);
                break;

            case 4:
                Connection.Query <TEntity, TChild1, TChild2, TChild3, TChild4, TEntity>(sqlQuery.GetSql(), (entity, child1, child2, child3, child4) =>
                                                                                        EntityJoinMapping <TChild1, TChild2, TChild3, TChild4, TChild5, TChild6>(lookup, keyProperties, childKeyProperties, childProperties, entity, child1,
                                                                                                                                                                 child2, child3, child4),
                                                                                        sqlQuery.Param, transaction, buffered, spiltOn);
                break;

            case 5:
                Connection.Query <TEntity, TChild1, TChild2, TChild3, TChild4, TChild5, TEntity>(sqlQuery.GetSql(), (entity, child1, child2, child3, child4, child5) =>
                                                                                                 EntityJoinMapping <TChild1, TChild2, TChild3, TChild4, TChild5, TChild6>(lookup, keyProperties, childKeyProperties, childProperties, entity, child1,
                                                                                                                                                                          child2, child3, child4, child5),
                                                                                                 sqlQuery.Param, transaction, buffered, spiltOn);
                break;

            case 6:
                Connection.Query <TEntity, TChild1, TChild2, TChild3, TChild4, TChild5, TChild6, TEntity>(sqlQuery.GetSql(),
                                                                                                          (entity, child1, child2, child3, child4, child5, child6) =>
                                                                                                          EntityJoinMapping <TChild1, TChild2, TChild3, TChild4, TChild5, TChild6>(lookup, keyProperties, childKeyProperties, childProperties, entity, child1,
                                                                                                                                                                                   child2, child3, child4, child5, child6),
                                                                                                          sqlQuery.Param, transaction, buffered, spiltOn);
                break;

            default:
                throw new NotSupportedException();
            }

            return(lookup.Values);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Retrieves the entity of type <typeparamref name="TReturn"/> with the specified id
 /// joined with the types specified as type parameters.
 /// </summary>
 /// <typeparam name="T1">The first type parameter. This is the source entity.</typeparam>
 /// <typeparam name="T2">The second type parameter.</typeparam>
 /// <typeparam name="TReturn">The return type parameter.</typeparam>
 /// <param name="connection">The connection to the database. This can either be open or closed.</param>
 /// <param name="id">The id of the entity in the database.</param>
 /// <param name="map">The mapping to perform on the entities in the result set.</param>
 /// <param name="transaction">Optional transaction for the command.</param>
 /// <param name="cancellationToken">Optional cancellation token for the command.</param>
 /// <returns>The entity with the corresponding id joined with the specified types.</returns>
 public static TReturn?Get <T1, T2, TReturn>(this IDbConnection connection, object id, Func <T1, T2, TReturn> map, IDbTransaction?transaction = null, CancellationToken cancellationToken = default) where TReturn : class
 => MultiMap <T1, T2, DontMap, DontMap, DontMap, DontMap, DontMap, TReturn>(connection, map, id, transaction).FirstOrDefault();
Ejemplo n.º 3
0
 /// <summary>
 /// Retrieves all the entities of type <typeparamref name="TReturn"/>
 /// joined with the types specified as type parameters.
 /// </summary>
 /// <typeparam name="T1">The first type parameter. This is the source entity.</typeparam>
 /// <typeparam name="T2">The second type parameter.</typeparam>
 /// <typeparam name="T3">The third type parameter.</typeparam>
 /// <typeparam name="T4">The fourth type parameter.</typeparam>
 /// <typeparam name="T5">The fifth type parameter.</typeparam>
 /// <typeparam name="T6">The sixth type parameter.</typeparam>
 /// <typeparam name="T7">The seventh type parameter.</typeparam>
 /// <typeparam name="TReturn">The return type parameter.</typeparam>
 /// <param name="connection">The connection to the database. This can either be open or closed.</param>
 /// <param name="map">The mapping to perform on the entities in the result set.</param>
 /// <param name="transaction">Optional transaction for the command.</param>
 /// <param name="buffered">
 /// A value indicating whether the result of the query should be executed directly,
 /// or when the query is materialized (using <c>ToList()</c> for example).
 /// </param>
 /// <returns>
 /// A collection of entities of type <typeparamref name="TReturn"/>
 /// joined with the specified type types.
 /// </returns>
 public static IEnumerable <TReturn> GetAll <T1, T2, T3, T4, T5, T6, T7, TReturn>(
     this IDbConnection connection,
     Func <T1, T2, T3, T4, T5, T6, T7, TReturn> map,
     IDbTransaction?transaction = null,
     bool buffered = true) => MultiMap <T1, T2, T3, T4, T5, T6, T7, TReturn>(connection, map, id: null, transaction, buffered);
Ejemplo n.º 4
0
        private static IEnumerable <TReturn> MultiMap <T1, T2, T3, T4, T5, T6, T7, TReturn>(IDbConnection connection, Delegate map, object?id, IDbTransaction?transaction = null, bool buffered = true)
        {
            var resultType   = typeof(TReturn);
            var includeTypes = new[]
            {
                typeof(T1),
                typeof(T2),
                typeof(T3),
                typeof(T4),
                typeof(T5),
                typeof(T6),
                typeof(T7)
            }
            .Where(t => t != typeof(DontMap))
            .ToArray();

            var sql = BuildMultiMapQuery(GetSqlBuilder(connection), resultType, includeTypes, id, out var parameters);

            LogQuery <TReturn>(sql);
            var splitOn = CreateSplitOn(includeTypes);

            return(includeTypes.Length switch
            {
                2 => connection.Query(sql, (Func <T1, T2, TReturn>)map, parameters, transaction, buffered, splitOn),
                3 => connection.Query(sql, (Func <T1, T2, T3, TReturn>)map, parameters, transaction, buffered, splitOn),
                4 => connection.Query(sql, (Func <T1, T2, T3, T4, TReturn>)map, parameters, transaction, buffered, splitOn),
                5 => connection.Query(sql, (Func <T1, T2, T3, T4, T5, TReturn>)map, parameters, transaction, buffered, splitOn),
                6 => connection.Query(sql, (Func <T1, T2, T3, T4, T5, T6, TReturn>)map, parameters, transaction, buffered, splitOn),
                7 => connection.Query(sql, (Func <T1, T2, T3, T4, T5, T6, T7, TReturn>)map, parameters, transaction, buffered, splitOn),
                _ => throw new InvalidOperationException($"Invalid amount of include types: {includeTypes.Length}."),
            });
Ejemplo n.º 5
0
        /// <summary>
        /// Returns the number of entities matching the specified predicate.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="connection">The connection to the database. This can either be open or closed.</param>
        /// <param name="predicate">A predicate to filter the results.</param>
        /// <param name="transaction">Optional transaction for the command.</param>
        /// <returns>The number of entities matching the specified predicate.</returns>
        public static Task <long> CountAsync <TEntity>(this IDbConnection connection, Expression <Func <TEntity, bool> > predicate, IDbTransaction?transaction = null)
        {
            var sql = BuildCountSql(GetSqlBuilder(connection), predicate, out var parameters);

            LogQuery <TEntity>(sql);
            return(connection.ExecuteScalarAsync <long>(sql, parameters, transaction));
        }
Ejemplo n.º 6
0
    protected Task <int> SafeDropDatabaseAsync(string databaseName, IDbTransaction?transaction = default)
    {
        Guard.Argument(databaseName).NotNull().NotEmpty().NotWhiteSpace().Matches(_namePattern);

        return(ExecuteAsync($"DROP DATABASE IF EXISTS `{databaseName}`;", transaction: transaction));
    }
Ejemplo n.º 7
0
    protected async IAsyncEnumerable <T> QueryAsync <T>(string sql, object?param = default, IDbTransaction?transaction = default, int?commandTimeout = default, CommandType?commandType = default)
    {
        Guard.Argument(sql).NotNull().NotEmpty().NotWhiteSpace();

        var results = await SafeExecuteAsync(() => _connection.QueryAsync <T>(sql, param, transaction, commandTimeout, commandType));

        foreach (var result in results)
        {
            yield return(result);
        }
    }
Ejemplo n.º 8
0
        /// <summary>
        /// Retrieves a paged set of entities of type <typeparamref name="TEntity"/>.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="connection">The connection to the database. This can either be open or closed.</param>
        /// <param name="pageNumber">The number of the page to fetch, starting at 1.</param>
        /// <param name="pageSize">The page size.</param>
        /// <param name="transaction">Optional transaction for the command.</param>
        /// <returns>A paged collection of entities of type <typeparamref name="TEntity"/>.</returns>
        public static Task <IEnumerable <TEntity> > GetPagedAsync <TEntity>(this IDbConnection connection, int pageNumber, int pageSize, IDbTransaction?transaction = null) where TEntity : class
        {
            var sql = BuildPagedQuery(connection, typeof(TEntity), pageNumber, pageSize);

            LogQuery <TEntity>(sql);
            return(connection.QueryAsync <TEntity>(sql, transaction: transaction));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Retrieves the entity of type <typeparamref name="TEntity"/> with the specified id.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="connection">The connection to the database. This can either be open or closed.</param>
        /// <param name="id">The id of the entity in the database.</param>
        /// <param name="transaction">Optional transaction for the command.</param>
        /// <returns>The entity with the corresponding id.</returns>
        public static Task <TEntity> GetAsync <TEntity>(this IDbConnection connection, object id, IDbTransaction?transaction = null) where TEntity : class
        {
            var sql = BuildGetById(GetSqlBuilder(connection), typeof(TEntity), id, out var parameters);

            LogQuery <TEntity>(sql);
            return(connection.QueryFirstOrDefaultAsync <TEntity>(sql, parameters, transaction));
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Retrieves all the entities of type <typeparamref name="TEntity"/>.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="connection">The connection to the database. This can either be open or closed.</param>
        /// <param name="transaction">Optional transaction for the command.</param>
        /// <returns>A collection of entities of type <typeparamref name="TEntity"/>.</returns>
        public static Task <IEnumerable <TEntity> > GetAllAsync <TEntity>(this IDbConnection connection, IDbTransaction?transaction = null) where TEntity : class
        {
            var sql = BuildGetAllQuery(connection, typeof(TEntity));

            LogQuery <TEntity>(sql);
            return(connection.QueryAsync <TEntity>(sql, transaction: transaction));
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Retrieves a paged set of entities of type <typeparamref name="TEntity"/>.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="connection">The connection to the database. This can either be open or closed.</param>
        /// <param name="pageNumber">The number of the page to fetch, starting at 1.</param>
        /// <param name="pageSize">The page size.</param>
        /// <param name="buffered">
        /// A value indicating whether the result of the query should be executed directly,
        /// or when the query is materialized (using <c>ToList()</c> for example).
        /// </param>
        /// <param name="transaction">Optional transaction for the command.</param>
        /// <returns>A paged collection of entities of type <typeparamref name="TEntity"/>.</returns>
        public static IEnumerable <TEntity> GetPaged <TEntity>(this IDbConnection connection, int pageNumber, int pageSize, IDbTransaction?transaction = null, bool buffered = true) where TEntity : class
        {
            var sql = BuildPagedQuery(connection, typeof(TEntity), pageNumber, pageSize);

            LogQuery <TEntity>(sql);
            return(connection.Query <TEntity>(sql, transaction: transaction, buffered: buffered));
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Retrieves all the entities of type <typeparamref name="TEntity"/>.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="connection">The connection to the database. This can either be open or closed.</param>
        /// <param name="buffered">
        /// A value indicating whether the result of the query should be executed directly,
        /// or when the query is materialized (using <c>ToList()</c> for example).
        /// </param>
        /// <param name="transaction">Optional transaction for the command.</param>
        /// <returns>A collection of entities of type <typeparamref name="TEntity"/>.</returns>
        public static IEnumerable <TEntity> GetAll <TEntity>(this IDbConnection connection, IDbTransaction?transaction = null, bool buffered = true) where TEntity : class
        {
            var sql = BuildGetAllQuery(connection, typeof(TEntity));

            LogQuery <TEntity>(sql);
            return(connection.Query <TEntity>(sql, transaction: transaction, buffered: buffered));
        }
Ejemplo n.º 13
0
 public Dictionary <string, string> GetColumnTypes(string tableName, IDbTransaction?transaction = null) =>
 _connection
 .Query <KeyValuePair <string, string> >(ColumnTypesQuery, new { TableName = tableName }, transaction)
 .ToDictionary(kv => kv.Key, kv => kv.Value);
Ejemplo n.º 14
0
 IOpenDataSource IRootDataSource.CreateOpenDataSource(IDbConnection connection, IDbTransaction?transaction)
 {
     return(CreateOpenDataSource((AbstractConnection)connection, (AbstractTransaction?)transaction));
 }
Ejemplo n.º 15
0
 /// <summary>
 /// 创建事务
 /// </summary>
 public async Task BeginTransactionAsync()
 {
     _dbTransaction = await CreateConnection().BeginTransactionAsync();
 }
Ejemplo n.º 16
0
        /// <summary>
        /// Retrieves the entity of type <typeparamref name="TEntity"/> with the specified id.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="connection">The connection to the database. This can either be open or closed.</param>
        /// <param name="ids">The id of the entity in the database.</param>
        /// <param name="transaction">Optional transaction for the command.</param>
        /// <returns>The entity with the corresponding id.</returns>
        public static TEntity Get <TEntity>(this IDbConnection connection, object[] ids, IDbTransaction?transaction = null) where TEntity : class
        {
            if (ids.Length == 1)
            {
                return(Get <TEntity>(connection, ids[0], transaction));
            }

            var sql = BuildGetByIds(connection, typeof(TEntity), ids, out var parameters);

            LogQuery <TEntity>(sql);
            return(connection.QueryFirstOrDefault <TEntity>(sql, parameters, transaction));
        }
Ejemplo n.º 17
0
 /// <summary>
 /// 创建事务
 /// </summary>
 public void BeginTransaction()
 {
     _dbTransaction = CreateConnection().BeginTransaction();
 }
Ejemplo n.º 18
0
 /// <inheritdoc />
 public virtual Task <bool> BulkUpdateAsync(IEnumerable <TEntity> instances, IDbTransaction?transaction)
 {
     return(BulkUpdateAsync(instances, transaction, CancellationToken.None));
 }
Ejemplo n.º 19
0
    protected Task <T> ExecuteScalarAsync <T>(string sql, object?param = default, IDbTransaction?transaction = default, int?commandTimeout = default, CommandType?commandType = default)
    {
        Guard.Argument(sql).NotNull().NotEmpty().NotWhiteSpace();

        return(SafeExecuteAsync(() => _connection.ExecuteScalarAsync <T>(sql, param, transaction, commandTimeout, commandType)));
    }
Ejemplo n.º 20
0
        /// <inheritdoc />
        public virtual async Task <bool> BulkUpdateAsync(IEnumerable <TEntity> instances, IDbTransaction?transaction, CancellationToken cancellationToken)
        {
            if (SqlGenerator.Provider == SqlProvider.MSSQL)
            {
                int count          = 0;
                int totalInstances = instances.Count();

                var properties = SqlGenerator.SqlProperties.ToList();

                int exceededTimes = (int)Math.Ceiling(totalInstances * properties.Count / 2099d);
                if (exceededTimes > 1)
                {
                    int maxAllowedInstancesPerBatch = totalInstances / exceededTimes;

                    for (int i = 0; i <= exceededTimes; i++)
                    {
                        var skips = i * maxAllowedInstancesPerBatch;

                        if (skips >= totalInstances)
                        {
                            break;
                        }

                        var items            = instances.Skip(skips).Take(maxAllowedInstancesPerBatch);
                        var msSqlQueryResult = SqlGenerator.GetBulkUpdate(items);
                        count += await Connection.ExecuteAsync(new CommandDefinition(msSqlQueryResult.GetSql(), msSqlQueryResult.Param, transaction,
                                                                                     cancellationToken : cancellationToken));
                    }

                    return(count > 0);
                }
            }

            var queryResult = SqlGenerator.GetBulkUpdate(instances);
            var result      = await Connection.ExecuteAsync(new CommandDefinition(queryResult.GetSql(), queryResult.Param, transaction, cancellationToken : cancellationToken)) > 0;

            return(result);
        }
Ejemplo n.º 21
0
 public SqlBulkCopy CreateBulkCopy(IDbConnection connection, SqlBulkCopyOptions options, IDbTransaction?transaction)
 => _createBulkCopy(connection, options, transaction);
Ejemplo n.º 22
0
        /// <summary>
        /// Selects all the entities matching the specified predicate.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="connection">The connection to the database. This can either be open or closed.</param>
        /// <param name="predicate">A predicate to filter the results.</param>
        /// <param name="transaction">Optional transaction for the command.</param>
        /// <param name="buffered">
        /// A value indicating whether the result of the query should be executed directly,
        /// or when the query is materialized (using <c>ToList()</c> for example).
        /// </param>
        /// <returns>
        /// A collection of entities of type <typeparamref name="TEntity"/> matching the specified
        /// <paramref name="predicate"/>.
        /// </returns>
        public static IEnumerable <TEntity> Select <TEntity>(this IDbConnection connection, Expression <Func <TEntity, bool> > predicate, IDbTransaction?transaction = null, bool buffered = true)
        {
            var sql = BuildSelectSql(connection, predicate, out var parameters);

            LogQuery <TEntity>(sql);
            return(connection.Query <TEntity>(sql, parameters, transaction, buffered));
        }
Ejemplo n.º 23
0
        /// <summary>
        /// Returns the number of all entities.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="connection">The connection to the database. This can either be open or closed.</param>
        /// <param name="transaction">Optional transaction for the command.</param>
        /// <returns>The number of entities matching the specified predicate.</returns>
        public static Task <long> CountAsync <TEntity>(this IDbConnection connection, IDbTransaction?transaction = null)
        {
            var sql = BuildCountAllSql(GetSqlBuilder(connection), typeof(TEntity));

            LogQuery <TEntity>(sql);
            return(connection.ExecuteScalarAsync <long>(sql, transaction));
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Selects all the entities matching the specified predicate.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="connection">The connection to the database. This can either be open or closed.</param>
        /// <param name="predicate">A predicate to filter the results.</param>
        /// <param name="transaction">Optional transaction for the command.</param>
        /// <param name="cancellationToken">Optional cancellation token for the command.</param>
        /// <returns>
        /// A collection of entities of type <typeparamref name="TEntity"/> matching the specified
        /// <paramref name="predicate"/>.
        /// </returns>
        public static Task <IEnumerable <TEntity> > SelectAsync <TEntity>(this IDbConnection connection, Expression <Func <TEntity, bool> > predicate, IDbTransaction?transaction = null, CancellationToken cancellationToken = default)
        {
            var sql = BuildSelectSql(connection, predicate, out var parameters);

            LogQuery <TEntity>(sql);
            return(connection.QueryAsync <TEntity>(new CommandDefinition(sql, parameters, transaction: transaction, cancellationToken: cancellationToken)));
        }
Ejemplo n.º 25
0
 /// <summary>
 /// Retrieves the entity of type <typeparamref name="TReturn"/> with the specified id
 /// joined with the types specified as type parameters.
 /// </summary>
 /// <typeparam name="T1">The first type parameter. This is the source entity.</typeparam>
 /// <typeparam name="T2">The second type parameter.</typeparam>
 /// <typeparam name="T3">The third type parameter.</typeparam>
 /// <typeparam name="T4">The fourth type parameter.</typeparam>
 /// <typeparam name="T5">The fifth type parameter.</typeparam>
 /// <typeparam name="T6">The sixth type parameter.</typeparam>
 /// <typeparam name="T7">The seventh type parameter.</typeparam>
 /// <typeparam name="TReturn">The return type parameter.</typeparam>
 /// <param name="connection">The connection to the database. This can either be open or closed.</param>
 /// <param name="id">The id of the entity in the database.</param>
 /// <param name="map">The mapping to perform on the entities in the result set.</param>
 /// <param name="transaction">Optional transaction for the command.</param>
 /// <returns>The entity with the corresponding id joined with the specified types.</returns>
 public static TReturn?Get <T1, T2, T3, T4, T5, T6, T7, TReturn>(this IDbConnection connection, object id, Func <T1, T2, T3, T4, T5, T6, T7, TReturn> map, IDbTransaction?transaction = null) where TReturn : class
 => MultiMap <T1, T2, T3, T4, T5, T6, T7, TReturn>(connection, map, id, transaction).FirstOrDefault();
Ejemplo n.º 26
0
        /// <summary>
        /// Selects the first entity matching the specified predicate, or a default value if no entity matched.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="connection">The connection to the database. This can either be open or closed.</param>
        /// <param name="predicate">A predicate to filter the results.</param>
        /// <param name="transaction">Optional transaction for the command.</param>
        /// <returns>
        /// A instance of type <typeparamref name="TEntity"/> matching the specified
        /// <paramref name="predicate"/>.
        /// </returns>
        public static TEntity?FirstOrDefault <TEntity>(this IDbConnection connection, Expression <Func <TEntity, bool> > predicate, IDbTransaction?transaction = null)
            where TEntity : class
        {
            var sql = BuildSelectSql(connection, predicate, out var parameters);

            LogQuery <TEntity>(sql);
            return(connection.QueryFirstOrDefault <TEntity>(sql, parameters, transaction));
        }
Ejemplo n.º 27
0
 /// <summary>
 /// Retrieves the entity of type <typeparamref name="TReturn"/> with the specified id
 /// joined with the types specified as type parameters.
 /// </summary>
 /// <typeparam name="T1">The first type parameter. This is the source entity.</typeparam>
 /// <typeparam name="T2">The second type parameter.</typeparam>
 /// <typeparam name="TReturn">The return type parameter.</typeparam>
 /// <param name="connection">The connection to the database. This can either be open or closed.</param>
 /// <param name="id">The id of the entity in the database.</param>
 /// <param name="map">The mapping to perform on the entities in the result set.</param>
 /// <param name="transaction">Optional transaction for the command.</param>
 /// <param name="cancellationToken">Optional cancellation token for the command.</param>
 /// <returns>The entity with the corresponding id joined with the specified types.</returns>
 public static async Task <TReturn?> GetAsync <T1, T2, TReturn>(this IDbConnection connection, object id, Func <T1, T2, TReturn> map, IDbTransaction?transaction = null, CancellationToken cancellationToken = default) where TReturn : class
 => (await MultiMapAsync <T1, T2, DontMap, DontMap, DontMap, DontMap, DontMap, TReturn>(connection, map, id, transaction, cancellationToken : cancellationToken)).FirstOrDefault();
Ejemplo n.º 28
0
        /// <summary>
        /// Selects the first entity matching the specified predicate, or a default value if no entity matched.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="connection">The connection to the database. This can either be open or closed.</param>
        /// <param name="predicate">A predicate to filter the results.</param>
        /// <param name="transaction">Optional transaction for the command.</param>
        /// <param name="cancellationToken">Optional cancellation token for the command.</param>
        /// <returns>
        /// A instance of type <typeparamref name="TEntity"/> matching the specified
        /// <paramref name="predicate"/>.
        /// </returns>
        public static async Task <TEntity?> FirstOrDefaultAsync <TEntity>(this IDbConnection connection, Expression <Func <TEntity, bool> > predicate, IDbTransaction?transaction = null, CancellationToken cancellationToken = default)
            where TEntity : class
        {
            var sql = BuildSelectSql(connection, predicate, out var parameters);

            LogQuery <TEntity>(sql);
            return(await connection.QueryFirstOrDefaultAsync <TEntity>(new CommandDefinition(sql, parameters, transaction, cancellationToken : cancellationToken)));
        }
Ejemplo n.º 29
0
 /// <summary>
 /// Retrieves all the entities of type <typeparamref name="TReturn"/>
 /// joined with the types specified as type parameters.
 /// </summary>
 /// <typeparam name="T1">The first type parameter. This is the source entity.</typeparam>
 /// <typeparam name="T2">The second type parameter.</typeparam>
 /// <typeparam name="T3">The third type parameter.</typeparam>
 /// <typeparam name="T4">The fourth type parameter.</typeparam>
 /// <typeparam name="T5">The fifth type parameter.</typeparam>
 /// <typeparam name="T6">The sixth type parameter.</typeparam>
 /// <typeparam name="T7">The seventh type parameter.</typeparam>
 /// <typeparam name="TReturn">The return type parameter.</typeparam>
 /// <param name="connection">The connection to the database. This can either be open or closed.</param>
 /// <param name="map">The mapping to perform on the entities in the result set.</param>
 /// <param name="transaction">Optional transaction for the command.</param>
 /// <param name="buffered">
 /// A value indicating whether the result of the query should be executed directly,
 /// or when the query is materialized (using <c>ToList()</c> for example).
 /// </param>
 /// <param name="cancellationToken">Optional cancellation token for the command.</param>
 /// <returns>
 /// A collection of entities of type <typeparamref name="TReturn"/>
 /// joined with the specified type types.
 /// </returns>
 public static Task <IEnumerable <TReturn> > GetAllAsync <T1, T2, T3, T4, T5, T6, T7, TReturn>(
     this IDbConnection connection,
     Func <T1, T2, T3, T4, T5, T6, T7, TReturn> map,
     IDbTransaction?transaction = null,
     bool buffered = true,
     CancellationToken cancellationToken = default) => MultiMapAsync <T1, T2, T3, T4, T5, T6, T7, TReturn>(connection, map, id: null, transaction, buffered, cancellationToken: cancellationToken);
Ejemplo n.º 30
0
 /// <summary>
 ///     Execute Join query
 /// </summary>
 protected virtual Task <IEnumerable <TEntity> > ExecuteJoinQueryAsync <TChild1, TChild2, TChild3, TChild4, TChild5, TChild6>(
     SqlQuery sqlQuery,
     IDbTransaction?transaction,
     params Expression <Func <TEntity, object> >[] includes)
 {
     return(ExecuteJoinQueryAsync <TChild1, TChild2, TChild3, TChild4, TChild5, TChild6>(sqlQuery, transaction, default, includes));