mirror of
https://github.com/ent/ent.git
synced 2026-05-22 09:31:45 +03:00
dialect/sql/sqljson: add {neq,gt,gte} to predicates (#756)
This commit is contained in:
@@ -28,7 +28,7 @@ func HasKey(column string, opts ...Option) *sql.Predicate {
|
||||
// ValueEQ return a predicate for checking that a JSON value
|
||||
// (returned by the path) is equal to the given argument.
|
||||
//
|
||||
// P().JSONValueEQ("column", "a.b[2].c", arg)
|
||||
// sqljson.ValueEQ("a", 1, sqljson.Path("b"))
|
||||
//
|
||||
func ValueEQ(column string, arg interface{}, opts ...Option) *sql.Predicate {
|
||||
return sql.P(func(b *sql.Builder) {
|
||||
@@ -37,6 +37,42 @@ func ValueEQ(column string, arg interface{}, opts ...Option) *sql.Predicate {
|
||||
})
|
||||
}
|
||||
|
||||
// ValueNEQ return a predicate for checking that a JSON value
|
||||
// (returned by the path) is not equal to the given argument.
|
||||
//
|
||||
// sqljson.ValueNEQ("a", 1, sqljson.Path("b"))
|
||||
//
|
||||
func ValueNEQ(column string, arg interface{}, opts ...Option) *sql.Predicate {
|
||||
return sql.P(func(b *sql.Builder) {
|
||||
WritePath(b, column, opts...)
|
||||
b.WriteOp(sql.OpNEQ).Arg(arg)
|
||||
})
|
||||
}
|
||||
|
||||
// ValueGT return a predicate for checking that a JSON value
|
||||
// (returned by the path) is greater than the given argument.
|
||||
//
|
||||
// sqljson.ValueGT("a", 1, sqljson.Path("b"))
|
||||
//
|
||||
func ValueGT(column string, arg interface{}, opts ...Option) *sql.Predicate {
|
||||
return sql.P(func(b *sql.Builder) {
|
||||
WritePath(b, column, opts...)
|
||||
b.WriteOp(sql.OpGT).Arg(arg)
|
||||
})
|
||||
}
|
||||
|
||||
// ValueGTE return a predicate for checking that a JSON value
|
||||
// (returned by the path) is greater than the given argument.
|
||||
//
|
||||
// sqljson.ValueGTE("a", 1, sqljson.Path("b"))
|
||||
//
|
||||
func ValueGTE(column string, arg interface{}, opts ...Option) *sql.Predicate {
|
||||
return sql.P(func(b *sql.Builder) {
|
||||
WritePath(b, column, opts...)
|
||||
b.WriteOp(sql.OpGTE).Arg(arg)
|
||||
})
|
||||
}
|
||||
|
||||
// WritePath writes the JSON path from the given options to the SQL builder.
|
||||
//
|
||||
// sqljson.WritePath(b, Path("a", "b", "[1]", "c"), Cast("int"))
|
||||
|
||||
@@ -92,6 +92,20 @@ func TestWritePath(t *testing.T) {
|
||||
wantQuery: `SELECT * FROM "users" WHERE CAST("a"->'b'->'c'->1->'d' AS int) = $1`,
|
||||
wantArgs: []interface{}{1},
|
||||
},
|
||||
{
|
||||
input: sql.Dialect(dialect.Postgres).
|
||||
Select("*").
|
||||
From(sql.Table("users")).
|
||||
Where(
|
||||
sql.Or(
|
||||
sqljson.ValueNEQ("a", 1, sqljson.Path("b")),
|
||||
sqljson.ValueGT("a", 1, sqljson.Path("c")),
|
||||
sqljson.ValueGTE("a", 1, sqljson.Path("d")),
|
||||
),
|
||||
),
|
||||
wantQuery: `SELECT * FROM "users" WHERE "a"->'b' <> $1 OR "a"->'c' > $2 OR "a"->'d' >= $3`,
|
||||
wantArgs: []interface{}{1, 1, 1},
|
||||
},
|
||||
}
|
||||
for i, tt := range tests {
|
||||
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user