public virtual SqlExpression? Translate()

in EFCore/src/Query/Internal/MySQLDateTimeMemberTranslator.cs [61:160]


    public virtual SqlExpression? Translate(
      SqlExpression? instance,
      MemberInfo member,
      Type returnType,
      IDiagnosticsLogger<DbLoggerCategory.Query> logger)
    {
      var declaringType = member.DeclaringType;

      if (declaringType == typeof(DateTime)
          || declaringType == typeof(DateTimeOffset)
          || declaringType == typeof(DateOnly)
          || declaringType == typeof(TimeOnly))
      {
        var memberName = member.Name;

        if (_datePartMapping.TryGetValue(memberName, out var datePart))
        {
          var extract = _sqlExpressionFactory.Function(
            "EXTRACT",
            new[]
            {
        _sqlExpressionFactory.ComplexFunctionArgument(
          new [] {
          _sqlExpressionFactory.Fragment($"{datePart.Part} FROM"),
          instance!
          },
          " ",
          typeof(string))
            },
            nullable: true,
            argumentsPropagateNullability: TrueArrays[1],
            returnType);

          if (datePart.Divisor != 1)
          {
            return _sqlExpressionFactory.MySqlIntegerDivide(
              extract,
              _sqlExpressionFactory.Constant(datePart.Divisor));
          }

          return extract;
        }

        switch (memberName)
        {
          case nameof(DateTime.DayOfYear):
            return _sqlExpressionFactory.NullableFunction(
              "DAYOFYEAR",
              new[] { instance! },
              returnType,
              false);

          case nameof(DateTime.Date):
            return _sqlExpressionFactory.NullableFunction(
              "CONVERT",
              new[]{
                instance!,
                _sqlExpressionFactory.Fragment("date")
              },
              returnType,
              false);

          case nameof(DateTime.TimeOfDay):
            return _sqlExpressionFactory.Convert(instance!, returnType);

          case nameof(DateTime.Now):
            return _sqlExpressionFactory.NonNullableFunction(
              declaringType == typeof(DateTimeOffset)
              ? "UTC_TIMESTAMP"
              : "CURRENT_TIMESTAMP",
              Array.Empty<SqlExpression>(),
              returnType);

          case nameof(DateTime.UtcNow):
            return _sqlExpressionFactory.NonNullableFunction(
              "UTC_TIMESTAMP",
              Array.Empty<SqlExpression>(),
              returnType);

          case nameof(DateTime.Today):
            return _sqlExpressionFactory.NonNullableFunction(
              declaringType == typeof(DateTimeOffset)
              ? "UTC_DATE"
              : "CURDATE",
              Array.Empty<SqlExpression>(),
              returnType);

          case nameof(DateTime.DayOfWeek):
            return _sqlExpressionFactory.Subtract(
              _sqlExpressionFactory.NullableFunction(
                "DAYOFWEEK",
                new[] { instance! },
                returnType,
                false),
              _sqlExpressionFactory.Constant(1));
        }
      }

      return null;
    }