-
Notifications
You must be signed in to change notification settings - Fork 0
/
sp_query.go
73 lines (63 loc) · 1.49 KB
/
sp_query.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
package facetpg
import (
"fmt"
"strings"
"github.com/gnames/gnparser/ent/parsed"
"github.com/lib/pq"
)
func (f *facetpg) spQuery() (string, []interface{}) {
sp, insertStr := f.prepareSpWord()
args := []interface{}{sp, pq.Array(f.spWordIDs)}
q := fmt.Sprintf(`
WITH sp AS (
SELECT DISTINCT v.name_string_id
FROM word_name_strings wc
JOIN words w ON w.id = wc.word_id
JOIN canonicals c ON c.id = wc.canonical_id
JOIN verification v ON c.id = v.canonical_id
WHERE %s
AND type_id = any($2::int[])`, insertStr,
)
spQ := []string{q}
gen := f.prepareGenWord()
if gen != "" {
args = append(args, gen)
genQ := fmt.Sprintf(" AND c.name LIKE $%d", len(args))
spQ = append(spQ, genQ)
}
// add higher taxon constraint
if tx := f.ParentTaxon; tx != "" {
tx = "%" + tx + "%"
args = append(args, tx)
clQ := fmt.Sprintf(" AND v.classification LIKE $%d", len(args))
spQ = append(spQ, clQ)
}
spQ = append(spQ, ")")
res := strings.Join(spQ, "\n")
return res, args
}
func (f *facetpg) prepareSpWord() (string, string) {
iStr := "normalized like $1"
bs := []byte(f.spWord)
l := len(bs)
if bs[l-1] == '.' {
bs[l-1] = '%'
return string(bs), iStr
}
iStr = "modified = $1"
st := parsed.NormalizeByType(f.spWord, parsed.SpEpithetType)
return st, iStr
}
func (f *facetpg) prepareGenWord() string {
g := f.Genus
if len(g) < 2 {
return ""
}
bs := []byte(g)
l := len(bs)
if bs[l-1] == '.' {
bs[l-1] = '%'
return string(bs)
}
return g + " %"
}