/// <inheritdoc />
        public Task <IEnumerable <TEntity> > FindAllBetweenAsync(object from, object to, Expression <Func <TEntity, object> > btwField, Expression <Func <TEntity, bool> > predicate,
                                                                 IDbTransaction transaction = null)
        {
            var queryResult = SqlGenerator.GetSelectBetween(from, to, btwField, predicate);

            return(TransientDapperExtentions.QueryWithRetryAsync(() => Connection.QueryAsync <TEntity>(queryResult.GetSql(), queryResult.Param, transaction)));
        }
        /// <inheritdoc />
        public virtual async Task <bool> UpSertAsync(TEntity instance, IDbTransaction transaction = null)
        {
            var  sqlQuery = SqlGenerator.GetUpSert(instance);
            bool modified;

            if (SqlGenerator.IsIdentity)
            {
                var newId = (await TransientDapperExtentions.QueryWithRetryAsync(() => Connection.QueryAsync <long>(sqlQuery.GetSql(), sqlQuery.Param, transaction))).FirstOrDefault();
                modified = newId > 0;

                if (modified)
                {
                    var newParsedId = Convert.ChangeType(newId, SqlGenerator.IdentitySqlProperty.PropertyInfo.PropertyType);
                    SqlGenerator.IdentitySqlProperty.PropertyInfo.SetValue(instance, newParsedId);
                }
            }
            else
            {
                modified = (await TransientDapperExtentions.ExecuteWithRetryAsync(() => Connection.ExecuteAsync(sqlQuery.GetSql(), instance, transaction))) > 0;
            }

            return(modified);
        }
Example #3
0
        /// <inheritdoc />
        public virtual Task <IEnumerable <TEntity> > FindAllAsync(Expression <Func <TEntity, bool> > predicate, IDbTransaction transaction = null)
        {
            var queryResult = SqlGenerator.GetSelectAll(predicate);

            return(TransientDapperExtentions.QueryWithRetryAsync(() => Connection.QueryAsync <TEntity>(queryResult.GetSql(), queryResult.Param, transaction)));
        }
Example #4
0
        /// <summary>
        ///     Execute Join query
        /// </summary>
        protected virtual async Task <IEnumerable <TEntity> > ExecuteJoinQueryAsync <TChild1, TChild2, TChild3, TChild4, TChild5, TChild6>(
            SqlQuery sqlQuery,
            IDbTransaction transaction,
            params Expression <Func <TEntity, object> >[] includes)
        {
            var type = typeof(TEntity);

            var childPropertyNames = includes.Select(ExpressionHelper.GetPropertyName).ToList();
            var childProperties    = childPropertyNames.Select(p => type.GetProperty(p)).ToList();

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

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

            foreach (var property in childProperties)
            {
                var childType  = property.PropertyType.IsGenericType() ? property.PropertyType.GenericTypeArguments[0] : property.PropertyType;
                var properties = childType.FindClassProperties().Where(ExpressionHelper.GetPrimitivePropertiesPredicate());
                var keyprop    = properties.Where(p => p.GetCustomAttributes <KeyAttribute>().Any());
                childKeyProperties.Add(keyprop.FirstOrDefault()); //just takes the first key in case of compound keys
            }

            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:
                await TransientDapperExtentions.QueryWithRetryAsync(() => Connection.QueryAsync <TEntity, TChild1, TEntity>(sqlQuery.GetSql(), (entity, child1) =>
                                                                                                                            EntityJoinMapping <TChild1, TChild2, TChild3, TChild4, TChild5, TChild6>(lookup, keyProperties, childKeyProperties, childProperties, childPropertyNames, type, entity, child1),
                                                                                                                            sqlQuery.Param, transaction, buffered, spiltOn));

                break;

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

                break;

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

                break;

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

                break;

            case 5:
                await TransientDapperExtentions.QueryWithRetryAsync(() => Connection.QueryAsync <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, childPropertyNames, type, entity, child1, child2, child3, child4, child5),
                                                                                                                                                                sqlQuery.Param, transaction, buffered, spiltOn));

                break;

            case 6:
                await TransientDapperExtentions.QueryWithRetryAsync(() => Connection.QueryAsync <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, childPropertyNames, type, entity, child1, child2, child3, child4, child5, child6),
                                                                                                                                                                         sqlQuery.Param, transaction, buffered, spiltOn));

                break;

            default:
                throw new NotSupportedException();
            }

            return(lookup.Values);
        }