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 1 commit
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
193 changes: 193 additions & 0 deletions src/languages/db2i/db2i.formatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
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',
'FROM',
'WHERE',
'GROUP BY',
'HAVING',
'PARTITION BY',
'ORDER [SIBLINGS] BY [INPUT SEQUENCE]',
'OFFSET {ROW | ROWS}',
'FETCH {FIRST | NEXT} {ROW | ROWS} ONLY',
'LIMIT',
'FOR UPDATE [OF]',
'FOR READ ONLY',
'OPTIMIZE FOR [ALL] {ROW | ROWS}',
// Data modification
// - insert:
'INSERT INTO',
'VALUES',
// - update:
'SET',
// - merge:
'MERGE INTO',
'WHEN [NOT] MATCHED [THEN]',
'UPDATE SET',
'DELETE',
'INSERT',
// Data definition
'CREATE [OR REPLACE] TABLE [FOR SYSTEM NAME]',
'CREATE [OR REPLACE] [RECURSIVE] VIEW [FOR SYSTEM NAME]',
]);

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',
// 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 DESCRIPTOR',
'ALTER FUNCTION',
'ALTER MASK',
'ALTER PERMISSION',
'ALTER PROCEDURE',
'ALTER SEQUENCE',
'ALTER TRIGGER',
'ASSOCIATE LOCATORS',
'BEGIN DECLARE SECTION',
'CALL',
'CLOSE',
'COMMENT',
'COMMIT',
'CONNECT',
'CREATE [OR REPLACE] ALIAS',
'CREATE [OR REPLACE] FUNCTION',
'CREATE INDEX',
'CREATE [OR REPLACE] MASK',
'CREATE [OR REPLACE] PERMISSION',
'CREATE [OR REPLACE] PROCEDURE',
'CREATE SCHEMA',
'CREATE [OR REPLACE] SEQUENCE',
'CREATE [OR REPLACE] TRIGGER',
'CREATE TYPE',
'CREATE [OR REPLACE] VARIABLE',
'DEALLOCATE DESCRIPTOR',
'DECLARE CURSOR',
'DECLARE GLOBAL TEMPORARY TABLE',
'DECLARE PROCEDURE',
'DECLARE STATEMENT',
'DECLARE VARIABLE',
'DESCRIBE',
'DESCRIBE CURSOR',
'DESCRIBE INPUT',
'DESCRIBE OUTPUT',
'DESCRIBE PROCEDURE',
'DESCRIBE TABLE',
'DISCONNECT',
'DROP',
'END DECLARE SECTION',
'EXECUTE',
'EXECUTE IMMEDIATE',
'FETCH',
'FREE LOCATOR',
'GET DESCRIPTOR',
'GET DIAGNOSTICS',
'GRANT',
'HOLD LOCATOR',
'INCLUDE',
'INSERT',
'LABEL',
'LOCK TABLE',
'OPEN',
'PREPARE',
'REFRESH TABLE',
'RELEASE',
'RELEASE SAVEPOINT',
'RENAME',
'REVOKE',
'ROLLBACK',
'SAVEPOINT',
'SELECT',
chrjorgensen marked this conversation as resolved.
Show resolved Hide resolved
'SELECT INTO',
'SET CONNECTION',
'SET CURRENT DEBUG MODE',
'SET CURRENT DECFLOAT ROUNDING MODE',
'SET CURRENT DEGREE',
'SET CURRENT IMPLICIT XMLPARSE OPTION',
'SET CURRENT TEMPORAL SYSTEM_TIME',
'SET DESCRIPTOR',
'SET ENCRYPTION PASSWORD',
'SET OPTION',
'SET PATH',
'SET RESULT SETS',
'SET SESSION AUTHORIZATION',
'SET TRANSACTION',
'SET',
'SIGNAL',
'TAG',
'TRANSFER OWNERSHIP',
'TRUNCATE',
'UPDATE',
'VALUES',
'VALUES INTO',
'WHENEVER',
]);

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: ['**', '¬=', '¬>', '¬<', '!>', '!<', '||', 'CONCAT'],
chrjorgensen marked this conversation as resolved.
Show resolved Hide resolved
},
formatOptions: {
onelineClauses,
},
};
Loading
Loading