Skip to content

Commit f9a6a7a

Browse files
feat: define or suppress the statement terminator
- fixes 16 Signed-off-by: Andreas Reichel <andreas@manticore-projects.com>
1 parent 9c7d661 commit f9a6a7a

6 files changed

Lines changed: 84 additions & 17 deletions

File tree

src/main/java/com/manticore/jsqlformatter/Comment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Manticore Projects JSQLFormatter is a SQL Beautifying and Formatting Software.
3-
* Copyright (C) 2023 Andreas Reichel <andreas@manticore-projects.com>
3+
* Copyright (C) 2024 Andreas Reichel <andreas@manticore-projects.com>
44
*
55
* This program is free software: you can redistribute it and/or modify
66
* it under the terms of the GNU Affero General Public License as published

src/main/java/com/manticore/jsqlformatter/CommentMap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Manticore Projects JSQLFormatter is a SQL Beautifying and Formatting Software.
3-
* Copyright (C) 2023 Andreas Reichel <andreas@manticore-projects.com>
3+
* Copyright (C) 2024 Andreas Reichel <andreas@manticore-projects.com>
44
*
55
* This program is free software: you can redistribute it and/or modify
66
* it under the terms of the GNU Affero General Public License as published

src/main/java/com/manticore/jsqlformatter/JSQLFormatter.java

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Manticore Projects JSQLFormatter is a SQL Beautifying and Formatting Software.
3-
* Copyright (C) 2023 Andreas Reichel <andreas@manticore-projects.com>
3+
* Copyright (C) 2024 Andreas Reichel <andreas@manticore-projects.com>
44
*
55
* This program is free software: you can redistribute it and/or modify
66
* it under the terms of the GNU Affero General Public License as published
@@ -169,6 +169,7 @@ public class JSQLFormatter {
169169
private static Spelling objectSpelling = Spelling.LOWER;
170170
private static OutputFormat outputFormat = OutputFormat.PLAIN;
171171
private static ShowLineNumbers showLineNumbers = ShowLineNumbers.NO;
172+
private static StatementTerminator statementTerminator = StatementTerminator.SEMICOLON;
172173

173174
private static BackSlashQuoting backSlashQuoting = BackSlashQuoting.NO;
174175
private static int indentWidth = 4;
@@ -191,6 +192,14 @@ public static void setBackSlashQuoting(BackSlashQuoting backSlashQuoting) {
191192
JSQLFormatter.backSlashQuoting = backSlashQuoting;
192193
}
193194

195+
public static StatementTerminator getStatementTerminator() {
196+
return statementTerminator;
197+
}
198+
199+
public static void setStatementTerminator(StatementTerminator statementTerminator) {
200+
JSQLFormatter.statementTerminator = statementTerminator;
201+
}
202+
194203
public static Separation getSeparation() {
195204
return separation;
196205
}
@@ -940,7 +949,21 @@ public static String format(String sqlStr, String... options) throws Exception {
940949
"The " + statement.getClass().getName() + " Statement is not supported yet.");
941950
}
942951
}
943-
appendNormalizedLineBreak(statementBuilder).append(";\n");
952+
953+
switch (statementTerminator) {
954+
case SEMICOLON:
955+
appendNormalizedLineBreak(statementBuilder).append(";\n");
956+
break;
957+
case NONE:
958+
appendNormalizedLineBreak(statementBuilder).append("\n\n");
959+
break;
960+
case GO:
961+
appendNormalizedLineBreak(statementBuilder).append("GO\n");
962+
break;
963+
case BACKSLASH:
964+
appendNormalizedLineBreak(statementBuilder).append("\\\n");
965+
break;
966+
}
944967

945968
builder.append(commentMap.isEmpty() ? statementBuilder
946969
: commentMap.insertComments(statementBuilder, outputFormat));
@@ -1076,6 +1099,13 @@ public static void applyFormattingOptions(String[] options) {
10761099
} catch (Exception ex) {
10771100
LOGGER.log(Level.WARNING, "Formatting Option {0} does not support {1} ", o);
10781101
}
1102+
} else if (key.equalsIgnoreCase(FormattingOption.STATEMENT_TERMINATOR.toString())) {
1103+
try {
1104+
statementTerminator = StatementTerminator.valueOf(value.toUpperCase());
1105+
} catch (Exception ex) {
1106+
LOGGER.log(Level.WARNING, "Formatting Option {0} does not support {1} ", o);
1107+
}
1108+
10791109
} else {
10801110
LOGGER.log(Level.WARNING, "Unknown Formatting Option {0} = {1} ", o);
10811111
}
@@ -3706,12 +3736,32 @@ public enum BackSlashQuoting {
37063736
YES, NO
37073737
}
37083738

3739+
public enum StatementTerminator {
3740+
SEMICOLON, NONE, GO, BACKSLASH
3741+
}
3742+
37093743
public enum FormattingOption {
3710-
SQUARE_BRACKET_QUOTATION("squareBracketQuotation"), BACKSLASH_QUOTING(
3711-
"backSlashQuoting"), OUTPUT_FORMAT("outputFormat"), KEYWORD_SPELLING(
3712-
"keywordSpelling"), FUNCTION_SPELLING("functionSpelling"), OBJECT_SPELLING(
3713-
"objectSpelling"), SEPARATION("separation"), INDENT_WIDTH(
3714-
"indentWidth"), SHOW_LINE_NUMBERS("showLineNumbers");
3744+
SQUARE_BRACKET_QUOTATION("squareBracketQuotation")
3745+
3746+
, BACKSLASH_QUOTING("backSlashQuoting")
3747+
3748+
, OUTPUT_FORMAT("outputFormat")
3749+
3750+
, KEYWORD_SPELLING("keywordSpelling")
3751+
3752+
, FUNCTION_SPELLING("functionSpelling")
3753+
3754+
, OBJECT_SPELLING("objectSpelling")
3755+
3756+
, SEPARATION("separation")
3757+
3758+
, INDENT_WIDTH("indentWidth")
3759+
3760+
, SHOW_LINE_NUMBERS("showLineNumbers")
3761+
3762+
, STATEMENT_TERMINATOR("statementTerminator")
3763+
3764+
;
37153765

37163766
public final String optionName;
37173767

src/site/sphinx/usage.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ Static Binaries
1010

1111
.. code:: Bash
1212
13-
java -jar JSQLFormatterCLI.jar [-i <arg>] [-o <arg>] [-f <arg> | --ansi | --html] [-t <arg> | -2 | -8] [--keywordSpelling <arg>] [--functionSpelling <arg>] [--objectSpelling <arg>] [--separation <arg>] [--squareBracketQuotation <arg>] --squareBracketQuotation <arg>
13+
java -jar JSQLFormatterCLI.jar [-i <arg>] [-o <arg>] [-f <arg> | --ansi | --html] [-t <arg> | -2 | -8] [--keywordSpelling <arg>] [--functionSpelling <arg>] [--objectSpelling <arg>] [--separation <arg>] [--squareBracketQuotation <arg>] [--statementTerminator <arg>]
1414
1515
.. tab:: Linux Shell
1616

1717
.. code:: Bash
1818
19-
./JSQLFormatterCLI [-i <arg>] [-o <arg>] [-f <arg> | --ansi | --html] [-t <arg> | -2 | -8] [--keywordSpelling <arg>] [--functionSpelling <arg>] [--objectSpelling <arg>] [--separation <arg>] [--squareBracketQuotation <arg>] --squareBracketQuotation <arg>
19+
./JSQLFormatterCLI [-i <arg>] [-o <arg>] [-f <arg> | --ansi | --html] [-t <arg> | -2 | -8] [--keywordSpelling <arg>] [--functionSpelling <arg>] [--objectSpelling <arg>] [--separation <arg>] [--squareBracketQuotation <arg>] [--statementTerminator <arg>]
2020
2121
.. tab:: Windows Power Shell
2222

2323
.. code:: Bash
2424
25-
JSQLFormatterCLI.exe [-i <arg>] [-o <arg>] [-f <arg> | --ansi | --html] [-t <arg> | -2 | -8] [--keywordSpelling <arg>] [--functionSpelling <arg>] [--objectSpelling <arg>] [--separation <arg>] [--squareBracketQuotation <arg>] --squareBracketQuotation <arg>
25+
JSQLFormatterCLI.exe [-i <arg>] [-o <arg>] [-f <arg> | --ansi | --html] [-t <arg> | -2 | -8] [--keywordSpelling <arg>] [--functionSpelling <arg>] [--objectSpelling <arg>] [--separation <arg>] [--squareBracketQuotation <arg>] [--statementTerminator <arg>]
2626
2727
..........................
2828
Command Line Options (CLI)
@@ -39,7 +39,8 @@ Command Line Options (CLI)
3939
--objectSpelling <arg> Spelling of object names. [UPPER* LOWER CAMEL KEEP]
4040
--functionSpelling <arg> Spelling of function names. [UPPER* LOWER CAMEL KEEP]
4141
--separation <arg> Position of the field separator. [BEFORE* AFTER]
42-
--squareBracketQuotation <arg> Interpret Square Brackets "[]" as quotes instead of arrays. [AUTO* YES NO]
42+
--squareBracketQuotation <arg> Interpret Square Brackets "[]" as quotes instead of arrays. [AUTO* YES NO]
43+
--statementTerminator <arg> Set the statement terminator. [SEMICOLON* NONE GO BACKSLASH]
4344

4445
.. note::
4546

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.manticore.jsqlformatter;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class FormatTest {
7+
@Test
8+
void testStatementTerminator() throws Exception {
9+
String provided = "Select 1";
10+
String expected = "SELECT 1";
11+
12+
String result = JSQLFormatter.format(provided, "statementTerminator=NONE");
13+
Assertions.assertEquals(expected, result);
14+
}
15+
}

src/test/java/com/manticore/jsqlformatter/ParserTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
2525
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
2626
import net.sf.jsqlparser.statement.Statement;
27+
import net.sf.jsqlparser.statement.StatementVisitor;
2728
import net.sf.jsqlparser.statement.StatementVisitorAdapter;
2829
import net.sf.jsqlparser.statement.select.PlainSelect;
2930
import net.sf.jsqlparser.statement.select.Select;
@@ -65,7 +66,7 @@ public void testExpressionVisitor() throws JSQLParserException {
6566
"SELECT * FROM test WHERE aaa = 1 or bbb = 2 and case when ccc = 3 then 4 else 5 end = 6";
6667
Statement statement = CCJSqlParserUtil.parse(sqlStr);
6768

68-
ExpressionVisitorAdapter expressionVisitorAdapter = new ExpressionVisitorAdapter() {
69+
ExpressionVisitorAdapter<?> expressionVisitorAdapter = new ExpressionVisitorAdapter<>() {
6970
@Override
7071
public void visit(OrExpression orExpression) {
7172
super.visit(orExpression);
@@ -79,17 +80,17 @@ public void visit(AndExpression andExpression) {
7980
}
8081
};
8182

82-
SelectVisitorAdapter selectVisitorAdapter = new SelectVisitorAdapter() {
83+
SelectVisitorAdapter<?> selectVisitorAdapter = new SelectVisitorAdapter<>() {
8384
@Override
8485
public void visit(PlainSelect plainSelect) {
8586
plainSelect.getWhere().accept(expressionVisitorAdapter);
8687
}
8788
};
8889

89-
StatementVisitorAdapter statementVisitorAdapter = new StatementVisitorAdapter() {
90+
StatementVisitorAdapter<?> statementVisitorAdapter = new StatementVisitorAdapter<>() {
9091
@Override
9192
public void visit(Select select) {
92-
select.accept(selectVisitorAdapter);
93+
select.accept((StatementVisitor<?>) selectVisitorAdapter);
9394
}
9495
};
9596

0 commit comments

Comments
 (0)