Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,39 +70,26 @@ public String perform() {
while ( tokens.hasMoreTokens() ) {
token = tokens.nextToken();

if ( "-".equals(token) && result.toString().endsWith("-") ) {
if ( "-".equals( token ) && result.toString().endsWith( "-" ) ) {
do {
result.append( token );
token = tokens.nextToken();
}
while ( !"\n".equals( token ) && tokens.hasMoreTokens() );
}

lcToken = token.toLowerCase(Locale.ROOT);
switch (lcToken) {

lcToken = token.toLowerCase( Locale.ROOT );
switch ( lcToken ) {
case "'":
case "`":
case "\"":
String t;
do {
t = tokens.nextToken();
token += t;
}
while ( !lcToken.equals( t ) && tokens.hasMoreTokens() );
lcToken = token;
appendUntilToken(lcToken);
misc();
break;
// SQL Server uses "[" and "]" to escape reserved words
// see SQLServerDialect.openQuote and SQLServerDialect.closeQuote
case "[":
String tt;
do {
tt = tokens.nextToken();
token += tt;
}
while ( !"]".equals( tt ) && tokens.hasMoreTokens() );
lcToken = token;
appendUntilToken("]");
misc();
break;

Expand Down Expand Up @@ -238,7 +225,7 @@ private void from() {
}

private void comma() {
if ( afterByOrSetOrFromOrSelect && inFunction==0 ) {
if ( afterByOrSetOrFromOrSelect && inFunction == 0 ) {
commaAfterByOrFromOrSelect();
}
// else if ( afterOn && inFunction==0 ) {
Expand Down Expand Up @@ -313,7 +300,7 @@ private void beginCase() {

private void misc() {
out();
if ( afterInsert && inFunction==0 ) {
if ( afterInsert && inFunction == 0 ) {
newline();
afterInsert = false;
}
Expand All @@ -329,7 +316,7 @@ private void white() {
}

private void updateOrInsertOrDelete() {
if ( indent>1 ) {
if ( indent > 1 ) {
//probably just the insert SQL function
out();
}
Expand Down Expand Up @@ -367,7 +354,7 @@ private void select() {

private void out() {
if ( result.charAt( result.length() - 1 ) == ',' ) {
result.append(" ");
result.append( " " );
}
result.append( token );
}
Expand All @@ -390,8 +377,8 @@ private void endNewClause() {
newline();
afterBeginBeforeEnd = false;
afterByOrSetOrFromOrSelect = "by".equals( lcToken )
|| "set".equals( lcToken )
|| "from".equals( lcToken );
|| "set".equals( lcToken )
|| "from".equals( lcToken );
}

private void beginNewClause() {
Expand Down Expand Up @@ -507,9 +494,21 @@ private static boolean isWhitespace(String token) {

private void newline() {
result.append( System.lineSeparator() )
.append( INDENT_STRING.repeat(indent) );
.append( INDENT_STRING.repeat( indent ) );
beginLine = true;
}

private void appendUntilToken(String stopToken) {
final StringBuilder sb = new StringBuilder( this.token );
String t;
do {
t = tokens.nextToken();
sb.append( t );
}
while ( !stopToken.equals( t ) && tokens.hasMoreTokens() );
this.token = sb.toString();
lcToken = token;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
* @author Steve Ebersole
*/
public class BasicFormatterTest extends BaseUnitTestCase {

@Test
public void testNoLoss() {
assertNoLoss( "insert into Address (city, state, zip, \"from\") values (?, ?, ?, 'insert value')" );
Expand All @@ -47,6 +46,30 @@ public void testNoLoss() {
"(select p.pid from Address where city = 'Boston') union (select p.pid from Address where city = 'Taipei')"
);
assertNoLoss( "select group0.[order] as order0 from [Group] group0 where group0.[order]=?1" );
assertNoLoss( """
INSERT INTO TEST_TABLE (JSON) VALUES
('{
"apiVersion": "2.0",
"data": {
"updated": "2010-01-07T19:58:42.949Z",
"totalItems": 800,
"startIndex": 1,
"itemsPerPage": 1,
"items": [
{
"id": "hYB0mn5zh2c",
"uploaded": "2007-06-05T22:07:03.000Z",
"updated": "2010-01-07T13:26:50.000Z",
"uploader": "GoogleDeveloperDay",
"category": "News",
"title": "Google Developers Day US - Maps API Introduction",
"description": "Google Maps API Introduction ..."
}
]
}
}')
"""
);
}

@Test
Expand Down