【问题标题】:Does jOOQ support putting names on SQL statements?jOOQ 是否支持在 SQL 语句上添加名称?
【发布时间】:2022-01-12 00:02:53
【问题描述】:

有没有办法在 jOOQ 的 SQL 语句上“标记”或命名,所以当我查看 AWS RDS 的性能洞察时,我可以看到比语句的前 500 个字符更有意义的东西?

例如,Performance Insights 显示此查询对我的数据库造成了影响:

选择 "my_schema"."custs"."id", "my_schema"."custs"."other_id", "my_schema"."custs"."cid_id", "my_schema"."custs"."valid_since ", "my_schema"."custs"."valid_until", "my_schema"."custs"."address", "my_schema"."custs"."address_id_1", "my_schema"."pets"."id", "my_schema"."pets"."cst_id", "my_schema"."pets"."tag", "my_schema"."pets"."name", "my_schema"."pets"."description", "my_schema "."pets"."owner", "my_schema"."pets"."created_on", "my_schema"."pets"."created_by", "my_schema"."pets"."modified_on",

但是当它被切碎时,要知道是哪个 jOOQ 代码生成了它并不直接。

我希望看到这样的东西:

客户 - 宠物查找

或:

(客户 - 宠物查找)选择“my_schema”。“custs”。“id”,“my_schema”。“custs”。“other_id”,“my_schema”。“custs”。“cid_id”,“my_schema”。 "custs"."valid_since", "my_schema"."custs"."valid_until", "my_schema"."custs"."address", "my_schema"."custs"."address_id_1", "my_schema"."pets "."id", "my_schema"."pets"."cst_id", "my_schema"."pets"."tag", "my_schema"."pets"."name", "my_schema"."pets". “description”,“my_schema”。“pets”。“owner”,“my_schema”。“pets”。“created_on”,“my_schema”。“pets”。“created_by”,“my_schema”。“pets”。“modified_on ",

【问题讨论】:

    标签: amazon-rds jooq


    【解决方案1】:

    对于您想要实现的目标,至少有两种开箱即用的方法,它们都完全与供应商无关:

    1。使用“提示”

    jOOQ 支持使用 hint() 方法的 Oracle 样式提示,至少对于 SELECT 语句。像这样写:

    ctx.select(T.A, T.B)
       .hint("/* my tag */")
       .from(T)
       .where(...)
    

    这里的限制是提示的位置,它将在 SELECT 关键字之后。不确定这是否适用于您的 RDBMS。

    2。使用ExecuteListener

    您可以为您的Configuration 提供ExecuteListener,它会使用您需要添加的任何内容来修补您生成的SQL 字符串:

    class MyListener extends DefaultExecuteListener {
    
        // renderEnd() is called after the SQL string is generated, but
        // before the prepared statement is created, let alone executed
        @Override
        public void renderEnd​(ExecuteContext ctx) {
            if (mechanismToDetermineIfTaggingIsNeeded())
                ctx.sql("/* My tag */ " + ctx.sql());
        }
    }
    

    使用正则表达式,您可以将该标记放置在 SQL 字符串中的任何特定位置。

    【讨论】:

    • 谢谢!我会试一试并报告
    猜你喜欢
    • 1970-01-01
    • 2017-12-05
    • 2013-04-10
    • 2010-11-20
    • 1970-01-01
    • 2021-05-24
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多