String literals in Java

M

Mark Thornton

Harri said:
How do you copy multi-line SQL statement into Java sources then?

String sql = "select a,b,c, ... "+
" from MyTable "+
"where x=y";
Note the the concatenations are done by the compiler, not at run time.
If it is too big for this to be attractive, read the statement from a
resource file (which would usually be packaged into the applications JAR
file when delivered).
 
R

Roedy Green

But this DEBUG = false is in the Java sources, right? I don't want to
change sources for debug/release build.

You change sources DRASTICALLY if you use a preprocessor. Changing
the value of DEBUG in Java is a minor change to source, and it
triggers a massive recompile to include or exclude the material in the
if (debug) blocks.

I sometimes switch back and forth from release to debug by swapping
two different *.java files of release vs debug settings. That way I
can burn in a string stating which version of the compiler was used.

Another technique is to use Logging or asserts which has less drastic
ways of removing debug code.
 
R

Roedy Green

How do you copy multi-line SQL statement into Java sources then?

you use something like the Quoter Amanuensis. I use it all the time
for turning raw HTML into Java strings, regexes etc.

Or raw text into HTML or ...


It would not take much to add an SQL converter. Explain the
transformations you need, and I'll add that feature for you.


You just put your code into the clipboard, select the conversion you
want, and click CONVERT then paste the clipboard into your java
program.

It may be a bitch to proofread, but at least it is accurate.

see http://mindprod.com/quoter.html

The online version requires you to cut and paste. The Application
version works directly on the clipboard.
 
J

Joona I Palaste

That's how I do it in Visual Basic. :-(

So? What's that got to do with anything? Just because Visual Basic uses
some style doesn't mean it's inherently evil.
 
H

Harri Pesonen

Roedy said:
you use something like the Quoter Amanuensis. I use it all the time
for turning raw HTML into Java strings, regexes etc.

Or raw text into HTML or ...

It would not take much to add an SQL converter. Explain the
transformations you need, and I'll add that feature for you.

You just put your code into the clipboard, select the conversion you
want, and click CONVERT then paste the clipboard into your java
program.

It may be a bitch to proofread, but at least it is accurate.

see http://mindprod.com/quoter.html

The online version requires you to cut and paste. The Application
version works directly on the clipboard.

Interesting. What I want is to have *verbatim* strings. So the
transformation should convert a string, whatever it contains (line
feeds, tab characters), into a valid Java string (containing several
lines and + operations if necessary). Of course it would be nice if it
was formatted to resemble the original string as much as possible.

So if I have the following code in the future Java syntax that I suggested:

String SQL = @"SELECT
A,
B,
C
FROM D
WHERE C LIKE 'A%'
AND C IS NOT NULL"@;

This should be translated to the following in the current Java:

String SQL = "SELECT\n" +
"\tA,\n" +
"\tB,\n" +
"\tC\n" +
"FROM\tD\n" +
"WHERE\tC LIKE 'A%'\n" +
"AND\tC IS NOT NULL";

It is ugly but this is the best that Java can do. Perhaps I should write
such a tool myself. Of course the tool should convert the string from
encoded format back to verbatim format for editing...

Harri
 
H

Harri Pesonen

Joona said:
So? What's that got to do with anything? Just because Visual Basic uses
some style doesn't mean it's inherently evil.

I am just disappointed that there has been no evolution whatsoever.

Harri
 
H

Harri Pesonen

Harri said:
Interesting. What I want is to have *verbatim* strings. So the
transformation should convert a string, whatever it contains (line
feeds, tab characters), into a valid Java string (containing several
lines and + operations if necessary). Of course it would be nice if it
was formatted to resemble the original string as much as possible.

So if I have the following code in the future Java syntax that I suggested:

String SQL = @"SELECT
A,
B,
C
FROM D
WHERE C LIKE 'A%'
AND C IS NOT NULL"@;

This should be translated to the following in the current Java:

String SQL = "SELECT\n" +
"\tA,\n" +
"\tB,\n" +
"\tC\n" +
"FROM\tD\n" +
"WHERE\tC LIKE 'A%'\n" +
"AND\tC IS NOT NULL";

It is ugly but this is the best that Java can do. Perhaps I should write
such a tool myself. Of course the tool should convert the string from
encoded format back to verbatim format for editing...

I tried it and it did the following:

"SELECT\n"
+ " A,\n"
+ " B,\n"
+ " C\n"
+ "FROM D\n"
+ "WHERE C LIKE \'A%\'\n"
+ "AND C IS NOT NULL"

Not bad. I didn't know that Java can have embedded tab characters. But
why ' was escaped?

Harri
 
R

Roedy Green

I poked this into Quoter and selected "to Java String literal" and
here is what it produced:

String SQL = @"SELECT
A,
B,
C
FROM D
WHERE C LIKE 'A%'
AND C IS NOT NULL"@;

"\tString SQL = @\"SELECT\n"
+ "\tA,\n"
+ "\tB,\n"
+ "\tC\n"
+ "FROM\tD\n"
+ "WHERE\tC LIKE \'A%\'\n"
+ "AND\tC IS NOT NULL\"@;\n"

if you let the editor expand tabs first, you get the more readable:

" String SQL = @\"SELECT\n"
+ " A,\n"
+ " B,\n"
+ " C\n"
+ "FROM D\n"
+ "WHERE C LIKE \'A%\'\n"
+ "AND C IS NOT NULL\"@;\n"


See http://mindprod.com/quoter.html

The application version does not require you to copy paste. It works
directly on the clipboard.
 
R

Roedy Green

Not bad. I didn't know that Java can have embedded tab characters. But
why ' was escaped?

so that you could use this same code for safely creating '\'' just by
changing the outside bracketers.

My other reason is it is more explicit that ' is being used literally
not in some screwy quoting scheme the programmer does not understand.
The rules are then the same for "..." and '..'
 
R

Roedy Green

I tried it and it did the following:

"SELECT\n"
+ " A,\n"
+ " B,\n"
+ " C\n"
+ "FROM D\n"
+ "WHERE C LIKE \'A%\'\n"
+ "AND C IS NOT NULL"

Not bad. I didn't know that Java can have embedded tab characters. But
why ' was escaped?

Try that again. They should come out as \t in version 2.8.

It a bit dangerous to have embedded tabs in strings. They are too
easily lost, converted to spaces, or understood as spaces.
 
H

Harri Pesonen

Roedy said:
so that you could use this same code for safely creating '\'' just by
changing the outside bracketers.

My other reason is it is more explicit that ' is being used literally
not in some screwy quoting scheme the programmer does not understand.
The rules are then the same for "..." and '..'

Encoding ' makes it unnecessary ugly because ' is very common in SQL
statements.

But the idea is great, I might write a similar application to my
tastes... it should be smart enough to do the conversion to both directions.

Harri
 
R

Roedy Green

Encoding ' makes it unnecessary ugly because ' is very common in SQL
statements.

But the idea is great, I might write a similar application to my
tastes... it should be smart enough to do the conversion to both directions.

the source code is posted. All you would have to do is change one
item in the table.

It is just a lookup table of how to encode each letter.

I suppose that is good enough justification for changing it.
 
J

Joona I Palaste

I am just disappointed that there has been no evolution whatsoever.

"Evolution"? Why does it need to "evolve"? I think Java's way of doing
it is perfectly fine. What don't you like about it? The spelling of the
+ operator? Would you want || or ' or a space instead? Or do you want
to do it like C does, with an explicit strcat() function?
 
R

Roedy Green

"Evolution"? Why does it need to "evolve"? I think Java's way of doing
it is perfectly fine. What don't you like about it? The spelling of the
+ operator? Would you want || or ' or a space instead? Or do you want
to do it like C does, with an explicit strcat() function?

see http://mindprod.com/projects/scid.html

Someday what we are doing will look as backward as punch cards.
 
H

Harri Pesonen

Roedy said:
Version 2.9 just posted works the way you want.

Thanks! Downloaded the source as well. I modified run.bat to be:

@echo off
@echo run Quoter as an application
rem C:
rem CD c:\com\mindprod\quoter

java.exe -jar quoter.jar

rem -30-

run.bat contained incorrect line feeds for Windows (only \n not \r\n),
it did not display correctly in Notepad.

Harri
 
R

Roedy Green

@echo off
@echo run Quoter as an application
rem C:
rem CD c:\com\mindprod\quoter

java.exe -jar quoter.jar

rem -30-

I use SlickEdit which is happy with \n. I have gradually flipped over
to \n as the line terminator in Windows because it is more compact and
easier to create using embedded \n. I also figured it was more
platform independent.

All the aux files for each app is automatically generated. I could
teach the save method to expand the \ns.
 
J

Jim Cochrane

see http://mindprod.com/projects/scid.html

Someday what we are doing will look as backward as punch cards.

I just skimmed over this, but it looks like there are some very good,
useful ideas there. One I particularly like is:

Show get/set method invocations as if they were direct access to an
associated property variable, similar to Delphi or Eiffel. This simplifies
the syntax. Instead of seeing:

setFudge(getFudge()+1);

you would see:

fudge++;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,001
Messages
2,570,250
Members
46,848
Latest member
Graciela Mitchell

Latest Threads

Top