-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsource.go
116 lines (95 loc) · 2.3 KB
/
source.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"database/sql"
"encoding/json"
"fmt"
)
var (
checkType *sql.Stmt
checkSample *sql.Stmt
)
// initSource should be called after the db is available.
func initSource() (err error) {
checkType, err = db.Prepare(`SELECT typeID
FROM fits.type
JOIN fits.type_method USING (typepk)
JOIN fits.method USING (methodpk)
WHERE
typeid = $1
AND
methodid = $2`)
if err != nil {
return err
}
checkSample, err = db.Prepare(`SELECT DISTINCT (sampleID) sampleID
FROM fits.sample join fits.system using (systempk)
WHERE sampleID = $1
AND
systemID = $2`)
if err != nil {
return err
}
return
}
type source struct {
Properties sourceProperties
Type string
Coordinates []float64
}
type sourceProperties struct {
SiteID, Name, TypeID, MethodID, SampleID, SystemID string
Height, GroundRelationship float64
}
func (s *source) longitude() float64 {
return s.Coordinates[0]
}
func (s *source) latitude() float64 {
return s.Coordinates[1]
}
func (s *source) unmarshall(b []byte) (err error) {
err = json.Unmarshal(b, s)
if err != nil {
return err
}
if s.Type != "Point" {
return fmt.Errorf("found non Point type: %s", s.Type)
}
if s.Coordinates == nil || len(s.Coordinates) != 2 {
return fmt.Errorf("didn't find correct coordinates for point")
}
if s.Properties.SampleID == "" {
s.Properties.SampleID = "none"
}
if s.Properties.SystemID == "" {
s.Properties.SystemID = "none"
}
return err
}
func (s *source) valid() (err error) {
var d string
err = checkType.QueryRow(s.Properties.TypeID, s.Properties.MethodID).Scan(&d)
if err == sql.ErrNoRows {
return fmt.Errorf("typeID.methodID not found in the DB for %s.%s", s.Properties.TypeID, s.Properties.MethodID)
}
if err != nil {
return err
}
err = checkSample.QueryRow(s.Properties.SampleID, s.Properties.SystemID).Scan(&d)
if err == sql.ErrNoRows {
return fmt.Errorf("sampleID not found in the DB: %s %s", s.Properties.SampleID, s.Properties.SystemID)
}
if err != nil {
return err
}
return err
}
func (s *source) saveSite() (err error) {
_, err = addSite.Exec(
s.Properties.SiteID,
s.Properties.Name,
s.longitude(),
s.latitude(),
s.Properties.Height,
s.Properties.GroundRelationship)
return err
}