-
Notifications
You must be signed in to change notification settings - Fork 0
/
logical.go
52 lines (45 loc) · 1.88 KB
/
logical.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package cql
import (
"github.com/elliotchance/pie/v2"
"github.com/FrancoLiberali/cql/condition"
"github.com/FrancoLiberali/cql/model"
"github.com/FrancoLiberali/cql/unsafe"
)
// Logical Operators
// ref: https://www.postgresql.org/docs/current/functions-logical.html
// And allows the connection of multiple conditions by the AND logical connector.
//
// Its use is optional as it is the default connector.
//
// Example:
//
// cql.And(conditions.City.Name.Is().Eq("Paris"), conditions.City.ZipCode.Is().Eq("75000"))
func And[T model.Model](firstCondition condition.WhereCondition[T], conditions ...condition.WhereCondition[T]) condition.WhereCondition[T] {
return condition.And(pie.Unshift(conditions, firstCondition)...)
}
// Or allows the connection of multiple conditions by the OR logical connector.
//
// Example:
//
// cql.Or(conditions.City.Name.Is().Eq("Paris"), conditions.City.Name.Is().Eq("Buenos Aires"))
func Or[T model.Model](firstCondition condition.WhereCondition[T], conditions ...condition.WhereCondition[T]) condition.WhereCondition[T] {
return condition.Or(pie.Unshift(conditions, firstCondition)...)
}
// Not allows the negation of the conditions within it. Multiple conditions are connected by an AND by default.
//
// Example:
//
// cql.Not(conditions.City.Name.Is().Eq("Paris"), conditions.City.Name.Is().Eq("Buenos Aires"))
//
// translates as
//
// NOT (name = "Paris" AND name = "Buenos Aires")
func Not[T model.Model](firstCondition condition.WhereCondition[T], conditions ...condition.WhereCondition[T]) condition.WhereCondition[T] {
return condition.Not(pie.Unshift(conditions, firstCondition)...)
}
// True represents a condition that is always true.
//
// In general, it should not be used. It can only be useful in case you want to perform an operation on all models in a table.
func True[T model.Model]() condition.Condition[T] {
return unsafe.NewCondition[T]("1 = 1")
}