Skip to content
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

Allow JSON binary operator to be used on a column expression #4776

Merged
merged 1 commit into from
Nov 12, 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
Expand Up @@ -3,15 +3,15 @@ package app.cash.sqldelight.dialects.sqlite_3_38.grammar.mixins
import app.cash.sqldelight.dialects.sqlite_3_38.grammar.psi.SqliteJsonExpression
import com.alecstrong.sql.psi.core.SqlAnnotationHolder
import com.alecstrong.sql.psi.core.psi.SqlColumnDef
import com.alecstrong.sql.psi.core.psi.SqlColumnName
import com.alecstrong.sql.psi.core.psi.SqlColumnExpr
import com.alecstrong.sql.psi.core.psi.SqlCompositeElementImpl
import com.intellij.lang.ASTNode

internal abstract class JsonExpressionMixin(node: ASTNode) :
SqlCompositeElementImpl(node),
SqliteJsonExpression {
override fun annotate(annotationHolder: SqlAnnotationHolder) {
val columnType = ((firstChild.reference?.resolve() as? SqlColumnName)?.parent as? SqlColumnDef)?.columnType?.typeName?.text
val columnType = ((firstChild as? SqlColumnExpr)?.columnName?.reference?.resolve()?.parent as? SqlColumnDef)?.columnType?.typeName?.text
if (columnType == null || columnType != "TEXT") {
annotationHolder.createErrorAnnotation(firstChild, "Left side of json expression must be a text column.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extension_expr ::= json_expression {
override = true
}

json_expression ::= {column_name} json_binary_operator <<expr '-1'>> {
json_expression ::= {column_expr} json_binary_operator <<expr '-1'>> {
mixin = "app.cash.sqldelight.dialects.sqlite_3_38.grammar.mixins.JsonExpressionMixin"
pin = 2
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CREATE TABLE myTable(
data TEXT NOT NULL,
otherData TEXT,
number INTEGER NOT NULL
);

Expand All @@ -13,3 +14,15 @@ WHERE
--error[col 2]: Left side of json expression must be a text column.
number ->> 'sup'
;

UPDATE myTable
SET otherData = subquery.data -> 'sup'
FROM (SELECT number, data FROM myTable) subquery
WHERE subquery.number = myTable.number;

UPDATE myTable
SET data = data -> newData
FROM(
SELECT 'sup' AS newData
)
WHERE data = '';