What is the Java equivalent of the Perl qq statement?

I have a very long line that includes many new lines (this is a really long SQL statement).

SQL is easier to read when I break it into new lines. But from time to time I need to copy the sql statement from the code to be inserted into the sql developer.

In Perl, I always liked the qq operator, which you can use instead 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 am uncomfortable breaking a line in chunks as follows:

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

and it's hard to copy an SQL statement from code. In the end, I need to remove all quotation marks and +

Is there a Java equivalent? Or is there a better trick for placing readable, copyable SQL statements in Java code?

+6
java operators string syntax perl
source share
5 answers

Multiline strings in Java

Is there a Java equivalent?

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


Or is there a better trick for readable, copied SQL statements in Java code?

Entering parameters into SQL queries using concatenation is not a good idea. There are classes and APIs to help you formulate queries.

Query Wrappers

Wrappers can improve readability, for example, in JDOQL (JDO), you can create a query 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 use simple JDBC, you can also watch PreparedStatement .

+5
source share

There is no equivalent. An alternative is to place SQL in the properties file and read in the properties file using the Properties object.

You still have a backslash, but not as much as all quotation marks.

 SELECT * \ FROM table_a a \ JOIN table_b b ON a.id = b.id \ 
+3
source share

I sometimes write a function like

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

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

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

I do not agree with the proposal to use the O / R structure.

0
source share

He does not exist right now. Multi-line support was discussed in Java 7, but it came out pretty early.

What am I doing

  • First I join all the lines, and then put them in quotation marks. If you are in an editor such as Eclipse, the cursor is inside the String, and you press Enter Eclipse, the IDE will split the string for you . Thus, you will get a bunch of "inconvenient" sequences "line1" + "line2" ....

  • I wrote a parser to scan addresses. During testing, I had the same problem, because address lines are inherently multiple lines. So for my test cases, I switched to Groovy because Groovy offers multi-line strings and much more: http://groovy.codehaus.org/Strings+and+GString .

0
source share

Here what I use works very well for me (I leave this 1st comment as-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 should have an ideal or another editor (does anyone know if Eclipse does this?), Which has a column selection mode before copying / pasting into your SQL tool.

0
source share

All Articles