ValueIsNull return a predicate for checking that a JSON value (returned by the path) is a null literal (JSON "null"). In order to check if the column is NULL (database NULL), or if the JSON key exists, use sql.IsNull or sqljson.HasKey. sqljson.ValueIsNull("a", sqljson.Path("b"))
(column string, opts ...Option)
| 43 | // |
| 44 | // sqljson.ValueIsNull("a", sqljson.Path("b")) |
| 45 | func ValueIsNull(column string, opts ...Option) *sql.Predicate { |
| 46 | return sql.P(func(b *sql.Builder) { |
| 47 | switch b.Dialect() { |
| 48 | case dialect.MySQL: |
| 49 | path := identPath(column, opts...) |
| 50 | b.WriteString("JSON_CONTAINS").Wrap(func(b *sql.Builder) { |
| 51 | b.Ident(column).Comma() |
| 52 | b.WriteString("'null'").Comma() |
| 53 | path.mysqlPath(b) |
| 54 | }) |
| 55 | case dialect.Postgres: |
| 56 | valuePath(b, column, append(opts, Cast("jsonb"))...) |
| 57 | b.WriteOp(sql.OpEQ).WriteString("'null'::jsonb") |
| 58 | case dialect.SQLite: |
| 59 | path := identPath(column, opts...) |
| 60 | path.mysqlFunc("JSON_TYPE", b) |
| 61 | b.WriteOp(sql.OpEQ).WriteString("'null'") |
| 62 | } |
| 63 | }) |
| 64 | } |
| 65 | |
| 66 | // ValueIsNotNull return a predicate for checking that a JSON value |
| 67 | // (returned by the path) is not null literal (JSON "null"). |
searching dependent graphs…