Beispiel #1
0
        public IQueryBuilder GenerateJoinQuery(JoinDefinition inputJoin, IQueryBuilder conditions, int?skip = null, int?limit = null, MemberInfo orderingMember = null, OrderingType otype = OrderingType.Asc, IQueryBuilder rootConditions = null)
        {
            if (rootConditions == null)
            {
                rootConditions = new QbFmt("true");
            }
            if (inputJoin.Joins.Count < 1)
            {
                throw new BDadosException("This join needs 1 or more tables.");
            }

            List <Type>           tables     = (from a in inputJoin.Joins select a.ValueObject).ToList();
            List <String>         tableNames = (from a in inputJoin.Joins select a.ValueObject.Name).ToList();
            List <String>         prefixes   = (from a in inputJoin.Joins select a.Prefix).ToList();
            List <String>         aliases    = (from a in inputJoin.Joins select a.Alias).ToList();
            List <String>         onclauses  = (from a in inputJoin.Joins select a.Args).ToList();
            List <List <String> > columns    = (from a in inputJoin.Joins select a.Columns).ToList();
            List <JoinType>       joinTypes  = (from a in inputJoin.Joins select a.Type).ToList();

            QueryBuilder Query = new QbFmt("SELECT sub.*\n");

            Query.Append($"\t FROM (SELECT\n");
            for (int i = 0; i < tables.Count; i++)
            {
                Query.Append($"\t\t-- Table {tableNames[i]}\n");
                var fields = ReflectionTool.FieldsAndPropertiesOf(
                    tables[i])
                             .Where((a) => a.GetCustomAttribute(typeof(FieldAttribute)) != null)
                             .ToArray();
                if (!columns[i].Contains("RID"))
                {
                    columns[i].Add("RID");
                }
                var nonexcl = columns[i];
                for (int j = 0; j < nonexcl.Count; j++)
                {
                    Query.Append($"\t\t{prefixes[i]}.{nonexcl[j]} AS {prefixes[i]}_{nonexcl[j]}");
                    if (true || j < nonexcl.Count - 1 || i < tables.Count - 1)
                    {
                        Query.Append(",");
                    }
                    Query.Append("\n");
                }
                Query.Append("\n");
            }

            Query.Append($"\t\t1 FROM (SELECT * FROM {tableNames[0]}");
            if (rootConditions != null)
            {
                Query.Append("WHERE ");
                Query.Append(rootConditions);
            }
            if (orderingMember != null)
            {
                Query.Append($"ORDER BY {orderingMember.Name} {otype.ToString().ToUpper()}");
            }
            if (limit != null)
            {
                Query.Append($"LIMIT");
                if (skip != null)
                {
                    Query.Append($"{skip},");
                }
                Query.Append($"{limit}");
            }
            Query.Append($"");
            Query.Append($") AS {prefixes[0]}\n");

            for (int i = 1; i < tables.Count; i++)
            {
                Query.Append($"\t\t{"LEFT"} JOIN {tableNames[i]} AS {prefixes[i]} ON {onclauses[i]}\n");
            }

            if (conditions != null && !conditions.IsEmpty)
            {
                Query.Append("\tWHERE");
                Query.Append(conditions);
            }
            if (orderingMember != null)
            {
                Query.Append($"ORDER BY {prefixes[0]}.{orderingMember.Name} {otype.ToString().ToUpper()}");
            }
            if (limit != null)
            {
                Query.Append($"LIMIT");
                if ((skip ?? 0) > 0)
                {
                    Query.Append($"{skip}, ");
                }
                Query.Append($"{limit}");
            }
            Query.Append(") AS sub\n");

            return(Query);
        }
Beispiel #2
0
 public static OrderByToken CreateDescending(string fieldName, OrderingType ordering)
 {
     return(new OrderByToken(fieldName, true, ordering));
 }
Beispiel #3
0
 private OrderByToken(string fieldName, bool descending, OrderingType ordering)
 {
     _fieldName  = fieldName;
     _descending = descending;
     _ordering   = ordering;
 }
Beispiel #4
0
        /// <inheritdoc />
        IDocumentQuery <T> IDocumentQueryBase <T, IDocumentQuery <T> > .AddOrder <TValue>(Expression <Func <T, TValue> > propertySelector, bool descending, OrderingType ordering)
        {
            var fieldName = GetMemberQueryPath(propertySelector.Body);

            if (descending)
            {
                OrderByDescending(fieldName, ordering);
            }
            else
            {
                OrderBy(fieldName, ordering);
            }

            return(this);
        }
Beispiel #5
0
        /// <inheritdoc />
        IDocumentQuery <T> IDocumentQueryBase <T, IDocumentQuery <T> > .AddOrder(string fieldName, bool descending, OrderingType ordering)
        {
            if (descending)
            {
                OrderByDescending(fieldName, ordering);
            }
            else
            {
                OrderBy(fieldName, ordering);
            }

            return(this);
        }
Beispiel #6
0
 /// <inheritdoc />
 IDocumentQuery <T> IDocumentQueryBase <T, IDocumentQuery <T> > .OrderByDescending(string field, OrderingType ordering)
 {
     OrderByDescending(field, ordering);
     return(this);
 }