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

            return(TransientDapperExtentions.QueryWithRetry(() => Connection.Query <TEntity>(queryResult.GetSql(), queryResult.Param, transaction)));
        }
        /// <inheritdoc />
        public virtual bool UpSert(TEntity instance, IDbTransaction transaction = null)
        {
            var  sqlQuery = SqlGenerator.GetUpSert(instance);
            bool modified;

            if (SqlGenerator.IsIdentity)
            {
                var newId = (TransientDapperExtentions.QueryWithRetry(() => Connection.Query <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 = TransientDapperExtentions.ExecuteWithRetry(() => Connection.Execute(sqlQuery.GetSql(), instance, transaction)) > 0;
            }

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

            return(TransientDapperExtentions.QueryWithRetry(() => Connection.Query <TEntity>(queryResult.GetSql(), queryResult.Param, transaction)));
        }
Beispiel #4
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)
        {
            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:
                //Connection.Query<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);

                TransientDapperExtentions.QueryWithRetry(() => Connection.Query <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:
                TransientDapperExtentions.QueryWithRetry(() => Connection.Query <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:
                TransientDapperExtentions.QueryWithRetry(() => Connection.Query <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:
                TransientDapperExtentions.QueryWithRetry(() => 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, childPropertyNames, type, entity, child1, child2, child3, child4),
                                                                                                                                       sqlQuery.Param, transaction, buffered, spiltOn));
                break;

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

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

            default:
                throw new NotSupportedException();
            }

            return(lookup.Values);
        }