Beispiel #1
0
 private SqlExpressionParser(int indentLevel, SqlExpressionParser outerStatement,
                            AggregateType aggregateType)
 {
     this.indentLevel = indentLevel;
     this.outerStatement = outerStatement;
     this.aggregateType = aggregateType;
 }
Beispiel #2
0
            public static JoinHandler GetJoinHandler(SqlExpressionParser outerStatement,
                                                     int indentLevel,
                                                     MethodCallExpression expression)
            {
                Debug.Assert(expression.Method.Name == "Join");

                Debug.Assert(expression.Arguments.Count == 5);

                return new JoinHandler(outerStatement, indentLevel, expression);
            }
Beispiel #3
0
            private JoinHandler(SqlExpressionParser outerStatement,
                                int indentLevel,
                                MethodCallExpression expression)
            {
                this.outerStatement = outerStatement;

                this.indentLevel = indentLevel;

                selector = GetLambdaExpression(expression.Arguments[4]);

                leftStatement = new SqlExpressionParser(indentLevel + 1, outerStatement);

                leftStatement.Translate(GetLeftSourceExpression(expression));

                rightStatement = new SqlExpressionParser(indentLevel + 1, outerStatement,
                                                         AggregateType.None);

                rightStatement.Translate(GetRightSourceExpression(expression));

                leftKeySelector = new LambdaExpressionHandler(indentLevel,
                                            (LambdaExpression)StripQuotes(expression.Arguments[2]));

                rightKeySelector = new LambdaExpressionHandler(indentLevel,
                                            (LambdaExpression)StripQuotes(expression.Arguments[3]));

                leftReturnType = leftStatement.selectHandler.ReturnType;

                rightReturnType = rightStatement.selectHandler.ReturnType;
            }
Beispiel #4
0
 private SqlExpressionParser(int indentLevel, SqlExpressionParser outerStatement)
     : this(indentLevel, outerStatement, AggregateType.None)
 {
 }
Beispiel #5
0
            private CrossJoinHandler(SqlExpressionParser outerStatement,
                                     int indentLevel,
                                     MethodCallExpression expression)
            {
                this.outerStatement = outerStatement;

                this.indentLevel = indentLevel;

                selector = GetLambdaExpression(expression.Arguments.Last());

                leftStatement = new SqlExpressionParser(indentLevel + 1, outerStatement);

                leftStatement.Translate(GetLeftSourceExpression(expression));

                rightStatement = new SqlExpressionParser(indentLevel + 1, outerStatement,
                                                         AggregateType.None);

                rightStatement.Translate(GetRightSourceExpression(expression));

                leftReturnType = leftStatement.selectHandler.ReturnType;

                rightReturnType = rightStatement.selectHandler.ReturnType;
            }
Beispiel #6
0
            public static CrossJoinHandler GetCrossJoinHandler(SqlExpressionParser outerStatement,
                                                               int indentLevel,
                                                               MethodCallExpression expression)
            {
                Debug.Assert(expression.Method.Name == "SelectMany");

                Debug.Assert(expression.Arguments.Count == 3);

                return new CrossJoinHandler(outerStatement, indentLevel, expression);
            }
Beispiel #7
0
        private static SqlExpressionParser GetSqlExpressionParser(Expression expression)
        {
            //var key = expression.ToString();

            //if (parserCache.ContainsKey(key))
            //{
            //    return parserCache[key];
            //}

            var sqlExpressionParser = new SqlExpressionParser();

            sqlExpressionParser.Translate(expression);

            //parserCache.TryAdd(key, sqlExpressionParser);

            //Debug.Print(sqlExpressionParser.GetSQLStatement());

            return sqlExpressionParser;
        }
Beispiel #8
0
 private string GetProjectionSql(int indentLevel, SqlExpressionParser project)
 {
     return Environment.NewLine +
         GetIndentation(indentLevel) +
         "(" +
         Environment.NewLine +
         project.GetSQLStatement() +
         GetIndentation(indentLevel) +
         ")" +
         Environment.NewLine;
 }
Beispiel #9
0
            private string GetNestedAggregate(MethodCallExpression method)
            {
                var accessLambda = (LambdaExpression)method.Arguments[1];

                var sourceType = accessLambda.Parameters[0].Type;

                Debug.Assert(sourceType != lambdaExpression.Parameters[0].Type);

                var whereCall = GetCorrelation(method, sourceType);

                var sumCall = Expression.Call(
                                typeof(Queryable).GetMethods()
                                    .Where(m => m.Name == method.Method.Name &&
                                                m.ReturnType == method.Type &&
                                                m.GetParameters().Length == 2)
                                    .Single().MakeGenericMethod(sourceType),
                                    whereCall, accessLambda);

                var foreignKey = ((StripQuotes(whereCall.Arguments[1]) as LambdaExpression).Body
                                   as BinaryExpression).Left;

                var keyValueType = typeof(KeyValuePair<int, int>)
                                    .GetGenericTypeDefinition()
                                    .MakeGenericType(foreignKey.Type,
                                                     accessLambda.Body.Type);

                var keyValueConstructor =
                    keyValueType.GetConstructor(new Type[]{foreignKey.Type,
                                                            accessLambda.Body.Type});

                var newKeyValue = Expression.New(keyValueConstructor,
                                                 new Expression[]{foreignKey,
                                                                   sumCall
                                                                   },
                                                 new PropertyInfo[]{
                                                          keyValueType.GetProperty("Key"),
                                                          keyValueType.GetProperty("Value")
                                                      });

                var selectorParam = Expression.Parameter(sourceType, "source");

                var projectionSelector = Expression.Lambda(newKeyValue, selectorParam);

                var aggregateSelect = QueryableMethodsProvider.GetSelectCall(
                                            whereCall, projectionSelector);

                SqlExpressionParser projector =
                    new SqlExpressionParser(indentLevel + 1, null,
                                            AggregateType.None);

                projector.Translate(aggregateSelect);

                accessedColumns.Add(GetProjectionSql(indentLevel, projector));

                return GetProjectionSql(indentLevel, projector);
            }
Beispiel #10
0
            private string GetCount(MethodCallExpression method)
            {
                Debug.Assert(method.Arguments.Count == 1);

                GetOperandValue();

                var sourceType = method.Method.GetGenericArguments()[0];

                var declaringType = GetSourceType(method.Arguments[0]);

                var foreignKey = GetForeignKey(declaringType, method.Arguments[0].Type);

                var foreignKeyExpression = Expression.MakeMemberAccess(
                                                Expression.Parameter(sourceType, sourceType.Name),
                                                sourceType.GetProperty(foreignKey));

                var whereCondition = Expression.Equal(foreignKeyExpression,
                                        Expression.Constant(
                                            new BoxedConstant(GetTableAlias(indentLevel) + "." +
                                                              GetPrimaryKey(declaringType))));

                var whereCall = QueryableMethodsProvider.GetWhereCall(sourceType, "source", whereCondition);

                var selectCall = QueryableMethodsProvider.GetSelectCall(whereCall);

                SqlExpressionParser projector = new SqlExpressionParser(indentLevel + 1, null,
                                                         GetAggregateTypeFromName(method.Method.Name));

                projector.Translate(selectCall);

                accessedColumns.Add(GetProjectionSql(indentLevel, projector));

                return GetProjectionSql(indentLevel + 1, projector);
            }
Beispiel #11
0
            private string GetAggregate(MethodCallExpression method)
            {
                if (method.Arguments.Count == 1)
                {
                    return GetCount(method);
                }

                Debug.Assert(method.Arguments.Count == 2);

                GetOperandValue();
                GetOperandValue();

                var accessLambda = (LambdaExpression)method.Arguments[1];

                var sourceType = accessLambda.Parameters[0].Type;

                if (sourceType != lambdaExpression.Parameters[0].Type
                    && accessLambda.Body.NodeType == ExpressionType.Call)
                {
                    return GetNestedAggregate(method);
                }

                var selectorParam = Expression.Parameter(sourceType,
                                                         accessLambda.Parameters[0].Name);

                var projectionSelector = Expression.Lambda(accessLambda.Body, selectorParam);

                var whereCall = GetCorrelation(method, sourceType);

                var selectCall = QueryableMethodsProvider.GetSelectCall(whereCall, projectionSelector);

                SqlExpressionParser projector =
                    new SqlExpressionParser(indentLevel + 1, null,
                                         GetAggregateTypeFromName(method.Method.Name));

                projector.Translate(selectCall);

                accessedColumns.Add(GetProjectionSql(indentLevel, projector));

                return GetProjectionSql(indentLevel + 1, projector);
            }