mirror of
https://github.com/ent/ent.git
synced 2026-04-30 06:30:55 +03:00
dialect/sql/sqljson: inline boolean values (#3570)
Some drivers like mysql encodes them as 0/1
This commit is contained in:
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -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("*").
|
||||
|
||||
Reference in New Issue
Block a user