Пример #1
0
        public string GetQueryText(System.Linq.Expressions.Expression query)
        {
            var builder = new FqlQueryBuilder(query);
            var fql     = builder.BuildQuery();

            return(fql);
        }
Пример #2
0
        public System.Data.Linq.IExecuteResult Execute(System.Linq.Expressions.Expression query, Type tableRowType)
        {
            var builder    = new FqlQueryBuilder(query);
            var fql        = builder.BuildQuery();
            var resultType = query.Type;            //

            if (resultType.IsGenericType)
            {
                resultType = resultType.GetGenericArguments()[0];
            }
            if (fql.StartsWith("from "))
            {
                fql = "select * " + fql;
            }


            //QueryBuilder2
            //var builder = new FqlQueryBuilder(query);
            //var fql = builder.BuildQuery();
            //var select = builder.Select;
            //var resultType = query.Type.GetGenericArguments()[0];
            fql = FqlDataContext.FixQuery(fql);
            var response = ExecuteFqlQuery(fql);

            return(new FqlExecuteResult(response, resultType, tableRowType, builder.QueryOptions));
        }
Пример #3
0
        public System.Data.Linq.IExecuteResult Execute(System.Linq.Expressions.Expression query, Type tableRowType)
        {
            var builder = new FqlQueryBuilder(query);
            var fql = builder.BuildQuery();
            var resultType = query.Type;//
            if (resultType.IsGenericType)
                    resultType = resultType.GetGenericArguments()[0];
            if (fql.StartsWith("from "))
                fql = "select * " + fql;

            //QueryBuilder2
            //var builder = new FqlQueryBuilder(query);
            //var fql = builder.BuildQuery();
            //var select = builder.Select;
            //var resultType = query.Type.GetGenericArguments()[0];
            fql = FqlDataContext.FixQuery(fql);
            var response = ExecuteFqlQuery(fql);
            return new FqlExecuteResult(response, resultType, tableRowType, builder.QueryOptions);
        }
Пример #4
0
        private void _Build(MethodCallExpression exp)
        {
            if (exp.Method.Name == "Where")
            {
                bool nestedWhere = (IsMethodCall(exp.Arguments[0], "Where"));

                if (InSelect == 0 && !nestedWhere) //No projection has been specified, and we're in a Where clause (hence select * is assumed)
                {
                    Write("from");
                }
                Build(exp.Arguments[0]);

                if (nestedWhere)
                {
                    //Nested where
                    Write("AND");
                }
                else
                {
                    Write("where");
                }
                Build(exp.Arguments[1]);
            }
            else if (exp.Method.Name == "Select")
            {
                InSelect++;
                if (!HasSelect)
                {
                    HasSelect = true;
                    QueryOptions.Select = ((UnaryExpression)exp.Arguments[1]).Operand;
                }
                Write("select");
                Build(exp.Arguments[1]);
                Write("from");
                Build(exp.Arguments[0]);
                InSelect--;
            }
            else if (exp.Method.Name == "Contains")
            {
                Build(exp.Arguments[1]);
                Write("in");
                string innerQuery = new FqlQueryBuilder(exp.Arguments[0]).BuildQuery().Trim();
                if (!innerQuery.StartsWith("("))
                    innerQuery = "(" + innerQuery + ")";
                Write(innerQuery);
            }
            else if (exp.Method.Name == "Take")
            {
                bool hasSkip = ((exp.Arguments[0] is MethodCallExpression) && (((((MethodCallExpression)(exp.Arguments[0])).Method)).Name == "Skip"));
                if (hasSkip)
                {
                    Build((exp.Arguments[0] as MethodCallExpression).Arguments[0]);
                }
                else
                {
                    Build(exp.Arguments[0]); // no skip specified
                }
                Write("limit ");
                Build(exp.Arguments[1]); //Take and Skip are used together, and in FQL / MySQL syntax
                if (hasSkip)
                {
                    Write("offset ");
                    Build((exp.Arguments[0] as MethodCallExpression).Arguments[1]);
                }
            }
            else if (exp.Method.Name == "Skip")
            {
                throw new Exception("Cannot use Skip without Take");
                //Build(exp.Arguments[0]);
                //Write("offset ");
                //Build(exp.Arguments[1]);
            }
            else if (exp.Method.Name == "First" || exp.Method.Name == "FirstOrDefault")
            {
                Build(exp.Arguments[0]);
                Write("limit 1");
            }
            else if (exp.Method.Name == "OrderBy")
            {
                Build(exp.Arguments[0]);
                Write("order by ");
                Build(exp.Arguments[1]);
            }
            else if (exp.Method.Name == "OrderByDescending")
            {
                Build(exp.Arguments[0]);
                Write("order by ");
                Build(exp.Arguments[1]);
                Write("DESC");
            }
            else if (exp.Method.Name == "ThenBy")
            {
                throw new NotImplementedException("ThenBy is not supported by FQL.  Use a calculated key in the OrderBy clause");
                //Build(exp.Arguments[0]);
                //var desc = "";
                //if (sb.ToString().EndsWith("DESC "))
                //{
                //  sb.Length -= 6;
                //  desc = "DESC";
                //}
                //Write(",");
                //Build(exp.Arguments[1]);
                //sb.Append(desc);
            }
            else if (exp.Method.Name == "Count")
            {
                Build(exp.Arguments[0]);
                QueryOptions.IsCount = true;
                QueryOptions.Select = null; //No need to select when performing Count()
                if (sb.ToString().StartsWith("from"))
                {
                    sb.Insert(0, "select '' ");
                }
                else if (sb.ToString().StartsWith("select"))
                {
                    var s = sb.ToString();
                    var idx = s.IndexOf("from");
                    sb.Length = 0;
                    sb.Append("select '' ");
                    sb.Append(s.Substring(idx));
                }
            }
            else if (exp.Method.Name == "Substring")
            {
                Write("substr(");
                Build(exp.Object);
                Write(", ");
                Build(exp.Arguments[0]);
                Write(", ");
                if (exp.Arguments.Count == 2)
                {
                    Build(exp.Arguments[1]);
                }
                else
                {
                    Build(999999);
                }
                Write(")");
            }
            else if (exp.Method.Name == "IndexOf")
            {
                Write("strpos(");
                Build(exp.Object);
                Write(", ");
                Build(exp.Arguments[0]);
                Write(")");
            }
            else
                throw new Exception();
        }
Пример #5
0
        private void _Build(MethodCallExpression exp)
        {
            if (exp.Method.Name == "Where")
            {
                bool nestedWhere = (IsMethodCall(exp.Arguments[0], "Where"));

                if (InSelect == 0 && !nestedWhere)                 //No projection has been specified, and we're in a Where clause (hence select * is assumed)
                {
                    Write("from");
                }
                Build(exp.Arguments[0]);

                if (nestedWhere)
                {
                    //Nested where
                    Write("AND");
                }
                else
                {
                    Write("where");
                }
                Build(exp.Arguments[1]);
            }
            else if (exp.Method.Name == "Select")
            {
                InSelect++;
                if (!HasSelect)
                {
                    HasSelect           = true;
                    QueryOptions.Select = ((UnaryExpression)exp.Arguments[1]).Operand;
                }
                Write("select");
                Build(exp.Arguments[1]);
                Write("from");
                Build(exp.Arguments[0]);
                InSelect--;
            }
            else if (exp.Method.Name == "Contains")
            {
                Build(exp.Arguments[1]);
                Write("in");
                string innerQuery = new FqlQueryBuilder(exp.Arguments[0]).BuildQuery().Trim();
                if (!innerQuery.StartsWith("("))
                {
                    innerQuery = "(" + innerQuery + ")";
                }
                Write(innerQuery);
            }
            else if (exp.Method.Name == "Take")
            {
                bool hasSkip = ((exp.Arguments[0] is MethodCallExpression) && (((((MethodCallExpression)(exp.Arguments[0])).Method)).Name == "Skip"));
                if (hasSkip)
                {
                    Build((exp.Arguments[0] as MethodCallExpression).Arguments[0]);
                }
                else
                {
                    Build(exp.Arguments[0]);                     // no skip specified
                }
                Write("limit ");
                Build(exp.Arguments[1]);                 //Take and Skip are used together, and in FQL / MySQL syntax
                if (hasSkip)
                {
                    Write("offset ");
                    Build((exp.Arguments[0] as MethodCallExpression).Arguments[1]);
                }
            }
            else if (exp.Method.Name == "Skip")
            {
                throw new Exception("Cannot use Skip without Take");
                //Build(exp.Arguments[0]);
                //Write("offset ");
                //Build(exp.Arguments[1]);
            }
            else if (exp.Method.Name == "First" || exp.Method.Name == "FirstOrDefault")
            {
                Build(exp.Arguments[0]);
                Write("limit 1");
            }
            else if (exp.Method.Name == "OrderBy")
            {
                Build(exp.Arguments[0]);
                Write("order by ");
                Build(exp.Arguments[1]);
            }
            else if (exp.Method.Name == "OrderByDescending")
            {
                Build(exp.Arguments[0]);
                Write("order by ");
                Build(exp.Arguments[1]);
                Write("DESC");
            }
            else if (exp.Method.Name == "ThenBy")
            {
                throw new NotImplementedException("ThenBy is not supported by FQL.  Use a calculated key in the OrderBy clause");
                //Build(exp.Arguments[0]);
                //var desc = "";
                //if (sb.ToString().EndsWith("DESC "))
                //{
                //  sb.Length -= 6;
                //  desc = "DESC";
                //}
                //Write(",");
                //Build(exp.Arguments[1]);
                //sb.Append(desc);
            }
            else if (exp.Method.Name == "Count")
            {
                Build(exp.Arguments[0]);
                QueryOptions.IsCount = true;
                QueryOptions.Select  = null;                //No need to select when performing Count()
                if (sb.ToString().StartsWith("from"))
                {
                    sb.Insert(0, "select '' ");
                }
                else if (sb.ToString().StartsWith("select"))
                {
                    var s   = sb.ToString();
                    var idx = s.IndexOf("from");
                    sb.Length = 0;
                    sb.Append("select '' ");
                    sb.Append(s.Substring(idx));
                }
            }
            else if (exp.Method.Name == "Substring")
            {
                Write("substr(");
                Build(exp.Object);
                Write(", ");
                Build(exp.Arguments[0]);
                Write(", ");
                if (exp.Arguments.Count == 2)
                {
                    Build(exp.Arguments[1]);
                }
                else
                {
                    Build(999999);
                }
                Write(")");
            }
            else if (exp.Method.Name == "IndexOf")
            {
                Write("strpos(");
                Build(exp.Object);
                Write(", ");
                Build(exp.Arguments[0]);
                Write(")");
            }
            else
            {
                throw new Exception();
            }
        }
Пример #6
0
 public string GetQueryText(System.Linq.Expressions.Expression query)
 {
     var builder = new FqlQueryBuilder(query);
     var fql = builder.BuildQuery();
     return fql;
 }