From cb83ef6859040bbebc70ad32221193b22235d4d6 Mon Sep 17 00:00:00 2001 From: Christian Jorgensen Date: Wed, 8 Nov 2023 16:01:13 +0100 Subject: [PATCH 01/10] Add SQL dialect for DB2 for IBM i --- README.md | 4 +- docs/dialect.md | 2 + docs/language.md | 2 + docs/params.md | 1 + package.json | 1 + src/allDialects.ts | 1 + src/languages/db2i/db2i.formatter.ts | 193 ++++++++++ src/languages/db2i/db2i.functions.ts | 367 +++++++++++++++++++ src/languages/db2i/db2i.keywords.ts | 512 +++++++++++++++++++++++++++ src/sqlFormatter.ts | 1 + static/index.html | 1 + test/db2i.test.ts | 140 ++++++++ 12 files changed, 1223 insertions(+), 2 deletions(-) create mode 100644 src/languages/db2i/db2i.formatter.ts create mode 100644 src/languages/db2i/db2i.functions.ts create mode 100644 src/languages/db2i/db2i.keywords.ts create mode 100644 test/db2i.test.ts diff --git a/README.md b/README.md index 067c31ac17..265de52556 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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) diff --git a/docs/dialect.md b/docs/dialect.md index 2e3ed8656a..0f35ad4a3d 100644 --- a/docs/dialect.md +++ b/docs/dialect.md @@ -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][] @@ -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/ diff --git a/docs/language.md b/docs/language.md index b71c4b0cab..9b586c58ec 100644 --- a/docs/language.md +++ b/docs/language.md @@ -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][] @@ -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/ diff --git a/docs/params.md b/docs/params.md index 2da66db1bd..575d54ceb9 100644 --- a/docs/params.md +++ b/docs/params.md @@ -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 - `?` diff --git a/package.json b/package.json index 36213b0528..d64c15d078 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "pl/sql", "transact-sql", "db2", + "db2i", "sqlite", "trino", "presto", diff --git a/src/allDialects.ts b/src/allDialects.ts index 7ba24ffb1e..5bc46b8996 100644 --- a/src/allDialects.ts +++ b/src/allDialects.ts @@ -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'; diff --git a/src/languages/db2i/db2i.formatter.ts b/src/languages/db2i/db2i.formatter.ts new file mode 100644 index 0000000000..f0083f37ea --- /dev/null +++ b/src/languages/db2i/db2i.formatter.ts @@ -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 + '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', + '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'], + }, + formatOptions: { + onelineClauses, + }, +}; diff --git a/src/languages/db2i/db2i.functions.ts b/src/languages/db2i/db2i.functions.ts new file mode 100644 index 0000000000..13269f8c3d --- /dev/null +++ b/src/languages/db2i/db2i.functions.ts @@ -0,0 +1,367 @@ +import { flatKeywordList } from '../../utils.js'; + +export const functions = flatKeywordList({ + // https://www.ibm.com/docs/en/i/7.5?topic=functions-aggregate + aggregate: [ + 'ANY', + 'ARRAY_AGG', + 'AVG', + 'CORR', + 'CORRELATION', + 'COUNT', + 'COUNT_BIG', + 'COVAR_POP', + 'COVARIANCE', + 'COVAR', + 'COVAR_SAMP', + 'COVARIANCE_SAMP', + 'EVERY', + 'GROUPING', + 'JSON_ARRAYAGG', + 'JSON_OBJECTAGG', + 'LISTAGG', + 'MAX', + 'MEDIAN', + 'MIN', + 'PERCENTILE_CONT', + 'PERCENTILE_DISC', + // https://www.ibm.com/docs/en/i/7.5?topic=functions-regression' + 'REGR_AVGX', + 'REGR_AVGY', + 'REGR_COUNT', + 'REGR_INTERCEPT', + 'REGR_R2', + 'REGR_SLOPE', + 'REGR_SXX', + 'REGR_SXY', + 'REGR_SYY', + 'SOME', + 'STDDEV_POP', + 'STDDEV', + 'STDDEV_SAMP', + 'SUM', + 'VAR_POP', + 'VARIANCE', + 'VAR', + 'VAR_SAMP', + 'VARIANCE_SAMP', + 'XMLAGG', + 'XMLGROUP', + ], + // https://www.ibm.com/docs/en/i/7.5?topic=functions-scalar + scalar: [ + 'ABS', + 'ABSVAL', + 'ACOS', + 'ADD_DAYS', + 'ADD_HOURS', + 'ADD_MINUTES', + 'ADD_MONTHS', + 'ADD_SECONDS', + 'ADD_YEARS', + 'ANTILOG', + 'ARRAY_MAX_CARDINALITY', + 'ARRAY_TRIM', + 'ASCII', + 'ASIN', + 'ATAN', + 'ATAN2', + 'ATANH', + 'BASE64_DECODE', + 'BASE64_ENCODE', + 'BIGINT', + 'BINARY', + 'BIT_LENGTH', + 'BITAND', + 'BITANDNOT', + 'BITNOT', + 'BITOR', + 'BITXOR', + 'BLOB', + 'BOOLEAN', + 'BSON_TO_JSON', + 'CARDINALITY', + 'CEIL', + 'CEILING', + 'CHAR_LENGTH', + 'CHAR', + 'CHARACTER_LENGTH', + 'CHR', + 'CLOB', + 'COALESCE', + 'COMPARE_DECFLOAT', + 'CONCAT', + 'CONTAINS', + 'COS', + 'COSH', + 'COT', + 'CURDATE', + 'CURTIME', + 'DATABASE', + 'DATAPARTITIONNAME', + 'DATAPARTITIONNUM', + 'DATE', + 'DAY', + 'DAYNAME', + 'DAYOFMONTH', + 'DAYOFWEEK_ISO', + 'DAYOFWEEK', + 'DAYOFYEAR', + 'DAYS', + 'DBCLOB', + 'DBPARTITIONNAME', + 'DBPARTITIONNUM', + 'DEC', + 'DECFLOAT_FORMAT', + 'DECFLOAT_SORTKEY', + 'DECFLOAT', + 'DECIMAL', + 'DECRYPT_BINARY', + 'DECRYPT_BIT', + 'DECRYPT_CHAR', + 'DECRYPT_DB', + 'DEGREES', + 'DIFFERENCE', + 'DIGITS', + 'DLCOMMENT', + 'DLLINKTYPE', + 'DLURLCOMPLETE', + 'DLURLPATH', + 'DLURLPATHONLY', + 'DLURLSCHEME', + 'DLURLSERVER', + 'DLVALUE', + 'DOUBLE_PRECISION', + 'DOUBLE', + 'ENCRPYT', + 'ENCRYPT_AES', + 'ENCRYPT_AES256', + 'ENCRYPT_RC2', + 'ENCRYPT_TDES', + 'EXP', + 'EXTRACT', + 'FIRST_DAY', + 'FLOAT', + 'FLOOR', + 'GENERATE_UNIQUE', + 'GET_BLOB_FROM_FILE', + 'GET_CLOB_FROM_FILE', + 'GET_DBCLOB_FROM_FILE', + 'GET_XML_FILE', + 'GETHINT', + 'GRAPHIC', + 'GREATEST', + 'HASH_MD5', + 'HASH_ROW', + 'HASH_SHA1', + 'HASH_SHA256', + 'HASH_SHA512', + 'HASH_VALUES', + 'HASHED_VALUE', + 'HEX', + 'HEXTORAW', + 'HOUR', + 'HTML_ENTITY_DECODE', + 'HTML_ENTITY_ENCODE', + 'HTTP_DELETE_BLOB', + 'HTTP_DELETE', + 'HTTP_GET_BLOB', + 'HTTP_GET', + 'HTTP_PATCH_BLOB', + 'HTTP_PATCH', + 'HTTP_POST_BLOB', + 'HTTP_POST', + 'HTTP_PUT_BLOB', + 'HTTP_PUT', + 'IDENTITY_VAL_LOCAL', + 'IFNULL', + 'INSERT', + 'INSTR', + 'INT', + 'INTEGER', + 'INTERPRET', + 'ISFALSE', + 'ISNOTFALSE', + 'ISNOTTRUE', + 'ISTRUE', + 'JSON_ARRAY', + 'JSON_OBJECT', + 'JSON_QUERY', + 'JSON_TO_BSON', + 'JSON_UPDATE', + 'JSON_VALUE', + 'JULIAN_DAY', + 'LAND', + 'LAST_DAY', + 'LCASE', + 'LEAST', + 'LEFT', + 'LENGTH', + 'LN', + 'LNOT', + 'LOCATE_IN_STRING', + 'LOCATE', + 'LOG10', + 'LOR', + 'LOWER', + 'LPAD', + 'LTRIM', + 'MAX_CARDINALITY', + 'MAX', + 'MICROSECOND', + 'MIDNIGHT_SECONDS', + 'MIN', + 'MINUTE', + 'MOD', + 'MONTH', + 'MONTHNAME', + 'MONTHS_BETWEEN', + 'MQREAD', + 'MQREADCLOB', + 'MQRECEIVE', + 'MQRECEIVECLOB', + 'MQSEND', + 'MULTIPLY_ALT', + 'NEXT_DAY', + 'NORMALIZE_DECFLOAT', + 'NOW', + 'NULLIF', + 'NVL', + 'OCTET_LENGTH', + 'OVERLAY', + 'PI', + 'POSITION', + 'POSSTR', + 'POW', + 'POWER', + 'QUANTIZE', + 'QUARTER', + 'RADIANS', + 'RAISE_ERROR', + 'RANDOM or RAND', + 'REAL', + 'REGEXP_COUNT', + 'REGEXP_INSTR', + 'REGEXP_REPLACE', + 'REGEXP_SUBSTR', + 'REPEAT', + 'REPLACE', + 'RID', + 'RIGHT', + 'ROUND_TIMESTAMP', + 'ROUND', + 'ROWID', + 'RPAD', + 'RRN', + 'RTRIM', + 'SCORE', + 'SECOND', + 'SIGN', + 'SIN', + 'SINH', + 'SMALLINT', + 'SOUNDEX', + 'SPACE', + 'SQRT', + 'STRIP', + 'STRLEFT', + 'STRPOS', + 'STRRIGHT', + 'SUBSTR', + 'SUBSTRING', + 'TABLE_NAME', + 'TABLE_SCHEMA', + 'TAN', + 'TANH', + 'TIME', + 'TIMESTAMP_FORMAT', + 'TIMESTAMP_ISO', + 'TIMESTAMP', + 'TIMESTAMPDIFF_BIG', + 'TIMESTAMPDIFF', + 'TO_CHAR', + 'TO_CLOB', + 'TO_DATE', + 'TO_NUMBER', + 'TO_TIMESTAMP', + 'TOTALORDER', + 'TRANSLATE', + 'TRIM_ARRAY', + 'TRIM', + 'TRUNC_TIMESTAMP', + 'TRUNC', + 'TRUNCATE', + 'UCASE', + 'UPPER', + 'URL_DECODE', + 'URL_ENCODE', + 'VALUE', + 'VARBINARY_FORMAT', + 'VARBINARY', + 'VARCHAR_BIT_FORMAT', + 'VARCHAR_FORMAT_BINARY', + 'VARCHAR_FORMAT', + 'VARCHAR', + 'VARGRAPHIC', + 'VERIFY_GROUP_FOR_USER', + 'WEEK_ISO', + 'WEEK', + 'WRAP', + 'XMLATTRIBUTES', + 'XMLCOMMENT', + 'XMLCONCAT', + 'XMLDOCUMENT', + 'XMLELEMENT', + 'XMLFOREST', + 'XMLNAMESPACES', + 'XMLPARSE', + 'XMLPI', + 'XMLROW', + 'XMLSERIALIZE', + 'XMLTEXT', + 'XMLVALIDATE', + 'XOR', + 'XSLTRANSFORM', + 'YEAR', + 'ZONED', + ], + // https://www.ibm.com/docs/en/i/7.5?topic=functions-table + table: [ + 'BASE_TABLE', + 'HTTP_DELETE_BLOB_VERBOSE', + 'HTTP_DELETE_VERBOSE', + 'HTTP_GET_BLOB_VERBOSE', + 'HTTP_GET_VERBOSE', + 'HTTP_PATCH_BLOB_VERBOSE', + 'HTTP_PATCH_VERBOSE', + 'HTTP_POST_BLOB_VERBOSE', + 'HTTP_POST_VERBOSE', + 'HTTP_PUT_BLOB_VERBOSE', + 'HTTP_PUT_VERBOSE', + 'JSON_TABLE', + 'MQREADALL', + 'MQREADALLCLOB', + 'MQRECEIVEALL', + 'MQRECEIVEALLCLOB', + 'XMLTABLE', + ], + // https://www.ibm.com/docs/en/db2-for-zos/11?topic=functions-row + row: ['UNPACK'], + // https://www.ibm.com/docs/en/i/7.5?topic=expressions-olap-specifications + olap: [ + 'CUME_DIST', + 'DENSE_RANK', + 'FIRST_VALUE', + 'LAG', + 'LAST_VALUE', + 'LEAD', + 'NTH_VALUE', + 'NTILE', + 'PERCENT_RANK', + 'RANK', + 'RATIO_TO_REPORT', + 'ROW_NUMBER', + ], + // Type casting + cast: ['CAST'], +}); diff --git a/src/languages/db2i/db2i.keywords.ts b/src/languages/db2i/db2i.keywords.ts new file mode 100644 index 0000000000..95b6504fd2 --- /dev/null +++ b/src/languages/db2i/db2i.keywords.ts @@ -0,0 +1,512 @@ +import { flatKeywordList } from '../../utils.js'; + +export const keywords = flatKeywordList({ + // https://www.ibm.com/docs/en/i/7.5?topic=words-reserved + standard: [ + 'ABSENT', + 'ACCORDING', + 'ACCTNG', + 'ACTION', + 'ACTIVATE', + 'ADD', + 'ALIAS', + 'ALL', + 'ALLOCATE', + 'ALLOW', + 'ALTER', + 'AND', + 'ANY', + 'APPEND', + 'APPLNAME', + 'ARRAY', + 'ARRAY_AGG', + 'ARRAY_TRIM', + 'AS', + 'ASC', + 'ASENSITIVE', + 'ASSOCIATE', + 'ATOMIC', + 'ATTACH', + 'ATTRIBUTES', + 'AUTHORIZATION', + 'AUTONOMOUS', + 'BEFORE', + 'BEGIN', + 'BETWEEN', + 'BINARY', + 'BIND', + 'BIT', + 'BOOLEAN', + 'BSON', + 'BUFFERPOOL', + 'BY', + 'CACHE', + 'CALL', + 'CALLED', + 'CARDINALITY', + 'CASE', + 'CAST', + 'CCSID', + 'CHAR', + 'CHARACTER', + 'CHECK', + 'CL', + 'CLOSE', + 'CLUSTER', + 'COLLECT', + 'COLLECTION', + 'COLUMN', + 'COMMENT', + 'COMMIT', + 'COMPACT', + 'COMPARISONS', + 'COMPRESS', + 'CONCAT', + 'CONCURRENT', + 'CONDITION', + 'CONNECT', + 'CONNECT_BY_ROOT', + 'CONNECTION', + 'CONSTANT', + 'CONSTRAINT', + 'CONTAINS', + 'CONTENT', + 'CONTINUE', + 'COPY', + 'COUNT', + 'COUNT_BIG', + 'CREATE', + 'CREATEIN', + 'CROSS', + 'CUBE', + 'CUME_DIST', + 'CURRENT', + 'CURRENT_DATE', + 'CURRENT_PATH', + 'CURRENT_SCHEMA', + 'CURRENT_SERVER', + 'CURRENT_TIME', + 'CURRENT_TIMESTAMP', + 'CURRENT_TIMEZONE', + 'CURRENT_USER', + 'CURSOR', + 'CYCLE', + 'DATA', + 'DATABASE', + 'DATAPARTITIONNAME', + 'DATAPARTITIONNUM', + 'DATE', + 'DAY', + 'DAYS', + 'DB2GENERAL', + 'DB2GENRL', + 'DB2SQL', + 'DBINFO', + 'DBPARTITIONNAME', + 'DBPARTITIONNUM', + 'DEACTIVATE', + 'DEALLOCATE', + 'DECLARE', + 'DEFAULT', + 'DEFAULTS', + 'DEFER', + 'DEFINE', + 'DEFINITION', + 'DELETE', + 'DELETING', + 'DENSE_RANK', + 'DENSERANK', + 'DESC', + 'DESCRIBE', + 'DESCRIPTOR', + 'DETACH', + 'DETERMINISTIC', + 'DIAGNOSTICS', + 'DISABLE', + 'DISALLOW', + 'DISCONNECT', + 'DISTINCT', + 'DO', + 'DOCUMENT', + 'DOUBLE', + 'DROP', + 'DYNAMIC', + 'EACH', + 'ELSE', + 'ELSEIF', + 'EMPTY', + 'ENABLE', + 'ENCODING', + 'ENCRYPTION', + 'END', + 'END-EXEC', + 'ENDING', + 'ENFORCED', + 'ERROR', + 'ESCAPE', + 'EVERY', + 'EXCEPT', + 'EXCEPTION', + 'EXCLUDING', + 'EXCLUSIVE', + 'EXECUTE', + 'EXISTS', + 'EXIT', + 'EXTEND', + 'EXTERNAL', + 'EXTRACT', + 'FALSE', + 'FENCED', + 'FETCH', + 'FIELDPROC', + 'FILE', + 'FINAL', + 'FIRST_VALUE', + 'FOR', + 'FOREIGN', + 'FORMAT', + 'FREE', + 'FREEPAGE', + 'FROM', + 'FULL', + 'FUNCTION', + 'GBPCACHE', + 'GENERAL', + 'GENERATED', + 'GET', + 'GLOBAL', + 'GO', + 'GOTO', + 'GRANT', + 'GRAPHIC', + 'GROUP', + 'HANDLER', + 'HASH', + 'HASH_ROW', + 'HASHED_VALUE', + 'HAVING', + 'HINT', + 'HOLD', + 'HOUR', + 'HOURS', + 'ID', + 'IDENTITY', + 'IF', + 'IGNORE', + 'IMMEDIATE', + 'IMPLICITLY', + 'IN', + 'INCLUDE', + 'INCLUDING', + 'INCLUSIVE', + 'INCREMENT', + 'INDEX', + 'INDEXBP', + 'INDICATOR', + 'INF', + 'INFINITY', + 'INHERIT', + 'INLINE', + 'INNER', + 'INOUT', + 'INSENSITIVE', + 'INSERT', + 'INSERTING', + 'INTEGRITY', + 'INTERPRET', + 'INTERSECT', + 'INTO', + 'IS', + 'ISNULL', + 'ISOLATION', + 'ITERATE', + 'JAVA', + 'JOIN', + 'JSON', + 'JSON_ARRAY', + 'JSON_ARRAYAGG', + 'JSON_EXISTS', + 'JSON_OBJECT', + 'JSON_OBJECTAGG', + 'JSON_QUERY', + 'JSON_TABLE', + 'JSON_VALUE', + 'KEEP', + 'KEY', + 'KEYS', + 'LABEL', + 'LAG', + 'LANGUAGE', + 'LAST_VALUE', + 'LATERAL', + 'LEAD', + 'LEAVE', + 'LEFT', + 'LEVEL2', + 'LIKE', + 'LIMIT', + 'LINKTYPE', + 'LISTAGG', + 'LOCAL', + 'LOCALDATE', + 'LOCALTIME', + 'LOCALTIMESTAMP', + 'LOCATION', + 'LOCATOR', + 'LOCK', + 'LOCKSIZE', + 'LOG', + 'LOGGED', + 'LONG', + 'LOOP', + 'MAINTAINED', + 'MASK', + 'MATCHED', + 'MATERIALIZED', + 'MAXVALUE', + 'MERGE', + 'MICROSECOND', + 'MICROSECONDS', + 'MINPCTUSED', + 'MINUTE', + 'MINUTES', + 'MINVALUE', + 'MIRROR', + 'MIXED', + 'MODE', + 'MODIFIES', + 'MONTH', + 'MONTHS', + 'NAMESPACE', + 'NAN', + 'NATIONAL', + 'NCHAR', + 'NCLOB', + 'NESTED', + 'NEW', + 'NEW_TABLE', + 'NEXTVAL', + 'NO', + 'NOCACHE', + 'NOCYCLE', + 'NODENAME', + 'NODENUMBER', + 'NOMAXVALUE', + 'NOMINVALUE', + 'NONE', + 'NOORDER', + 'NORMALIZED', + 'NOT', + 'NOTNULL', + 'NTH_VALUE', + 'NTILE', + 'NULL', + 'NULLS', + 'NVARCHAR', + 'OBID', + 'OBJECT', + 'OF', + 'OFF', + 'OFFSET', + 'OLD', + 'OLD_TABLE', + 'OMIT', + 'ON', + 'ONLY', + 'OPEN', + 'OPTIMIZE', + 'OPTION', + 'OR', + 'ORDER', + 'ORDINALITY', + 'ORGANIZE', + 'OUT', + 'OUTER', + 'OVER', + 'OVERLAY', + 'OVERRIDING', + 'PACKAGE', + 'PADDED', + 'PAGE', + 'PAGESIZE', + 'PARAMETER', + 'PART', + 'PARTITION', + 'PARTITIONED', + 'PARTITIONING', + 'PARTITIONS', + 'PASSING', + 'PASSWORD', + 'PATH', + 'PCTFREE', + 'PERCENT_RANK', + 'PERCENTILE_CONT', + 'PERCENTILE_DISC', + 'PERIOD', + 'PERMISSION', + 'PIECESIZE', + 'PIPE', + 'PLAN', + 'POSITION', + 'PREPARE', + 'PREVVAL', + 'PRIMARY', + 'PRIOR', + 'PRIQTY', + 'PRIVILEGES', + 'PROCEDURE', + 'PROGRAM', + 'PROGRAMID', + 'QUERY', + 'RANGE', + 'RANK', + 'RATIO_TO_REPORT', + 'RCDFMT', + 'READ', + 'READS', + 'RECOVERY', + 'REFERENCES', + 'REFERENCING', + 'REFRESH', + 'REGEXP_LIKE', + 'RELEASE', + 'RENAME', + 'REPEAT', + 'RESET', + 'RESIGNAL', + 'RESTART', + 'RESULT', + 'RESULT_SET_LOCATOR', + 'RETURN', + 'RETURNING', + 'RETURNS', + 'REVOKE', + 'RID', + 'RIGHT', + 'ROLLBACK', + 'ROLLUP', + 'ROUTINE', + 'ROW', + 'ROW_NUMBER', + 'ROWNUMBER', + 'ROWS', + 'RRN', + 'RUN', + 'SAVEPOINT', + 'SBCS', + 'SCALAR', + 'SCHEMA', + 'SCRATCHPAD', + 'SCROLL', + 'SEARCH', + 'SECOND', + 'SECONDS', + 'SECQTY', + 'SECURED', + 'SELECT', + 'SENSITIVE', + 'SEQUENCE', + 'SESSION', + 'SESSION_USER', + 'SET', + 'SIGNAL', + 'SIMPLE', + 'SKIP', + 'SNAN', + 'SOME', + 'SOURCE', + 'SPECIFIC', + 'SQL', + 'SQLID', + 'SQLIND_DEFAULT', + 'SQLIND_UNASSIGNED', + 'STACKED', + 'START', + 'STARTING', + 'STATEMENT', + 'STATIC', + 'STOGROUP', + 'SUBSTRING', + 'SUMMARY', + 'SYNONYM', + 'SYSTEM_TIME', + 'SYSTEM_USER', + 'TABLE', + 'TABLESPACE', + 'TABLESPACES', + 'TAG', + 'THEN', + 'THREADSAFE', + 'TIME', + 'TIMESTAMP', + 'TO', + 'TRANSACTION', + 'TRANSFER', + 'TRIGGER', + 'TRIM', + 'TRIM_ARRAY', + 'TRUE', + 'TRUNCATE', + 'TRY_CAST', + 'TYPE', + 'UNDO', + 'UNION', + 'UNIQUE', + 'UNIT', + 'UNKNOWN', + 'UNNEST', + 'UNTIL', + 'UPDATE', + 'UPDATING', + 'URI', + 'USAGE', + 'USE', + 'USER', + 'USERID', + 'USING', + 'VALUE', + 'VALUES', + 'VARIABLE', + 'VARIANT', + 'VCAT', + 'VERSION', + 'VERSIONING', + 'VIEW', + 'VOLATILE', + 'WAIT', + 'WHEN', + 'WHENEVER', + 'WHERE', + 'WHILE', + 'WITH', + 'WITHIN', + 'WITHOUT', + 'WRAPPED', + 'WRAPPER', + 'WRITE', + 'WRKSTNNAME', + 'XMLAGG', + 'XMLATTRIBUTES', + 'XMLCAST', + 'XMLCOMMENT', + 'XMLCONCAT', + 'XMLDOCUMENT', + 'XMLELEMENT', + 'XMLFOREST', + 'XMLGROUP', + 'XMLNAMESPACES', + 'XMLPARSE', + 'XMLPI', + 'XMLROW', + 'XMLSERIALIZE', + 'XMLTABLE', + 'XMLTEXT', + 'XMLVALIDATE', + 'XSLTRANSFORM', + 'XSROBJECT', + 'YEAR', + 'YEARS', + 'YES', + 'ZONE', + ], +}); diff --git a/src/sqlFormatter.ts b/src/sqlFormatter.ts index a58924b2c6..dc5e2c97f6 100644 --- a/src/sqlFormatter.ts +++ b/src/sqlFormatter.ts @@ -8,6 +8,7 @@ import { ConfigError, validateConfig } from './validateConfig.js'; const dialectNameMap: Record = { bigquery: 'bigquery', db2: 'db2', + db2i: 'db2i', hive: 'hive', mariadb: 'mariadb', mysql: 'mysql', diff --git a/static/index.html b/static/index.html index 1e6cab5505..9e2a3af4a3 100644 --- a/static/index.html +++ b/static/index.html @@ -60,6 +60,7 @@

Options

+ diff --git a/test/db2i.test.ts b/test/db2i.test.ts new file mode 100644 index 0000000000..98dce50bd3 --- /dev/null +++ b/test/db2i.test.ts @@ -0,0 +1,140 @@ +import dedent from 'dedent-js'; + +import { format as originalFormat, FormatFn } from '../src/sqlFormatter.js'; +import behavesLikeSqlFormatter from './behavesLikeSqlFormatter.js'; + +import supportsAlterTable from './features/alterTable.js'; +import supportsBetween from './features/between.js'; +import supportsCreateTable from './features/createTable.js'; +import supportsDropTable from './features/dropTable.js'; +import supportsJoin from './features/join.js'; +import supportsOperators from './features/operators.js'; +import supportsSchema from './features/schema.js'; +import supportsStrings from './features/strings.js'; +import supportsConstraints from './features/constraints.js'; +import supportsDeleteFrom from './features/deleteFrom.js'; +import supportsComments from './features/comments.js'; +import supportsCommentOn from './features/commentOn.js'; +import supportsIdentifiers from './features/identifiers.js'; +import supportsParams from './options/param.js'; +import supportsSetOperations from './features/setOperations.js'; +import supportsLimiting from './features/limiting.js'; +import supportsInsertInto from './features/insertInto.js'; +import supportsUpdate from './features/update.js'; +import supportsTruncateTable from './features/truncateTable.js'; +import supportsMergeInto from './features/mergeInto.js'; +import supportsCreateView from './features/createView.js'; + +describe('Db2iFormatter', () => { + const language = 'db2i'; + const format: FormatFn = (query, cfg = {}) => originalFormat(query, { ...cfg, language }); + + behavesLikeSqlFormatter(format); + supportsComments(format); + supportsCommentOn(format); + supportsCreateView(format, { orReplace: true }); + supportsCreateTable(format); + supportsDropTable(format); + supportsConstraints(format, ['NO ACTION', 'RESTRICT', 'CASCADE', 'SET NULL']); + supportsAlterTable(format, { + addColumn: true, + dropColumn: true, + renameColumn: true, + }); + supportsDeleteFrom(format); + supportsInsertInto(format); + supportsUpdate(format, { whereCurrentOf: true }); + supportsTruncateTable(format, { withoutTable: true }); + supportsMergeInto(format); + supportsStrings(format, ["''-qq", "X''", "U&''", "N''"]); + supportsIdentifiers(format, [`""-qq`]); + supportsBetween(format); + supportsSchema(format); + supportsOperators(format, ['**', '¬=', '¬>', '¬<', '!>', '!<', '||']); + supportsJoin(format, { without: ['NATURAL'], supportsUsing: false }); + supportsSetOperations(format, [ + 'UNION', + 'UNION ALL', + 'EXCEPT', + 'EXCEPT ALL', + 'INTERSECT', + 'INTERSECT ALL', + ]); + supportsParams(format, { positional: true, named: [':'] }); + supportsLimiting(format, { fetchFirst: true }); + + it('formats only -- as a line comment', () => { + const result = format(` + SELECT col FROM + -- This is a comment + MyTable; + `); + expect(result).toBe(dedent` + SELECT + col + FROM + -- This is a comment + MyTable; + `); + }); + + // DB2-specific string types + it('supports strings with G, GX, BX, UX prefixes', () => { + expect(format(`SELECT G'blah blah', GX'01AC', BX'0101', UX'CCF239' FROM foo`)).toBe(dedent` + SELECT + G'blah blah', + GX'01AC', + BX'0101', + UX'CCF239' + FROM + foo + `); + }); + + it('supports @, #, $ characters at the start of identifiers', () => { + expect(format(`SELECT @foo, #bar, $zap`)).toBe(dedent` + SELECT + @foo, + #bar, + $zap + `); + }); + + it('supports @, #, $ characters in named parameters', () => { + expect(format(`SELECT :foo@bar, :foo#bar, :foo$bar, :@zip, :#zap, :$zop`)).toBe(dedent` + SELECT + :foo@bar, + :foo#bar, + :foo$bar, + :@zip, + :#zap, + :$zop + `); + }); + + it('supports WITH isolation level modifiers for UPDATE statement', () => { + expect(format('UPDATE foo SET x = 10 WITH CS')).toBe(dedent` + UPDATE foo + SET + x = 10 + WITH CS + `); + }); + + it('formats ALTER TABLE ... ALTER COLUMN', () => { + expect( + format( + `ALTER TABLE t ALTER COLUMN foo SET DATA TYPE VARCHAR; + ALTER TABLE t ALTER COLUMN foo SET NOT NULL;` + ) + ).toBe(dedent` + ALTER TABLE t + ALTER COLUMN foo + SET DATA TYPE VARCHAR; + + ALTER TABLE t + ALTER COLUMN foo + SET NOT NULL; + `); + }); +}); From c5feee5c37921f43a33841d27495b6a62ed0e119 Mon Sep 17 00:00:00 2001 From: Christian Jorgensen Date: Thu, 9 Nov 2023 12:15:26 +0100 Subject: [PATCH 02/10] Fix DB2 for IBM i formatter errors --- src/languages/db2i/db2i.formatter.ts | 99 ++++++++++++++-------------- src/languages/db2i/db2i.functions.ts | 2 +- 2 files changed, 51 insertions(+), 50 deletions(-) diff --git a/src/languages/db2i/db2i.formatter.ts b/src/languages/db2i/db2i.formatter.ts index f0083f37ea..ef874bca1d 100644 --- a/src/languages/db2i/db2i.formatter.ts +++ b/src/languages/db2i/db2i.formatter.ts @@ -8,18 +8,19 @@ 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 {ROW | ROWS}', - 'FETCH {FIRST | NEXT} {ROW | ROWS} ONLY', + 'OFFSET', + 'FETCH {FIRST | NEXT}', 'LIMIT', 'FOR UPDATE [OF]', 'FOR READ ONLY', - 'OPTIMIZE FOR [ALL] {ROW | ROWS}', + 'OPTIMIZE FOR', // Data modification // - insert: 'INSERT INTO', @@ -32,9 +33,11 @@ const reservedClauses = expandPhrases([ 'UPDATE SET', 'DELETE', 'INSERT', - // Data definition - 'CREATE [OR REPLACE] TABLE [FOR SYSTEM NAME]', - 'CREATE [OR REPLACE] [RECURSIVE] VIEW [FOR SYSTEM NAME]', + // Data definition - table + 'CREATE [OR REPLACE] TABLE', + 'FOR SYSTEM NAME', + // Data definition - view + 'CREATE [OR REPLACE] [RECURSIVE] VIEW', ]); const onelineClauses = expandPhrases([ @@ -62,25 +65,31 @@ const onelineClauses = expandPhrases([ 'TRUNCATE [TABLE]', // other 'SET [CURRENT] SCHEMA', + 'SET CURRENT_SCHEMA', // https://www.ibm.com/docs/en/i/7.5?topic=reference-statements 'ALLOCATE CURSOR', - 'ALLOCATE DESCRIPTOR', - 'ALTER FUNCTION', + 'ALLOCATE [SQL] DESCRIPTOR [LOCAL | GLOBAL] SQL', + 'ALTER [SPECIFIC] FUNCTION', 'ALTER MASK', 'ALTER PERMISSION', - 'ALTER PROCEDURE', + 'ALTER [SPECIFIC] PROCEDURE', 'ALTER SEQUENCE', 'ALTER TRIGGER', - 'ASSOCIATE LOCATORS', + 'ASSOCIATE [RESULT SET] {LOCATOR | LOCATORS}', 'BEGIN DECLARE SECTION', 'CALL', 'CLOSE', - 'COMMENT', - 'COMMIT', - 'CONNECT', + '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', 'CREATE [OR REPLACE] FUNCTION', - 'CREATE INDEX', + 'CREATE [ENCODED VECTOR] INDEX', + 'CREATE UNIQUE [WHERE NOT NULL] INDEX', 'CREATE [OR REPLACE] MASK', 'CREATE [OR REPLACE] PERMISSION', 'CREATE [OR REPLACE] PROCEDURE', @@ -89,66 +98,58 @@ const onelineClauses = expandPhrases([ 'CREATE [OR REPLACE] TRIGGER', 'CREATE TYPE', 'CREATE [OR REPLACE] VARIABLE', - 'DEALLOCATE DESCRIPTOR', + 'DEALLOCATE [SQL] DESCRIPTOR [LOCAL | GLOBAL]', 'DECLARE CURSOR', 'DECLARE GLOBAL TEMPORARY TABLE', - 'DECLARE PROCEDURE', - 'DECLARE STATEMENT', - 'DECLARE VARIABLE', - 'DESCRIBE', + 'DECLARE', 'DESCRIBE CURSOR', 'DESCRIBE INPUT', - 'DESCRIBE OUTPUT', - 'DESCRIBE PROCEDURE', + 'DESCRIBE [OUTPUT]', + 'DESCRIBE {PROCEDURE | ROUTINE}', 'DESCRIBE TABLE', - 'DISCONNECT', - 'DROP', + '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', - 'EXECUTE IMMEDIATE', - 'FETCH', + 'EXECUTE [IMMEDIATE]', + // 'FETCH {NEXT | PRIOR | FIRST | LAST | BEFORE | AFTER | CURRENT} [FROM]', 'FREE LOCATOR', - 'GET DESCRIPTOR', - 'GET DIAGNOSTICS', - 'GRANT', + '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', - 'INSERT', - 'LABEL', + '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 SAVEPOINT', - 'RENAME', - 'REVOKE', - 'ROLLBACK', + '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', - 'SELECT', - '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 [SQL] DESCRIPTOR [LOCAL | GLOBAL]', 'SET ENCRYPTION PASSWORD', 'SET OPTION', - 'SET PATH', - 'SET RESULT SETS', + 'SET {[CURRENT [FUNCTION]] PATH | CURRENT_PATH}', + 'SET RESULT SETS [WITH RETURN [TO CALLER | TO CLIENT]', 'SET SESSION AUTHORIZATION', + 'SET SESSION_USER', 'SET TRANSACTION', - 'SET', - 'SIGNAL', + 'SIGNAL SQLSTATE [VALUE]', 'TAG', - 'TRANSFER OWNERSHIP', - 'TRUNCATE', - 'UPDATE', - 'VALUES', - 'VALUES INTO', - 'WHENEVER', + 'TRANSFER OWNERSHIP OF', + 'WHENEVER {NOT FOUND | SQLERROR | SQLWARNING}', ]); const reservedSetOperations = expandPhrases(['UNION [ALL]', 'EXCEPT [ALL]', 'INTERSECT [ALL]']); @@ -185,7 +186,7 @@ export const db2i: DialectOptions = { identChars: { first: '@#$' }, paramTypes: { positional: true, named: [':'] }, paramChars: { first: '@#$', rest: '@#$' }, - operators: ['**', '¬=', '¬>', '¬<', '!>', '!<', '||', 'CONCAT'], + operators: ['**', '¬=', '¬>', '¬<', '!>', '!<', '||'], }, formatOptions: { onelineClauses, diff --git a/src/languages/db2i/db2i.functions.ts b/src/languages/db2i/db2i.functions.ts index 13269f8c3d..6c933dcc2e 100644 --- a/src/languages/db2i/db2i.functions.ts +++ b/src/languages/db2i/db2i.functions.ts @@ -3,7 +3,7 @@ import { flatKeywordList } from '../../utils.js'; export const functions = flatKeywordList({ // https://www.ibm.com/docs/en/i/7.5?topic=functions-aggregate aggregate: [ - 'ANY', + // TODO: 'ANY', - conflicts with test for ANY predicate in 'operators.ys'!! 'ARRAY_AGG', 'AVG', 'CORR', From 7540fbfc75419e3dc2034adbb07542bbfff698f5 Mon Sep 17 00:00:00 2001 From: Christian Jorgensen Date: Thu, 9 Nov 2023 12:26:30 +0100 Subject: [PATCH 03/10] Combine common db2i oneline clauses --- src/languages/db2i/db2i.formatter.ts | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/src/languages/db2i/db2i.formatter.ts b/src/languages/db2i/db2i.formatter.ts index ef874bca1d..d76e17cdf0 100644 --- a/src/languages/db2i/db2i.formatter.ts +++ b/src/languages/db2i/db2i.formatter.ts @@ -69,12 +69,8 @@ const onelineClauses = expandPhrases([ // https://www.ibm.com/docs/en/i/7.5?topic=reference-statements 'ALLOCATE CURSOR', 'ALLOCATE [SQL] DESCRIPTOR [LOCAL | GLOBAL] SQL', - 'ALTER [SPECIFIC] FUNCTION', - 'ALTER MASK', - 'ALTER PERMISSION', - 'ALTER [SPECIFIC] PROCEDURE', - 'ALTER SEQUENCE', - 'ALTER TRIGGER', + 'ALTER [SPECIFIC] {FUNCTION | PROCEDURE}', + 'ALTER {MASK | PERMISSION | SEQUENCE | TRIGGER}', 'ASSOCIATE [RESULT SET] {LOCATOR | LOCATORS}', 'BEGIN DECLARE SECTION', 'CALL', @@ -86,18 +82,11 @@ const onelineClauses = expandPhrases([ 'COMMENT ON [TABLE FUNCTION] RETURN COLUMN SPECIFIC [PROCEDURE | ROUTINE]', 'COMMIT [WORK] [HOLD]', 'CONNECT [TO | RESET] USER', - 'CREATE [OR REPLACE] ALIAS', - 'CREATE [OR REPLACE] FUNCTION', + 'CREATE [OR REPLACE] {ALIAS | FUNCTION | MASK | PERMISSION | PROCEDURE | SEQUENCE | TRIGGER | VARIABLE}', 'CREATE [ENCODED VECTOR] INDEX', 'CREATE UNIQUE [WHERE NOT NULL] 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 [SQL] DESCRIPTOR [LOCAL | GLOBAL]', 'DECLARE CURSOR', 'DECLARE GLOBAL TEMPORARY TABLE', @@ -133,11 +122,7 @@ const onelineClauses = expandPhrases([ 'ROLLBACK [WORK] [HOLD | TO SAVEPOINT]', 'SAVEPOINT', '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 CURRENT {DEBUG MODE | DECFLOAT ROUNDING MODE | DEGREE | IMPLICIT XMLPARSE OPTION | TEMPORAL SYSTEM_TIME}', 'SET [SQL] DESCRIPTOR [LOCAL | GLOBAL]', 'SET ENCRYPTION PASSWORD', 'SET OPTION', From d0c9dafb6ee30a5c4ccc86b535cce6112bf5b7a1 Mon Sep 17 00:00:00 2001 From: Christian Jorgensen Date: Thu, 9 Nov 2023 12:45:28 +0100 Subject: [PATCH 04/10] Add replace and exists options to db2i tests --- test/db2i.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/db2i.test.ts b/test/db2i.test.ts index 98dce50bd3..8d264ae539 100644 --- a/test/db2i.test.ts +++ b/test/db2i.test.ts @@ -33,8 +33,8 @@ describe('Db2iFormatter', () => { supportsComments(format); supportsCommentOn(format); supportsCreateView(format, { orReplace: true }); - supportsCreateTable(format); - supportsDropTable(format); + supportsCreateTable(format, { orReplace: true }); + supportsDropTable(format, { ifExists: true }); supportsConstraints(format, ['NO ACTION', 'RESTRICT', 'CASCADE', 'SET NULL']); supportsAlterTable(format, { addColumn: true, From 6802136c104a3bb09c162481cbcf1da0fde11284 Mon Sep 17 00:00:00 2001 From: Christian Jorgensen Date: Thu, 9 Nov 2023 13:40:07 +0100 Subject: [PATCH 05/10] db2i supports 'using' on joins --- test/db2i.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/db2i.test.ts b/test/db2i.test.ts index 8d264ae539..7d713b9e33 100644 --- a/test/db2i.test.ts +++ b/test/db2i.test.ts @@ -51,7 +51,7 @@ describe('Db2iFormatter', () => { supportsBetween(format); supportsSchema(format); supportsOperators(format, ['**', '¬=', '¬>', '¬<', '!>', '!<', '||']); - supportsJoin(format, { without: ['NATURAL'], supportsUsing: false }); + supportsJoin(format, { without: ['NATURAL'], supportsUsing: true }); supportsSetOperations(format, [ 'UNION', 'UNION ALL', From dd91db17d61af00536605c12c086a97b6d40d8ca Mon Sep 17 00:00:00 2001 From: Christian Jorgensen Date: Thu, 9 Nov 2023 14:19:07 +0100 Subject: [PATCH 06/10] Fix join tests - 'id' is a reserved word in db2i --- test/features/join.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/features/join.ts b/test/features/join.ts index c12d6687d9..5cd00bd574 100644 --- a/test/features/join.ts +++ b/test/features/join.ts @@ -42,7 +42,7 @@ export default function supportsJoin( const result = format(` SELECT * FROM customers ${join} orders ON customers.customer_id = orders.customer_id - ${join} items ON items.id = orders.id; + ${join} items ON items.col1 = orders.col1; `); expect(result).toBe(dedent` SELECT @@ -50,13 +50,13 @@ export default function supportsJoin( FROM customers ${join} orders ON customers.customer_id = orders.customer_id - ${join} items ON items.id = orders.id; + ${join} items ON items.col1 = orders.col1; `); }); }); it('properly uppercases JOIN ... ON', () => { - const result = format(`select * from customers join foo on foo.id = customers.id;`, { + const result = format(`select * from customers join foo on foo.col1 = customers.col1;`, { keywordCase: 'upper', }); expect(result).toBe(dedent` @@ -64,13 +64,13 @@ export default function supportsJoin( * FROM customers - JOIN foo ON foo.id = customers.id; + JOIN foo ON foo.col1 = customers.col1; `); }); if (supportsUsing) { it('properly uppercases JOIN ... USING', () => { - const result = format(`select * from customers join foo using (id);`, { + const result = format(`select * from customers join foo using (col1);`, { keywordCase: 'upper', }); expect(result).toBe(dedent` @@ -78,7 +78,7 @@ export default function supportsJoin( * FROM customers - JOIN foo USING (id); + JOIN foo USING (col1); `); }); } @@ -87,13 +87,13 @@ export default function supportsJoin( ['CROSS APPLY', 'OUTER APPLY'].forEach(apply => { // TODO: improve formatting of custom functions it(`supports ${apply}`, () => { - const result = format(`SELECT * FROM customers ${apply} fn(customers.id)`); + const result = format(`SELECT * FROM customers ${apply} fn(customers.col1)`); expect(result).toBe(dedent` SELECT * FROM customers - ${apply} fn (customers.id) + ${apply} fn (customers.col1) `); }); }); From 2ae7fbf02355a40b93c86119679332c0a703a6fc Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Thu, 9 Nov 2023 20:02:11 +0200 Subject: [PATCH 07/10] Revert "Fix join tests - 'id' is a reserved word in db2i" ID is not actually a reserved keyword in DB2i. It's only a keyword. This reverts commit dd91db17d61af00536605c12c086a97b6d40d8ca. --- test/features/join.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/features/join.ts b/test/features/join.ts index 5cd00bd574..c12d6687d9 100644 --- a/test/features/join.ts +++ b/test/features/join.ts @@ -42,7 +42,7 @@ export default function supportsJoin( const result = format(` SELECT * FROM customers ${join} orders ON customers.customer_id = orders.customer_id - ${join} items ON items.col1 = orders.col1; + ${join} items ON items.id = orders.id; `); expect(result).toBe(dedent` SELECT @@ -50,13 +50,13 @@ export default function supportsJoin( FROM customers ${join} orders ON customers.customer_id = orders.customer_id - ${join} items ON items.col1 = orders.col1; + ${join} items ON items.id = orders.id; `); }); }); it('properly uppercases JOIN ... ON', () => { - const result = format(`select * from customers join foo on foo.col1 = customers.col1;`, { + const result = format(`select * from customers join foo on foo.id = customers.id;`, { keywordCase: 'upper', }); expect(result).toBe(dedent` @@ -64,13 +64,13 @@ export default function supportsJoin( * FROM customers - JOIN foo ON foo.col1 = customers.col1; + JOIN foo ON foo.id = customers.id; `); }); if (supportsUsing) { it('properly uppercases JOIN ... USING', () => { - const result = format(`select * from customers join foo using (col1);`, { + const result = format(`select * from customers join foo using (id);`, { keywordCase: 'upper', }); expect(result).toBe(dedent` @@ -78,7 +78,7 @@ export default function supportsJoin( * FROM customers - JOIN foo USING (col1); + JOIN foo USING (id); `); }); } @@ -87,13 +87,13 @@ export default function supportsJoin( ['CROSS APPLY', 'OUTER APPLY'].forEach(apply => { // TODO: improve formatting of custom functions it(`supports ${apply}`, () => { - const result = format(`SELECT * FROM customers ${apply} fn(customers.col1)`); + const result = format(`SELECT * FROM customers ${apply} fn(customers.id)`); expect(result).toBe(dedent` SELECT * FROM customers - ${apply} fn (customers.col1) + ${apply} fn (customers.id) `); }); }); From f2f13cc49027038424eb5d91330ab61fc0a766a6 Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Thu, 9 Nov 2023 20:06:34 +0200 Subject: [PATCH 08/10] Remove ID from DB2i reserved keywords list --- src/languages/db2i/db2i.keywords.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/languages/db2i/db2i.keywords.ts b/src/languages/db2i/db2i.keywords.ts index 95b6504fd2..0d254ddd57 100644 --- a/src/languages/db2i/db2i.keywords.ts +++ b/src/languages/db2i/db2i.keywords.ts @@ -2,6 +2,8 @@ import { flatKeywordList } from '../../utils.js'; export const keywords = flatKeywordList({ // https://www.ibm.com/docs/en/i/7.5?topic=words-reserved + // TODO: This list likely contains all keywords, not only the reserved ones, + // try to filter it down to just the reserved keywords. standard: [ 'ABSENT', 'ACCORDING', @@ -189,7 +191,7 @@ export const keywords = flatKeywordList({ 'HOLD', 'HOUR', 'HOURS', - 'ID', + // 'ID', Not actually a reserved keyword 'IDENTITY', 'IF', 'IGNORE', From a66d2fd3cd246ae9197de97e1792da919f9215a5 Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Thu, 9 Nov 2023 20:08:33 +0200 Subject: [PATCH 09/10] Add Christian Jorgensen to contributors list --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index e70abaeff7..fbbfee1fd4 100644 --- a/AUTHORS +++ b/AUTHORS @@ -10,6 +10,7 @@ Andrew Benjamin Bellamy bingou Boris Verkhovskiy +Christian Jorgensen Christopher Manouvrier Damon Davison Davut Can Abacigil From 31e7b7ee236f65d29aeb0e000d6fbca2c3bb94cb Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Thu, 9 Nov 2023 20:27:32 +0200 Subject: [PATCH 10/10] Label the DB2i dialect as experimental for now --- docs/dialect.md | 2 +- docs/language.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/dialect.md b/docs/dialect.md index 0f35ad4a3d..fc973f58f5 100644 --- a/docs/dialect.md +++ b/docs/dialect.md @@ -23,7 +23,7 @@ The following dialects can be imported from `"sql-formatter"` module: - `sql` - [Standard SQL][] - `bigquery` - [GCP BigQuery][] - `db2` - [IBM DB2][] -- `db2i` - [IBM DB2i][] +- `db2i` - [IBM DB2i][] (experimental) - `hive` - [Apache Hive][] - `mariadb` - [MariaDB][] - `mysql` - [MySQL][] diff --git a/docs/language.md b/docs/language.md index 9b586c58ec..df38263c11 100644 --- a/docs/language.md +++ b/docs/language.md @@ -15,7 +15,7 @@ const result = format('SELECT * FROM tbl', { dialect: 'sqlite' }); - `"sql"` - (default) [Standard SQL][] - `"bigquery"` - [GCP BigQuery][] - `"db2"` - [IBM DB2][] -- `"db2i"` - [IBM DB2i][] +- `"db2i"` - [IBM DB2i][] (experimental) - `"hive"` - [Apache Hive][] - `"mariadb"` - [MariaDB][] - `"mysql"` - [MySQL][]