/
ancestors.go
executable file
·160 lines (111 loc) · 3.08 KB
/
ancestors.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package tables
import (
"context"
"fmt"
"github.com/aaronland/go-sqlite"
"github.com/whosonfirst/go-whosonfirst-feature/alt"
"github.com/whosonfirst/go-whosonfirst-feature/properties"
"github.com/whosonfirst/go-whosonfirst-sqlite-features"
"strings"
)
type AncestorsTable struct {
features.FeatureTable
name string
}
type AncestorsRow struct {
Id int64
AncestorID int64
AncestorPlacetype string
LastModified int64
}
func NewAncestorsTableWithDatabase(ctx context.Context, db sqlite.Database) (sqlite.Table, error) {
t, err := NewAncestorsTable(ctx)
if err != nil {
return nil, fmt.Errorf("Failed to create ancestors table, %w", err)
}
err = t.InitializeTable(ctx, db)
if err != nil {
return nil, InitializeTableError(t, err)
}
return t, nil
}
func NewAncestorsTable(ctx context.Context) (sqlite.Table, error) {
t := AncestorsTable{
name: "ancestors",
}
return &t, nil
}
func (t *AncestorsTable) Name() string {
return t.name
}
func (t *AncestorsTable) Schema() string {
sql := `CREATE TABLE %s (
id INTEGER NOT NULL,
ancestor_id INTEGER NOT NULL,
ancestor_placetype TEXT,
lastmodified INTEGER
);
CREATE INDEX ancestors_by_id ON %s (id,ancestor_placetype,lastmodified);
CREATE INDEX ancestors_by_ancestor ON %s (ancestor_id,ancestor_placetype,lastmodified);
CREATE INDEX ancestors_by_lastmod ON %s (lastmodified);`
return fmt.Sprintf(sql, t.Name(), t.Name(), t.Name(), t.Name())
}
func (t *AncestorsTable) InitializeTable(ctx context.Context, db sqlite.Database) error {
return sqlite.CreateTableIfNecessary(ctx, db, t)
}
func (t *AncestorsTable) IndexRecord(ctx context.Context, db sqlite.Database, i interface{}) error {
return t.IndexFeature(ctx, db, i.([]byte))
}
func (t *AncestorsTable) IndexFeature(ctx context.Context, db sqlite.Database, f []byte) error {
if alt.IsAlt(f) {
return nil
}
id, err := properties.Id(f)
if err != nil {
return MissingPropertyError(t, "id", err)
}
conn, err := db.Conn()
if err != nil {
return DatabaseConnectionError(t, err)
}
tx, err := conn.Begin()
if err != nil {
return BeginTransactionError(t, err)
}
sql := fmt.Sprintf(`DELETE FROM %s WHERE id = ?`, t.Name())
stmt, err := tx.Prepare(sql)
if err != nil {
return PrepareStatementError(t, err)
}
defer stmt.Close()
_, err = stmt.Exec(id)
if err != nil {
return ExecuteStatementError(t, err)
}
hierarchies := properties.Hierarchies(f)
lastmod := properties.LastModified(f)
for _, h := range hierarchies {
for pt_key, ancestor_id := range h {
ancestor_placetype := strings.Replace(pt_key, "_id", "", -1)
sql := fmt.Sprintf(`INSERT OR REPLACE INTO %s (
id, ancestor_id, ancestor_placetype, lastmodified
) VALUES (
?, ?, ?, ?
)`, t.Name())
stmt, err := tx.Prepare(sql)
if err != nil {
return PrepareStatementError(t, err)
}
defer stmt.Close()
_, err = stmt.Exec(id, ancestor_id, ancestor_placetype, lastmod)
if err != nil {
return ExecuteStatementError(t, err)
}
}
}
err = tx.Commit()
if err != nil {
return CommitTransactionError(t, err)
}
return nil
}