-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix 4501 PostgreSql sequence (#4528)
* create sequence grammer * test create sequence grammer * add mixin to validate owner table * add fixture tests with variants * fix spotless * add currval, nextval, lastval and setval * add integration tests * add function setval add test * Update PostgreSqlTest.kt fix spotless whitespace --------- Co-authored-by: griffio <griffio@users.noreply.github.com>
- Loading branch information
Showing
6 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...main/kotlin/app/cash/sqldelight/dialects/postgresql/grammar/mixins/CreateSequenceMixin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package app.cash.sqldelight.dialects.postgresql.grammar.mixins | ||
|
||
import app.cash.sqldelight.dialects.postgresql.grammar.psi.PostgreSqlCreateSequenceStmt | ||
import com.alecstrong.sql.psi.core.psi.QueryElement | ||
import com.alecstrong.sql.psi.core.psi.SqlCompositeElementImpl | ||
import com.intellij.lang.ASTNode | ||
import com.intellij.psi.PsiElement | ||
|
||
internal abstract class CreateSequenceMixin(node: ASTNode) : | ||
SqlCompositeElementImpl(node), | ||
PostgreSqlCreateSequenceStmt { | ||
// Query any owner tableName element to allow the columnName to be resolved | ||
override fun queryAvailable(child: PsiElement): Collection<QueryElement.QueryResult> { | ||
return tablesAvailable(child).map { it.query } | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
dialects/postgresql/src/test/fixtures_postgresql/create-sequence/Sample.s
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
CREATE TABLE abc ( | ||
id INTEGER PRIMARY KEY | ||
); | ||
|
||
CREATE SEQUENCE integers_01; | ||
|
||
CREATE SEQUENCE integers_02 START 31 | ||
OWNED BY abc.id; | ||
|
||
CREATE SEQUENCE integers_03 AS INTEGER | ||
INCREMENT 10 | ||
MINVALUE 100 | ||
MAXVALUE 250000 | ||
START 101 | ||
CACHE 1 | ||
NO CYCLE; | ||
|
||
CREATE TEMPORARY SEQUENCE IF NOT EXISTS integers_04 AS SMALLINT | ||
INCREMENT BY 2 | ||
NO MINVALUE | ||
NO MAXVALUE | ||
START WITH 3 | ||
CYCLE; | ||
|
||
SELECT nextval('integers_01'); | ||
|
||
SELECT nextval('integers_02'); | ||
|
||
SELECT nextval('integers_03'); | ||
|
||
SELECT nextval('integers_04'); |
21 changes: 21 additions & 0 deletions
21
...on-postgresql/src/main/sqldelight/app/cash/sqldelight/postgresql/integration/Sequences.sq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
CREATE SEQUENCE serial START 200; | ||
|
||
CREATE TABLE sigma ( | ||
id INTEGER PRIMARY KEY | ||
); | ||
|
||
insertNextVal: | ||
INSERT INTO sigma VALUES (nextval('serial')) | ||
RETURNING id; | ||
|
||
selectNextVal: | ||
SELECT nextval('serial'); | ||
|
||
selectCurrentVal: | ||
SELECT currval('serial'); | ||
|
||
selectLastVal: | ||
SELECT lastval(); | ||
|
||
selectSetVal: | ||
SELECT setval('serial', 200, TRUE); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters