-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiltering.go
92 lines (71 loc) · 1.88 KB
/
filtering.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
package pgspecification
import (
"strings"
)
func ComputeSpecifications(ss ...Specification) (string, []interface{}) {
b := strings.Builder{}
var values []interface{}
qConditions, vConditions := ComputeConditions(ss...)
b.WriteString(qConditions)
values = append(values, vConditions...)
qOrdering := ComputeOrderingsOnly(ss...)
b.WriteString(qOrdering)
qLimiting, vLimitings := ComputeLimitingsOnly(ss...)
b.WriteString(qLimiting)
values = append(values, vLimitings...)
return b.String(), values
}
func Filter(t Type, ss ...Specification) []Specification {
result := make([]Specification, 0)
for _, s := range ss {
if s == nil {
continue
}
if s.Type() == t {
result = append(result, s)
}
}
return result
}
func ConditionsOnly(ss ...Specification) []Specification {
return Filter(TypeCondition, ss...)
}
func ComputeConditions(ss ...Specification) (string, []interface{}) {
b := strings.Builder{}
var values []interface{}
for _, c := range ConditionsOnly(ss...) {
if c.IsValid() {
b.WriteString(" and ")
b.WriteString(c.Query())
values = append(values, c.Value()...)
}
}
return b.String(), values
}
func OrderingsOnly(ss ...Specification) []Specification {
return Filter(TypeOrdering, ss...)
}
func ComputeOrderingsOnly(ss ...Specification) string {
b := strings.Builder{}
for i, c := range OrderingsOnly(ss...) {
if i == 0 {
b.WriteString(" order by ")
}
b.WriteString(c.Query())
b.WriteString(c.Value()[0].(OrderingDirection).String())
}
return b.String()
}
func LimitingsOnly(ss ...Specification) []Specification {
return Filter(TypeLimiting, ss...)
}
func ComputeLimitingsOnly(ss ...Specification) (string, []interface{}) {
b := strings.Builder{}
var values []interface{}
for _, c := range LimitingsOnly(ss...) {
b.WriteString(" ")
b.WriteString(c.Query())
values = append(values, c.Value()...)
}
return b.String(), values
}