dialect/sql/sqljson: add all predicates for length (#764)

This commit is contained in:
Ariel Mashraki
2020-09-15 12:38:36 +03:00
committed by GitHub
parent 3578d73a8f
commit 2c0d7e5a42
2 changed files with 84 additions and 0 deletions

View File

@@ -106,6 +106,11 @@ func ValueLTE(column string, arg interface{}, opts ...Option) *sql.Predicate {
})
}
// LenEQ return a predicate for checking that an array length
// of a JSON (returned by the path) is equal to the given argument.
//
// sqljson.LenEQ("a", 1, sqljson.Path("b"))
//
func LenEQ(column string, size int, opts ...Option) *sql.Predicate {
return sql.P(func(b *sql.Builder) {
LenPath(b, column, opts...)
@@ -113,6 +118,70 @@ func LenEQ(column string, size int, opts ...Option) *sql.Predicate {
})
}
// LenNEQ return a predicate for checking that an array length
// of a JSON (returned by the path) is not equal to the given argument.
//
// sqljson.LenEQ("a", 1, sqljson.Path("b"))
//
func LenNEQ(column string, size int, opts ...Option) *sql.Predicate {
return sql.P(func(b *sql.Builder) {
LenPath(b, column, opts...)
b.WriteOp(sql.OpNEQ).Arg(size)
})
}
// LenGT return a predicate for checking that an array length
// of a JSON (returned by the path) is greater than the given
// argument.
//
// sqljson.LenGT("a", 1, sqljson.Path("b"))
//
func LenGT(column string, size int, opts ...Option) *sql.Predicate {
return sql.P(func(b *sql.Builder) {
LenPath(b, column, opts...)
b.WriteOp(sql.OpGT).Arg(size)
})
}
// LenGTE return a predicate for checking that an array length
// of a JSON (returned by the path) is greater than or equal to
// the given argument.
//
// sqljson.LenGTE("a", 1, sqljson.Path("b"))
//
func LenGTE(column string, size int, opts ...Option) *sql.Predicate {
return sql.P(func(b *sql.Builder) {
LenPath(b, column, opts...)
b.WriteOp(sql.OpGTE).Arg(size)
})
}
// LenLT return a predicate for checking that an array length
// of a JSON (returned by the path) is less than the given
// argument.
//
// sqljson.LenLT("a", 1, sqljson.Path("b"))
//
func LenLT(column string, size int, opts ...Option) *sql.Predicate {
return sql.P(func(b *sql.Builder) {
LenPath(b, column, opts...)
b.WriteOp(sql.OpLT).Arg(size)
})
}
// LenLTE return a predicate for checking that an array length
// of a JSON (returned by the path) is less than or equal to
// the given argument.
//
// sqljson.LenLTE("a", 1, sqljson.Path("b"))
//
func LenLTE(column string, size int, opts ...Option) *sql.Predicate {
return sql.P(func(b *sql.Builder) {
LenPath(b, column, opts...)
b.WriteOp(sql.OpLTE).Arg(size)
})
}
// ValuePath writes to the given SQL builder the JSON path for
// getting the value of a given JSON path.
//

View File

@@ -132,6 +132,21 @@ func TestWritePath(t *testing.T) {
wantQuery: "SELECT * FROM `users` WHERE JSON_ARRAY_LENGTH(`a`, \"$\") = ?",
wantArgs: []interface{}{1},
},
{
input: sql.Dialect(dialect.SQLite).
Select("*").
From(sql.Table("users")).
Where(
sql.Or(
sqljson.LenGT("a", 1, sqljson.Path("b")),
sqljson.LenGTE("a", 1, sqljson.Path("c")),
sqljson.LenLT("a", 1, sqljson.Path("d")),
sqljson.LenLTE("a", 1, sqljson.Path("e")),
),
),
wantQuery: "SELECT * FROM `users` WHERE JSON_ARRAY_LENGTH(`a`, \"$.b\") > ? OR JSON_ARRAY_LENGTH(`a`, \"$.c\") >= ? OR JSON_ARRAY_LENGTH(`a`, \"$.d\") < ? OR JSON_ARRAY_LENGTH(`a`, \"$.e\") <= ?",
wantArgs: []interface{}{1, 1, 1, 1},
},
}
for i, tt := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {