public SqlConstantExpression Constant(object value, CoreTypeMapping typeMapping = null)
 {
     return(new SqlConstantExpression(Expression.Constant(value), typeMapping));
 }
Exemple #2
0
 public SqlBinaryExpression Coalesce(SqlExpression left, SqlExpression right, CoreTypeMapping typeMapping = null)
 {
     return(MakeBinary(ExpressionType.Coalesce, left, right, typeMapping));
 }
Exemple #3
0
 public SqlUnaryExpression Convert(SqlExpression operand, Type type, CoreTypeMapping typeMapping = null)
 {
     return(MakeUnary(ExpressionType.Convert, operand, type, typeMapping));
 }
Exemple #4
0
        private SqlExpression ApplyTypeMappingOnSqlBinary(
            SqlBinaryExpression sqlBinaryExpression, CoreTypeMapping typeMapping)
        {
            var left  = sqlBinaryExpression.Left;
            var right = sqlBinaryExpression.Right;

            Type            resultType;
            CoreTypeMapping resultTypeMapping;
            CoreTypeMapping inferredTypeMapping;

            switch (sqlBinaryExpression.OperatorType)
            {
            case ExpressionType.Equal:
            case ExpressionType.GreaterThan:
            case ExpressionType.GreaterThanOrEqual:
            case ExpressionType.LessThan:
            case ExpressionType.LessThanOrEqual:
            case ExpressionType.NotEqual:
            {
                inferredTypeMapping = ExpressionExtensions.InferTypeMapping(left, right)
                                      ?? _typeMappingSource.FindMapping(left.Type);
                resultType        = typeof(bool);
                resultTypeMapping = _boolTypeMapping;
            }
            break;

            case ExpressionType.AndAlso:
            case ExpressionType.OrElse:
            {
                inferredTypeMapping = _boolTypeMapping;
                resultType          = typeof(bool);
                resultTypeMapping   = _boolTypeMapping;
            }
            break;

            case ExpressionType.Add:
            case ExpressionType.Subtract:
            case ExpressionType.Multiply:
            case ExpressionType.Divide:
            case ExpressionType.Modulo:
            case ExpressionType.LeftShift:
            case ExpressionType.RightShift:
            case ExpressionType.Coalesce:
            case ExpressionType.And:
            case ExpressionType.Or:
            {
                inferredTypeMapping = typeMapping ?? ExpressionExtensions.InferTypeMapping(left, right);
                resultType          = left.Type;
                resultTypeMapping   = inferredTypeMapping;
            }
            break;

            default:
                throw new InvalidOperationException("Incorrect operatorType for SqlBinaryExpression");
            }

            return(new SqlBinaryExpression(
                       sqlBinaryExpression.OperatorType,
                       ApplyTypeMapping(left, inferredTypeMapping),
                       ApplyTypeMapping(right, inferredTypeMapping),
                       resultType,
                       resultTypeMapping));
        }
Exemple #5
0
 public SqlBinaryExpression Subtract(SqlExpression left, SqlExpression right, CoreTypeMapping typeMapping = null)
 {
     return(MakeBinary(ExpressionType.Subtract, left, right, typeMapping));
 }
Exemple #6
0
 /// <summary>
 ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
 ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
 ///     any release. You should only use it directly in your code with extreme caution and knowing that
 ///     doing so can result in application failures when updating to a new Entity Framework Core release.
 /// </summary>
 public virtual SqlBinaryExpression And(SqlExpression left, SqlExpression right, CoreTypeMapping typeMapping = null)
 => MakeBinary(ExpressionType.And, left, right, typeMapping);
Exemple #7
0
 /// <summary>
 ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
 ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
 ///     any release. You should only use it directly in your code with extreme caution and knowing that
 ///     doing so can result in application failures when updating to a new Entity Framework Core release.
 /// </summary>
 public virtual SqlUnaryExpression Convert(SqlExpression operand, Type type, CoreTypeMapping typeMapping = null)
 => MakeUnary(ExpressionType.Convert, operand, type, typeMapping);
Exemple #8
0
 /// <summary>
 ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
 ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
 ///     any release. You should only use it directly in your code with extreme caution and knowing that
 ///     doing so can result in application failures when updating to a new Entity Framework Core release.
 /// </summary>
 protected SqlExpression([NotNull] Type type, [CanBeNull] CoreTypeMapping typeMapping)
 {
     Type        = type;
     TypeMapping = typeMapping;
 }
Exemple #9
0
 protected SqlExpression(Type type, CoreTypeMapping typeMapping)
 {
     Type        = type;
     TypeMapping = typeMapping;
 }
Exemple #10
0
 /// <summary>
 ///     Sets the <see cref="CoreTypeMapping" /> for the given property
 /// </summary>
 /// <param name="property"> The property. </param>
 /// <param name="typeMapping"> The <see cref="CoreTypeMapping" /> for this property. </param>
 public static CoreTypeMapping SetTypeMapping(
     [NotNull] this IMutableProperty property,
     [NotNull] CoreTypeMapping typeMapping)
 => ((Property)property).SetTypeMapping(typeMapping, ConfigurationSource.Explicit);
Exemple #11
0
 internal SqlParameterExpression(ParameterExpression parameterExpression, CoreTypeMapping typeMapping)
     : base(parameterExpression.Type, typeMapping)
 {
     _parameterExpression = parameterExpression;
 }
 /// <summary>
 ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
 ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
 ///     any release. You should only use it directly in your code with extreme caution and knowing that
 ///     doing so can result in application failures when updating to a new Entity Framework Core release.
 /// </summary>
 public virtual SqlExpression ApplyTypeMapping([NotNull] CoreTypeMapping typeMapping)
 => new SqlConstantExpression(_constantExpression, typeMapping);
 /// <summary>
 ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
 ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
 ///     any release. You should only use it directly in your code with extreme caution and knowing that
 ///     doing so can result in application failures when updating to a new Entity Framework Core release.
 /// </summary>
 public SqlConstantExpression([NotNull] ConstantExpression constantExpression, [NotNull] CoreTypeMapping typeMapping)
     : base(constantExpression.Type, typeMapping)
 {
     _constantExpression = constantExpression;
 }
Exemple #14
0
 /// <summary>
 ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
 ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
 ///     any release. You should only use it directly in your code with extreme caution and knowing that
 ///     doing so can result in application failures when updating to a new Entity Framework Core release.
 /// </summary>
 public SqlExpressionFactory(ITypeMappingSource typeMappingSource, IModel model)
 {
     _typeMappingSource = typeMappingSource;
     _model             = model;
     _boolTypeMapping   = typeMappingSource.FindMapping(typeof(bool), model) !;
 }
Exemple #15
0
 /// <summary>
 ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
 ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
 ///     any release. You should only use it directly in your code with extreme caution and knowing that
 ///     doing so can result in application failures when updating to a new Entity Framework Core release.
 /// </summary>
 public virtual SqlBinaryExpression Subtract(SqlExpression left, SqlExpression right, CoreTypeMapping typeMapping = null)
 => MakeBinary(ExpressionType.Subtract, left, right, typeMapping);
Exemple #16
0
 public static CoreTypeMapping SetTypeMapping(
     [NotNull] this IConventionProperty property,
     [NotNull] CoreTypeMapping typeMapping,
     bool fromDataAnnotation = false)
 => ((Property)property).SetTypeMapping(
     typeMapping, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention);
Exemple #17
0
 /// <summary>
 ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
 ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
 ///     any release. You should only use it directly in your code with extreme caution and knowing that
 ///     doing so can result in application failures when updating to a new Entity Framework Core release.
 /// </summary>
 public virtual SqlBinaryExpression Multiply(SqlExpression left, SqlExpression right, CoreTypeMapping typeMapping = null)
 => MakeBinary(ExpressionType.Multiply, left, right, typeMapping);
 /// <summary>
 ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
 ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
 ///     any release. You should only use it directly in your code with extreme caution and knowing that
 ///     doing so can result in application failures when updating to a new Entity Framework Core release.
 /// </summary>
 public SqlParameterExpression([NotNull] ParameterExpression parameterExpression, [CanBeNull] CoreTypeMapping typeMapping)
     : base(parameterExpression.Type, typeMapping)
 {
     _parameterExpression = parameterExpression;
 }
Exemple #19
0
 /// <summary>
 ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
 ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
 ///     any release. You should only use it directly in your code with extreme caution and knowing that
 ///     doing so can result in application failures when updating to a new Entity Framework Core release.
 /// </summary>
 public virtual SqlBinaryExpression Coalesce(SqlExpression left, SqlExpression right, CoreTypeMapping typeMapping = null)
 => MakeBinary(ExpressionType.Coalesce, left, right, typeMapping);
 /// <summary>
 ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
 ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
 ///     any release. You should only use it directly in your code with extreme caution and knowing that
 ///     doing so can result in application failures when updating to a new Entity Framework Core release.
 /// </summary>
 public SqlExpression ApplyTypeMapping([CanBeNull] CoreTypeMapping typeMapping)
 => new SqlParameterExpression(_parameterExpression, typeMapping);
Exemple #21
0
 /// <summary>
 ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
 ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
 ///     any release. You should only use it directly in your code with extreme caution and knowing that
 ///     doing so can result in application failures when updating to a new Entity Framework Core release.
 /// </summary>
 public virtual SqlConstantExpression Constant(object value, CoreTypeMapping typeMapping = null)
 => new SqlConstantExpression(Expression.Constant(value), typeMapping);
 /// <summary>
 ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
 ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
 ///     any release. You should only use it directly in your code with extreme caution and knowing that
 ///     doing so can result in application failures when updating to a new Entity Framework Core release.
 /// </summary>
 public virtual SqlFunctionExpression ApplyTypeMapping([CanBeNull] CoreTypeMapping typeMapping)
 => new SqlFunctionExpression(
     Name,
     Arguments,
     Type,
     typeMapping ?? TypeMapping);
Exemple #23
0
 public SqlExpressionFactory(ITypeMappingSource typeMappingSource)
 {
     _typeMappingSource = typeMappingSource;
     _boolTypeMapping   = typeMappingSource.FindMapping(typeof(bool));
 }
        private SqlExpression ApplyTypeMappingOnSqlBinary(
            SqlBinaryExpression sqlBinaryExpression, CoreTypeMapping typeMapping)
        {
            var left  = sqlBinaryExpression.Left;
            var right = sqlBinaryExpression.Right;

            Type            resultType;
            CoreTypeMapping resultTypeMapping;
            CoreTypeMapping inferredTypeMapping;

            switch (sqlBinaryExpression.OperatorType)
            {
            case ExpressionType.Equal:
            case ExpressionType.GreaterThan:
            case ExpressionType.GreaterThanOrEqual:
            case ExpressionType.LessThan:
            case ExpressionType.LessThanOrEqual:
            case ExpressionType.NotEqual:
            {
                inferredTypeMapping = ExpressionExtensions.InferTypeMapping(left, right)
                                      // We avoid object here since the result does not get typeMapping from outside.
                                      ?? (left.Type != typeof(object)
                            ? _typeMappingSource.FindMapping(left.Type)
                            : _typeMappingSource.FindMapping(right.Type));
                resultType        = typeof(bool);
                resultTypeMapping = _boolTypeMapping;
            }
            break;

            case ExpressionType.AndAlso:
            case ExpressionType.OrElse:
            {
                inferredTypeMapping = _boolTypeMapping;
                resultType          = typeof(bool);
                resultTypeMapping   = _boolTypeMapping;
            }
            break;

            case ExpressionType.Add:
            case ExpressionType.Subtract:
            case ExpressionType.Multiply:
            case ExpressionType.Divide:
            case ExpressionType.Modulo:
            case ExpressionType.LeftShift:
            case ExpressionType.RightShift:
            case ExpressionType.And:
            case ExpressionType.Or:
            {
                inferredTypeMapping = typeMapping ?? ExpressionExtensions.InferTypeMapping(left, right);
                resultType          = inferredTypeMapping?.ClrType ?? left.Type;
                resultTypeMapping   = inferredTypeMapping;
            }
            break;

            default:
                throw new InvalidOperationException(CoreStrings.IncorrectOperatorType);
            }

            return(new SqlBinaryExpression(
                       sqlBinaryExpression.OperatorType,
                       ApplyTypeMapping(left, inferredTypeMapping),
                       ApplyTypeMapping(right, inferredTypeMapping),
                       resultType,
                       resultTypeMapping));
        }
Exemple #25
0
 public SqlBinaryExpression Modulo(SqlExpression left, SqlExpression right, CoreTypeMapping typeMapping = null)
 {
     return(MakeBinary(ExpressionType.Modulo, left, right, typeMapping));
 }
Exemple #26
0
            public override Expression Visit(Expression expression)
            {
                if (expression is InExpression inExpression)
                {
                    var             inValues     = new List <object>();
                    var             hasNullValue = false;
                    CoreTypeMapping typeMapping  = null;

                    switch (inExpression.Values)
                    {
                    case SqlConstantExpression sqlConstant:
                    {
                        typeMapping = sqlConstant.TypeMapping;
                        var values = (IEnumerable)sqlConstant.Value;
                        foreach (var value in values)
                        {
                            if (value == null)
                            {
                                hasNullValue = true;
                                continue;
                            }

                            inValues.Add(value);
                        }
                    }
                    break;

                    case SqlParameterExpression sqlParameter:
                    {
                        typeMapping = sqlParameter.TypeMapping;
                        var values = (IEnumerable)_parametersValues[sqlParameter.Name];
                        foreach (var value in values)
                        {
                            if (value == null)
                            {
                                hasNullValue = true;
                                continue;
                            }

                            inValues.Add(value);
                        }
                    }
                    break;
                    }

                    var updatedInExpression = inValues.Count > 0
                        ? _sqlExpressionFactory.In(
                        (SqlExpression)Visit(inExpression.Item),
                        _sqlExpressionFactory.Constant(inValues, typeMapping),
                        inExpression.Negated)
                        : null;

                    var nullCheckExpression = hasNullValue
                        ? _sqlExpressionFactory.IsNull(inExpression.Item)
                        : null;

                    if (updatedInExpression != null && nullCheckExpression != null)
                    {
                        return(_sqlExpressionFactory.OrElse(updatedInExpression, nullCheckExpression));
                    }

                    if (updatedInExpression == null && nullCheckExpression == null)
                    {
                        return(_sqlExpressionFactory.Equal(_sqlExpressionFactory.Constant(true), _sqlExpressionFactory.Constant(false)));
                    }

                    return((SqlExpression)updatedInExpression ?? nullCheckExpression);
                }

                return(base.Visit(expression));
            }
Exemple #27
0
 private SqlUnaryExpression MakeUnary(
     ExpressionType operatorType, SqlExpression operand, Type type, CoreTypeMapping typeMapping = null)
 {
     return((SqlUnaryExpression)ApplyTypeMapping(new SqlUnaryExpression(operatorType, operand, type, null), typeMapping));
 }
Exemple #28
0
            private Expression CreateGetValueExpression(
                Expression jObjectExpression,
                string storeName,
                Type clrType,
                CoreTypeMapping typeMapping = null)
            {
                var innerExpression = jObjectExpression;

                if (_projectionBindings.TryGetValue(jObjectExpression, out var innerVariable))
                {
                    innerExpression = innerVariable;
                }
                else if (jObjectExpression is RootReferenceExpression rootReferenceExpression)
                {
                    innerExpression = CreateGetValueExpression(
                        _jObjectParameter, rootReferenceExpression.Alias, typeof(JObject));
                }
                else if (jObjectExpression is ObjectAccessExpression objectAccessExpression)
                {
                    var innerAccessExpression = objectAccessExpression.AccessExpression;

                    innerExpression = CreateGetValueExpression(
                        innerAccessExpression, ((IAccessExpression)innerAccessExpression).Name, typeof(JObject));
                }

                var jTokenExpression = CreateReadJTokenExpression(innerExpression, storeName);

                Expression valueExpression;
                var        converter = typeMapping?.Converter;

                if (converter != null)
                {
                    var jTokenParameter = Expression.Parameter(typeof(JToken));

                    var body
                        = ReplacingExpressionVisitor.Replace(
                              converter.ConvertFromProviderExpression.Parameters.Single(),
                              Expression.Call(
                                  jTokenParameter,
                                  _jTokenToObjectMethodInfo.MakeGenericMethod(converter.ProviderClrType)),
                              converter.ConvertFromProviderExpression.Body);

                    if (body.Type != clrType)
                    {
                        body = Expression.Convert(body, clrType);
                    }

                    body = Expression.Condition(
                        Expression.OrElse(
                            Expression.Equal(jTokenParameter, Expression.Default(typeof(JToken))),
                            Expression.Equal(
                                Expression.MakeMemberAccess(jTokenParameter, _jTokenTypePropertyInfo),
                                Expression.Constant(JTokenType.Null))),
                        Expression.Default(clrType),
                        body);

                    valueExpression = Expression.Invoke(Expression.Lambda(body, jTokenParameter), jTokenExpression);
                }
                else
                {
                    valueExpression = ConvertJTokenToType(jTokenExpression, typeMapping?.ClrType.MakeNullable() ?? clrType);

                    if (valueExpression.Type != clrType)
                    {
                        valueExpression = Expression.Convert(valueExpression, clrType);
                    }
                }

                return(valueExpression);
            }
Exemple #29
0
        public SqlFunctionExpression Function(
            string functionName, IEnumerable <SqlExpression> arguments, Type returnType, CoreTypeMapping typeMapping = null)
        {
            var typeMappedArguments = new List <SqlExpression>();

            foreach (var argument in arguments)
            {
                typeMappedArguments.Add(ApplyDefaultTypeMapping(argument));
            }

            return(new SqlFunctionExpression(
                       functionName,
                       typeMappedArguments,
                       returnType,
                       typeMapping));
        }
Exemple #30
0
 public SqlFunctionExpression ApplyTypeMapping(CoreTypeMapping typeMapping)
 => new SqlFunctionExpression(
     FunctionName,
     Arguments,
     Type,
     typeMapping ?? TypeMapping);