dialect/sql/sqljson: fix sqlite haskey and add docs

This commit is contained in:
Ariel Mashraki
2021-11-11 18:04:17 +02:00
committed by Ariel Mashraki
parent 3ba2b4e173
commit 0dd7b0d7c3
4 changed files with 120 additions and 6 deletions

View File

@@ -21,8 +21,24 @@ import (
//
func HasKey(column string, opts ...Option) *sql.Predicate {
return sql.P(func(b *sql.Builder) {
ValuePath(b, column, opts...)
b.WriteOp(sql.OpNotNull)
switch b.Dialect() {
case dialect.SQLite:
b.Nested(func(b *sql.Builder) {
b.Join(
sql.Or(
// The result is NULL for JSON null.
sql.P(func(b *sql.Builder) {
ValuePath(b, column, opts...)
b.WriteOp(sql.OpNotNull)
}),
ValueIsNull(column, opts...),
),
)
})
default:
ValuePath(b, column, opts...)
b.WriteOp(sql.OpNotNull)
}
})
}