Skip to content

Commit f9bfc20

Browse files
author
Vincent Potucek
committed
fix SQLTokenizedFormatter
1 parent 584bdbc commit f9bfc20

File tree

2 files changed

+15
-29
lines changed

2 files changed

+15
-29
lines changed

lib/src/main/java/com/diffplug/spotless/sql/dbeaver/SQLTokenizedFormatter.java

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,38 +38,24 @@ public class SQLTokenizedFormatter {
3838

3939
private static final String[] JOIN_BEGIN = {"LEFT", "RIGHT", "INNER", "OUTER", "JOIN"};
4040
private static final SQLDialect sqlDialect = SQLDialect.INSTANCE;
41-
private DBeaverSQLFormatterConfiguration formatterCfg;
42-
private List<Boolean> functionBracket = new ArrayList<>();
43-
private List<String> statementDelimiters = new ArrayList<>(2);
41+
private final DBeaverSQLFormatterConfiguration formatterCfg;
42+
private final List<Boolean> functionBracket = new ArrayList<>();
43+
private final List<String> statementDelimiters = new ArrayList<>(2);
4444

4545
public SQLTokenizedFormatter(DBeaverSQLFormatterConfiguration formatterCfg) {
4646
this.formatterCfg = formatterCfg;
4747
}
4848

4949
public String format(final String argSql) {
50-
5150
statementDelimiters.add(formatterCfg.getStatementDelimiter());
52-
SQLTokensParser fParser = new SQLTokensParser();
53-
5451
functionBracket.clear();
55-
56-
boolean isSqlEndsWithNewLine = false;
57-
if (argSql.endsWith("\n")) {
58-
isSqlEndsWithNewLine = true;
59-
}
60-
61-
List<FormatterToken> list = fParser.parse(argSql);
62-
list = format(list);
63-
6452
StringBuilder after = new StringBuilder(argSql.length() + 20);
65-
for (FormatterToken token : list) {
53+
for (FormatterToken token : format(new SQLTokensParser().parse(argSql))) {
6654
after.append(token.getString());
6755
}
68-
69-
if (isSqlEndsWithNewLine) {
56+
if (argSql.endsWith("\n")) {
7057
after.append(getDefaultLineSeparator());
7158
}
72-
7359
return after.toString();
7460
}
7561

@@ -351,7 +337,7 @@ private boolean isJoinStart(List<FormatterToken> argList, int index) {
351337
// And we must be in the beginning of sequence
352338

353339
// check current token
354-
if (!contains(JOIN_BEGIN, argList.get(index).getString())) {
340+
if (!contains(argList.get(index).getString())) {
355341
return false;
356342
}
357343
// check previous token
@@ -360,7 +346,7 @@ private boolean isJoinStart(List<FormatterToken> argList, int index) {
360346
if (token.getType() == TokenType.SPACE) {
361347
continue;
362348
}
363-
if (contains(JOIN_BEGIN, token.getString())) {
349+
if (contains(token.getString())) {
364350
// It is not the begin of sequence
365351
return false;
366352
} else {
@@ -376,7 +362,7 @@ private boolean isJoinStart(List<FormatterToken> argList, int index) {
376362
if (token.getString().equals("JOIN")) {
377363
return true;
378364
}
379-
if (!contains(JOIN_BEGIN, token.getString())) {
365+
if (!contains(token.getString())) {
380366
// It is not the begin of sequence
381367
return false;
382368
}
@@ -403,7 +389,7 @@ private int insertReturnAndIndent(final List<FormatterToken> argList, final int
403389
final FormatterToken token = argList.get(argIndex);
404390
final FormatterToken prevToken = argList.get(argIndex - 1);
405391
if (token.getType() == TokenType.COMMENT &&
406-
isCommentLine(sqlDialect, token.getString()) &&
392+
isCommentLine(token.getString()) &&
407393
prevToken.getType() != TokenType.END) {
408394
s.setCharAt(0, ' ');
409395
s.setLength(1);
@@ -444,19 +430,19 @@ private int insertReturnAndIndent(final List<FormatterToken> argList, final int
444430
}
445431
}
446432

447-
private static boolean isCommentLine(SQLDialect dialect, String line) {
448-
for (String slc : dialect.getSingleLineComments()) {
433+
private static boolean isCommentLine(String line) {
434+
for (String slc : SQLTokenizedFormatter.sqlDialect.getSingleLineComments()) {
449435
if (line.startsWith(slc)) {
450436
return true;
451437
}
452438
}
453439
return false;
454440
}
455441

456-
private static <OBJECT_TYPE> boolean contains(OBJECT_TYPE[] array, OBJECT_TYPE value) {
457-
if (array == null || array.length == 0)
442+
private static <OBJECT_TYPE> boolean contains(OBJECT_TYPE value) {
443+
if (SQLTokenizedFormatter.JOIN_BEGIN == null)
458444
return false;
459-
for (OBJECT_TYPE anArray : array) {
445+
for (OBJECT_TYPE anArray : (OBJECT_TYPE[]) SQLTokenizedFormatter.JOIN_BEGIN) {
460446
if (Objects.equals(value, anArray))
461447
return true;
462448
}

lib/src/main/java/com/diffplug/spotless/sql/dbeaver/SQLTokensParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ List<FormatterToken> parse(final String argSql) {
292292
}
293293

294294
private static boolean contains(char[] array, char value) {
295-
if (array == null || array.length == 0)
295+
if (array == null)
296296
return false;
297297
for (char aChar : array) {
298298
if (aChar == value)

0 commit comments

Comments
 (0)