/
concordances.go
executable file
·143 lines (100 loc) · 2.7 KB
/
concordances.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
package tables
import (
"context"
"fmt"
"github.com/aaronland/go-sqlite/v2"
"github.com/whosonfirst/go-whosonfirst-feature/alt"
"github.com/whosonfirst/go-whosonfirst-feature/properties"
sql_tables "github.com/whosonfirst/go-whosonfirst-sql/tables"
"github.com/whosonfirst/go-whosonfirst-sqlite-features/v2"
)
type ConcordancesTable struct {
features.FeatureTable
name string
}
type ConcordancesRow struct {
Id int64
OtherID string
OtherSource string
LastModified int64
}
func NewConcordancesTableWithDatabase(ctx context.Context, db sqlite.Database) (sqlite.Table, error) {
t, err := NewConcordancesTable(ctx)
if err != nil {
return nil, err
}
err = t.InitializeTable(ctx, db)
if err != nil {
return nil, InitializeTableError(t, err)
}
return t, nil
}
func NewConcordancesTable(ctx context.Context) (sqlite.Table, error) {
t := ConcordancesTable{
name: sql_tables.CONCORDANCES_TABLE_NAME,
}
return &t, nil
}
func (t *ConcordancesTable) Name() string {
return t.name
}
func (t *ConcordancesTable) Schema() string {
schema, _ := sql_tables.LoadSchema("sqlite", sql_tables.CONCORDANCES_TABLE_NAME)
return schema
}
func (t *ConcordancesTable) InitializeTable(ctx context.Context, db sqlite.Database) error {
return sqlite.CreateTableIfNecessary(ctx, db, t)
}
func (t *ConcordancesTable) IndexRecord(ctx context.Context, db sqlite.Database, i interface{}) error {
return t.IndexFeature(ctx, db, i.([]byte))
}
func (t *ConcordancesTable) 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(ctx)
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)
}
concordances := properties.Concordances(f)
lastmod := properties.LastModified(f)
for other_source, other_id := range concordances {
sql := fmt.Sprintf(`INSERT OR REPLACE INTO %s (
id, other_id, other_source, lastmodified
) VALUES (
?, ?, ?, ?
)`, t.Name())
stmt, err := tx.Prepare(sql)
if err != nil {
return PrepareStatementError(t, err)
}
defer stmt.Close()
_, err = stmt.Exec(id, other_id, other_source, lastmod)
if err != nil {
return ExecuteStatementError(t, err)
}
}
err = tx.Commit()
if err != nil {
return CommitTransactionError(t, err)
}
return nil
}