-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.go
More file actions
executable file
·80 lines (71 loc) · 1.9 KB
/
Copy pathquery.go
File metadata and controls
executable file
·80 lines (71 loc) · 1.9 KB
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
package repmeta
import (
"fmt"
"strings"
reptext "github.com/radiochild/utils/text"
"go.uber.org/zap"
)
func formatWhere(ds *DatasetSpec, filters []FilterSpec, logger *zap.SugaredLogger) string {
numFilters := len(filters)
if numFilters < 1 {
return ""
}
if numFilters == 1 {
term, err := filters[0].WhereTerm(ds, logger)
if err != nil {
logger.Warnf("%s", err.Error())
return ""
}
return fmt.Sprintf("where %s", term)
}
wrappedFilters := []string{}
for _, filter := range filters {
term, err := filter.WhereTerm(ds, logger)
if err != nil {
logger.Warnf("%s", err.Error())
continue
}
wrappedFilter := fmt.Sprintf("(%s)", term)
wrappedFilters = append(wrappedFilters, wrappedFilter)
}
numFilters = len(wrappedFilters)
if numFilters == 0 {
return ""
}
if numFilters == 1 {
return fmt.Sprintf("where %s", wrappedFilters[0])
}
allFilters := strings.Join(wrappedFilters, " and ")
return fmt.Sprintf("where (%s)", allFilters)
}
func formatOrder(groups []string) string {
if len(groups) < 1 {
return ""
}
allGroups := strings.Join(groups, ", ")
return fmt.Sprintf("order by %s", allGroups)
}
func formatOffset(page, maxRecs int) string {
if page < 0 {
return ""
}
startRec := page * maxRecs
// return fmt.Sprintf("offset %d limit %d", startRec, maxRecs)
return fmt.Sprintf("limit %d offset %d", maxRecs, startRec)
}
func FormatQuery(spec *ReportSpec, maxRecs int, logger *zap.SugaredLogger) string {
page := 0
if maxRecs < 0 {
page = -1
}
table := spec.Dataset.ViewName
colNames := ColSpecFldNames(spec.Columns)
allCols := append(spec.ExtraColumns, colNames...)
fldList := strings.Join(allCols, ", ")
where := formatWhere(&spec.Dataset, spec.Filters, logger)
order := formatOrder(spec.Groups)
paging := formatOffset(page, maxRecs)
suffix := reptext.AppendText(where, order, paging)
qry := fmt.Sprintf("select %s from %s %s", fldList, table, suffix)
return qry
}