-
Notifications
You must be signed in to change notification settings - Fork 0
/
criteria.go
60 lines (55 loc) · 1.65 KB
/
criteria.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
package fbc
import (
"fmt"
"github.com/viant/dsc"
"github.com/viant/toolbox"
"strings"
)
//asCriteriaMap returns criteria map
func asCriteriaMap(sqlCriteria *dsc.SQLCriteria, paramIterator toolbox.Iterator) (map[string]interface{}, error) {
var result = make(map[string]interface{})
if len(sqlCriteria.Criteria) == 0 {
return result, nil
}
var value interface{}
if len(sqlCriteria.Criteria) == 1 {
criteria := sqlCriteria.Criteria[0]
if !(criteria.Operator == "=" || criteria.Operator == "IN") {
return nil, fmt.Errorf("unsuppored operator: %v", criteria.Operator)
}
column, ok := criteria.LeftOperand.(string)
columnValue := criteria.RightOperand.(string)
if !ok || column == "?" {
column, ok = criteria.RightOperand.(string)
}
bindParamCount := strings.Count(columnValue, "?")
switch bindParamCount {
case 0:
result[column] = strings.Trim(columnValue, "'")
case 1:
if !paramIterator.HasNext() {
return nil, fmt.Errorf("missing bind param: %v %v %v", criteria.LeftOperand, criteria.Operator, criteria.RightOperand)
}
if err := paramIterator.Next(&value); err != nil {
return nil, err
}
result[column] = value
break
default:
var values = make([]interface{}, 0)
for i := 0; i < bindParamCount; i++ {
if !paramIterator.HasNext() {
return nil, fmt.Errorf("missing bind param: %v %v %v", criteria.LeftOperand, criteria.Operator, criteria.RightOperand)
}
if err := paramIterator.Next(&value); err != nil {
return nil, err
}
values = append(values, value)
}
result[column] = values
}
} else {
return nil, fmt.Errorf("criteria on one key is supported")
}
return result, nil
}