Beispiel #1
0
        public static (IEnumerable <StringBasedMappedProperty> property, IEnumerable <MappedTable> tables) BuildMap(
            BuildMapState state,
            MethodCallExpression caseExpression,
            MapType nextMap,
            string toPrefix = null)
        {
            var(isElse, elseResult) = IsElse(caseExpression);
            if (!isElse)
            {
                throw new InvalidOperationException("The method only handles sql case statements");
            }

            var(@else, tables) = ComplexMapBuilder.BuildMap(state, elseResult);
            var el = @else.ToArray();

            if (el.Length != 1)
            {
                throw new SqlBuilderException(state.MappingPurpose, caseExpression.Arguments[0]);
            }

            var(cases, tables2) = GetDslCases(state, caseExpression.Object);
            return(CompileCases(
                       state,
                       cases,
                       el[0],
                       tables.Concat(tables2),
                       caseExpression.Type,
                       toPrefix));
        }
Beispiel #2
0
        public static (IEnumerable <QueryElementBasedMappedProperty> properties, IEnumerable <StrongMappedTable> tables) BuildMapFromRoot(BuildMapState state, Expression expression)
        {
            var(properties, tables) = ComplexMapBuilder.BuildMap(state, expression);

            return(
                properties.Select(p => p.Convert(state)).ToArray(),
                tables.Select(p => p.Convert(state.WrappedSqlStatement)).ToArray());
        }
Beispiel #3
0
        static (IEnumerable <(StringBasedMappedProperty when, StringBasedMappedProperty then)> cases, StringBasedMappedProperty @else, IEnumerable <MappedTable> tables) GetCasesX(
            BuildMapState state,
            Expression expr)
        {
            var caseExpression = expr as ConditionalExpression;

            if (caseExpression == null)
            {
                var(elseP, elseTables) = ComplexMapBuilder.BuildMap(
                    state,
                    ReflectionUtils.RemoveConvert(expr));

                var elseProperties = elseP.ToArray();
                if (elseProperties.Length != 1)
                {
                    throw new SqlBuilderException(state.MappingPurpose, caseExpression);
                }

                return(CodingConstants.Empty.Case, elseProperties[0], elseTables);
            }

            var(ifP, ifTables) = ComplexMapBuilder.BuildMap(
                state,
                ReflectionUtils.RemoveConvert(caseExpression.Test));

            var ifProperties = ifP.ToArray();

            if (ifProperties.Length != 1)
            {
                throw new SqlBuilderException(state.MappingPurpose, caseExpression);
            }

            var(thenP, thenTables) = ComplexMapBuilder.BuildMap(
                state,
                ReflectionUtils.RemoveConvert(caseExpression.IfTrue));

            var thenProperties = thenP.ToArray();

            if (thenProperties.Length != 1)
            {
                throw new SqlBuilderException(state.MappingPurpose, caseExpression);
            }

            var(otherCases, @else, otherTables) = GetCasesX(
                state,
                ReflectionUtils.RemoveConvert(caseExpression.IfFalse));

            return(
                otherCases.Prepend((ifProperties[0], thenProperties[0])),
                @else,
                ifTables.Concat(thenTables).Concat(otherTables)
                );
        }
Beispiel #4
0
        static (IEnumerable <(StringBasedMappedProperty when, StringBasedMappedProperty then)> cases, IEnumerable <MappedTable> tables) GetDslCases(
            BuildMapState state,
            Expression caseExpression)
        {
            if (caseExpression == null)
            {
                return(CodingConstants.Empty.Case, CodingConstants.Empty.MappedTable);
            }

            var thenObject = caseExpression as MethodCallExpression;

            if (thenObject == null)
            {
                throw new SqlBuilderException(state.MappingPurpose, caseExpression);
            }

            var(isThen, thenResult) = IsThen(thenObject);
            if (!isThen)
            {
                throw new SqlBuilderException(state.MappingPurpose, thenObject);
            }

            var whenObject = thenObject.Object as MethodCallExpression;

            if (whenObject == null)
            {
                throw new SqlBuilderException(state.MappingPurpose, thenObject.Object);
            }

            var(isWhen, whenCondition) = IsWhen(whenObject);
            if (!isWhen)
            {
                throw new SqlBuilderException(state.MappingPurpose, whenObject);
            }

            var(whenProperties, whenTables) = ComplexMapBuilder.BuildMap(state, whenCondition);
            var(thenProperties, thenTables) = ComplexMapBuilder.BuildMap(state, thenResult);

            var whenP = whenProperties.ToArray();
            var thenP = thenProperties.ToArray();

            if (whenP.Length != 1 || thenP.Length != 1)
            {
                throw new SqlBuilderException(state.MappingPurpose, caseExpression);
            }

            var(cases, tables) = GetDslCases(state, whenObject.Object);
            return(
                cases.Append((whenP[0], thenP[0])),
                tables.Concat(whenTables).Concat(thenTables)
                );
        }
Beispiel #5
0
        public static (IEnumerable <StringBasedMappedProperty> property, IEnumerable <MappedTable> tables) BuildMap(
            BuildMapState state,
            MethodCallExpression caseExpression,
            MapType nextMap,
            string toPrefix = null)
        {
            var(isElse, elseResult) = IsElse(caseExpression);
            if (!isElse)
            {
                throw new InvalidOperationException("The method only handles sql case statements");
            }

            var(@else, tables) = ComplexMapBuilder.BuildMap(state, elseResult);
            var el = @else.ToArray();

            if (el.Length != 1)
            {
                throw new SqlBuilderException(state.MappingPurpose, caseExpression.Arguments[0]);
            }

            var(subject, cases, tables2) = GetCases(state, caseExpression.Object, MapType.Other, toPrefix);

            var prop = new StringBasedMappedProperty(
                new SimpleCaseSqlExpression <StringBasedElement>(
                    subject.FromParams,
                    cases.Select(c => (c.when.FromParams, c.then.FromParams)),
                    el[0].FromParams),
                toPrefix,
                caseExpression.Type,
                state.MappingContext.propertyName,
                cases
                .SelectMany(x => new [] { x.when, x.then })
                .Concat(@else)
                .SelectMany(c => c.PropertySegmentConstructors)
                .ToArray());

            return(
                prop.ToEnumerable(),
                tables.Concat(tables2));
        }
Beispiel #6
0
        static (StringBasedMappedProperty subject, IEnumerable <(StringBasedMappedProperty when, StringBasedMappedProperty then)> cases, IEnumerable <MappedTable> tables) GetCases(
            BuildMapState state,
            Expression caseExpression,
            MapType nextMap,
            string toPrefix = null)
        {
            var thenObject = caseExpression as MethodCallExpression;

            if (thenObject == null)
            {
                throw new SqlBuilderException(state.MappingPurpose, caseExpression);
            }

            var(isSubject, subject) = IsSubject(thenObject);
            if (isSubject)
            {
                var(subjectProperties, subjectTables) = ComplexMapBuilder.BuildMap(state, subject);
                var subjectP = subjectProperties.ToArray();
                if (subjectP.Length != 1)
                {
                    throw new SqlBuilderException(state.MappingPurpose, caseExpression);
                }

                return(subjectP[0], CodingConstants.Empty.Case, subjectTables);
            }

            var(isThen, thenResult) = IsThen(thenObject);
            if (!isThen)
            {
                throw new SqlBuilderException(state.MappingPurpose, thenObject);
            }

            var whenObject = thenObject.Object as MethodCallExpression;

            if (whenObject == null)
            {
                throw new SqlBuilderException(state.MappingPurpose, thenObject.Object);
            }

            var(isWhen, whenCondition) = IsWhen(whenObject);
            if (!isWhen)
            {
                throw new SqlBuilderException(state.MappingPurpose, whenObject);
            }

            var(whenProperties, whenTables) = ComplexMapBuilder.BuildMap(state, whenCondition);
            var(thenProperties, thenTables) = ComplexMapBuilder.BuildMap(state, thenResult);

            var whenP = whenProperties.ToArray();
            var thenP = thenProperties.ToArray();

            if (whenP.Length != 1 || thenP.Length != 1)
            {
                throw new SqlBuilderException(state.MappingPurpose, caseExpression);
            }

            var(sjt, cases, tables) = GetCases(state, whenObject.Object, MapType.Other, toPrefix);
            return(
                sjt,
                cases.Append((whenP[0], thenP[0])),
                tables.Concat(whenTables).Concat(thenTables)
                );
        }