From d44ca8d93dc7f43b481b0da4b31fe3f2d47d7ebc Mon Sep 17 00:00:00 2001 From: Kevin Cianfarini Date: Fri, 24 Sep 2021 20:16:08 -0400 Subject: [PATCH 1/5] Add grammar for PostgreSQL `GENERATED` columns Closes #294 --- .../com/alecstrong/sql/psi/core/postgresql/PostgreSql.bnf | 6 ++++++ core/src/test/fixtures_postgresql/column_types/Sample.s | 6 ++++++ .../test/kotlin/com/alecstrong/sql/psi/core/FixturesTest.kt | 2 ++ 3 files changed, 14 insertions(+) diff --git a/core/src/main/kotlin/com/alecstrong/sql/psi/core/postgresql/PostgreSql.bnf b/core/src/main/kotlin/com/alecstrong/sql/psi/core/postgresql/PostgreSql.bnf index e6408292..c5f80df7 100644 --- a/core/src/main/kotlin/com/alecstrong/sql/psi/core/postgresql/PostgreSql.bnf +++ b/core/src/main/kotlin/com/alecstrong/sql/psi/core/postgresql/PostgreSql.bnf @@ -60,6 +60,7 @@ column_constraint ::= [ CONSTRAINT {identifier} ] ( [ NOT ] NULL {conflict_clause} | UNIQUE {conflict_clause} | {check_constraint} | + generated_clause | {default_constraint} | COLLATE {collation_name} | {foreign_key_clause} @@ -93,6 +94,11 @@ bind_parameter ::= ( '?' | ':' {identifier} ) { override = true } +// TODO include the sequence creation clause '(' create_sequence_stmt ')' +identity_clause ::= 'IDENTITY' + +generated_clause ::= 'GENERATED' ('ALWAYS' | 'BY' 'DEFAULT') 'AS' ( identity_clause | (<> ['STORED']) ) + small_int_data_type ::= 'SMALLINT' | 'INT2' int_data_type ::= 'INTEGER' | 'INT' | 'INT4' big_int_data_type ::= 'BIGINT' | 'INT8' diff --git a/core/src/test/fixtures_postgresql/column_types/Sample.s b/core/src/test/fixtures_postgresql/column_types/Sample.s index 411a8f19..431c8ff8 100644 --- a/core/src/test/fixtures_postgresql/column_types/Sample.s +++ b/core/src/test/fixtures_postgresql/column_types/Sample.s @@ -45,6 +45,12 @@ CREATE TABLE all_types( some_timestamptz_shorthand TIMESTAMPTZ, + some_always_generated_identity BIGINT GENERATED ALWAYS AS IDENTITY, + some_default_generated_identity BIGINT GENERATED BY DEFAULT AS IDENTITY, + + height_in_cm NUMERIC, + generated_height_in_cm_stored NUMERIC GENERATED ALWAYS AS ( height_in_cm / 2.54 ) STORED, + some_boolean BOOLEAN, some_bool BOOL, some_json JSON, diff --git a/core/src/test/kotlin/com/alecstrong/sql/psi/core/FixturesTest.kt b/core/src/test/kotlin/com/alecstrong/sql/psi/core/FixturesTest.kt index e7e6ec96..6663a41c 100644 --- a/core/src/test/kotlin/com/alecstrong/sql/psi/core/FixturesTest.kt +++ b/core/src/test/kotlin/com/alecstrong/sql/psi/core/FixturesTest.kt @@ -2,6 +2,7 @@ package com.alecstrong.sql.psi.core import com.intellij.psi.PsiDocumentManager import com.intellij.psi.PsiElement +import org.junit.Assume.assumeTrue import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.Parameterized @@ -11,6 +12,7 @@ import java.io.File @RunWith(Parameterized::class) class FixturesTest(val dialect: DialectPreset, val name: String, val fixtureRoot: File) { @Test fun execute() { + assumeTrue(dialect == DialectPreset.POSTGRESQL) dialect.setup() val parser = TestHeadlessParser() val errors = ArrayList() From 09fc32e0317e854f5508378ddd786e223c8e16aa Mon Sep 17 00:00:00 2001 From: Kevin Cianfarini Date: Fri, 24 Sep 2021 20:18:52 -0400 Subject: [PATCH 2/5] Remove test assumption --- core/src/test/kotlin/com/alecstrong/sql/psi/core/FixturesTest.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/core/src/test/kotlin/com/alecstrong/sql/psi/core/FixturesTest.kt b/core/src/test/kotlin/com/alecstrong/sql/psi/core/FixturesTest.kt index 6663a41c..d5ba0740 100644 --- a/core/src/test/kotlin/com/alecstrong/sql/psi/core/FixturesTest.kt +++ b/core/src/test/kotlin/com/alecstrong/sql/psi/core/FixturesTest.kt @@ -12,7 +12,6 @@ import java.io.File @RunWith(Parameterized::class) class FixturesTest(val dialect: DialectPreset, val name: String, val fixtureRoot: File) { @Test fun execute() { - assumeTrue(dialect == DialectPreset.POSTGRESQL) dialect.setup() val parser = TestHeadlessParser() val errors = ArrayList() From 52429ebb3eae6ea69ae8ed4fb8e3338d0e03edf2 Mon Sep 17 00:00:00 2001 From: Kevin Cianfarini Date: Fri, 24 Sep 2021 20:20:54 -0400 Subject: [PATCH 3/5] Removed unused import --- core/src/test/kotlin/com/alecstrong/sql/psi/core/FixturesTest.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/core/src/test/kotlin/com/alecstrong/sql/psi/core/FixturesTest.kt b/core/src/test/kotlin/com/alecstrong/sql/psi/core/FixturesTest.kt index d5ba0740..e7e6ec96 100644 --- a/core/src/test/kotlin/com/alecstrong/sql/psi/core/FixturesTest.kt +++ b/core/src/test/kotlin/com/alecstrong/sql/psi/core/FixturesTest.kt @@ -2,7 +2,6 @@ package com.alecstrong.sql.psi.core import com.intellij.psi.PsiDocumentManager import com.intellij.psi.PsiElement -import org.junit.Assume.assumeTrue import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.Parameterized From 219b2aa05a6255216554c257a39ca9d329e01b72 Mon Sep 17 00:00:00 2001 From: Kevin Cianfarini Date: Fri, 24 Sep 2021 20:35:48 -0400 Subject: [PATCH 4/5] Fix grammar to line up with postgres create table documentation --- .../com/alecstrong/sql/psi/core/postgresql/PostgreSql.bnf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/kotlin/com/alecstrong/sql/psi/core/postgresql/PostgreSql.bnf b/core/src/main/kotlin/com/alecstrong/sql/psi/core/postgresql/PostgreSql.bnf index c5f80df7..d67be0ea 100644 --- a/core/src/main/kotlin/com/alecstrong/sql/psi/core/postgresql/PostgreSql.bnf +++ b/core/src/main/kotlin/com/alecstrong/sql/psi/core/postgresql/PostgreSql.bnf @@ -97,7 +97,7 @@ bind_parameter ::= ( '?' | ':' {identifier} ) { // TODO include the sequence creation clause '(' create_sequence_stmt ')' identity_clause ::= 'IDENTITY' -generated_clause ::= 'GENERATED' ('ALWAYS' | 'BY' 'DEFAULT') 'AS' ( identity_clause | (<> ['STORED']) ) +generated_clause ::= 'GENERATED' ( ('ALWAYS' 'AS' <> 'STORED') | ( ('ALWAYS' | 'BY' 'DEFAULT') 'AS' identity_clause ) ) small_int_data_type ::= 'SMALLINT' | 'INT2' int_data_type ::= 'INTEGER' | 'INT' | 'INT4' From d6e33b8490dac358eb629c772cc99b5db5051a80 Mon Sep 17 00:00:00 2001 From: Kevin Cianfarini Date: Mon, 27 Sep 2021 12:40:09 -0400 Subject: [PATCH 5/5] Address comments --- .../com/alecstrong/sql/psi/core/postgresql/PostgreSql.bnf | 1 - core/src/test/fixtures_postgresql/column_types/Sample.s | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/core/src/main/kotlin/com/alecstrong/sql/psi/core/postgresql/PostgreSql.bnf b/core/src/main/kotlin/com/alecstrong/sql/psi/core/postgresql/PostgreSql.bnf index d67be0ea..7b147729 100644 --- a/core/src/main/kotlin/com/alecstrong/sql/psi/core/postgresql/PostgreSql.bnf +++ b/core/src/main/kotlin/com/alecstrong/sql/psi/core/postgresql/PostgreSql.bnf @@ -94,7 +94,6 @@ bind_parameter ::= ( '?' | ':' {identifier} ) { override = true } -// TODO include the sequence creation clause '(' create_sequence_stmt ')' identity_clause ::= 'IDENTITY' generated_clause ::= 'GENERATED' ( ('ALWAYS' 'AS' <> 'STORED') | ( ('ALWAYS' | 'BY' 'DEFAULT') 'AS' identity_clause ) ) diff --git a/core/src/test/fixtures_postgresql/column_types/Sample.s b/core/src/test/fixtures_postgresql/column_types/Sample.s index 431c8ff8..defec99b 100644 --- a/core/src/test/fixtures_postgresql/column_types/Sample.s +++ b/core/src/test/fixtures_postgresql/column_types/Sample.s @@ -49,7 +49,7 @@ CREATE TABLE all_types( some_default_generated_identity BIGINT GENERATED BY DEFAULT AS IDENTITY, height_in_cm NUMERIC, - generated_height_in_cm_stored NUMERIC GENERATED ALWAYS AS ( height_in_cm / 2.54 ) STORED, + generated_height_in_inches_stored NUMERIC GENERATED ALWAYS AS ( height_in_cm / 2.54 ) STORED, some_boolean BOOLEAN, some_bool BOOL,