mirror of
https://github.com/ent/ent.git
synced 2026-04-28 13:40:56 +03:00
dialect/sql: add pattern matching predicates for json values
This commit is contained in:
@@ -195,6 +195,62 @@ func TestWritePath(t *testing.T) {
|
||||
wantQuery: "SELECT * FROM \"users\" WHERE (\"tags\"->'a')::jsonb @> $1",
|
||||
wantArgs: []interface{}{"1"},
|
||||
},
|
||||
{
|
||||
input: sql.Dialect(dialect.Postgres).
|
||||
Select("*").
|
||||
From(sql.Table("users")).
|
||||
Where(sqljson.StrValueContains("a", "substr", sqljson.Path("b", "c", "[1]", "d"))),
|
||||
wantQuery: `SELECT * FROM "users" WHERE "a"->'b'->'c'->1->>'d' LIKE $1`,
|
||||
wantArgs: []interface{}{"%substr%"},
|
||||
},
|
||||
{
|
||||
input: sql.Dialect(dialect.MySQL).
|
||||
Select("*").
|
||||
From(sql.Table("users")).
|
||||
Where(sqljson.StrValueContains("a", "substr", sqljson.Path("b", "c", "[1]", "d"))),
|
||||
wantQuery: "SELECT * FROM `users` WHERE JSON_UNQUOTE(JSON_EXTRACT(`a`, \"$.b.c[1].d\")) LIKE ?",
|
||||
wantArgs: []interface{}{"%substr%"},
|
||||
},
|
||||
{
|
||||
input: sql.Dialect(dialect.SQLite).
|
||||
Select("*").
|
||||
From(sql.Table("users")).
|
||||
Where(sqljson.StrValueContains("a", "substr", sqljson.Path("b", "c", "[1]", "d"))),
|
||||
wantQuery: "SELECT * FROM `users` WHERE JSON_EXTRACT(`a`, \"$.b.c[1].d\") LIKE ?",
|
||||
wantArgs: []interface{}{"%substr%"},
|
||||
},
|
||||
{
|
||||
input: sql.Dialect(dialect.Postgres).
|
||||
Select("*").
|
||||
From(sql.Table("users")).
|
||||
Where(sqljson.StrValueHasPrefix("a", "substr", sqljson.Path("b", "c", "[1]", "d"))),
|
||||
wantQuery: `SELECT * FROM "users" WHERE "a"->'b'->'c'->1->>'d' LIKE $1`,
|
||||
wantArgs: []interface{}{"substr%"},
|
||||
},
|
||||
{
|
||||
input: sql.Dialect(dialect.MySQL).
|
||||
Select("*").
|
||||
From(sql.Table("users")).
|
||||
Where(sqljson.StrValueHasPrefix("a", "substr", sqljson.Path("b", "c", "[1]", "d"))),
|
||||
wantQuery: "SELECT * FROM `users` WHERE JSON_UNQUOTE(JSON_EXTRACT(`a`, \"$.b.c[1].d\")) LIKE ?",
|
||||
wantArgs: []interface{}{"substr%"},
|
||||
},
|
||||
{
|
||||
input: sql.Dialect(dialect.Postgres).
|
||||
Select("*").
|
||||
From(sql.Table("users")).
|
||||
Where(sqljson.StrValueHasSuffix("a", "substr", sqljson.Path("b", "c", "[1]", "d"))),
|
||||
wantQuery: `SELECT * FROM "users" WHERE "a"->'b'->'c'->1->>'d' LIKE $1`,
|
||||
wantArgs: []interface{}{"%substr"},
|
||||
},
|
||||
{
|
||||
input: sql.Dialect(dialect.MySQL).
|
||||
Select("*").
|
||||
From(sql.Table("users")).
|
||||
Where(sqljson.StrValueHasSuffix("a", "substr", sqljson.Path("b", "c", "[1]", "d"))),
|
||||
wantQuery: "SELECT * FROM `users` WHERE JSON_UNQUOTE(JSON_EXTRACT(`a`, \"$.b.c[1].d\")) LIKE ?",
|
||||
wantArgs: []interface{}{"%substr"},
|
||||
},
|
||||
}
|
||||
for i, tt := range tests {
|
||||
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user