如何从IntelliJ中的SQL格式中获取纯字符串值?

huangapple go评论66阅读模式
英文:

How to get pure String value from SQL format in IntelliJ?

问题

@Query(value = " with cte as (\n" +
            "    select id, 1 as quantity\n" +
            "    from product\n" +
            "    where id = :productId\n" +
            "    union all\n" +
            "    select distinct combo_id as id,\n" +
            "                    quantity\n" +
            "    from combo\n" +
            "    where product_id = :productId),\n" +
            "     result as (\n" +
            "         select o.product_id, o.quantity * c.quantity as 'quantity'\n" +
            "         from order_product o\n" +
            "                  inner join cte c\n" +
            "                             on o.product_id = c.id\n" +
            "     )\n" +
            "select sum(quantity)\n" +
            "from result", nativeQuery = true)
Object method(@Param("productId") String productId);
英文:
    @Query(value = " with cte as (\n" +
            "    select id, 1 as quantity\n" +
            "    from product\n" +
            "    where id = :productId\n" +
            "    union all\n" +
            "    select distinct combo_id as id,\n" +
            "                    quantity\n" +
            "    from combo\n" +
            "    where product_id = :productId),\n" +
            "     result as (\n" +
            "         select o.product_id, o.quantity * c.quantity as 'quantity'\n" +
            "         from order_product o\n" +
            "                  inner join cte c\n" +
            "                             on o.product_id = c.id\n" +
            "     )\n" +
            "select sum(quantity)\n" +
            "from result", nativeQuery = true)
    Object method(@Param("productId") String productId);

I am using Spring Jpa to write the SQL query, the IntelliJ supports me to reformat SQL easy to read.
However, when I try to copy this SQL to run in the database, it has a lot of redundant characters I need to remove such as + " \n. How can I do for copying only the value of this query?

答案1

得分: 0

  1. 将一个 public static void main() 方法添加到您当前的类中。
  2. 将查询字符串剪切并粘贴为 String value = " with cte as ..."
  3. main() 方法中添加一个 println()

然后运行您的 main() 方法,您应该能够将输出剪切并粘贴到您的 SQL 工具中。

英文:

You may do the following:

  1. Add a public static void main() method to your current class.
  2. Cut and paste the query string as String value = " with cte as ..."
  3. Add a println() in the main() method

Then run your main() method and you should be able to cut and paste that output into your SQL tool.

答案2

得分: 0

Type Alt+Enter on the query string and invoke Copy string concatenation text to the clipboard.

Also if you are working on Java 15 or higher, consider converting the string concatenation to a Text Block. (IntelliJ IDEA has a "Text block can be used" inspection for that). Text blocks are easier to copy and paste and easier to read as well:

    @Query(value = """
         with cte as (
            select id, 1 as quantity
            from product
            where id = :productId
            union all
            select distinct combo_id as id,
                            quantity
            from combo
            where product_id = :productId),
             result as (
                 select o.product_id, o.quantity * c.quantity as 'quantity'
                 from order_product o
                          inner join cte c
                                     on o.product_id = c.id
             )
        select sum(quantity)
        from result""", nativeQuery = true)
    Object method(@Param("productId") String productId);
英文:

Type <kbd>Alt</kbd>+<kbd>Enter</kbd> on the query string and invoke Copy string concatenation text to the clipboard.

Also if you are working on Java 15 or higher, consider converting the string concatenation to a Text Block. (IntelliJ IDEA has a "Text block can be used" inspection for that). Text blocks are easier to copy and paste and easier to read as well:

    @Query(value = &quot;&quot;&quot;
         with cte as (
            select id, 1 as quantity
            from product
            where id = :productId
            union all
            select distinct combo_id as id,
                            quantity
            from combo
            where product_id = :productId),
             result as (
                 select o.product_id, o.quantity * c.quantity as &#39;quantity&#39;
                 from order_product o
                          inner join cte c
                                     on o.product_id = c.id
             )
        select sum(quantity)
        from result&quot;&quot;&quot;, nativeQuery = true)
    Object method(@Param(&quot;productId&quot;) String productId);

huangapple
  • 本文由 发表于 2023年6月29日 12:23:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578033.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定