Skip to content

Support PostgreSQL RETURNING #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,41 @@
psiClassPrefix = "PostgreSql"

parserImports=[
"static com.alecstrong.sql.psi.core.psi.SqlTypes.CONSTRAINT"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.PRIMARY"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.KEY"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.ABORT"
"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"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.COLLATE"
"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.DESC"
"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.IGNORE"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.INSERT"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.INTO"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.KEY"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.LIMIT"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.NOT"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.NULL"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.OFFSET"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.OR"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.ORDER"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.PRIMARY"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.REPLACE"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.ROLLBACK"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.SET"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.UNIQUE"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.COLLATE"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.UPDATE"
"static com.alecstrong.sql.psi.core.psi.SqlTypes.WHERE"
]
}
overrides ::= type_name
| column_constraint
| bind_parameter
| delete_stmt_limited
| insert_stmt
| update_stmt_limited

column_constraint ::= [ CONSTRAINT {identifier} ] (
PRIMARY KEY [ ASC | DESC ] {conflict_clause} |
Expand Down Expand Up @@ -79,3 +100,23 @@ date_data_type ::= 'DATE' | (('TIME' | 'TIMESTAMP') [ '(' {signed_number} ')' ])
boolean_data_type ::= 'BOOLEAN' | 'BOOL'

json_data_type ::= 'JSON'

delete_stmt_limited ::= [ {with_clause} ] DELETE FROM {qualified_table_name} [ WHERE <<expr '-1'>> ] [ [ ORDER BY {ordering_term} ( ',' {ordering_term} ) * ] LIMIT <<expr '-1'>> [ ( OFFSET | ',' ) <<expr '-1'>> ] ] [ returning_clause ] {
extends = "com.alecstrong.sql.psi.core.psi.impl.SqlDeleteStmtLimitedImpl"
implements = "com.alecstrong.sql.psi.core.psi.SqlDeleteStmtLimited"
override = true
}

insert_stmt ::= [ {with_clause} ] ( INSERT OR REPLACE | REPLACE | INSERT OR ROLLBACK | INSERT OR ABORT | INSERT OR FAIL | INSERT OR IGNORE | INSERT ) INTO [ {database_name} '.' ] {table_name} [ AS {table_alias} ] [ '(' {column_name} ( ',' {column_name} ) * ')' ] {insert_stmt_values} [ returning_clause ] {
extends = "com.alecstrong.sql.psi.core.psi.impl.SqlInsertStmtImpl"
implements = "com.alecstrong.sql.psi.core.psi.SqlInsertStmt"
override = true
}

update_stmt_limited ::= [ {with_clause} ] UPDATE [ OR ROLLBACK | OR ABORT | OR REPLACE | OR FAIL | OR IGNORE ] {qualified_table_name} SET {column_name} '=' {setter_expression} {update_stmt_subsequent_setter} * [ WHERE <<expr '-1'>> ] [ [ ORDER BY {ordering_term} ( ',' {ordering_term} ) * ] LIMIT <<expr '-1'>> [ ( OFFSET | ',' ) <<expr '-1'>> ] ] [ returning_clause ] {
extends = "com.alecstrong.sql.psi.core.psi.impl.SqlUpdateStmtLimitedImpl"
implements = "com.alecstrong.sql.psi.core.psi.SqlUpdateStmtLimited"
override = true
}

returning_clause ::= 'RETURNING' {result_column} ( ',' {result_column} ) *
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ internal abstract class ResultColumnMixin(
node: ASTNode
) : SqlCompositeElementImpl(node),
SqlResultColumn {
override fun getParent(): SelectStmtMixin? = super.getParent() as SelectStmtMixin?

private val queryExposed: Collection<QueryResult> by ModifiableFileLazy(containingFile) lazy@{
val parent = parent as? SelectStmtMixin ?: return@lazy emptyList<QueryResult>()
tableName?.let { tableNameElement ->
// table_name '.' '*'
return@lazy parent!!.fromQuery().filter { it.table?.name == tableNameElement.name }
return@lazy parent.fromQuery().filter { it.table?.name == tableNameElement.name }
}
expr?.let {
var column: QueryElement.QueryColumn
Expand All @@ -38,7 +37,7 @@ internal abstract class ResultColumnMixin(
}

// *
val queryAvailable = parent!!.fromQuery()
val queryAvailable = parent.fromQuery()
if (queryAvailable.size <= 1) {
return@lazy queryAvailable
}
Expand Down
18 changes: 18 additions & 0 deletions core/src/test/fixtures_postgresql/returning/Sample.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CREATE TABLE account(
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
balance INT NOT NULL
);

INSERT INTO account(name, balance)
VALUES ('Jane Doe', 0), ('John Doe', 0)
RETURNING id;
Comment on lines +7 to +9
Copy link
Contributor Author

@veyndan veyndan Aug 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The RETURNING clauses in this file aren't very useful, as we don't read the output of INSERT in SqlDelight, but it is nevertheless valid PostgreSQL. This PR is mostly just a precursor to #162.


UPDATE account
SET balance = 100
WHERE name = 'Jane Doe'
RETURNING *;

DELETE FROM account
WHERE name = 'Jane Doe'
RETURNING id, name;