Esempio n. 1
0
        public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList <SqlExpression> arguments)
        {
            if (!method.Equals(MethodInfo))
            {
                return(null);
            }

            var patternExpression  = arguments[0];
            var endsWithExpression = _fbSqlExpressionFactory.Equal(
                _fbSqlExpressionFactory.Function(
                    "RIGHT",
                    new[] {
                instance,
                _fbSqlExpressionFactory.Function(
                    "CHARACTER_LENGTH",
                    new[] { patternExpression },
                    typeof(int))
            },
                    instance.Type),
                patternExpression);

            return(patternExpression is SqlConstantExpression sqlConstantExpression
                                ? (string)sqlConstantExpression.Value == string.Empty
                                        ? (SqlExpression)_fbSqlExpressionFactory.Constant(true)
                                        : endsWithExpression
                                : _fbSqlExpressionFactory.OrElse(
                       endsWithExpression,
                       _fbSqlExpressionFactory.Equal(
                           patternExpression,
                           _fbSqlExpressionFactory.Constant(string.Empty))));
        }
Esempio n. 2
0
 public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList <SqlExpression> arguments, IDiagnosticsLogger <DbLoggerCategory.Query> logger)
 {
     if (SupportedMethodTranslations.TryGetValue(method, out var sqlFunctionName))
     {
         return(_fbSqlExpressionFactory.Function(sqlFunctionName, arguments, true, arguments.Select(_ => true), method.ReturnType));
     }
     if (TruncateMethodInfos.Contains(method))
     {
         return(_fbSqlExpressionFactory.ApplyDefaultTypeMapping(_fbSqlExpressionFactory.Function(
                                                                    "TRUNC",
                                                                    new[] { _fbSqlExpressionFactory.ApplyDefaultTypeMapping(arguments[0]), _fbSqlExpressionFactory.Constant(0) },
                                                                    true,
                                                                    new[] { true, false },
                                                                    method.ReturnType)));
     }
     if (RoundMethodInfos.Contains(method))
     {
         var roundArguments = arguments.Count == 1
                             ? new[] { _fbSqlExpressionFactory.ApplyDefaultTypeMapping(arguments[0]), _fbSqlExpressionFactory.Constant(0) }
                             : new[] { _fbSqlExpressionFactory.ApplyDefaultTypeMapping(arguments[0]), _fbSqlExpressionFactory.ApplyDefaultTypeMapping(arguments[1]) };
         var nullability = arguments.Count == 1
                             ? new[] { true, false }
                             : new[] { true, true };
         return(_fbSqlExpressionFactory.ApplyDefaultTypeMapping(_fbSqlExpressionFactory.Function(
                                                                    "ROUND",
                                                                    roundArguments,
                                                                    true,
                                                                    nullability,
                                                                    method.ReturnType)));
     }
     return(null);
 }
    public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList <SqlExpression> arguments, IDiagnosticsLogger <DbLoggerCategory.Query> logger)
    {
        if (!method.Equals(MethodInfo))
        {
            return(null);
        }

        var patternExpression  = _fbSqlExpressionFactory.ApplyDefaultTypeMapping(arguments[0]);
        var endsWithExpression = _fbSqlExpressionFactory.Equal(
            _fbSqlExpressionFactory.ApplyDefaultTypeMapping(_fbSqlExpressionFactory.Function(
                                                                "RIGHT",
                                                                new[] {
            instance,
            _fbSqlExpressionFactory.Function(
                "CHAR_LENGTH",
                new[] { patternExpression },
                true,
                new[] { true },
                typeof(int))
        },
                                                                true,
                                                                new[] { true, true },
                                                                instance.Type)),
            patternExpression);

        return(patternExpression is SqlConstantExpression sqlConstantExpression
                        ? (string)sqlConstantExpression.Value == string.Empty
                                ? (SqlExpression)_fbSqlExpressionFactory.Constant(true)
                                : endsWithExpression
                        : _fbSqlExpressionFactory.OrElse(
                   endsWithExpression,
                   _fbSqlExpressionFactory.Equal(
                       patternExpression,
                       _fbSqlExpressionFactory.Constant(string.Empty))));
    }
 public SqlExpression Translate(SqlExpression instance, MemberInfo member, Type returnType)
 {
     if (member.DeclaringType == typeof(DateTime) && member.Name == nameof(DateTime.Now))
     {
         // LOCALTIMESTAMP
         return(_fbSqlExpressionFactory.Function("CURRENT_TIMESTAMP", typeof(DateTime)));
     }
     if (member.DeclaringType == typeof(DateTime) && member.Name == nameof(DateTime.Today))
     {
         return(_fbSqlExpressionFactory.Function("CURRENT_DATE", typeof(DateTime)));
     }
     return(null);
 }
 public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList <SqlExpression> arguments, IDiagnosticsLogger <DbLoggerCategory.Query> logger)
 {
     if (SupportedMethodTranslations.TryGetValue(method, out var sqlFunctionName))
     {
         return(_fbSqlExpressionFactory.Function(sqlFunctionName, arguments, true, arguments.Select(_ => true), method.ReturnType));
     }
     if (TruncateMethodInfos.Contains(method))
     {
         return(_fbSqlExpressionFactory.Function(
                    "TRUNC",
                    new[] { arguments[0], _fbSqlExpressionFactory.Constant(0) },
                    true,
                    new[] { true, default },
Esempio n. 6
0
        public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList <SqlExpression> arguments, IDiagnosticsLogger <DbLoggerCategory.Query> logger)
        {
            if (!method.Equals(MethodInfo))
            {
                return(null);
            }

            var patternExpression  = _fbSqlExpressionFactory.ApplyDefaultTypeMapping(arguments[0]);
            var positionExpression = _fbSqlExpressionFactory.GreaterThan(
                _fbSqlExpressionFactory.Function(
                    "POSITION",
                    new[] { patternExpression, instance },
                    true,
                    new[] { true, true },
                    typeof(int)),
                _fbSqlExpressionFactory.Constant(0));

            return(patternExpression is SqlConstantExpression sqlConstantExpression
                                ? ((string)sqlConstantExpression.Value)?.Length == 0
                                        ? (SqlExpression)_fbSqlExpressionFactory.Constant(true)
                                        : positionExpression
                                : _fbSqlExpressionFactory.OrElse(
                       positionExpression,
                       _fbSqlExpressionFactory.Equal(
                           patternExpression,
                           _fbSqlExpressionFactory.Constant(string.Empty))));
        }
        public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList <SqlExpression> arguments)
        {
            if (!method.Equals(MethodInfo))
            {
                return(null);
            }

            var patternExpression  = arguments[0];
            var positionExpression = _fbSqlExpressionFactory.GreaterThan(
                _fbSqlExpressionFactory.Function(
                    "POSITION",
                    new[] { patternExpression, instance },
                    typeof(int)),
                _fbSqlExpressionFactory.Constant(0));

            return(patternExpression is SqlConstantExpression sqlConstantExpression
                                ? ((string)sqlConstantExpression.Value)?.Length == 0
                                        ? (SqlExpression)_fbSqlExpressionFactory.Constant(true)
                                        : positionExpression
                                : _fbSqlExpressionFactory.OrElse(
                       positionExpression,
                       _fbSqlExpressionFactory.Equal(
                           patternExpression,
                           _fbSqlExpressionFactory.Constant(string.Empty))));
        }
 public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList <SqlExpression> arguments, IDiagnosticsLogger <DbLoggerCategory.Query> logger)
 {
     if (method.DeclaringType == typeof(string) && method.Name == nameof(string.ToLower))
     {
         return(_fbSqlExpressionFactory.Function("LOWER", new[] { instance }, true, new[] { true }, typeof(string)));
     }
     return(null);
 }
 public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList <SqlExpression> arguments)
 {
     if (method.DeclaringType == typeof(Guid) && method.Name == nameof(Guid.NewGuid))
     {
         return(_fbSqlExpressionFactory.Function("GEN_UUID", new[] { instance }, typeof(Guid)));
     }
     return(null);
 }
 public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList <SqlExpression> arguments, IDiagnosticsLogger <DbLoggerCategory.Query> logger)
 {
     if (method.DeclaringType == typeof(Guid) && method.Name == nameof(Guid.NewGuid))
     {
         return(_fbSqlExpressionFactory.Function("GEN_UUID", Array.Empty <SqlExpression>(), false, Array.Empty <bool>(), typeof(Guid)));
     }
     return(null);
 }
 public SqlExpression Translate(SqlExpression instance, MemberInfo member, Type returnType)
 {
     if (member.DeclaringType == typeof(string) && member.Name == nameof(string.Length))
     {
         return(_fbSqlExpressionFactory.Function("CHARACTER_LENGTH", new[] { instance }, typeof(int)));
     }
     return(null);
 }
 public SqlExpression Translate(SqlExpression instance, MemberInfo member, Type returnType, IDiagnosticsLogger <DbLoggerCategory.Query> logger)
 {
     if (member.DeclaringType == typeof(string) && member.Name == nameof(string.Length))
     {
         return(_fbSqlExpressionFactory.Function("CHAR_LENGTH", new[] { instance }, true, new[] { true }, typeof(int)));
     }
     return(null);
 }
Esempio n. 13
0
 public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList <SqlExpression> arguments)
 {
     if (method.DeclaringType == typeof(string) && method.Name == nameof(string.ToUpper))
     {
         return(_fbSqlExpressionFactory.Function("UPPER", new[] { instance }, typeof(string)));
     }
     return(null);
 }
Esempio n. 14
0
        public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList <SqlExpression> arguments)
        {
            if (!method.Equals(ReplaceMethod))
            {
                return(null);
            }

            return(_fbSqlExpressionFactory.Function("REPLACE", new[] { instance }.Concat(arguments), instance.Type));
        }
 public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList <SqlExpression> arguments)
 {
     if (method.DeclaringType == typeof(string) && method.Name == nameof(string.IndexOf))
     {
         return(_fbSqlExpressionFactory.Subtract(
                    _fbSqlExpressionFactory.Function("POSITION", new[] { arguments[0], instance }.Concat(arguments.Skip(1)), typeof(int)),
                    _fbSqlExpressionFactory.Constant(1)));
     }
     return(null);
 }
 public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList <SqlExpression> arguments, IDiagnosticsLogger <DbLoggerCategory.Query> logger)
 {
     if (method.DeclaringType == typeof(string) && method.Name == nameof(string.IndexOf))
     {
         var args = new[] { arguments[0], instance }.Concat(arguments.Skip(1));
         return(_fbSqlExpressionFactory.Subtract(
                    _fbSqlExpressionFactory.Function("POSITION", args, true, args.Select(_ => true), typeof(int)),
                    _fbSqlExpressionFactory.Constant(1)));
     }
     return(null);
 }
Esempio n. 17
0
        public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList <SqlExpression> arguments, IDiagnosticsLogger <DbLoggerCategory.Query> logger)
        {
            if (!method.Equals(ReplaceMethod))
            {
                return(null);
            }

            var args = new[] { instance }.Concat(arguments);

            return(_fbSqlExpressionFactory.Function("REPLACE", args, true, args.Select(_ => true), instance.Type));
        }
Esempio n. 18
0
        public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList <SqlExpression> arguments, IDiagnosticsLogger <DbLoggerCategory.Query> logger)
        {
            if (!MethodInfoDatePartMapping.TryGetValue(method, out var part))
            {
                return(null);
            }

            return(_fbSqlExpressionFactory.Function(
                       "DATEADD",
                       new[] { _fbSqlExpressionFactory.Fragment(part), arguments[0], instance },
                       true,
                       new[] { default, true, true },
        public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList <SqlExpression> arguments, IDiagnosticsLogger <DbLoggerCategory.Query> logger)
        {
            if (method.IsGenericMethod &&
                method.GetGenericMethodDefinition().Equals(EnumerableMethods.Contains) &&
                arguments[0].Type == typeof(byte[]))
            {
                var value = arguments[1] is SqlConstantExpression constantValue
                                        ? _fbSqlExpressionFactory.Function("ASCII_CHAR", new[] { _fbSqlExpressionFactory.Constant((byte)constantValue.Value) }, false, new[] { false }, typeof(string))
                                        : _fbSqlExpressionFactory.Function("ASCII_CHAR", new[] { _fbSqlExpressionFactory.Convert(_fbSqlExpressionFactory.ApplyDefaultTypeMapping(arguments[1]), typeof(byte)) }, true, new[] { true }, typeof(string));

                return(_fbSqlExpressionFactory.GreaterThan(
                           _fbSqlExpressionFactory.Function(
                               "POSITION",
                               new[] { value, _fbSqlExpressionFactory.ApplyDefaultTypeMapping(arguments[0]) },
                               true,
                               new[] { true, true },
                               typeof(int)),
                           _fbSqlExpressionFactory.Constant(0)));
            }
            return(null);
        }
Esempio n. 20
0
        public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList <SqlExpression> arguments)
        {
            if (!MethodInfoDatePartMapping.TryGetValue(method, out var part))
            {
                return(null);
            }

            return(_fbSqlExpressionFactory.Function(
                       "DATEADD",
                       new[] { _fbSqlExpressionFactory.Fragment(part), arguments[0], instance },
                       instance.Type));
        }
        public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList <SqlExpression> arguments, IDiagnosticsLogger <DbLoggerCategory.Query> logger)
        {
            if (!method.Equals(StartsWithMethod))
            {
                return(null);
            }

            var patternExpression = arguments[0];

            var startsWithExpression = _fbSqlExpressionFactory.AndAlso(
                _fbSqlExpressionFactory.Like(
                    instance,
                    _fbSqlExpressionFactory.Add(patternExpression, _fbSqlExpressionFactory.Constant("%"))),
                _fbSqlExpressionFactory.Equal(
                    _fbSqlExpressionFactory.Function(
                        "LEFT",
                        new[] {
                instance,
                _fbSqlExpressionFactory.Function(
                    "CHAR_LENGTH",
                    new[] { patternExpression },
                    true,
                    new[] { true },
                    typeof(int))
            },
                        true,
                        new[] { true, true },
                        instance.Type),
                    patternExpression));

            return(patternExpression is SqlConstantExpression sqlConstantExpression
                                ? (string)sqlConstantExpression.Value == string.Empty
                                        ? (SqlExpression)_fbSqlExpressionFactory.Constant(true)
                                        : startsWithExpression
                                : _fbSqlExpressionFactory.OrElse(
                       startsWithExpression,
                       _fbSqlExpressionFactory.Equal(
                           patternExpression,
                           _fbSqlExpressionFactory.Constant(string.Empty))));
        }
	public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList<SqlExpression> arguments, IDiagnosticsLogger<DbLoggerCategory.Query> logger)
	{
		if (!method.Equals(MethodInfo))
			return null;

		var argument = _fbSqlExpressionFactory.ApplyDefaultTypeMapping(arguments[0]);
		return _fbSqlExpressionFactory.Function(
			"RIGHT",
			new[] { argument, _fbSqlExpressionFactory.Constant(1) },
			true,
			new[] { true, false },
			typeof(string));
	}
        public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList <SqlExpression> arguments, IDiagnosticsLogger <DbLoggerCategory.Query> logger)
        {
            if (!method.Equals(MethodInfo))
            {
                return(null);
            }

            var argument = arguments[0];

            return(_fbSqlExpressionFactory.Function(
                       "LEFT",
                       new[] { argument, _fbSqlExpressionFactory.Constant(1) },
                       true,
                       new[] { true, default },
Esempio n. 24
0
 public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList <SqlExpression> arguments)
 {
     if (SupportedMethodTranslations.TryGetValue(method, out var sqlFunctionName))
     {
         return(_fbSqlExpressionFactory.Function(sqlFunctionName, arguments, method.ReturnType));
     }
     if (TruncateMethodInfos.Contains(method))
     {
         return(_fbSqlExpressionFactory.Function(
                    "TRUNC",
                    new[] { arguments[0], _fbSqlExpressionFactory.Constant(0) },
                    method.ReturnType));
     }
     if (RoundMethodInfos.Contains(method))
     {
         return(_fbSqlExpressionFactory.Function(
                    "ROUND",
                    arguments.Count == 1
                                         ? new[] { arguments[0], _fbSqlExpressionFactory.Constant(0) }
                                         : new[] { arguments[0], arguments[1] },
                    method.ReturnType));
     }
     return(null);
 }
Esempio n. 25
0
 public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList <SqlExpression> arguments)
 {
     if (method.Name == nameof(ToString) && method.GetParameters().Length == 0)
     {
         var type = instance.Type.UnwrapNullableType();
         if (SupportedTypes.Contains(type))
         {
             return(_fbSqlExpressionFactory.Convert(instance, typeof(string)));
         }
         else if (type == typeof(Guid))
         {
             return(_fbSqlExpressionFactory.Function("UUID_TO_CHAR", new[] { instance }, typeof(string)));
         }
     }
     return(null);
 }
Esempio n. 26
0
        public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList <SqlExpression> arguments, IDiagnosticsLogger <DbLoggerCategory.Query> logger)
        {
            if (!method.Equals(IsNullOrWhiteSpaceMethod))
            {
                return(null);
            }

            var argument = arguments[0];

            return(_fbSqlExpressionFactory.OrElse(
                       _fbSqlExpressionFactory.IsNull(argument),
                       _fbSqlExpressionFactory.Equal(
                           _fbSqlExpressionFactory.Function("TRIM", new[] { argument }, true, new[] { true }, typeof(string)),
                           _fbSqlExpressionFactory.Constant(string.Empty))
                       ));
        }
 public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList <SqlExpression> arguments, IDiagnosticsLogger <DbLoggerCategory.Query> logger)
 {
     if (method.DeclaringType == typeof(string) && method.Name == nameof(string.IndexOf))
     {
         var args = new List <SqlExpression>();
         args.Add(_fbSqlExpressionFactory.ApplyDefaultTypeMapping(arguments[0]));
         args.Add(instance);
         foreach (var a in arguments.Skip(1))
         {
             args.Add(_fbSqlExpressionFactory.ApplyDefaultTypeMapping(a));
         }
         return(_fbSqlExpressionFactory.Subtract(
                    _fbSqlExpressionFactory.Function("POSITION", args, true, Enumerable.Repeat(true, args.Count), typeof(int)),
                    _fbSqlExpressionFactory.Constant(1)));
     }
     return(null);
 }
Esempio n. 28
0
    public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList <SqlExpression> arguments, IDiagnosticsLogger <DbLoggerCategory.Query> logger)
    {
        if (!method.Equals(ReplaceMethod))
        {
            return(null);
        }

        var args = new List <SqlExpression>();

        args.Add(instance);
        foreach (var a in arguments)
        {
            args.Add(_fbSqlExpressionFactory.ApplyDefaultTypeMapping(a));
        }
        return(_fbSqlExpressionFactory.ApplyDefaultTypeMapping(
                   _fbSqlExpressionFactory.Function("REPLACE", args, true, Enumerable.Repeat(true, args.Count), instance.Type)));
    }
    public SqlExpression Translate(SqlExpression instance, MemberInfo member, Type returnType, IDiagnosticsLogger <DbLoggerCategory.Query> logger)
    {
        if (!MemberMapping.TryGetValue(member, out var part))
        {
            return(null);
        }

        var result = (SqlExpression)_fbSqlExpressionFactory.SpacedFunction(
            "EXTRACT",
            new[] { _fbSqlExpressionFactory.Fragment(part), _fbSqlExpressionFactory.Fragment("FROM"), instance },
            true,
            new[] { false, false, true },
            typeof(int));

        if (part == SecondPart || part == MillisecondPart)
        {
            result = _fbSqlExpressionFactory.Function("TRUNC", new[] { result }, true, new[] { true }, typeof(int));
        }
        return(result);
    }