This repository has been archived by the owner on Jul 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
66 lines (61 loc) · 1.72 KB
/
filter.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
package database
import (
"fmt"
"strings"
)
// A Filter holds all the fields available to select a group
// of students on.
type Filter struct {
// Effective date to use for membership of the group.
// Holds the id number, but held as string for easier
// parsing from query strings.
Date string
// Which set of assessment results to use.
// Holds id number as above.
Resultset string
// Which year to use for national data comparisons.
NatYear string
// Which yeargroup to include in the group
Year string
// Pupil premium: "", "1", or "0" for Any/True/False
PP string
// EAL students: "", "1", or "0" for Any/True/False
EAL string
// Gender: "", "1", "0" for Any/Male/Female
Gender string
// SEN types to include - Empty for any
SEN []string
// Which Ethnic groups to include, empty for any
Ethnicities []string
// Which KS2 bands to include, empty for any
KS2Bands []string
}
// sql generates a query string from the filter, used to select
// student upns.
func (f Filter) sql(table string) string {
query := fmt.Sprintf(`SELECT upn FROM %v
WHERE date_id=%v`,
table, f.Date)
if f.Year != "" {
query += fmt.Sprintf(" AND year=%v", f.Year)
}
if f.PP != "" {
query += fmt.Sprintf(" AND pp=%v", f.PP)
}
if f.EAL != "" {
query += fmt.Sprintf(" AND eal=%v", f.EAL)
}
if f.Gender != "" {
query += fmt.Sprintf(" AND gender=%v", f.Gender)
}
if len(f.SEN) > 0 {
query += fmt.Sprintf(` AND sen_status IN ("` + strings.Join(f.SEN, `", "`) + `")`)
}
if len(f.Ethnicities) > 0 {
query += fmt.Sprintf(` AND ethnicity IN ("` + strings.Join(f.Ethnicities, `", "`) + `")`)
}
if len(f.KS2Bands) > 0 {
query += fmt.Sprintf(` AND ks2_band IN ("` + strings.Join(f.KS2Bands, `", "`) + `")`)
}
return query
}