-
Notifications
You must be signed in to change notification settings - Fork 421
Upper case option affect table fields names #156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The issue here has multiple levels:
|
Just ran into this now too, CREATE TABLE
actors (
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
NAME VARCHAR(80) NOT NULL
); @nene what are your thoughts on extending
const options = {
language: 'postgresql',
keywordCase: {
upper: true, // true: default case
lower: ['name', 'varchar', 'integer'],
preserve: ['in'],
},
}
const options = {
language: 'postgresql',
keywordCase: 'upper',
keywordCaseIgnore: [
// Avoid changing case of `name` fields in tables
'name',
// Avoid changing case of data types
'varchar',
'integer',
// ...
],
} Edit: Just realized I submitted a similar feature request last month and forgot about it, would also probably resolve this one: |
I also just saw the following PR by @chrjorgensen for a new option I haven't yet upgraded to 14.0.0, but maybe this also resolves the issue with field names being capitalized in Edit: Just upgraded to 14.0.0 and no it doesn't resolve the issue with fields being uppercased to |
WorkaroundUntil it's possible to configure the keywords in a more granular way, one workaround is to patch Use showNonTabularKw(node) {
+ if (['NAME'].includes(node.text)) {
+ return node.text.toLowerCase();
+ }
+
switch (this.cfg.keywordCase) {
case 'preserve':
return equalizeWhitespace(node.raw);
case 'upper':
return node.text;
case 'lower':
return node.text.toLowerCase();
}
} |
@karlhorky I'd rather not add a separate way of tweaking which keywords get capitalized. There already exists a way to create a custom dialect. One can simply extend an existing dialect and change the list of keywords. See docs for the "dialect" option. |
Interesting! Maybe our organization can create a custom PostgreSQL dialect that will work for most cases... 🤔 I guess I would still like to be able to have lowercase data types, but maybe the combination |
Opened a draft PR for experimental I'm basing it off the |
Custom Dialect solution
Using the import { formatDialect, postgresql } from 'sql-formatter';
// Avoid identifiers being uppercased
// https://github.com/sql-formatter-org/sql-formatter/issues/156#issuecomment-1826846259
const postgresWithoutNameKeyword = {
...postgresql,
tokenizerOptions: {
...postgresql.tokenizerOptions,
reservedKeywords: postgresql.tokenizerOptions.reservedKeywords.filter(
(reservedKeyword) =>
![
// Uncommon usage with SECURITY LABEL
// https://www.postgresql.org/docs/current/sql-security-label.html
'LABEL',
// Uncommon usage with CREATE TABLESPACE
// CREATE TABLESPACE dbspace LOCATION '/data/dbs';
// https://www.postgresql.org/docs/current/sql-createtablespace.html
'LOCATION',
// Data type for use in internal system catalogs
// https://www.postgresql.org/docs/current/datatype-character.html#DATATYPE-CHARACTER-SPECIAL-TABLE
'NAME',
// Uncommon usage with RELEASE SAVEPOINT
// https://www.postgresql.org/docs/current/sql-release-savepoint.html
'RELEASE',
].includes(reservedKeyword),
),
},
};
formatDialect('cReAtE tAbLe actors ( name VARCHAR(80) NOT NULL )', {
dialect: postgresWithoutNameKeyword,
keywordCase: 'upper'
});
/*
Output:
CREATE TABLE
actors (
name VARCHAR(80) NOT NULL
)
*/ The |
I did a sort-of basic fix for this, removing the following from list of keywords:
There are quite a bit of other keywords in this list that we probably should drop, but at least for start the most problematic ones should be gone now. |
Oh nice, that's great! I'll remove my custom dialect once d34557b is published in a release 🙌 |
Hi, in the case were the field name is a also a keyword in the language chosen, the field is being capitilized.
for example:
running formatter on the following with
-u -l postgresql
options:CREATE TABLE users(id INT PRIMARY KEY, name TEXT NOT NULL);
yields:
CREATE TABLE users(id INT PRIMARY KEY, NAME TEXT NOT NULL);
as opposed to just leaving it the same way.
tnx!
The text was updated successfully, but these errors were encountered: