-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsql_predicate.go
executable file
·141 lines (129 loc) · 4.1 KB
/
sql_predicate.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package dsc
import (
"errors"
"fmt"
"github.com/viant/toolbox"
"strings"
)
func getOperandValue(operand interface{}, parameters toolbox.Iterator) (interface{}, error) {
if operand != "?" {
return operand, nil
}
var values = make([]interface{}, 1)
if !parameters.HasNext() {
return "", fmt.Errorf("missing binding parameters ?, %v", parameters)
}
err := parameters.Next(&values[0])
if err != nil {
return nil, err
}
return values[0], nil
}
func getOperandValues(operands []interface{}, parameters toolbox.Iterator) ([]interface{}, error) {
var result = make([]interface{}, 0)
for _, operand := range operands {
operand, err := getOperandValue(operand, parameters)
if err != nil {
return nil, err
}
result = append(result, operand)
}
return result, nil
}
//NewSQLCriterionPredicate create a new predicate for passed in SQLCriterion
func NewSQLCriterionPredicate(criterion *SQLCriterion, parameters toolbox.Iterator) (toolbox.Predicate, error) {
if criterion.Operator == "" {
return nil, errors.New("criterion.Operator was empty")
}
switch strings.ToLower(criterion.Operator) {
case "in":
operands, err := getOperandValues(criterion.RightOperands, parameters)
if err != nil {
return nil, fmt.Errorf("missing binding parameters for %v", criterion)
}
return toolbox.NewInPredicate(operands...), nil
case "like":
operand, err := getOperandValue(criterion.RightOperand, parameters)
if err != nil {
return nil, fmt.Errorf("missing binding parameters for %v", criterion)
}
return toolbox.NewLikePredicate(toolbox.AsString(operand)), nil
case "between":
operands, err := getOperandValues(criterion.RightOperands, parameters)
if err != nil || len(operands) != 2 {
return nil, fmt.Errorf("missing binding parameters for %v", criterion)
}
return toolbox.NewBetweenPredicate(operands[0], operands[1]), nil
case "is":
return toolbox.NewNilPredicate(), nil
default:
operand, err := getOperandValue(criterion.RightOperand, parameters)
if err != nil {
return nil, fmt.Errorf("missing binding parameters for %v", criterion)
}
return toolbox.NewComparablePredicate(criterion.Operator, operand), nil
}
}
type booleanPredicate struct {
operator string
leftOperand bool
}
func (p *booleanPredicate) Apply(value interface{}) bool {
rightOperand := toolbox.AsBoolean(value)
switch strings.ToLower(p.operator) {
case "or":
return p.leftOperand || rightOperand
case "and":
return p.leftOperand && rightOperand
}
return false
}
//NewBooleanPredicate returns a new boolean predicate. It takes left operand and logical operator: 'OR' or 'AND'
func NewBooleanPredicate(leftOperand bool, operator string) toolbox.Predicate {
return &booleanPredicate{operator, leftOperand}
}
type sqlCriteriaPredicate struct {
*SQLCriteria
predicates []toolbox.Predicate
}
func (p *sqlCriteriaPredicate) Apply(source interface{}) bool {
var sourceMap, ok = source.(map[string]interface{})
if !ok {
return false
}
result := true
var logicalPredicate toolbox.Predicate
for i := 0; i < len(p.Criteria); i++ {
criterion := p.Criteria[i]
value := sourceMap[toolbox.AsString(criterion.LeftOperand)]
predicate := p.predicates[i]
result = predicate.Apply(value)
if criterion.Inverse {
result = !result
}
if logicalPredicate != nil {
result = logicalPredicate.Apply(result)
}
if p.LogicalOperator != "" {
if strings.ToLower(p.LogicalOperator) == "and" && !result {
//shortcut
break
}
logicalPredicate = NewBooleanPredicate(result, p.LogicalOperator)
}
}
return result
}
//NewSQLCriteriaPredicate create a new sql criteria predicate, it takes binding parameters iterator, and actual criteria.
func NewSQLCriteriaPredicate(parameters toolbox.Iterator, sqlCriteria *SQLCriteria) (toolbox.Predicate, error) {
var predicates = make([]toolbox.Predicate, 0)
for i := 0; i < len(sqlCriteria.Criteria); i++ {
criterion := sqlCriteria.Criteria[i]
predicate, err := NewSQLCriterionPredicate(criterion, parameters)
if err != nil {
return nil, err
}
predicates = append(predicates, predicate)
}
return &sqlCriteriaPredicate{SQLCriteria: sqlCriteria, predicates: predicates}, nil
}