From 335875eee6a594f8fe11b854db5b69ccb4cf3e59 Mon Sep 17 00:00:00 2001 From: hfhbd Date: Fri, 15 Jul 2022 14:32:14 +0200 Subject: [PATCH 1/2] PostgreSQL: Support using DEFAULT for generated columns in INSERT https://github.com/AlecStrong/sql-psi/issues/287 --- .../sqldelight/dialects/postgresql/grammar/PostgreSql.bnf | 2 +- .../src/test/fixtures_postgresql/serial/ExplicitNotNull.s | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/dialects/postgresql/src/main/kotlin/app/cash/sqldelight/dialects/postgresql/grammar/PostgreSql.bnf b/dialects/postgresql/src/main/kotlin/app/cash/sqldelight/dialects/postgresql/grammar/PostgreSql.bnf index 56e4a953845..d19cf4f4bd3 100644 --- a/dialects/postgresql/src/main/kotlin/app/cash/sqldelight/dialects/postgresql/grammar/PostgreSql.bnf +++ b/dialects/postgresql/src/main/kotlin/app/cash/sqldelight/dialects/postgresql/grammar/PostgreSql.bnf @@ -97,7 +97,7 @@ type_name ::= ( implements = "com.alecstrong.sql.psi.core.psi.SqlTypeName" override = true } -bind_parameter ::= ( '?' | ':' {identifier} ) { +bind_parameter ::= ( 'DEFAULT' | '?' | ':' {identifier} ) { extends = "com.alecstrong.sql.psi.core.psi.impl.SqlBindParameterImpl" implements = "com.alecstrong.sql.psi.core.psi.SqlBindParameter" override = true diff --git a/dialects/postgresql/src/test/fixtures_postgresql/serial/ExplicitNotNull.s b/dialects/postgresql/src/test/fixtures_postgresql/serial/ExplicitNotNull.s index 35e6f83b7ab..7ecd2e3dafe 100644 --- a/dialects/postgresql/src/test/fixtures_postgresql/serial/ExplicitNotNull.s +++ b/dialects/postgresql/src/test/fixtures_postgresql/serial/ExplicitNotNull.s @@ -19,5 +19,5 @@ CREATE TABLE test2( words TEXT NOT NULL ); -INSERT INTO test2(words) -VALUES ('foo'); +INSERT INTO test2(id, words) +VALUES (DEFAULT, 'foo'); From aa5eb6dfc8bd04afb1572c724caefecea569a3f0 Mon Sep 17 00:00:00 2001 From: hfhbd Date: Fri, 15 Jul 2022 14:55:35 +0200 Subject: [PATCH 2/2] PostgreSQL: Use more predefined token --- .../dialects/postgresql/grammar/PostgreSql.bnf | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/dialects/postgresql/src/main/kotlin/app/cash/sqldelight/dialects/postgresql/grammar/PostgreSql.bnf b/dialects/postgresql/src/main/kotlin/app/cash/sqldelight/dialects/postgresql/grammar/PostgreSql.bnf index d19cf4f4bd3..e485738a661 100644 --- a/dialects/postgresql/src/main/kotlin/app/cash/sqldelight/dialects/postgresql/grammar/PostgreSql.bnf +++ b/dialects/postgresql/src/main/kotlin/app/cash/sqldelight/dialects/postgresql/grammar/PostgreSql.bnf @@ -9,6 +9,7 @@ parserImports=[ "static com.alecstrong.sql.psi.core.psi.SqlTypes.ABORT" + "static com.alecstrong.sql.psi.core.psi.SqlTypes.ALWAYS" "static com.alecstrong.sql.psi.core.psi.SqlTypes.AS" "static com.alecstrong.sql.psi.core.psi.SqlTypes.ASC" "static com.alecstrong.sql.psi.core.psi.SqlTypes.BY" @@ -16,10 +17,13 @@ "static com.alecstrong.sql.psi.core.psi.SqlTypes.CONFLICT" "static com.alecstrong.sql.psi.core.psi.SqlTypes.CONSTRAINT" "static com.alecstrong.sql.psi.core.psi.SqlTypes.DELETE" + "static com.alecstrong.sql.psi.core.psi.SqlTypes.DEFAULT" "static com.alecstrong.sql.psi.core.psi.SqlTypes.DESC" "static com.alecstrong.sql.psi.core.psi.SqlTypes.DO" + "static com.alecstrong.sql.psi.core.psi.SqlTypes.DROP" "static com.alecstrong.sql.psi.core.psi.SqlTypes.FAIL" "static com.alecstrong.sql.psi.core.psi.SqlTypes.FROM" + "static com.alecstrong.sql.psi.core.psi.SqlTypes.GENERATED" "static com.alecstrong.sql.psi.core.psi.SqlTypes.ID" "static com.alecstrong.sql.psi.core.psi.SqlTypes.STRING" "static com.alecstrong.sql.psi.core.psi.SqlTypes.IGNORE" @@ -97,7 +101,7 @@ type_name ::= ( implements = "com.alecstrong.sql.psi.core.psi.SqlTypeName" override = true } -bind_parameter ::= ( 'DEFAULT' | '?' | ':' {identifier} ) { +bind_parameter ::= ( DEFAULT | '?' | ':' {identifier} ) { extends = "com.alecstrong.sql.psi.core.psi.impl.SqlBindParameterImpl" implements = "com.alecstrong.sql.psi.core.psi.SqlBindParameter" override = true @@ -105,7 +109,7 @@ bind_parameter ::= ( 'DEFAULT' | '?' | ':' {identifier} ) { identity_clause ::= 'IDENTITY' -generated_clause ::= 'GENERATED' ( ('ALWAYS' 'AS' <> 'STORED') | ( ('ALWAYS' | 'BY' 'DEFAULT') 'AS' identity_clause ) ) { +generated_clause ::= GENERATED ( (ALWAYS AS <> 'STORED') | ( (ALWAYS | BY DEFAULT) AS identity_clause ) ) { extends = "com.alecstrong.sql.psi.core.psi.impl.SqlGeneratedClauseImpl" implements = "com.alecstrong.sql.psi.core.psi.SqlGeneratedClause" override = true @@ -126,7 +130,7 @@ string_data_type ::= ((( 'CHARACTER' 'VARYING' ) | 'VARCHAR' | 'CHARACTER' | 'CH uuid_data_type ::= 'UUID' -date_data_type ::= 'DATE' | (('TIME' | 'TIMESTAMP') [ '(' {signed_number} ')' ] [('WITH' | 'WITHOUT') 'TIME' 'ZONE']) | 'TIMESTAMPTZ' | 'INTERVAL' +date_data_type ::= 'DATE' | (('TIME' | 'TIMESTAMP') [ '(' {signed_number} ')' ] [(WITH | 'WITHOUT') 'TIME' 'ZONE']) | 'TIMESTAMPTZ' | 'INTERVAL' boolean_data_type ::= 'BOOLEAN' | 'BOOL' @@ -218,12 +222,12 @@ alter_table_column_alias ::= id | string { ] } -alter_table_drop_column ::= 'DROP' [ COLUMN ] {column_name} { +alter_table_drop_column ::= DROP [ COLUMN ] {column_name} { mixin = "app.cash.sqldelight.dialects.postgresql.grammar.mixins.AlterTableDropColumnMixin" pin = 1 } -compound_select_stmt ::= [ {with_clause} ] {select_stmt} ( {compound_operator} {select_stmt} ) * [ 'ORDER' 'BY' {ordering_term} ( ',' {ordering_term} ) * ] [ 'LIMIT' {limiting_term} ] [ ( 'OFFSET' | ',' ) {limiting_term} ] [ 'FOR' 'UPDATE' [ 'SKIP' 'LOCKED' ] ] { +compound_select_stmt ::= [ {with_clause} ] {select_stmt} ( {compound_operator} {select_stmt} ) * [ ORDER BY {ordering_term} ( ',' {ordering_term} ) * ] [ LIMIT {limiting_term} ] [ ( OFFSET | ',' ) {limiting_term} ] [ 'FOR' UPDATE [ 'SKIP' 'LOCKED' ] ] { extends = "com.alecstrong.sql.psi.core.psi.impl.SqlCompoundSelectStmtImpl" implements = "com.alecstrong.sql.psi.core.psi.SqlCompoundSelectStmt" override = true @@ -250,7 +254,7 @@ extension_stmt ::= copy_stdin { override = true } -copy_stdin ::= 'COPY' [ {database_name} '.' ] {table_name} [ AS {table_alias} ] [ '(' {column_name} ( ',' {column_name} ) * ')' ] FROM 'STDIN' [ [ 'WITH' ] '(' copy_option ( ',' copy_option) * ')' ] [ WHERE <> ] { +copy_stdin ::= 'COPY' [ {database_name} '.' ] {table_name} [ AS {table_alias} ] [ '(' {column_name} ( ',' {column_name} ) * ')' ] FROM 'STDIN' [ [ WITH ] '(' copy_option ( ',' copy_option) * ')' ] [ WHERE <> ] { mixin = "app.cash.sqldelight.dialects.postgresql.grammar.mixins.CopyMixin" }