How to use it¶
As a Java library¶
The entry point is com.manticore.jsqlformatter.JSQLFormatter. A single
static call covers the common case:
import com.manticore.jsqlformatter.JSQLFormatter;
class Sample {
public static void main(String[] args) {
String formattedSql = JSQLFormatter.format("select * from dual;");
System.out.println(formattedSql);
}
}
Formatting options are passed as trailing key=value arguments, using the
same names as the CLI flags and the inline directive:
String formattedSql = JSQLFormatter.format(
"select * from dual;",
"indentWidth=2",
"keywordSpelling=LOWER",
"separation=AFTER");
Note
The class name is JSQLFormatter, all caps. Earlier revisions of this
page showed JSqlFormatter, which will not compile.
From the command line¶
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>]
./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>]
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>]
Typical invocations:
# format a file, ANSI highlighted, straight to the terminal
java -jar JSQLFormatterCLI.jar -i queries.sql --ansi
# reformat a whole folder into one target file, 2-space indent, lowercase keywords
java -jar JSQLFormatterCLI.jar -i ./sql/ -o formatted.sql -2 --keywordSpelling LOWER
# format a statement passed directly as an argument
java -jar JSQLFormatterCLI.jar "select * from dual;"
Command line options¶
Option |
Description |
Values (default marked |
|---|---|---|
|
The input SQL file or folder |
path |
|
The output SQL file for the formatted statements |
path |
|
The output format |
|
|
Shorthand for ANSI annotated output |
|
|
Shorthand for HTML annotated output |
|
|
The indent width |
|
|
Shorthand for indent width 2 or 8 |
|
|
Spelling of keywords |
|
|
Spelling of function names |
|
|
Spelling of object names |
|
|
Position of the field separator |
|
|
Interpret square brackets |
|
|
The statement terminator |
|
Warning
On Windows 10 you need to enable ANSI output before --ansi will render:
Set-ItemProperty HKCU:\Console VirtualTerminalLevel -Type DWORD 1
Options inside the SQL¶
Any statement can carry its own formatting options in a leading comment. This is the most useful feature for repositories that serve more than one house style, because different statements in the same file can follow different conventions.
-- @JSQLFormatter(indentWidth=8, keywordSpelling=UPPER, functionSpelling=CAMEL, objectSpelling=LOWER, separation=BEFORE)
SELECT 'something' FROM DUAL;
The directive accepts the same keys as the CLI flags, and applies to every statement following it until the next directive.
From C and other natives¶
The GraalVM shared library exposes format through the standard isolate
API.
#include <stdlib.h>
#include <stdio.h>
#include <libSQLFormatter.h>
int main(int argc, char **argv) {
graal_isolate_t *isolate = NULL;
graal_isolatethread_t *thread = NULL;
if (graal_create_isolate(NULL, &isolate, &thread) != 0) {
fprintf(stderr, "graal_create_isolate error\n");
return 1;
}
printf("%s", format(thread, "select * from dual;"));
if (graal_detach_thread(thread) != 0) {
fprintf(stderr, "graal_detach_thread error\n");
return 1;
}
return 0;
}