Пример #1
0
            Expression BuildExpression(int fieldIndex, ISqlExpression?sqlExpression)
            {
                Expression expr;

                if (SequenceHelper.UnwrapSubqueryContext(Sequence) is DefaultIfEmptyBuilder.DefaultIfEmptyContext defaultIfEmpty)
                {
                    expr = Builder.BuildSql(_returnType, fieldIndex, sqlExpression);
                    if (defaultIfEmpty.DefaultValue != null && expr is ConvertFromDataReaderExpression convert)
                    {
                        var generator = new ExpressionGenerator();
                        expr = convert.MakeNullable();
                        if (expr.Type.IsNullable())
                        {
                            var exprVar      = generator.AssignToVariable(expr, "nullable");
                            var defaultValue = defaultIfEmpty.DefaultValue;
                            if (defaultValue.Type != expr.Type)
                            {
                                var convertLambda = Builder.MappingSchema.GenerateSafeConvert(defaultValue.Type, expr.Type);
                                defaultValue = InternalExtensions.ApplyLambdaToExpression(convertLambda, defaultValue);
                            }

                            var resultVar = generator.AssignToVariable(defaultValue, "result");

                            generator.AddExpression(Expression.IfThen(
                                                        Expression.NotEqual(exprVar, ExpressionInstances.UntypedNull),
                                                        Expression.Assign(resultVar, Expression.Convert(exprVar, resultVar.Type))));

                            generator.AddExpression(resultVar);

                            expr = generator.Build();
                        }
                    }
                }
                else
                if (_methodName == "Sum" || _returnType.IsNullableType())
                {
                    expr = Builder.BuildSql(_returnType, fieldIndex, sqlExpression);
                }
                else
                {
                    expr = Expression.Block(
                        Expression.Call(null, MemberHelper.MethodOf(() => CheckNullValue(false, null !)), Expression.Call(ExpressionBuilder.DataReaderParam, Methods.ADONet.IsDBNull, ExpressionInstances.Constant0), Expression.Constant(_methodName)),
                        Builder.BuildSql(_returnType, fieldIndex, sqlExpression));
                }

                return(expr);
            }
Пример #2
0
        internal static ParameterAccessor GetParameter(Type type, IDataContext dataContext, SqlField field)
        {
            var exprParam = Expression.Parameter(typeof(Expression), "expr");

            Expression getter = Expression.Convert(
                Expression.Property(
                    Expression.Convert(exprParam, typeof(ConstantExpression)),
                    ReflectionHelper.Constant.Value),
                type);

            var descriptor    = field.ColumnDescriptor;
            var dbValueLambda = descriptor.GetDbParamLambda();

            Expression?valueGetter;
            Expression?dbDataTypeExpression;

            valueGetter = InternalExtensions.ApplyLambdaToExpression(dbValueLambda, getter);

            if (typeof(DataParameter).IsSameOrParentOf(valueGetter.Type))
            {
                dbDataTypeExpression = Expression.Call(Expression.Constant(field.ColumnDescriptor.GetDbDataType(false)),
                                                       DbDataType.WithSetValuesMethodInfo,
                                                       Expression.PropertyOrField(valueGetter, nameof(DataParameter.DbDataType)));
                valueGetter = Expression.PropertyOrField(valueGetter, nameof(DataParameter.Value));
            }
            else
            {
                var dbDataType = field.ColumnDescriptor.GetDbDataType(true).WithSystemType(valueGetter.Type);
                dbDataTypeExpression = Expression.Constant(dbDataType);
            }

            var param = ExpressionBuilder.CreateParameterAccessor(
                dataContext, valueGetter, getter, dbDataTypeExpression, valueGetter, exprParam, Expression.Parameter(typeof(object[]), "ps"), Expression.Parameter(typeof(IDataContext), "ctx"), field.Name.Replace('.', '_'));

            return(param);
        }