Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix 4545 Sqlite 3-35 On Conflict #4551

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 3 commits into from
Aug 28, 2023
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
@@ -0,0 +1,65 @@
package app.cash.sqldelight.dialects.sqlite_3_35.grammar.mixins

import app.cash.sqldelight.dialects.sqlite_3_35.grammar.psi.SqliteInsertStmt
import com.alecstrong.sql.psi.core.SqlAnnotationHolder
import com.alecstrong.sql.psi.core.psi.SqlTypes
import com.alecstrong.sql.psi.core.psi.impl.SqlInsertStmtImpl
import com.intellij.lang.ASTNode

internal abstract class InsertStmtMixin(
node: ASTNode,
) : SqlInsertStmtImpl(node),
SqliteInsertStmt {
override fun annotate(annotationHolder: SqlAnnotationHolder) {
super.annotate(annotationHolder)
val insertDefaultValues = insertStmtValues?.node?.findChildByType(
SqlTypes.DEFAULT,
) != null

upsertClauseList.forEachIndexed { index, upsert ->

val upsertDoUpdate = upsert.upsertDoUpdate
if (insertDefaultValues && upsertDoUpdate != null) {
annotationHolder.createErrorAnnotation(
upsert,
"The upsert clause is not supported after DEFAULT VALUES",
)
}

val insertOr = node.findChildByType(
SqlTypes.INSERT,
)?.treeNext
val replace = node.findChildByType(
SqlTypes.REPLACE,
)
val conflictResolution = when {
replace != null -> SqlTypes.REPLACE
insertOr != null && insertOr.elementType == SqlTypes.OR -> {
val type = insertOr.treeNext.elementType
check(
type == SqlTypes.ROLLBACK || type == SqlTypes.ABORT ||
type == SqlTypes.FAIL || type == SqlTypes.IGNORE,
)
type
}

else -> null
}

if (conflictResolution != null && upsertDoUpdate != null) {
annotationHolder.createErrorAnnotation(
upsertDoUpdate,
"Cannot use DO UPDATE while " +
"also specifying a conflict resolution algorithm ($conflictResolution)",
)
}

if (upsert.upsertConflictTarget == null && index != upsertClauseList.lastIndex) {
annotationHolder.createErrorAnnotation(
upsert,
"Only the final ON CONFLICT clause may omit the conflict target",
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,14 @@ alter_table_drop_column ::= DROP COLUMN {column_name} {
insert_stmt ::= [ {with_clause} ]
( INSERT OR REPLACE | REPLACE | INSERT OR ROLLBACK | INSERT OR ABORT | INSERT OR FAIL | INSERT OR IGNORE | INSERT )
INTO [ {database_name} DOT ] {table_name} [ AS {table_alias} ]
[ LP {column_name} ( COMMA {column_name} ) * RP ] {insert_stmt_values} [ upsert_clause ] [ returning_clause ] {
extends = "app.cash.sqldelight.dialects.sqlite_3_24.grammar.psi.impl.SqliteInsertStmtImpl"
implements = "app.cash.sqldelight.dialects.sqlite_3_24.grammar.psi.SqliteInsertStmt"
[ LP {column_name} ( COMMA {column_name} ) * RP ] {insert_stmt_values} [ ( upsert_clause ) * ] [ returning_clause ] {
mixin = "app.cash.sqldelight.dialects.sqlite_3_35.grammar.mixins.InsertStmtMixin"
pin = 5
override = true
}

upsert_clause ::= ON CONFLICT ( upsert_conflict_target DO UPDATE upsert_do_update | [ upsert_conflict_target ] DO NOTHING ) {
upsert_clause ::= ON CONFLICT [ upsert_conflict_target ] DO UPDATE upsert_do_update | ON CONFLICT [ upsert_conflict_target ] DO NOTHING {
extends = "app.cash.sqldelight.dialects.sqlite_3_24.grammar.psi.impl.SqliteUpsertClauseImpl"
implements = "app.cash.sqldelight.dialects.sqlite_3_24.grammar.psi.SqliteUpsertClause"
}

upsert_conflict_target ::= LP {indexed_column} ( COMMA {indexed_column} ) * RP [ WHERE <<expr '-1'>> ] {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CREATE TABLE test_conflict (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
size TEXT NOT NULL,
price TEXT NOT NULL,
quantity INTEGER,
CONSTRAINT test_name_size_key UNIQUE (name, size)
);

-- succeeds with multiple ON CONFLICT clauses
INSERT INTO test_conflict (id, name, size, price, quantity) VALUES(31, 'Test', 'XL', 3.99, 3)
ON CONFLICT(name, size) DO UPDATE SET size = excluded.size, quantity = excluded.quantity
ON CONFLICT DO UPDATE SET name = excluded.name = excluded.size, quantity = excluded.quantity;

-- fails with optional conflict target is only allowed with the final ON CONFLICT clause
INSERT INTO test_conflict (id, name, size, price, quantity) VALUES(31, 'Test', 'XL', 3.99, 3)
ON CONFLICT DO UPDATE SET name = excluded.name = excluded.size, quantity = excluded.quantity
ON CONFLICT(name, size) DO UPDATE SET size = excluded.size, quantity = excluded.quantity;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test.s line 17:0 - Only the final ON CONFLICT clause may omit the conflict target
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,8 @@ deleteAndReturnAll:
DELETE FROM person
WHERE last_name = ?
RETURNING *;

performUpsert:
INSERT INTO person (_id, first_name, last_name) VALUES (?, ?, ?)
ON CONFLICT(_id) DO UPDATE SET first_name=excluded.first_name, last_name=excluded.last_name
ON CONFLICT DO NOTHING;
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,11 @@ class IntegrationTests {
assertThat(personQueries.deleteAndReturnAll("Strong").executeAsOne())
.isEqualTo(Person(1, "Alec", "Strong"))
}

@Test fun upsertWithMultipleConflict() {
personQueries.performUpsert(1, "First", "Last")
personQueries.performUpsert(1, "Alpha", "Omega")
assertThat(personQueries.deleteAndReturnAll("Omega").executeAsOne())
.isEqualTo(Person(1, "Alpha", "Omega"))
}
}