-
Notifications
You must be signed in to change notification settings - Fork 0
/
facetpg.go
64 lines (57 loc) · 1.56 KB
/
facetpg.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
package facetpg
import (
"context"
"database/sql"
"errors"
"github.com/gnames/gnames/internal/ent/facet"
"github.com/gnames/gnames/internal/ent/verifier"
"github.com/gnames/gnames/internal/io/dbshare"
"github.com/gnames/gnames/pkg/config"
vlib "github.com/gnames/gnlib/ent/verifier"
"github.com/gnames/gnparser/ent/parsed"
"github.com/gnames/gnquery/ent/search"
"github.com/rs/zerolog/log"
)
type facetpg struct {
db *sql.DB
search.Input
spWord string
spWordIDs []int
dsm map[int]*vlib.DataSource
}
func New(cnf config.Config) facet.Facet {
dbURL := dbshare.DBURL(cnf)
db, err := sql.Open("postgres", dbURL)
if err != nil {
log.Fatal().Err(err).Msg("Cannot create PostgreSQL connection")
}
return &facetpg{db: db, dsm: dbshare.DataSourcesMap(db)}
}
func (f *facetpg) Search(
ctx context.Context,
inp search.Input,
) (map[string]*verifier.MatchRecord, error) {
var err error
res := make(map[string]*verifier.MatchRecord)
f.Input = inp
f.spWordIDs, f.spWord = f.spInput()
if f.spWordIDs == nil {
return res, errors.New("cannot run search without species epithet data")
}
q, args := f.setQuery()
res, err = f.runQuery(ctx, q, args)
if err != nil {
return res, err
}
return res, nil
}
func (f *facetpg) spInput() ([]int, string) {
if f.SpeciesInfra != "" {
return []int{int(parsed.InfraspEpithetType)}, f.SpeciesInfra
} else if f.SpeciesAny != "" {
return []int{int(parsed.InfraspEpithetType), int(parsed.SpEpithetType)}, f.SpeciesAny
} else if f.Species != "" {
return []int{int(parsed.SpEpithetType)}, f.Species
}
return nil, ""
}