dialect/sql: fix Builder.Join implementation for postgres (#1212)

Don't use .Total() in builder.Join and purely rely on len(args).
This commit is contained in:
Ruben de Vries
2021-02-07 17:07:48 +01:00
committed by GitHub
parent ba9f837c7d
commit 9e610c7b54
2 changed files with 38 additions and 4 deletions

View File

@@ -2244,10 +2244,7 @@ func (b *Builder) join(qs []Querier, sep string) *Builder {
query, args := q.Query()
b.WriteString(query)
b.args = append(b.args, args...)
b.total = len(b.args)
if ok {
b.total = st.Total()
}
b.total += len(args)
}
return b
}

View File

@@ -1372,6 +1372,24 @@ WHERE
wantQuery: "SELECT * FROM `users` JOIN `pets` AS `t0` ON `users`.`id` = `t0`.`owner_id` WHERE `t0`.`name` = ?",
wantArgs: []interface{}{"pedro"},
},
{
input: func() Querier {
t1 := Table("users")
sel := Select("*").
From(t1).
Where(P(func(b *Builder) {
b.Join(Expr("name = $1", "pedro"))
})).
Where(P(func(b *Builder) {
b.Join(Expr("name = $2", "pedro"))
})).
Where(EQ("name", "pedro"))
sel.SetDialect(dialect.Postgres)
return sel
}(),
wantQuery: `SELECT * FROM "users" WHERE (name = $1 AND name = $2) AND "name" = $3`,
wantArgs: []interface{}{"pedro", "pedro", "pedro"},
},
}
for i, tt := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
@@ -1382,6 +1400,25 @@ WHERE
}
}
func TestAnd(t *testing.T) {
assert := require.New(t)
p1 := P(func(b *Builder) {
b.Join(Expr("name = $1", "pedro"))
})
p2 := P(func(b *Builder) {
b.Join(Expr("name = $2", "pedro"))
})
and := And(p1, p2)
_, _ = and.Query()
assert.Equal(1, p1.Total())
assert.Equal(2, p2.Total())
assert.Equal(2, and.Total())
}
func TestBuilder_Err(t *testing.T) {
b := Select("i-")
require.NoError(t, b.Err())