dialect/sql/sqljson: add {neq,gt,gte} to predicates (#756)

This commit is contained in:
Ariel Mashraki
2020-09-13 15:25:02 +03:00
committed by GitHub
parent 98bc30d618
commit 690e9cd402
2 changed files with 51 additions and 1 deletions

View File

@@ -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"))