From b2f4281a62c5020084af34978f760b0c45913cf8 Mon Sep 17 00:00:00 2001 From: taozhi8833998 Date: Mon, 27 May 2024 08:51:37 +0800 Subject: [PATCH] fix: create database with quoted db name in all dialect --- pegjs/athena.pegjs | 2 +- pegjs/bigquery.pegjs | 2 +- pegjs/db2.pegjs | 2 +- pegjs/flinksql.pegjs | 2 +- pegjs/hive.pegjs | 2 +- pegjs/mariadb.pegjs | 2 +- pegjs/mysql.pegjs | 2 +- pegjs/noql.pegjs | 4 ++-- pegjs/postgresql.pegjs | 4 ++-- pegjs/redshift.pegjs | 4 ++-- pegjs/snowflake.pegjs | 4 ++-- pegjs/sqlite.pegjs | 2 +- pegjs/transactsql.pegjs | 2 +- pegjs/trino.pegjs | 4 ++-- src/create.js | 2 +- test/create.spec.js | 8 ++++---- 16 files changed, 24 insertions(+), 24 deletions(-) diff --git a/pegjs/athena.pegjs b/pegjs/athena.pegjs index c81a09bc..5c964501 100644 --- a/pegjs/athena.pegjs +++ b/pegjs/athena.pegjs @@ -287,7 +287,7 @@ create_db_stmt = a:KW_CREATE __ k:(KW_DATABASE / KW_SCHEME) __ ife:if_not_exists_stmt? __ - t:ident_name __ + t:ident_without_kw_type __ c:create_db_definition? { return { tableList: Array.from(tableList), diff --git a/pegjs/bigquery.pegjs b/pegjs/bigquery.pegjs index 3f31f91a..f4334dff 100644 --- a/pegjs/bigquery.pegjs +++ b/pegjs/bigquery.pegjs @@ -904,7 +904,7 @@ create_db_stmt = a:KW_CREATE __ k:(KW_DATABASE / KW_SCHEMA) __ ife:if_not_exists_stmt? __ - t:ident_name __ + t:ident_without_kw_type __ c:create_db_definition? { return { tableList: Array.from(tableList), diff --git a/pegjs/db2.pegjs b/pegjs/db2.pegjs index 0857d6bb..a7f74abb 100644 --- a/pegjs/db2.pegjs +++ b/pegjs/db2.pegjs @@ -286,7 +286,7 @@ create_db_stmt = a:KW_CREATE __ k:(KW_DATABASE / KW_SCHEME) __ ife:if_not_exists_stmt? __ - t:ident_name __ + t:ident_without_kw_type __ c:create_db_definition? { return { tableList: Array.from(tableList), diff --git a/pegjs/flinksql.pegjs b/pegjs/flinksql.pegjs index 36691a7d..eb3eea34 100644 --- a/pegjs/flinksql.pegjs +++ b/pegjs/flinksql.pegjs @@ -628,7 +628,7 @@ create_db_stmt = a:KW_CREATE __ k:(KW_DATABASE / KW_SCHEME) __ ife:if_not_exists_stmt? __ - t:ident_name __ + t:ident_without_kw_type __ c:create_db_definition? { return { tableList: Array.from(tableList), diff --git a/pegjs/hive.pegjs b/pegjs/hive.pegjs index d116be61..ce83e2cb 100644 --- a/pegjs/hive.pegjs +++ b/pegjs/hive.pegjs @@ -287,7 +287,7 @@ create_db_stmt = a:KW_CREATE __ k:(KW_DATABASE / KW_SCHEME) __ ife:if_not_exists_stmt? __ - t:ident_name __ + t:ident_without_kw_type __ c:create_db_definition? { return { tableList: Array.from(tableList), diff --git a/pegjs/mariadb.pegjs b/pegjs/mariadb.pegjs index d3e6069e..a49f4d3a 100644 --- a/pegjs/mariadb.pegjs +++ b/pegjs/mariadb.pegjs @@ -344,7 +344,7 @@ create_db_stmt = a:KW_CREATE __ k:(KW_DATABASE / KW_SCHEMA) __ ife:if_not_exists_stmt? __ - t:ident_name __ + t:ident_without_kw_type __ c:create_db_definition? { return { tableList: Array.from(tableList), diff --git a/pegjs/mysql.pegjs b/pegjs/mysql.pegjs index 7c220b84..8f412b3b 100644 --- a/pegjs/mysql.pegjs +++ b/pegjs/mysql.pegjs @@ -540,7 +540,7 @@ create_db_stmt = a:KW_CREATE __ k:(KW_DATABASE / KW_SCHEMA) __ ife:if_not_exists_stmt? __ - t:ident_name __ + t:ident_without_kw_type __ c:create_db_definition? { return { tableList: Array.from(tableList), diff --git a/pegjs/noql.pegjs b/pegjs/noql.pegjs index 206e29b6..8d2e1d93 100644 --- a/pegjs/noql.pegjs +++ b/pegjs/noql.pegjs @@ -359,14 +359,14 @@ create_db_stmt = a:KW_CREATE __ k:(KW_DATABASE / KW_SCHEMA) __ ife:if_not_exists_stmt? __ - t:ident_name __ + t:ident_without_kw_type __ c:create_db_definition? { /* export type create_db_stmt = { type: 'create', keyword: 'database' | 'schema', if_not_exists?: 'if not exists', - database: string, + database: ident_without_kw_type, create_definitions?: create_db_definition } => AstStatement diff --git a/pegjs/postgresql.pegjs b/pegjs/postgresql.pegjs index 39729da6..23b70292 100644 --- a/pegjs/postgresql.pegjs +++ b/pegjs/postgresql.pegjs @@ -372,14 +372,14 @@ create_db_stmt = a:KW_CREATE __ k:(KW_DATABASE / KW_SCHEMA) __ ife:if_not_exists_stmt? __ - t:ident_name __ + t:ident_without_kw_type __ c:create_db_definition? { /* export type create_db_stmt_t = { type: 'create', keyword: 'database' | 'schema', if_not_exists?: 'if not exists', - database: string, + database: ident_without_kw_type, create_definitions?: create_db_definition } => AstStatement diff --git a/pegjs/redshift.pegjs b/pegjs/redshift.pegjs index 5b9528c5..c5730feb 100644 --- a/pegjs/redshift.pegjs +++ b/pegjs/redshift.pegjs @@ -369,14 +369,14 @@ create_db_stmt = a:KW_CREATE __ k:(KW_DATABASE / KW_SCHEMA) __ ife:if_not_exists_stmt? __ - t:ident_name __ + t:ident_without_kw_type __ c:create_db_definition? { /* export type create_db_stmt = { type: 'create', keyword: 'database' | 'schema', if_not_exists?: 'if not exists', - database: string, + database: ident_without_kw_type, create_definitions?: create_db_definition } => AstStatement diff --git a/pegjs/snowflake.pegjs b/pegjs/snowflake.pegjs index 98b224c2..29f01816 100644 --- a/pegjs/snowflake.pegjs +++ b/pegjs/snowflake.pegjs @@ -351,14 +351,14 @@ create_db_stmt = a:KW_CREATE __ k:(KW_DATABASE / KW_SCHEMA) __ ife:if_not_exists_stmt? __ - t:ident_name __ + t:ident_without_kw_type __ c:create_db_definition? { /* export type create_db_stmt = { type: 'create', keyword: 'database' | 'schema', if_not_exists?: 'if not exists', - database: string, + database: ident_without_kw_type, create_definitions?: create_db_definition } => AstStatement diff --git a/pegjs/sqlite.pegjs b/pegjs/sqlite.pegjs index 1bc1d990..e4c29935 100644 --- a/pegjs/sqlite.pegjs +++ b/pegjs/sqlite.pegjs @@ -365,7 +365,7 @@ create_db_stmt = a:KW_CREATE __ k:(KW_DATABASE / KW_SCHEME) __ ife:if_not_exists_stmt? __ - t:ident_name __ + t:ident_without_kw_type __ c:create_db_definition? { return { tableList: Array.from(tableList), diff --git a/pegjs/transactsql.pegjs b/pegjs/transactsql.pegjs index 2aa6e28d..f9d698d7 100644 --- a/pegjs/transactsql.pegjs +++ b/pegjs/transactsql.pegjs @@ -380,7 +380,7 @@ create_db_stmt = a:KW_CREATE __ k:(KW_DATABASE / KW_SCHEME) __ ife:if_not_exists_stmt? __ - t:ident_name __ + t:ident_without_kw_type __ c:create_db_definition? { return { tableList: Array.from(tableList), diff --git a/pegjs/trino.pegjs b/pegjs/trino.pegjs index 17954878..7601cee8 100644 --- a/pegjs/trino.pegjs +++ b/pegjs/trino.pegjs @@ -380,14 +380,14 @@ create_db_stmt = a:KW_CREATE __ k:(KW_DATABASE / KW_SCHEMA) __ ife:if_not_exists_stmt? __ - t:ident_name __ + t:ident_without_kw_type __ c:create_db_definition? { /* export type create_db_stmt = { type: 'create', keyword: 'database' | 'schema', if_not_exists?: 'if not exists', - database: string, + database: ident_without_kw_type, create_definitions?: create_db_definition } => AstStatement diff --git a/src/create.js b/src/create.js index e60e7b51..976d6c73 100644 --- a/src/create.js +++ b/src/create.js @@ -215,7 +215,7 @@ function createDatabaseToSQL(stmt) { toUpper(type), toUpper(keyword), toUpper(ifNotExists), - columnIdentifierToSql(database), + literalToSQL(database), ] if (createDefinition) sql.push(createDefinition.map(tableOptionToSQL).join(' ')) return sql.filter(hasVal).join(' ') diff --git a/test/create.spec.js b/test/create.spec.js index 8cbe5c5f..aae23c1b 100644 --- a/test/create.spec.js +++ b/test/create.spec.js @@ -590,10 +590,10 @@ describe('create', () => { describe('create database', () => { it('should support create database', () => { - expect(getParsedSql('CREATE DATABASE abc')).to.equal('CREATE DATABASE `abc`') - expect(getParsedSql('CREATE DATABASE IF NOT EXISTS abc')).to.equal('CREATE DATABASE IF NOT EXISTS `abc`') - expect(getParsedSql('CREATE DATABASE IF NOT EXISTS abc default CHARACTER SET utf8mb4')).to.equal('CREATE DATABASE IF NOT EXISTS `abc` DEFAULT CHARACTER SET utf8mb4') - expect(getParsedSql('CREATE DATABASE IF NOT EXISTS abc CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci')).to.equal('CREATE DATABASE IF NOT EXISTS `abc` CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci') + expect(getParsedSql('CREATE DATABASE abc')).to.equal('CREATE DATABASE abc') + expect(getParsedSql('CREATE DATABASE IF NOT EXISTS `abc`')).to.equal('CREATE DATABASE IF NOT EXISTS `abc`') + expect(getParsedSql('CREATE DATABASE IF NOT EXISTS abc default CHARACTER SET utf8mb4')).to.equal('CREATE DATABASE IF NOT EXISTS abc DEFAULT CHARACTER SET utf8mb4') + expect(getParsedSql('CREATE DATABASE IF NOT EXISTS `abc` CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci')).to.equal('CREATE DATABASE IF NOT EXISTS `abc` CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci') }) })