dialect/sql/sqljson: add {lt,lte} to predicates (#757)

This commit is contained in:
Ariel Mashraki
2020-09-13 16:12:37 +03:00
committed by GitHub
parent 690e9cd402
commit cbff065b51
2 changed files with 31 additions and 3 deletions

View File

@@ -62,7 +62,8 @@ func ValueGT(column string, arg interface{}, opts ...Option) *sql.Predicate {
}
// ValueGTE return a predicate for checking that a JSON value
// (returned by the path) is greater than the given argument.
// (returned by the path) is greater than or equal to the given
// argument.
//
// sqljson.ValueGTE("a", 1, sqljson.Path("b"))
//
@@ -73,6 +74,31 @@ func ValueGTE(column string, arg interface{}, opts ...Option) *sql.Predicate {
})
}
// ValueLT return a predicate for checking that a JSON value
// (returned by the path) is less than the given argument.
//
// sqljson.ValueLT("a", 1, sqljson.Path("b"))
//
func ValueLT(column string, arg interface{}, opts ...Option) *sql.Predicate {
return sql.P(func(b *sql.Builder) {
WritePath(b, column, opts...)
b.WriteOp(sql.OpLT).Arg(arg)
})
}
// ValueLTE return a predicate for checking that a JSON value
// (returned by the path) is less than or equal to the given
// argument.
//
// sqljson.ValueLTE("a", 1, sqljson.Path("b"))
//
func ValueLTE(column string, arg interface{}, opts ...Option) *sql.Predicate {
return sql.P(func(b *sql.Builder) {
WritePath(b, column, opts...)
b.WriteOp(sql.OpLTE).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"))