Skip to content

Commit

Permalink
Allow JSON binary operator to be used on a column expression (#4776)
Browse files Browse the repository at this point in the history
  • Loading branch information
eygraber authored and hfhbd committed Apr 2, 2024
1 parent 33e4eda commit dea4fc1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
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 = '';

0 comments on commit dea4fc1

Please sign in to comment.