mirror of
https://github.com/ent/ent.git
synced 2026-05-28 09:49:08 +03:00
dialect/sql/sqljson: initial work for json_contains predicate (#886)
This commit is contained in:
@@ -39,9 +39,11 @@ func (User) Fields() []ent.Field {
|
||||
}
|
||||
|
||||
type T struct {
|
||||
I int `json:"i,omitempty"`
|
||||
F float64 `json:"f,omitempty"`
|
||||
B bool `json:"b,omitempty"`
|
||||
S string `json:"s,omitempty"`
|
||||
T *T `json:"t,omitempty"`
|
||||
I int `json:"i,omitempty"`
|
||||
F float64 `json:"f,omitempty"`
|
||||
B bool `json:"b,omitempty"`
|
||||
S string `json:"s,omitempty"`
|
||||
T *T `json:"t,omitempty"`
|
||||
Li []int `json:"li,omitempty"`
|
||||
Ls []string `json:"ls,omitempty"`
|
||||
}
|
||||
|
||||
@@ -243,8 +243,8 @@ func Predicates(t *testing.T, client *ent.Client) {
|
||||
client.User.Delete().ExecX(ctx)
|
||||
users, err = client.User.CreateBulk(
|
||||
client.User.Create().SetInts([]int{1}),
|
||||
client.User.Create().SetInts([]int{1, 2}),
|
||||
client.User.Create().SetInts([]int{1, 2, 3}),
|
||||
client.User.Create().SetInts([]int{1, 2}).SetT(&schema.T{Li: []int{1, 2}, Ls: []string{"a"}}),
|
||||
client.User.Create().SetInts([]int{1, 2, 3}).SetT(&schema.T{Li: []int{3, 4}, Ls: []string{"b"}}),
|
||||
).Save(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -254,4 +254,18 @@ func Predicates(t *testing.T, client *ent.Client) {
|
||||
}).OnlyX(ctx)
|
||||
require.Equal(t, u.Ints, r.Ints)
|
||||
}
|
||||
|
||||
r := client.User.Query().Where(func(s *sql.Selector) {
|
||||
s.Where(sqljson.ValueContains(user.FieldInts, 3))
|
||||
}).OnlyX(ctx)
|
||||
require.Contains(t, r.Ints, 3)
|
||||
r = client.User.Query().Where(func(s *sql.Selector) {
|
||||
s.Where(sqljson.ValueContains(user.FieldT, 3, sqljson.Path("li")))
|
||||
}).OnlyX(ctx)
|
||||
require.Contains(t, r.T.Li, 3)
|
||||
|
||||
r = client.User.Query().Where(func(s *sql.Selector) {
|
||||
s.Where(sqljson.ValueContains(user.FieldT, "a", sqljson.Path("ls")))
|
||||
}).OnlyX(ctx)
|
||||
require.Contains(t, r.T.Ls, "a")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user