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

@@ -23,6 +23,7 @@ title: Predicates
- =, !=, >, <, >=, <= on nested values (JSON path).
- Contains on nested values (JSON path).
- HasKey, Len&lt;P>
- `null` checks for nested values (JSON path).
- **Optional** fields:
- IsNil, NotNil
@@ -185,7 +186,8 @@ SELECT DISTINCT `users`.`id`, `users`.`age`, `users`.`name` FROM `users` JOIN `c
SELECT DISTINCT `users`.`id`, `users`.`age`, `users`.`name` FROM `users` WHERE EXISTS (SELECT * FROM `cars` WHERE `cars`.`model` = 'Tesla' AND `users`.`id` = `cars`.`owner_id`)
```
#### Get all pets where pet name contains a specific pattern
#### Get all pets where pet name contains a specific pattern
The generated code provides the `HasPrefix`, `HasSuffix`, `Contains`, and `ContainsFold` predicates for pattern matching.
However, in order to use the `LIKE` operator with a custom pattern, use the following example.
@@ -202,3 +204,65 @@ The above code will produce the following SQL query:
```sql
SELECT DISTINCT `pets`.`id`, `pets`.`owner_id`, `pets`.`name`, `pets`.`age`, `pets`.`species` FROM `pets` WHERE `name` LIKE '_B%'
```
## JSON predicates
JSON predicates are not generated by default as part of the code generation. However, ent provides an official package
named [`sqljson`](https://pkg.go.dev/entgo.io/ent/dialect/sql/sqljson) for applying predicates on JSON columns using the
[custom predicates option](#custom-predicates).
#### Compare a JSON value
```go
sqljson.ValueEQ(user.FieldData, data)
sqljson.ValueEQ(user.FieldURL, "https", sqljson.Path("Scheme"))
sqljson.ValueNEQ(user.FieldData, content, sqljson.DotPath("attributes[1].body.content"))
sqljson.ValueGTE(user.FieldData, status.StatusBadRequest, sqljson.Path("response", "status"))
```
#### Check a presence of a JSON key
```go
sqljson.HasKey(user.FieldData, sqljson.Path("attributes", "[1]", "body"))
sqljson.HasKey(user.FieldData, sqljson.DotPath("attributes[1].body"))
```
Note that, a key with the `null` literal as a value also matches this operation.
#### Check JSON `null` literals
```go
sqljson.ValueIsNull(user.FieldData)
sqljson.ValueIsNull(user.FieldData, sqljson.Path("attributes"))
sqljson.ValueIsNull(user.FieldData, sqljson.DotPath("attributes[1].body"))
```
Note that, the `ValueIsNull` returns true if the value is JSON `null`,
but not database `NULL`.
#### Compare the length of a JSON array
```go
sqljson.LenEQ(user.FieldAttrs, 2)
sql.Or(
sqljson.LenGT(user.FieldData, 10, sqljson.Path("attributes")),
sqljson.LenLT(user.FieldData, 20, sqljson.Path("attributes")),
)
```
#### Check if a JSON value contains another value
```go
sqljson.ValueContains(user.FieldData, data)
sqljson.ValueContains(user.FieldData, attrs, sqljson.Path("attributes"))
sqljson.ValueContains(user.FieldData, code, sqljson.DotPath("attributes[0].status_code"))
```