Skip to content

Commit

Permalink
fix regression with db.clause not support db.list as field name
Browse files Browse the repository at this point in the history
  • Loading branch information
leafo committed Dec 24, 2022
1 parent 012a4e8 commit 9e4b9b3
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lapis/db/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ build_helpers = function(escape_literal, escape_identifier)
if "string" == _exp_0 or "table" == _exp_0 then
local field
if type(k) == "table" then
assert(is_raw(k), "db.encode_clause: got unknown table as key")
assert(is_raw(k) or is_list(k), "db.encode_clause: got unknown table as key: " .. tostring(require("moon").dump(k)))
field = k
elseif opts and opts.table_name then
field = raw(tostring(escape_identifier(opts.table_name)) .. "." .. tostring(escape_identifier(k)))
Expand Down
3 changes: 2 additions & 1 deletion lapis/db/base.moon
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ build_helpers = (escape_literal, escape_identifier) ->
switch k_type
when "string", "table"
field = if type(k) == "table"
assert is_raw(k), "db.encode_clause: got unknown table as key"
assert is_raw(k) or is_list(k),
"db.encode_clause: got unknown table as key: #{require("moon").dump k}"
k
elseif opts and opts.table_name
raw "#{escape_identifier opts.table_name}.#{escape_identifier k}"
Expand Down
35 changes: 35 additions & 0 deletions spec/postgres_spec.moon
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,41 @@ tests = {
[[("age" = 99 OR "color" = 'blue') AND "gold" IS NULL AND "sigma" AND (("prefix" = 'zup_' AND "used_count" = 0) OR not "delta" OR "status" = 'spam')]]
}

{
-> db.encode_clause {
[db.list {"a", "b"}]: db.list {
db.list {1,2}
db.list {3,4}
}
}
[[("a", "b") IN ((1, 2), (3, 4))]]
}

{
-> db.encode_clause {
[db.list {db.raw("a"), db.raw("b")}]: db.raw "(1, 2)"
}
[[(a, b) = (1, 2)]]
}

{
-> db.encode_clause db.clause {
[db.list {"a", "b"}]: db.list {
db.list {1,2}
db.list {3,4}
}
}
[[("a", "b") IN ((1, 2), (3, 4))]]
}

{
-> db.encode_clause db.clause {
[db.list {db.raw("a"), db.raw("b")}]: db.raw "(1, 2)"
}
[[(a, b) = (1, 2)]]
}


{
-> db.interpolate_query "update items set x = ?", db.raw"y + 1"
"update items set x = y + 1"
Expand Down
32 changes: 32 additions & 0 deletions spec/relations_spec.moon
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,38 @@ describe "lapis.db.model.relations", ->
'SELECT * FROM "user_profiles" WHERE ("id", "id2") IN ((111, 222))'
}

it "has_one with composite key and where", ->
import preload from require "lapis.db.model"
models.Notifications = class Notifications extends Model

models.Followings = class Followings extends Model
@primary_key: {"source_user_id", "object_type", "object_id"}
@relations: {
{"notification"
has_one: "Notifications"
key: {
object_id: "source_user_id"
user_id: "object_id"
}
where: {
type: 2
object_type: 1
}
}
}

f = models.Followings\load {
source_user_id: 1
object_type: 2
object_id: 3
}

preload {f}, "notification"

assert_queries {
[[SELECT * FROM "notifications" WHERE ("object_id", "user_id") IN ((1, 3)) AND "object_type" = 1 AND "type" = 2]]
}

it "should make has_one getter with custom key", ->
mock_query "SELECT", { { id: 101 } }

Expand Down

0 comments on commit 9e4b9b3

Please sign in to comment.