views:

144

answers:

4

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?

+2  A: 

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 \
Jeanne Boyarsky
+5  A: 

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.

Bakkal
Or use 2010 tools and use a object mapping framework
TheLQ
That *is* what specifications like JPA and JDO are for.
Bakkal
Thanks, I was hoping for something to make my life a bit easier. I use PreparedStatement now and replace the "?"s with parameters. I can always System.output the SQL statement if I need to copy it and paste into a Query browser
jeph perro
A: 

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.

Burleigh Bear
A: 

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.

Christian Ullenboom