vertical-blank / sql-formatter Goto Github PK
View Code? Open in Web Editor NEWSQL formatter written with only Java Standard Library, without dependencies.
License: MIT License
SQL formatter written with only Java Standard Library, without dependencies.
License: MIT License
Hi, I'm trying to use sqlFormatter with vertica which uses "//" for integer division. It doesn't look like there's an exact dialect of of the box for vertica. Wondering if there's a way to disable adding the extra space?
We have:
ALTER TABLE public.Orders ADD CONSTRAINT "fk_Orders_ref_Customers" FOREIGN KEY (customer_id) REFERENCES public.Customers (id)
After standart SqlFormatter.format(sqlScript, FormatConfig.builder().build()):
ALTER TABLE
public.Orders
ADD
CONSTRAINT "fk_Orders_ref_Customers" FOREIGN KEY (customer_id) REFERENCES public.Customers (id)
Between public.Orders and ADD elements only row delimiter, without any spaces.
And after replace all \n or \r we get invalid script:
ALTER TABLE public.OrdersADD CONSTRAINT
IndexedParams(List<?> params) {
List tempParam = new ArrayList<>(params.size());
for (Object param : params) {
if (param == null){
param = "null";
}else if (param instanceof Number){
}else if (param instanceof String){
param = "'"+param+"'";
}else if (param instanceof Date){
param = "'" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(param) + "'";
}else{
}
tempParam.add(param);
}
this.params = new ArrayDeque<>(tempParam);
}
Hi,
Would it be possible to make a release 1.0.4 before the big catch up ?
Regards,
Thats it.
The query
SqlFormatter.of(Dialect.PlSql)
.extend(cfg -> cfg.plusOperators("->"))
.format("SELECT `My field with \\` back () <<<<<< tick and a verryyyyyyyy long name` FROM MY_TABLE");
produces
SELECT
`My field with \` back < < < < < < tick
and a verryyyyyyyy long name ` FROM MY_TABLE
Which is no longer a valid query as the name has been split over multiple lines and whitespaces have been included.
I think that the first `
is detected as the end of the name even though it is escaped with a \
.
Is there any way to specify such an escape pattern in column names ?
Date query parameters do not come out quoted, and don't use the standard ISO format accepted by most DB platforms.
Please enhance the library to format this SQL.
merge into DW_STG_USER.ACCOUNT_DIM target using (
select
COMMON_NAME m_commonName,
ORIGIN m_origin,
USAGE_TYPE m_usageType,
CATEGORY m_category
from
MY_TABLE
where
USAGE_TYPE = :value
) source on source.m_usageType = target.USAGE_TYPE when matched then
update
set
target.COMMON_NAME = source.m_commonName,
target.ORIGIN = source.m_origin,
target.USAGE_TYPE = source.m_usageType,
target.CATEGORY = source.m_category
where
(
(source.m_commonName <> target.COMMON_NAME)
or(source.m_origin <> target.ORIGIN)
or(source.m_usageType <> target.USAGE_TYPE)
or(source.m_category <> target.CATEGORY)
)
when not matched then
insert
(
target.COMMON_NAME,
target.ORIGIN,
target.USAGE_TYPE,
target.CATEGORY
)
values
(
source.m_commonName,
source.m_origin,
source.m_usageType,
source.m_category
)
A space is created as shown in the example below.
@Test
void test() {
String sql = "SELECT * FROM TEST WHERE ABC != '4'";
String format = SqlFormatter.format(sql);
System.out.println("format: " + format);
}
// "!=" ==> "! ="
// output :
format: SELECT
*
FROM
TEST
WHERE
ABC ! = '4'
Thank you very much.
If we put comments and all the sql in one line, it would not identify the sql and still in one line.
The between ... and ... clause should in the same line, right now the behavior is that before every and it would have a newline.
And just for suggestion, maybe your guys could consider some caching or other ways to improve the performance, it's almost 100X slower than hibernate core sql formatter when it comes to long queries.
The @@ operator is used in PostgreSQL for text search
https://www.postgresql.org/docs/13/functions-textsearch.html
However, the formatter obtained by SqlFormatter.of(Dialect.PostgreSql) splits those two @ symbols with space, making a query using that operator invalid.
Hi there,
I tested the demo of sql-formatter, https://sql-formatter-org.github.io/sql-formatter/
There is an option "Indentation style: Tabular, Right",
is there any way of veritcal-blank edition to archive this?
Many thanks.
@@SESSION
is a reserved word in MySQL. https://dev.mysql.com/doc/refman/8.0/en/session-state-tracking.html
The formatter is currently adding a space after the first @
as it's considering it as a special word char.
Maybe a same fix as #42 would work? although it's not an operator in MySQL
Name placeholders like WHERE name = :name
are broken and do not replace at all.
Example:
SELECT supplier_name, city FROM suppliers
WHERE supplier_id = :name
this formats as:
SELECT
supplier_name,
city
FROM
suppliers
WHERE
supplier_id =: name
Notice the =:
and the space between the :
and name
- =: name
You can see this right on the demo page:
I know this is a SQL Formatter, but can it also be used to extract table names from a SQL Statement!
Previously I have worked with:
SQL Table Name Parser -- https://github.com/mnadeem/sql-table-name-parser
Which uses "regex" to extract table names -- but it's not really reliable
JSqlParser -- https://github.com/JSQLParser/JSqlParser
Which is a full-blown parser, so if the parser hasn't implemeted various stuff (for example: old-style-outer-joins in T-SQL, it will fail)
So I thought... with a little bit of changes... it might be easy to implement: Set<String> SqlFormatter.getTableNames(sql)
in this project!
Would it be hard to enhance this package to also extract table names (at a "best effort", 100% correctness is not important, just as long as it's better than the SQL Table Name Parser
)
hi,
in jdbc,the named parameter is :param
but i find parameter in lib is @param
any idea about replcase @ to : ?
thanks~
My script starts as
DECLARE v_number NUMBER := 0;
then SqlFormatter.format(sql). But return like this
DECLARE v_number NUMBER: = 0;
This script throw exception in Oracle. It seperates := operator.
My team is looking for a SQL formatter that meets our needs. sql-formatter
gets pretty close to what we want but I want to add some features that would satisfy our needs:
I'd like to make these be toggleable options. If I made the changes, would you accept a PR?
https://github.com/takayahilton/sql-formatter was recently updated to support Scala Native
This means that vertical-blank's sql-formatter could easily support Scala Native compilation by depending on https://github.com/takayahilton/sql-formatter.
Other tools using vertical-blank's sql-formatter (such as quill) could thus also be ported to SN without having to change the sql-formatter namespace.
And this would add this dependency only for the SN build.
Table names like "MY_SCHEMA.C$_MY_TABLE" will be formatted into three parts"MY_SCHEMA.C $ _MY_TABLE".
Using StandardSql or PlSql dialect, sql-formatter incorrectly interprets the Oracle quotation (q'...') operator.
For input string Select q'{test message}' from dual
gives wrong result
Select q '{test message}' from dual
In Dialect.PostgreSql, when using the string concatenation operator (||), extra whitespace is inserted.
val res = SqlFormatter
.of(Dialect.PostgreSql)
.format("select aa || bb from zzz")
println(res)
// select
// aa | | bb
// ^
// from
// zzz
If you run dialectConfig.plusOperators("||")
, no extra whitespace will be inserted.
val res = SqlFormatter
.of(Dialect.PostgreSql)
.extend { dialectConfig -> dialectConfig.plusOperators("||") }
.format("select aa || bb from zzz")
println(res)
// select
// aa || bb
// from
// zzz
Similar whitespace will be inserted in Dialect.N1ql and Dialect.TSql.
I can't judge whether the response is appropriate, but after applying the following patch, plusOperators("||")
is no longer needed.
diff --git a/src/main/java/com/github/vertical_blank/sqlformatter/languages/PostgreSqlFormatter.java b/src/main/java/com/github/vertical_blank/sqlformatter/languages/PostgreSqlFormatter.java
index e8c4f0a..15e76ae 100644
--- a/src/main/java/com/github/vertical_blank/sqlformatter/languages/PostgreSqlFormatter.java
+++ b/src/main/java/com/github/vertical_blank/sqlformatter/languages/PostgreSqlFormatter.java
@@ -531,7 +531,7 @@ public class PostgreSqlFormatter extends AbstractFormatter {
.operators(
Arrays.asList(
"!=", "<<", ">>", "||/", "|/", "::", "->>", "->", "~~*", "~~", "!~~*", "!~~", "~*",
- "!~*", "!~", "!!", "@@", "@@@"))
+ "!~*", "!~", "!!", "@@", "@@@", "||"))
.build();
}
In Postgres such a query is valid:
select A as "A\""B" from "T1";
The single result column will be named A\"B
.
The backslash escape isn't used as escape here, the first inner "
will escape the following "
.
To demonstrate the logic a bit more a few examples:
A\B
=> A\B
A""B
=> A"B
Now the formatter chokes on this.
select A as "A\""B" from "T1";
will be formatted as
select A as "A\"" B " from " T1 ";
Which is not even executable anymore.
Is there any configuration that could alleviate this problem ?
This only seems to happen when using '' as the escape character. I've tired all the supported languages using the of(...) with the same results. I've provide a simple sample code and console output.
String a = "SELECT * FROM psprcsrqst WHERE prcsname LIKE 'A\_%' ESCAPE '\' AND prcstype = :1";
System.out.println(SqlFormatter.format(a));
Output:
SELECT
*
FROM
psprcsrqst
WHERE
prcsname LIKE 'A_%' ESCAPE '' AND prcstype = :1
Using some other character, in this case '!'
String a = "SELECT * FROM psprcsrqst WHERE prcsname LIKE 'A!_%' ESCAPE '!' AND prcstype = :1";
System.out.println(SqlFormatter.format(a));
Output:
SELECT
*
FROM
psprcsrqst
WHERE
prcsname LIKE 'A!_%' ESCAPE '!'
AND prcstype = :1
select '123' || 'abc' from table1
after
select
'123' | | 'abc'
from
table1
extra space between the ||
I'm specially interested in the following questions:
If I format an SQL string like this:
SELECT * FROM `tableName`;
The resulting formatted string is like this:
SELECT * FROM ` tableName `;
The extra spaces break my query. Is there a way to fix this, or an option to disable this particular extra space?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.