-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In this PR we refactor de **README** file and add some doc files in order to clarify the use of criteria4s: - Defining criteria expressions - Evaluating criteria expressions using: - PostgreSQL dialect (non-official) - MongoDB dialect - MySQL dialect (non-official) - Custom dialect
- Loading branch information
Showing
6 changed files
with
374 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Custom Dialect | ||
|
||
The `WeirdDatastore` dialect using in this document is an own-implemented dialect for a custom or "weird" datastore. | ||
The examples provided illustrate what the output would look like if a `WeirdDatastore` dialect needs be implemented. | ||
The results are intentionally generated using weird expressions to highlight the differences that occur when applying | ||
the same criteria expression across different dialects. | ||
|
||
The `WeirdDatastore` dialect implementation is located in the [WeirdDatastore.scala](../examples/src/main/scala/io/github/rafafrdz/criteria4s/examples/datastores/WeirdDatastore.scala) file. | ||
|
||
For these examples we will use the defined expressions in | ||
the [Defining Criteria Expressions](defining-criteria-expressions.md) document. | ||
|
||
```scala | ||
expr[WeirdDatastore] | ||
// res: {left: {left: {left: field1, opt: <=, right: 3 }, opt: AND, right: {left: field2, opt: <=, right: 4 } }, opt: OR, right: {left: field3, opt: =, right: c } } | ||
``` | ||
|
||
## Case of use | ||
|
||
```scala | ||
ageCriteria[WeirdDatastore] | ||
// res: {left: {left: age, opt: >, right: 18 }, opt: AND, right: {left: age, opt: <, right: 65 } } | ||
``` | ||
|
||
```scala | ||
refCriteria[WeirdDatastore]("id", UUID.randomUUID()) | ||
// res: {left: id, opt: =, right: dfacb848-6d39-404b-9e6b-f8cbc1f52918 } | ||
``` | ||
|
||
### Using `IS NULL` expression | ||
|
||
```scala | ||
isNullExpr[WeirdDatastore]("field") | ||
// res: {left: field, opt: IS NULL } | ||
``` | ||
|
||
### Using `NOT` expression | ||
|
||
```scala | ||
notExpr[WeirdDatastore]("field") | ||
// res: {left: {left: {left: field, opt: >, right: 2 }, opt: NOT }, opt: AND, right: {left: field, opt: <=, right: 10 } } | ||
``` | ||
|
||
### Using `BETWEEN` expression | ||
|
||
```scala | ||
betweenExpr[WeirdDatastore]("field") | ||
// res: {left: field, opt: BETWEEN, right: [100, 150] } | ||
``` | ||
|
||
### Using `ARRAY` expression | ||
|
||
```scala | ||
arrayExpr[WeirdDatastore]("field") | ||
// res: {left: field, opt: IN, right: [1, 2, 3] } | ||
``` | ||
|
||
### Using `LIKE` expression | ||
|
||
```scala | ||
likeExpr[WeirdDatastore]("field") | ||
// res: {left: field, opt: LIKE, right: a% } | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Defining Criteria Expressions | ||
|
||
We can define criteria expressions in a polymorphic way by using the Criteria DSL: | ||
|
||
```scala | ||
def expr[T <: CriteriaTag : LEQ : EQ : AND : OR : Show[Column, *]]: Criteria[T] = | ||
(col[T]("field1") leq lit(3)) and (col[T]("field2") leq lit(4)) or (col[T]("field3") === lit("c")) | ||
``` | ||
|
||
## Case of use | ||
|
||
```scala | ||
def ageCriteria[T <: CriteriaTag : GT : LT : AND : Show[Column, *]]: Criteria[T] = | ||
(col[T]("age") gt lit(18)) and (col[T]("age") lt lit(65)) | ||
|
||
def refCriteria[T <: CriteriaTag : EQ : Show[Column, *]](fieldName: String, id: UUID): Criteria[T] = | ||
col[T](fieldName) === lit(id.toString) | ||
``` | ||
|
||
### Using `IS NULL` expression | ||
|
||
```scala | ||
def isNullExpr[T <: CriteriaTag : ISNULL : Show[Column, *]](fieldName: String): Criteria[T] = | ||
col[T](fieldName).isNull | ||
``` | ||
|
||
### Using `NOT` expression | ||
|
||
```scala | ||
def notExpr[T <: CriteriaTag : NOT : GT : LEQ : AND : Show[Column, *] : Show[(Int, Int), *]](fieldName: String): Criteria[T] = | ||
not(col[T](fieldName) gt lit(2)) && (col[T](fieldName) leq lit(10)) | ||
``` | ||
|
||
### Using `BETWEEN` expression | ||
|
||
```scala | ||
def betweenExpr[T <: CriteriaTag : BETWEEN : Show[Column, *] : Show[(Int, Int), *]](fieldName: String): Criteria[T] = | ||
col[T](fieldName) between range(100, 150) | ||
``` | ||
|
||
### Using `ARRAY` expression | ||
|
||
```scala | ||
def arrayExpr[T <: CriteriaTag : IN : Show[Column, *] : Show[Seq[Int], *]](fieldName: String): Criteria[T] = | ||
col[T](fieldName) in array(1, 2, 3) | ||
``` | ||
|
||
### Using `LIKE` expression | ||
|
||
```scala | ||
def likeExpr[T <: CriteriaTag : LIKE : Show[Column, *]](fieldName: String): Criteria[T] = | ||
col[T](fieldName) like lit("a%") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# MongoDB Dialect | ||
|
||
The `MongoDB` dialect is available. The examples provided illustrate what the output look like using the MongoDB | ||
dialect. | ||
|
||
For these examples we will use the defined expressions in | ||
the [Defining Criteria Expressions](defining-criteria-expressions.md) document. | ||
|
||
```scala | ||
expr[MongoDB] | ||
// res: {$or: [{$and: [{"field1": {$lte: 3}}, {"field2": {$lte: 4}}]}, {"field3": {$eq: c}}]} | ||
``` | ||
|
||
## Case of use | ||
|
||
```scala | ||
ageCriteria[MongoDB] | ||
// res: {$and: [{"age": {$gt: 18}}, {"age": {$lt: 65}}]} | ||
``` | ||
|
||
```scala | ||
refCriteria[MongoDB]("id", UUID.randomUUID()) | ||
// res: {"id": {$eq: d0ba6ea7-4cff-44e4-b612-ebe12242bd18}} | ||
``` | ||
|
||
### Using `IS NULL` expression | ||
|
||
```scala | ||
isNullExpr[MongoDB]("field") | ||
// res: {"field": null} | ||
``` | ||
|
||
### Using `NOT` expression | ||
|
||
```scala | ||
notExpr[MongoDB]("field") | ||
// res: {$and: [{"field": {$not: {$gt: 2}}}, {"field": {$lte: 10}}]} | ||
``` | ||
|
||
### Using `BETWEEN` expression | ||
|
||
```scala | ||
betweenExpr[MongoDB]("field") | ||
// res: {"field": { $gte: 100, $lt: 150 }} | ||
``` | ||
|
||
### Using `ARRAY` expression | ||
|
||
```scala | ||
arrayExpr[MongoDB]("field") | ||
// res: {"field": {$in: [1, 2, 3]}} | ||
``` | ||
|
||
### Using `LIKE` expression | ||
|
||
```scala | ||
likeExpr[MongoDB]("field") | ||
// res: {"field": {$regex: \a%\}} | ||
``` |
Oops, something went wrong.