Skip to content

Commit

Permalink
feat: define or suppress the statement terminator
Browse files Browse the repository at this point in the history
- fixes 16

Signed-off-by: Andreas Reichel <andreas@manticore-projects.com>
  • Loading branch information
manticore-projects committed Jun 27, 2024
1 parent 9c7d661 commit f9a6a7a
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/manticore/jsqlformatter/Comment.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Manticore Projects JSQLFormatter is a SQL Beautifying and Formatting Software.
* Copyright (C) 2023 Andreas Reichel <andreas@manticore-projects.com>
* Copyright (C) 2024 Andreas Reichel <andreas@manticore-projects.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/manticore/jsqlformatter/CommentMap.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Manticore Projects JSQLFormatter is a SQL Beautifying and Formatting Software.
* Copyright (C) 2023 Andreas Reichel <andreas@manticore-projects.com>
* Copyright (C) 2024 Andreas Reichel <andreas@manticore-projects.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
Expand Down
64 changes: 57 additions & 7 deletions src/main/java/com/manticore/jsqlformatter/JSQLFormatter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Manticore Projects JSQLFormatter is a SQL Beautifying and Formatting Software.
* Copyright (C) 2023 Andreas Reichel <andreas@manticore-projects.com>
* Copyright (C) 2024 Andreas Reichel <andreas@manticore-projects.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
Expand Down Expand Up @@ -169,6 +169,7 @@ public class JSQLFormatter {
private static Spelling objectSpelling = Spelling.LOWER;
private static OutputFormat outputFormat = OutputFormat.PLAIN;
private static ShowLineNumbers showLineNumbers = ShowLineNumbers.NO;
private static StatementTerminator statementTerminator = StatementTerminator.SEMICOLON;

private static BackSlashQuoting backSlashQuoting = BackSlashQuoting.NO;
private static int indentWidth = 4;
Expand All @@ -191,6 +192,14 @@ public static void setBackSlashQuoting(BackSlashQuoting backSlashQuoting) {
JSQLFormatter.backSlashQuoting = backSlashQuoting;
}

public static StatementTerminator getStatementTerminator() {
return statementTerminator;
}

public static void setStatementTerminator(StatementTerminator statementTerminator) {
JSQLFormatter.statementTerminator = statementTerminator;
}

public static Separation getSeparation() {
return separation;
}
Expand Down Expand Up @@ -940,7 +949,21 @@ public static String format(String sqlStr, String... options) throws Exception {
"The " + statement.getClass().getName() + " Statement is not supported yet.");
}
}
appendNormalizedLineBreak(statementBuilder).append(";\n");

switch (statementTerminator) {
case SEMICOLON:
appendNormalizedLineBreak(statementBuilder).append(";\n");
break;
case NONE:
appendNormalizedLineBreak(statementBuilder).append("\n\n");
break;
case GO:
appendNormalizedLineBreak(statementBuilder).append("GO\n");
break;
case BACKSLASH:
appendNormalizedLineBreak(statementBuilder).append("\\\n");
break;
}

builder.append(commentMap.isEmpty() ? statementBuilder
: commentMap.insertComments(statementBuilder, outputFormat));
Expand Down Expand Up @@ -1076,6 +1099,13 @@ public static void applyFormattingOptions(String[] options) {
} catch (Exception ex) {
LOGGER.log(Level.WARNING, "Formatting Option {0} does not support {1} ", o);
}
} else if (key.equalsIgnoreCase(FormattingOption.STATEMENT_TERMINATOR.toString())) {
try {
statementTerminator = StatementTerminator.valueOf(value.toUpperCase());
} catch (Exception ex) {
LOGGER.log(Level.WARNING, "Formatting Option {0} does not support {1} ", o);
}

} else {
LOGGER.log(Level.WARNING, "Unknown Formatting Option {0} = {1} ", o);
}
Expand Down Expand Up @@ -3706,12 +3736,32 @@ public enum BackSlashQuoting {
YES, NO
}

public enum StatementTerminator {
SEMICOLON, NONE, GO, BACKSLASH
}

public enum FormattingOption {
SQUARE_BRACKET_QUOTATION("squareBracketQuotation"), BACKSLASH_QUOTING(
"backSlashQuoting"), OUTPUT_FORMAT("outputFormat"), KEYWORD_SPELLING(
"keywordSpelling"), FUNCTION_SPELLING("functionSpelling"), OBJECT_SPELLING(
"objectSpelling"), SEPARATION("separation"), INDENT_WIDTH(
"indentWidth"), SHOW_LINE_NUMBERS("showLineNumbers");
SQUARE_BRACKET_QUOTATION("squareBracketQuotation")

, BACKSLASH_QUOTING("backSlashQuoting")

, OUTPUT_FORMAT("outputFormat")

, KEYWORD_SPELLING("keywordSpelling")

, FUNCTION_SPELLING("functionSpelling")

, OBJECT_SPELLING("objectSpelling")

, SEPARATION("separation")

, INDENT_WIDTH("indentWidth")

, SHOW_LINE_NUMBERS("showLineNumbers")

, STATEMENT_TERMINATOR("statementTerminator")

;

public final String optionName;

Expand Down
9 changes: 5 additions & 4 deletions src/site/sphinx/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ Static Binaries

.. code:: Bash
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>
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>]
.. tab:: Linux Shell

.. code:: Bash
./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>
./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>]
.. tab:: Windows Power Shell

.. code:: Bash
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>
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>]
..........................
Command Line Options (CLI)
Expand All @@ -39,7 +39,8 @@ Command Line Options (CLI)
--objectSpelling <arg> Spelling of object names. [UPPER* LOWER CAMEL KEEP]
--functionSpelling <arg> Spelling of function names. [UPPER* LOWER CAMEL KEEP]
--separation <arg> Position of the field separator. [BEFORE* AFTER]
--squareBracketQuotation <arg> Interpret Square Brackets "[]" as quotes instead of arrays. [AUTO* YES NO]
--squareBracketQuotation <arg> Interpret Square Brackets "[]" as quotes instead of arrays. [AUTO* YES NO]
--statementTerminator <arg> Set the statement terminator. [SEMICOLON* NONE GO BACKSLASH]

.. note::

Expand Down
15 changes: 15 additions & 0 deletions src/test/java/com/manticore/jsqlformatter/FormatTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.manticore.jsqlformatter;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class FormatTest {
@Test
void testStatementTerminator() throws Exception {
String provided = "Select 1";
String expected = "SELECT 1";

String result = JSQLFormatter.format(provided, "statementTerminator=NONE");
Assertions.assertEquals(expected, result);
}
}
9 changes: 5 additions & 4 deletions src/test/java/com/manticore/jsqlformatter/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.StatementVisitor;
import net.sf.jsqlparser.statement.StatementVisitorAdapter;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
Expand Down Expand Up @@ -65,7 +66,7 @@ public void testExpressionVisitor() throws JSQLParserException {
"SELECT * FROM test WHERE aaa = 1 or bbb = 2 and case when ccc = 3 then 4 else 5 end = 6";
Statement statement = CCJSqlParserUtil.parse(sqlStr);

ExpressionVisitorAdapter expressionVisitorAdapter = new ExpressionVisitorAdapter() {
ExpressionVisitorAdapter<?> expressionVisitorAdapter = new ExpressionVisitorAdapter<>() {
@Override
public void visit(OrExpression orExpression) {
super.visit(orExpression);
Expand All @@ -79,17 +80,17 @@ public void visit(AndExpression andExpression) {
}
};

SelectVisitorAdapter selectVisitorAdapter = new SelectVisitorAdapter() {
SelectVisitorAdapter<?> selectVisitorAdapter = new SelectVisitorAdapter<>() {
@Override
public void visit(PlainSelect plainSelect) {
plainSelect.getWhere().accept(expressionVisitorAdapter);
}
};

StatementVisitorAdapter statementVisitorAdapter = new StatementVisitorAdapter() {
StatementVisitorAdapter<?> statementVisitorAdapter = new StatementVisitorAdapter<>() {
@Override
public void visit(Select select) {
select.accept(selectVisitorAdapter);
select.accept((StatementVisitor<?>) selectVisitorAdapter);
}
};

Expand Down

0 comments on commit f9a6a7a

Please sign in to comment.