Skip to content
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

Add SQL dialect for DB2 for IBM i #658

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ sql-formatter -h

```
usage: sql-formatter [-h] [-o OUTPUT] \
[-l {bigquery,db2,hive,mariadb,mysql,n1ql,plsql,postgresql,redshift,singlestoredb,snowflake,spark,sql,sqlite,transactsql,trino,tsql}] [-c CONFIG] [--version] [FILE]
[-l {bigquery,db2,db2i,hive,mariadb,mysql,n1ql,plsql,postgresql,redshift,singlestoredb,snowflake,spark,sql,sqlite,transactsql,trino,tsql}] [-c CONFIG] [--version] [FILE]

SQL Formatter

Expand All @@ -107,7 +107,7 @@ optional arguments:
-o, --output OUTPUT
File to write SQL output (defaults to stdout)
--fix Update the file in-place
-l, --language {bigquery,db2,hive,mariadb,mysql,n1ql,plsql,postgresql,redshift,singlestoredb,snowflake,spark,sql,sqlite,trino,tsql}
-l, --language {bigquery,db2,db2i,hive,mariadb,mysql,n1ql,plsql,postgresql,redshift,singlestoredb,snowflake,spark,sql,sqlite,trino,tsql}
SQL dialect (defaults to basic sql)
-c, --config CONFIG
Path to config json file (will use default configs if unspecified)
Expand Down
2 changes: 2 additions & 0 deletions docs/dialect.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The following dialects can be imported from `"sql-formatter"` module:
- `sql` - [Standard SQL][]
- `bigquery` - [GCP BigQuery][]
- `db2` - [IBM DB2][]
- `db2i` - [IBM DB2i][]
- `hive` - [Apache Hive][]
- `mariadb` - [MariaDB][]
- `mysql` - [MySQL][]
Expand Down Expand Up @@ -69,6 +70,7 @@ You likely only want to use this if your other alternative is to fork SQL Format
[standard sql]: https://en.wikipedia.org/wiki/SQL:2011
[gcp bigquery]: https://cloud.google.com/bigquery
[ibm db2]: https://www.ibm.com/analytics/us/en/technology/db2/
[ibm db2i]: https://www.ibm.com/docs/en/i/7.5?topic=overview-db2-i
[apache hive]: https://hive.apache.org/
[mariadb]: https://mariadb.com/
[mysql]: https://www.mysql.com/
Expand Down
2 changes: 2 additions & 0 deletions docs/language.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const result = format('SELECT * FROM tbl', { dialect: 'sqlite' });
- `"sql"` - (default) [Standard SQL][]
- `"bigquery"` - [GCP BigQuery][]
- `"db2"` - [IBM DB2][]
- `"db2i"` - [IBM DB2i][]
- `"hive"` - [Apache Hive][]
- `"mariadb"` - [MariaDB][]
- `"mysql"` - [MySQL][]
Expand Down Expand Up @@ -47,6 +48,7 @@ See docs for [dialect][] option.
[standard sql]: https://en.wikipedia.org/wiki/SQL:2011
[gcp bigquery]: https://cloud.google.com/bigquery
[ibm db2]: https://www.ibm.com/analytics/us/en/technology/db2/
[ibm db2i]: https://www.ibm.com/docs/en/i/7.5?topic=overview-db2-i
[apache hive]: https://hive.apache.org/
[mariadb]: https://mariadb.com/
[mysql]: https://www.mysql.com/
Expand Down
1 change: 1 addition & 0 deletions docs/params.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ The placeholder types available by default depend on SQL dialect used:
- sql - `?`
- bigquery - `?`, `@name`, `` @`name` ``
- db2 - `?`, `:name`
- db2i - `?`, `:name`
- hive - _no support_
- mariadb - `?`
- mysql - `?`
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"pl/sql",
"transact-sql",
"db2",
"db2i",
"sqlite",
"trino",
"presto",
Expand Down
1 change: 1 addition & 0 deletions src/allDialects.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { bigquery } from './languages/bigquery/bigquery.formatter.js';
export { db2 } from './languages/db2/db2.formatter.js';
export { db2i } from './languages/db2i/db2i.formatter.js';
export { hive } from './languages/hive/hive.formatter.js';
export { mariadb } from './languages/mariadb/mariadb.formatter.js';
export { mysql } from './languages/mysql/mysql.formatter.js';
Expand Down
179 changes: 179 additions & 0 deletions src/languages/db2i/db2i.formatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import { DialectOptions } from '../../dialect.js';
import { expandPhrases } from '../../expandPhrases.js';
import { functions } from './db2i.functions.js';
import { keywords } from './db2i.keywords.js';

const reservedSelect = expandPhrases(['SELECT [ALL | DISTINCT]']);

const reservedClauses = expandPhrases([
// queries
'WITH',
'INTO',
'FROM',
'WHERE',
'GROUP BY',
'HAVING',
'PARTITION BY',
'ORDER [SIBLINGS] BY [INPUT SEQUENCE]',
'OFFSET',
'FETCH {FIRST | NEXT}',
'LIMIT',
'FOR UPDATE [OF]',
'FOR READ ONLY',
'OPTIMIZE FOR',
// Data modification
// - insert:
'INSERT INTO',
'VALUES',
// - update:
'SET',
// - merge:
'MERGE INTO',
'WHEN [NOT] MATCHED [THEN]',
'UPDATE SET',
'DELETE',
'INSERT',
// Data definition - table
'CREATE [OR REPLACE] TABLE',
'FOR SYSTEM NAME',
// Data definition - view
'CREATE [OR REPLACE] [RECURSIVE] VIEW',
]);

const onelineClauses = expandPhrases([
// - update:
'UPDATE',
'WHERE CURRENT OF',
'WITH {NC | RR | RS | CS | UR}',
// - delete:
'DELETE FROM',
// - drop table:
'DROP TABLE',
// alter table:
'ALTER TABLE',
'ADD [COLUMN]',
'ALTER [COLUMN]',
'DROP [COLUMN]',
'RENAME [COLUMN]',
'SET DATA TYPE', // for alter column
'SET {GENERATED ALWAYS | GENERATED BY DEFAULT}', // for alter column
'SET NOT NULL', // for alter column
'SET {NOT HIDDEN | IMPLICITLY HIDDEN}', // for alter column
'SET FIELDPROC', // for alter column
'DROP {DEFAULT | NOT NULL | GENERATED | IDENTITY | ROW CHANGE TIMESTAMP | FIELDPROC}', // for alter column
// - truncate:
'TRUNCATE [TABLE]',
// other
'SET [CURRENT] SCHEMA',
'SET CURRENT_SCHEMA',
// https://www.ibm.com/docs/en/i/7.5?topic=reference-statements
chrjorgensen marked this conversation as resolved.
Show resolved Hide resolved
'ALLOCATE CURSOR',
'ALLOCATE [SQL] DESCRIPTOR [LOCAL | GLOBAL] SQL',
'ALTER [SPECIFIC] {FUNCTION | PROCEDURE}',
'ALTER {MASK | PERMISSION | SEQUENCE | TRIGGER}',
'ASSOCIATE [RESULT SET] {LOCATOR | LOCATORS}',
'BEGIN DECLARE SECTION',
'CALL',
'CLOSE',
'COMMENT ON {ALIAS | COLUMN | CONSTRAINT | INDEX | MASK | PACKAGE | PARAMETER | PERMISSION | SEQUENCE | TABLE | TRIGGER | VARIABLE | XSROBJECT}',
'COMMENT ON [SPECIFIC] {FUNCTION | PROCEDURE | ROUTINE}',
'COMMENT ON PARAMETER SPECIFIC {FUNCTION | PROCEDURE | ROUTINE}',
'COMMENT ON [TABLE FUNCTION] RETURN COLUMN',
'COMMENT ON [TABLE FUNCTION] RETURN COLUMN SPECIFIC [PROCEDURE | ROUTINE]',
'COMMIT [WORK] [HOLD]',
'CONNECT [TO | RESET] USER',
'CREATE [OR REPLACE] {ALIAS | FUNCTION | MASK | PERMISSION | PROCEDURE | SEQUENCE | TRIGGER | VARIABLE}',
'CREATE [ENCODED VECTOR] INDEX',
'CREATE UNIQUE [WHERE NOT NULL] INDEX',
'CREATE SCHEMA',
'CREATE TYPE',
'DEALLOCATE [SQL] DESCRIPTOR [LOCAL | GLOBAL]',
'DECLARE CURSOR',
'DECLARE GLOBAL TEMPORARY TABLE',
'DECLARE',
'DESCRIBE CURSOR',
'DESCRIBE INPUT',
'DESCRIBE [OUTPUT]',
'DESCRIBE {PROCEDURE | ROUTINE}',
'DESCRIBE TABLE',
'DISCONNECT ALL [SQL]',
'DISCONNECT [CURRENT]',
'DROP {ALIAS | INDEX | MASK | PACKAGE | PERMISSION | SCHEMA | SEQUENCE | TABLE | TYPE | VARIABLE | XSROBJECT} [IF EXISTS]',
'DROP [SPECIFIC] {FUNCTION | PROCEDURE | ROUTINE} [IF EXISTS]',
'END DECLARE SECTION',
'EXECUTE [IMMEDIATE]',
// 'FETCH {NEXT | PRIOR | FIRST | LAST | BEFORE | AFTER | CURRENT} [FROM]',
'FREE LOCATOR',
'GET [SQL] DESCRIPTOR [LOCAL | GLOBAL]',
'GET [CURRENT | STACKED] DIAGNOSTICS',
'GRANT {ALL [PRIVILEGES] | ALTER | EXECUTE} ON {FUNCTION | PROCEDURE | ROUTINE | PACKAGE | SCHEMA | SEQUENCE | TABLE | TYPE | VARIABLE | XSROBJECT}',
'HOLD LOCATOR',
'INCLUDE',
'LABEL ON {ALIAS | COLUMN | CONSTRAINT | INDEX | MASK | PACKAGE | PERMISSION | SEQUENCE | TABLE | TRIGGER | VARIABLE | XSROBJECT}',
'LABEL ON [SPECIFIC] {FUNCTION | PROCEDURE | ROUTINE}',
'LOCK TABLE',
'OPEN',
'PREPARE',
'REFRESH TABLE',
'RELEASE',
'RELEASE [TO] SAVEPOINT',
'RENAME [TABLE | INDEX] TO',
'REVOKE {ALL [PRIVILEGES] | ALTER | EXECUTE} ON {FUNCTION | PROCEDURE | ROUTINE | PACKAGE | SCHEMA | SEQUENCE | TABLE | TYPE | VARIABLE | XSROBJECT}',
'ROLLBACK [WORK] [HOLD | TO SAVEPOINT]',
'SAVEPOINT',
'SET CONNECTION',
'SET CURRENT {DEBUG MODE | DECFLOAT ROUNDING MODE | DEGREE | IMPLICIT XMLPARSE OPTION | TEMPORAL SYSTEM_TIME}',
'SET [SQL] DESCRIPTOR [LOCAL | GLOBAL]',
'SET ENCRYPTION PASSWORD',
'SET OPTION',
'SET {[CURRENT [FUNCTION]] PATH | CURRENT_PATH}',
'SET RESULT SETS [WITH RETURN [TO CALLER | TO CLIENT]',
'SET SESSION AUTHORIZATION',
'SET SESSION_USER',
'SET TRANSACTION',
'SIGNAL SQLSTATE [VALUE]',
'TAG',
'TRANSFER OWNERSHIP OF',
'WHENEVER {NOT FOUND | SQLERROR | SQLWARNING}',
]);

const reservedSetOperations = expandPhrases(['UNION [ALL]', 'EXCEPT [ALL]', 'INTERSECT [ALL]']);

const reservedJoins = expandPhrases([
'JOIN',
'{LEFT | RIGHT | FULL} [OUTER] JOIN',
'{LEFT | RIGHT } EXCEPTION JOIN',
'{INNER | CROSS} JOIN',
]);

const reservedPhrases = expandPhrases([
'ON DELETE',
'ON UPDATE',
'SET NULL',
'{ROWS | RANGE} BETWEEN',
]);

// https://www.ibm.com/docs/en/i/7.5?topic=reference-sql
export const db2i: DialectOptions = {
tokenizerOptions: {
reservedSelect,
reservedClauses: [...reservedClauses, ...onelineClauses],
reservedSetOperations,
reservedJoins,
reservedPhrases,
reservedKeywords: keywords,
reservedFunctionNames: functions,
stringTypes: [
{ quote: "''-qq", prefixes: ['G', 'N', 'U&'] },
{ quote: "''-raw", prefixes: ['X', 'BX', 'GX', 'UX'], requirePrefix: true },
],
identTypes: [`""-qq`],
identChars: { first: '@#$' },
paramTypes: { positional: true, named: [':'] },
paramChars: { first: '@#$', rest: '@#$' },
operators: ['**', '¬=', '¬>', '¬<', '!>', '!<', '||'],
},
formatOptions: {
onelineClauses,
},
};
Loading
Loading