Skip to content

Commit

Permalink
fix: updates to left join on relationships (#1600)
Browse files Browse the repository at this point in the history
Co-authored-by: Radu Gruia <radu@keel.xyz>
  • Loading branch information
davenewza and RutZap authored Sep 13, 2024
1 parent 2df84ba commit c93211a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
37 changes: 21 additions & 16 deletions runtime/actions/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -966,15 +966,9 @@ func (query *QueryBuilder) UpdateStatement(ctx context.Context) *Statement {

args = append(args, query.args...)

var from string
if len(query.joins) > 0 {
for i, j := range query.joins {
if i == 0 {
from = fmt.Sprintf("FROM %s AS %s", j.table, j.alias)
queryFilters = append([]string{j.condition, "AND"}, queryFilters...)
} else {
joins += fmt.Sprintf("%s JOIN %s AS %s ON %s ", query.joinType, j.table, j.alias, j.condition)
}
for _, j := range query.joins {
joins += fmt.Sprintf("%s JOIN %s AS %s ON %s ", query.joinType, j.table, j.alias, j.condition)
}
}

Expand Down Expand Up @@ -1004,14 +998,25 @@ func (query *QueryBuilder) UpdateStatement(ctx context.Context) *Statement {
commonTableExpressions = fmt.Sprintf("WITH %s", strings.Join(ctes, ", "))
}

template := fmt.Sprintf("%s UPDATE %s SET %s %s %s %s %s",
commonTableExpressions,
sqlQuote(query.table),
strings.Join(sets, ", "),
from,
joins,
filters,
returning)
var template string
if len(query.joins) == 0 {
template = fmt.Sprintf("%s UPDATE %s SET %s %s %s",
commonTableExpressions,
sqlQuote(query.table),
strings.Join(sets, ", "),
filters,
returning)
} else {
template = fmt.Sprintf("%s UPDATE %s SET %s WHERE \"id\" = (SELECT %s.\"id\" FROM %s %s %s) %s",
commonTableExpressions,
sqlQuote(query.table),
strings.Join(sets, ", "),
sqlQuote(query.table),
sqlQuote(query.table),
joins,
filters,
returning)
}

return &Statement{
template: template,
Expand Down
28 changes: 15 additions & 13 deletions runtime/actions/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2561,11 +2561,11 @@ var testCases = []testCase{
expectedTemplate: `
UPDATE "product"
SET is_active = ?
FROM "brand" AS "product$brand"
WHERE
"product$brand"."id" = "product"."brand_id" AND
"product"."product_code" IS NOT DISTINCT FROM ? AND
"product$brand"."code" IS NOT DISTINCT FROM ?
WHERE "id" = (
SELECT "product"."id"
FROM "product"
LEFT JOIN "brand" AS "product$brand" ON "product$brand"."id" = "product"."brand_id"
WHERE "product"."product_code" IS NOT DISTINCT FROM ? AND "product$brand"."code" IS NOT DISTINCT FROM ?)
RETURNING "product".*`,
expectedArgs: []any{false, "prodcode", "brand"},
},
Expand Down Expand Up @@ -2611,14 +2611,16 @@ var testCases = []testCase{
expectedTemplate: `
UPDATE "product"
SET is_active = ?
FROM "brand" AS "product$brand"
LEFT JOIN "supplier" AS "product$supplier" ON "product$supplier"."id" = "product"."supplier_id"
WHERE
"product$brand"."id" = "product"."brand_id" AND
"product"."product_code" IS NOT DISTINCT FROM ? AND
"product$brand"."code" IS NOT DISTINCT FROM ? AND
"product"."is_active" IS NOT DISTINCT FROM ? AND
"product$supplier"."is_registered" IS NOT DISTINCT FROM ?
WHERE "id" = (
SELECT "product"."id"
FROM "product"
LEFT JOIN "brand" AS "product$brand" ON "product$brand"."id" = "product"."brand_id"
LEFT JOIN "supplier" AS "product$supplier" ON "product$supplier"."id" = "product"."supplier_id"
WHERE
"product"."product_code" IS NOT DISTINCT FROM ? AND
"product$brand"."code" IS NOT DISTINCT FROM ? AND
"product"."is_active" IS NOT DISTINCT FROM ? AND
"product$supplier"."is_registered" IS NOT DISTINCT FROM ?)
RETURNING "product".*`,
expectedArgs: []any{false, "prodcode", "brand", true, true},
},
Expand Down

0 comments on commit c93211a

Please sign in to comment.