在PostgreSql中使用SqlFunc.TrimStart发现的两个问题 返回

SqlSugar 沟通中
3 198
该叫什么 Kane 发布于2周前
悬赏:0 飞吻

c#代码,使用SqlSugar版本5.1.4.214

var ven = await db.Queryable<SapVendor>().Where(e => e.Mandt == sapEnt && e.Land1 == "CN" && SqlFunc.TrimStart(e.VenCode, "0") == "1000003").FirstAsync();

生成的Sql语句

SELECT "mandt","lifnr","land1","name1" FROM "ods_sap_vendor"   WHERE ((( "mandt" = N'300' ) AND ( "land1" = N'CN' )) AND (CASE WHEN LEFT("lifnr", 1) = N'0' THEN RIGHT("lifnr", LEN("lifnr") - 1) ELSE "lifnr" END = N'1000003' ))   LIMIT 1 offset 0


报错消息:SqlSugar.SqlSugarException: 42883: function len(character varying) does not exist


错误堆栈:

Npgsql.PostgresException (0x80004005): 42883: function len(character varying) does not exist

         at Npgsql.NpgsqlConnector.<ReadMessage>g__ReadMessageLong|201_0(NpgsqlConnector connector, Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isReadingPrependedMessage)

         at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)

         at Npgsql.NpgsqlDataReader.NextResult()

         at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)

         at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)

         at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior)

         at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior)

         at SqlSugar.AdoProvider.GetDataReader(String sql, SugarParameter[] parameters)

         at SqlSugar.QueryableProvider`1.GetData[TResult](KeyValuePair`2 sqlObj)

         at SqlSugar.QueryableProvider`1.<>c__DisplayClass251_0`1.<_ToListAsync>b__0()

         at Admin.NET.Core.SqlSugarCache.<>c__DisplayClass6_0`1.<GetOrCreate>b__0(String cacheKey) in E:\Code\Best.Sqs\Admin.NET\Admin.NET.Core\Cache\SqlSugarCache.cs:line 48

         at NewLife.Caching.MemoryCache.GetOrAdd[T](String key, Func`2 callback, Int32 expire)

         at Admin.NET.Core.Service.SysCacheService.GetOrAdd[T](String key, Func`2 callback, Int32 expire) in E:\Code\Best.Sqs\Admin.NET\Admin.NET.Core\Service\Cache\SysCacheService.cs:line 267

         at Admin.NET.Core.SqlSugarCache.GetOrCreate[V](String key, Func`1 create, Int32 cacheDurationInSeconds) in E:\Code\Best.Sqs\Admin.NET\Admin.NET.Core\Cache\SqlSugarCache.cs:line 46

         at SqlSugar.CacheSchemeMain.GetOrCreate[T](ICacheService cacheService, QueryBuilder queryBuilder, Func`1 getData, Int32 cacheDurationInSeconds, SqlSugarProvider context, String cacheKey)

         at SqlSugar.QueryableProvider`1._ToListAsync[TResult]()

         at SqlSugar.QueryableProvider`1.FirstAsync()

         at Best.Sqs.Application.SapVenPriceJob.ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken) in E:\Code\Best.Sqs\Admin.NET\Best.Sqs.Application\Job\SapVenPriceJob.cs:line 82

        Exception data:

          Severity: ERROR

          SqlState: 42883

          MessageText: function len(character varying) does not exist

          Hint: No function matches the given name and argument types. You might need to add explicit type casts.

          Position: 168

          File: parse_func.c

          Line: 629

          Routine: ParseFuncOrColumn

SqlSugar.SqlSugarException: 42883: function len(character varying) does not exis

问题一:PostgreSQL用 LENGTH() 而不是LEN()

问题二:为什么不是使用 ltrim() 方法处理?


感谢~

热忱回答3

  • Kane Kane VIP0
    2周前

    目前使用扩展方法处理

    new SqlFuncExternal()

    {

        UniqueMethodName = "LTrim",

        MethodValue = (expInfo, dbType, expContext) =>

        {

            if (dbType == SqlSugar.DbType.PostgreSQL)

            {

                return string.Format("ltrim({0},'{1}')", expInfo.Args[0].MemberName,expInfo.Args[1].MemberValue);

            }

            throw new Exception($"数据库类型 {dbType} 未实现 LTrim 函数转换。");

        }

    }


    public static string LTrim(string field, string value)

        {

            throw new NotSupportedException("Can only be used in expressions");

        }


    0 回复
  • SqlSugarCore5.1.4.215-preview12


    预览版本已修复

    0 回复
  • Kane Kane VIP0
    2周前

    非常感谢老大的关注

    0 回复