开发者

What is the Java equivalent of Perl's qq operator?

开发者 https://www.devze.com 2023-01-09 11:09 出处:网络
I have a very long string which includes many new lines ( it\'s a really long SQL statement ). The SQL is easier to read when I break it up with newlines.But from time to time, I need to

I have a very long string which includes many new lines ( it's a really long SQL statement ).

The SQL is easier to read when I break it up with newlines. But from time to time, I need to copy the sql statement from code to paste into sql developer.

In Perl, I always loved the qq operator, 开发者_运维知识库which you can use in place of double quotes:

You use it something like this:

$myString = qq{       
                      SELECT * 
                      FROM table_a a
                      JOIN table_b b ON a.id = b.id ... etc
                };

Is there an equivalent in JAVA? I find it awkward to have to break up the string in chunks like this:

String myString = "    SELECT *  " + 
                  "    FROM table_a a " + 
                  "    JOIN table_b b ON a.id = b.id ... etc ";

and it's hard to copy the SQL statement from code. I end up having to remove all the quotes and +'s

Is there a Java equivalent? Or is there a better trick to putting readable, copy-able SQL statements in Java code?


Multi-line Strings in Java

Is there a Java equivalent?

At the time of writing I do not think so. However there is a proposal to include multi-line Strings in Java.


Or is there a better trick to putting readable, copy-able SQL statements in Java code?

Putting parameters into SQL queries through concatenation isn't a very good idea. There are classes and API to help you form queries.

"Query Wrappers"

Wrappers can enhance readability, for example in JDOQL (JDO), you can create a query by using setWhere(), setLimit(), setDistinct() etc.

Query q = pm.newQuery (...);
q.setWhere(...);
q.setRange (...);
q.setOrdering (...);
q.setLimit(...);
q.newParameter(...); // declare a query parameter

q.execute(34.5); // execute the SQL query with a parameter

For JPA, you can read JPQL.

if you are using plain JDBC, you might also have a look at PreparedStatement.


There's no equivalent. The alternative is to put the SQL in a property file and read in the property file using the Properties object.

You still have the backslashes, but not as much as all the quotes.

SELECT * \
FROM table_a a \
JOIN table_b b ON a.id = b.id \


I sometimes write a function like

public static String cat(String ... lines) { }

which concatenates the lines and adds newlines. If the strings are known at compile time, this is inefficient.

In the SQL case, I agree with Bakkal - use a wrapping library. Empire-db is great. http://incubator.apache.org/empire-db/

I disagree with the suggestion to use an O/R framework.


It doesn't exist right now. Multi line support was discussed in Java 7, but it kicked out quite early.

What I do

  1. I join all the lines first and then put them in quotes. If you cursor in an editor like Eclipse is inside the String and you hit the Enter key Eclipse the IDE will split the String for you. So you will get a bunch of "awkward" sequences "line1" + "line2" ... .

  2. I wrote a parser for scanning addresses. While testing I had the same problem because address lines are by nature several lines long. So I switched to Groovy for my test cases because Groovy offers multi line strings and much more: http://groovy.codehaus.org/Strings+and+GString.


Here's what I use - works pretty well for me (I leave that 1st comment as-is for others)

// Tip: UltraEdit column-copy mode (alt-c) gets the SQL without quotes
String sql = 
    "select                                          " + 
   //-- comment here
    "big-long-query goes here...                     " +   
    "last line                                       ".replaceAll("\\s+", " ");

So you have to have an ide or another editor (anyone know if Eclipse does this?) that has a column highlighting mode before you copy/paste into your SQL tool.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号