예제 #1
0
        private string GetWhereClause(QueryBuilderConfig config)
        {
            var entityName = config.QueryBuilderSettings.FirstOrDefault(x => x.Name == "EntityName").Value;
            var root       = config.Query.GetRoot();

            if (root.PredicateNode)
            {
                return(GetPredicateQuery(root.Predicate, entityName));
            }

            if (root.Left.PredicateNode && root.Right.PredicateNode)
            {
                return(GetLogicPredicateQuery(root, entityName));
            }

            var left  = TraverseTreeBuildingQuery(root.Left, entityName);
            var right = TraverseTreeBuildingQuery(root.Right, entityName);

            if (right.Count() == 1 && left.Count() == 1)
            {
                return($"{left.Pop()} {GetLogicOperator(root.Operator.Value)} {right.Pop()}");
            }

            throw new ApplicationException("Error in generating query");
        }
예제 #2
0
        public string GetQuery(QueryBuilderConfig config)
        {
            //first get the root of the tree
            var root = config.Query.GetRoot();

            //simple case is, its only one query without a logical operator
            if (root.PredicateNode)
            {
                var q = GetPredicateQuery(root.Predicate);
                return(q);
            }

            //second simple case both left and right nodes are predicates
            if (root.Left.PredicateNode && root.Right.PredicateNode)
            {
                return(GetLogicPredicateQuery(root));
            }
            //now we begin the fun part

            var left            = TraverseTreeBuildingQuery(root.Left);
            var right           = TraverseTreeBuildingQuery(root.Right);
            var logicQueryStart = GetQueryForLogic(root.Operator.Value);

            if (right.Count() == 1 && left.Count() == 1)
            {
                return($"{logicQueryStart}{left.Pop()},{right.Pop()}]}}}}");
            }

            throw new ApplicationException("Error in generating query");
        }
예제 #3
0
        public string GetQuery(QueryBuilderConfig config)
        {
            var tableName   = config.QueryBuilderSettings.FirstOrDefault(x => x.Name == "TableName");
            var whereClause = GetWhereClause(config);
            var sqlQuery    = $"SELECT * FROM {tableName.Value} WHERE {whereClause};";

            return(sqlQuery);
        }