dialect/sql/sqljson: inline boolean values (#3570)

Some drivers like mysql encodes them as 0/1
This commit is contained in:
Ariel Mashraki
2023-05-29 17:15:32 +03:00
committed by GitHub
parent 633d021f48
commit a8851db571
3 changed files with 31 additions and 1 deletions

View File

@@ -7,6 +7,7 @@ package sqljson
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"unicode"
@@ -95,7 +96,13 @@ func ValueEQ(column string, arg any, opts ...Option) *sql.Predicate {
return sql.P(func(b *sql.Builder) {
opts = normalizePG(b, arg, opts)
valuePath(b, column, opts...)
b.WriteOp(sql.OpEQ).Arg(arg)
b.WriteOp(sql.OpEQ)
// Inline boolean values, as some drivers (e.g., MySQL) encode them as 0/1.
if v, ok := arg.(bool); ok {
b.WriteString(strconv.FormatBool(v))
} else {
b.Arg(arg)
}
})
}

View File

@@ -36,6 +36,13 @@ func TestWritePath(t *testing.T) {
wantQuery: "SELECT * FROM `users` WHERE JSON_EXTRACT(`a`, '$.b.c[1].d') = ?",
wantArgs: []any{"a"},
},
{
input: sql.Dialect(dialect.MySQL).
Select("*").
From(sql.Table("users")).
Where(sqljson.ValueEQ("a", true, sqljson.DotPath("b.c[1].d"))),
wantQuery: "SELECT * FROM `users` WHERE JSON_EXTRACT(`a`, '$.b.c[1].d') = true",
},
{
input: sql.Dialect(dialect.MySQL).
Select("*").