public IEnumerable <WhereClause> Get(
            Models.Criteria criteria, string criteriaTableName, string criteriaTableAlias)
        {
            var type          = criteria.GetType();
            var propertyInfos = type.GetProperties()
                                .Where(pi => pi.HasAttribute <WhereAttribute>());
            var whereClauses = new List <WhereClause>();

            foreach (var propertyInfo in propertyInfos)
            {
                object value;
                var    whereAttributes = propertyInfo.GetCustomAttributes <WhereAttribute>();
                if ((value = propertyInfo.GetValue(criteria, null)) == null)
                {
                    continue;
                }
                if (propertyInfo.PropertyType == typeof(bool) && !(bool)value)
                {
                    continue;
                }
                foreach (var whereAttribute in whereAttributes)
                {
                    string tableName;

                    if (!String.IsNullOrEmpty(whereAttribute.TableAlias))
                    {
                        tableName = whereAttribute.TableAlias;
                    }
                    else if (!String.IsNullOrEmpty(whereAttribute.TableName))
                    {
                        tableName = whereAttribute.TableName;
                    }
                    else if (!String.IsNullOrEmpty(criteriaTableAlias))
                    {
                        tableName = criteriaTableAlias;
                    }
                    else
                    {
                        tableName = criteriaTableName;
                    }

                    string paramName = $"@{NormalizeTableName(tableName)}{propertyInfo.Name}";
                    string str       = GeWheretSting(whereAttribute, propertyInfo, tableName, paramName, ref value);
                    whereClauses.Add(new WhereClause
                    {
                        ParameterName  = paramName,
                        ParameterValue = value,
                        Sql            = str,
                        WithoutValue   = _whereAttributeManager.IsWithoutValue(whereAttribute.WhereType)
                    });
                }
            }
            return(whereClauses);
        }
Esempio n. 2
0
        public IEnumerable <SelectClause> Get(Models.Criteria criteria, string tableName, string criteriaTableAlias)
        {
            var res = new List <SelectClause>();

            if (criteria.SelectClause != null)
            {
                res.Add(new SelectClause
                {
                    IsExpression = criteria.SelectClause.IsExpression,
                    Select       = criteria.SelectClause.Select,
                    Table        = !string.IsNullOrWhiteSpace(criteria.SelectClause.Table)
                            ? criteria.SelectClause.Table
                            : tableName,
                    Alias = !string.IsNullOrWhiteSpace(criteria.SelectClause.Alias)
                        ? criteria.SelectClause.Alias
                        : criteriaTableAlias
                });
            }
            if (criteria.QueryType == QueryType.Sum)
            {
                return(res);
            }
            var type  = criteria.GetType();
            var props = type.GetProperties().Where(pi => pi.HasAttribute <AddSelectAttribute>());

            foreach (var propertyInfo in props)
            {
                var propIsBool     = propertyInfo.PropertyType == typeof(bool);
                var propIsNullable = propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>);
                var propIsString   = propertyInfo.PropertyType == typeof(string);
                if (!propIsBool && !propIsString && !propIsNullable)
                {
                    throw new NotImplementedException("Select implemented to only bool or string or nullable properties");
                }
                var addSelectAttribute = propertyInfo.GetCustomAttribute <AddSelectAttribute>();
                if (propIsBool)
                {
                    if (!(bool)propertyInfo.GetValue(criteria, null))
                    {
                        continue;
                    }
                    foreach (var tableSelectColumn in addSelectAttribute.TableSelectColumns)
                    {
                        res.AddRange(tableSelectColumn.Value);
                    }
                }
                else if (propIsNullable)
                {
                    if (propertyInfo.GetValue(criteria, null) == null)
                    {
                        continue;
                    }
                    foreach (var tableSelectColumn in addSelectAttribute.TableSelectColumns)
                    {
                        res.AddRange(tableSelectColumn.Value);
                    }
                }
                else if (propIsString)
                {
                    var str = (string)propertyInfo.GetValue(criteria, null);
                    if (string.IsNullOrWhiteSpace(str))
                    {
                        continue;
                    }
                    var clauses = addSelectAttribute.SelectParser.Parse(str);
                    foreach (var tableSelectColumn in clauses)
                    {
                        res.AddRange(tableSelectColumn.Value);
                    }
                }
            }
            return(res);
        }
Esempio n. 3
0
 public IQueryableTypeFilter GetTypeFilter(Type type, Models.Criteria criteria) =>
 _filterMap.GetFilter(type, criteria.Filter);
Esempio n. 4
0
        public IEnumerable <JoinClause> Get(Models.Criteria criteria, string criteriaTableName, string criteriaTableAlias)
        {
            var type        = criteria.GetType();
            var props       = type.GetProperties().Where(pi => pi.HasAttribute <JoinAttribute>());
            var joinClauses = new List <JoinClause>();

            foreach (var propertyInfo in props)
            {
                var isNullable = propertyInfo.PropertyType == typeof(string) ||
                                 propertyInfo.PropertyType.IsClass ||
                                 propertyInfo.PropertyType.IsInterface ||
                                 propertyInfo.PropertyType.IsGenericType &&
                                 propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>);
                var isBool = propertyInfo.PropertyType == typeof(bool);
                if (!isBool && !isNullable)
                {
                    throw new NotImplementedException("Join implemented to only bool or nullable properties");
                }
                var joinAttributes = propertyInfo.GetCustomAttributes <JoinAttribute>();
                var value          = propertyInfo.GetValue(criteria, null);
                foreach (var joinAttribute in joinAttributes)
                {
                    var joiner = _joinClauseCreatorFactory.Get(joinAttribute.GetType());
                    if ((isBool && !(bool)value) || (isNullable && value == null))
                    {
                        var clause = joiner.CreateNotJoin(joinAttribute);
                        if (joinClauses.All(x => x.Splitter != clause.Splitter))
                        {
                            joinClauses.Add(clause);
                        }
                        continue;
                    }
                    if (!string.IsNullOrWhiteSpace(joinAttribute.Including))
                    {
                        var includedProp = props.SingleOrDefault(x => x.Name == joinAttribute.Including);
                        if (includedProp == null)
                        {
                            throw new Exception($"Property {joinAttribute.Including} not found");
                        }
                        var includedPropIsNullable = includedProp.PropertyType == typeof(string) ||
                                                     includedProp.PropertyType.IsGenericType &&
                                                     includedProp.PropertyType.GetGenericTypeDefinition() ==
                                                     typeof(Nullable <>);
                        var includedPropIsBool = includedProp.PropertyType == typeof(bool);

                        if (!includedPropIsNullable && !includedPropIsBool)
                        {
                            throw new NotImplementedException(
                                      "Including implemented to only bool or nullable properties");
                        }
                        var includedPropValue = includedProp.GetValue(criteria, null);
                        if ((includedPropIsBool && (bool)includedPropValue) ||
                            (includedPropIsNullable && includedPropValue != null))
                        {
                            continue;
                        }
                    }

                    joinAttribute.CurrentTableAlias = String.IsNullOrWhiteSpace(joinAttribute.CurrentTableAlias)
                        ? criteriaTableAlias
                        : joinAttribute.CurrentTableAlias;

                    joinAttribute.CurrentTable = string.IsNullOrWhiteSpace(joinAttribute.CurrentTable)
                                                     ? criteriaTableName
                                                     : joinAttribute.CurrentTable;
                    joinAttribute.CurrentTableField = string.IsNullOrWhiteSpace(joinAttribute.CurrentTableField)
                                                          ? propertyInfo.Name
                                                          : joinAttribute.CurrentTableField;
                    joinClauses.Add(joiner.Create(joinAttribute));
                }
            }
            return(joinClauses);
        }