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

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

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

问题

  1. @Query(value = " with cte as (\n" +
  2. " select id, 1 as quantity\n" +
  3. " from product\n" +
  4. " where id = :productId\n" +
  5. " union all\n" +
  6. " select distinct combo_id as id,\n" +
  7. " quantity\n" +
  8. " from combo\n" +
  9. " where product_id = :productId),\n" +
  10. " result as (\n" +
  11. " select o.product_id, o.quantity * c.quantity as 'quantity'\n" +
  12. " from order_product o\n" +
  13. " inner join cte c\n" +
  14. " on o.product_id = c.id\n" +
  15. " )\n" +
  16. "select sum(quantity)\n" +
  17. "from result", nativeQuery = true)
  18. Object method(@Param("productId") String productId);
英文:
  1. @Query(value = " with cte as (\n" +
  2. " select id, 1 as quantity\n" +
  3. " from product\n" +
  4. " where id = :productId\n" +
  5. " union all\n" +
  6. " select distinct combo_id as id,\n" +
  7. " quantity\n" +
  8. " from combo\n" +
  9. " where product_id = :productId),\n" +
  10. " result as (\n" +
  11. " select o.product_id, o.quantity * c.quantity as 'quantity'\n" +
  12. " from order_product o\n" +
  13. " inner join cte c\n" +
  14. " on o.product_id = c.id\n" +
  15. " )\n" +
  16. "select sum(quantity)\n" +
  17. "from result", nativeQuery = true)
  18. 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:

  1. @Query(value = """
  2. with cte as (
  3. select id, 1 as quantity
  4. from product
  5. where id = :productId
  6. union all
  7. select distinct combo_id as id,
  8. quantity
  9. from combo
  10. where product_id = :productId),
  11. result as (
  12. select o.product_id, o.quantity * c.quantity as 'quantity'
  13. from order_product o
  14. inner join cte c
  15. on o.product_id = c.id
  16. )
  17. select sum(quantity)
  18. from result""", nativeQuery = true)
  19. 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:

  1. @Query(value = &quot;&quot;&quot;
  2. with cte as (
  3. select id, 1 as quantity
  4. from product
  5. where id = :productId
  6. union all
  7. select distinct combo_id as id,
  8. quantity
  9. from combo
  10. where product_id = :productId),
  11. result as (
  12. select o.product_id, o.quantity * c.quantity as &#39;quantity&#39;
  13. from order_product o
  14. inner join cte c
  15. on o.product_id = c.id
  16. )
  17. select sum(quantity)
  18. from result&quot;&quot;&quot;, nativeQuery = true)
  19. 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:

确定