-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
connection.go
168 lines (143 loc) · 3.67 KB
/
connection.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
161
162
163
164
165
166
167
168
package sqlite
import (
"database/sql"
_ "github.com/mattn/go-sqlite3" // required database driver
)
// schema copied from https://github.com/mfn/osmlib-sqlite
// Connection - Connection
type Connection struct {
db *sql.DB
Stmt *Statements
}
// GetDB - expose the underlying db object
func (c *Connection) GetDB() *sql.DB {
return c.db
}
// Open - open connection and set up
func (c *Connection) Open(path string) {
db, err := sql.Open("sqlite3", path)
if err != nil {
panic(err)
}
c.db = db
c.tables()
c.prepare()
// https://github.com/mattn/go-sqlite3/issues/274
db.SetMaxOpenConns(1)
// start transaction
_, err = c.db.Exec("BEGIN TRANSACTION")
if err != nil {
panic(err)
}
}
// Close - close connection and clean up
func (c *Connection) Close() {
defer c.Stmt.Close()
defer c.db.Close()
// commit transaction
_, err := c.db.Exec("END TRANSACTION")
if err != nil {
panic(err)
}
}
// created tables
func (c *Connection) tables() {
_, err := c.db.Exec(`
PRAGMA main.foreign_keys=OFF;
PRAGMA main.page_size=4096;
PRAGMA main.cache_size=-2000;
PRAGMA main.synchronous=OFF;
PRAGMA main.journal_mode=OFF;
PRAGMA main.temp_store=MEMORY;
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS nodes (
id INTEGER NOT NULL PRIMARY KEY,
lon REAL NOT NULL,
lat REAL NOT NULL
);
CREATE TABLE IF NOT EXISTS node_tags (
ref INTEGER NOT NULL,
key TEXT,
value TEXT,
UNIQUE( ref, key ) ON CONFLICT REPLACE
);
CREATE TABLE IF NOT EXISTS ways (
id INTEGER NOT NULL PRIMARY KEY
);
CREATE TABLE IF NOT EXISTS way_tags (
ref INTEGER NOT NULL,
key TEXT,
value TEXT,
UNIQUE( ref, key ) ON CONFLICT REPLACE
);
CREATE TABLE IF NOT EXISTS way_nodes (
way INTEGER NOT NULL,
num INTEGER NOT NULL,
node INTEGER NOT NULL,
UNIQUE( way, num ) ON CONFLICT REPLACE
);
CREATE TABLE IF NOT EXISTS relations (
id INTEGER NOT NULL PRIMARY KEY
);
CREATE TABLE IF NOT EXISTS relation_tags (
ref INTEGER NOT NULL,
key TEXT,
value TEXT,
UNIQUE( ref, key ) ON CONFLICT REPLACE
);
CREATE TABLE IF NOT EXISTS members (
relation INTEGER NOT NULL,
type TEXT,
ref INTEGER NOT NULL,
role TEXT
);
COMMIT TRANSACTION;`)
if err != nil {
panic(err)
}
}
// created prepared statement
func (c *Connection) prepare() {
node, err := c.db.Prepare("INSERT OR REPLACE INTO nodes (id, lon, lat) VALUES (:id, :lon, :lat)")
if err != nil {
panic(err)
}
nodeTags, err := c.db.Prepare("INSERT OR REPLACE INTO node_tags (ref, key, value) VALUES (:ref, :key, :value)")
if err != nil {
panic(err)
}
way, err := c.db.Prepare("INSERT OR REPLACE INTO ways (id) VALUES (:id)")
if err != nil {
panic(err)
}
wayTags, err := c.db.Prepare("INSERT OR REPLACE INTO way_tags (ref, key, value) VALUES (:ref, :key, :value)")
if err != nil {
panic(err)
}
wayNodes, err := c.db.Prepare("INSERT OR REPLACE INTO way_nodes (way, num, node) VALUES (:way, :num, :node)")
if err != nil {
panic(err)
}
relation, err := c.db.Prepare("INSERT OR REPLACE INTO relations (id) VALUES (:id)")
if err != nil {
panic(err)
}
relationTags, err := c.db.Prepare("INSERT OR REPLACE INTO relation_tags (ref, key, value) VALUES (:ref, :key, :value)")
if err != nil {
panic(err)
}
member, err := c.db.Prepare("INSERT OR REPLACE INTO members (relation, type, ref, role) VALUES (:relation, :type, :ref, :role)")
if err != nil {
panic(err)
}
c.Stmt = &Statements{
Node: node,
NodeTags: nodeTags,
Way: way,
WayTags: wayTags,
WayNodes: wayNodes,
Relation: relation,
RelationTags: relationTags,
Member: member,
}
}