-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbinit.go
243 lines (172 loc) · 5.54 KB
/
dbinit.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"log"
"strings"
)
func InitializeDatabase(fileName string) {
db, err := sql.Open("sqlite3", fileName)
if err!=nil {
log.Fatal(err)
return
}
defer func() {
db.Close()
}()
createTables(db)
populateMasterTables(db)
}
func createTables(db *sql.DB) {
createGenreTable(db)
createHardwareTypeTable(db)
createPlatformTable(db)
createHardwareTable(db)
createGameTable(db)
}
func createGenreTable(db *sql.DB) {
stmt := "CREATE TABLE Genre (Name TEXT NOT NULL PRIMARY KEY, Description TEXT);"
createTableIfNotExist(db, "Genre", stmt)
}
func createHardwareTypeTable(db *sql.DB) {
stmt := "CREATE TABLE HardwareType(Name TEXT NOT NULL PRIMARY KEY, Description TEXT);"
createTableIfNotExist(db, "HardwareType", stmt)
}
func createPlatformTable(db *sql.DB) {
stmt := "CREATE TABLE Platform(Name TEXT NOT NULL PRIMARY KEY, Description TEXT);"
createTableIfNotExist(db, "Platform", stmt)
}
func createHardwareTable(db *sql.DB) {
cols := [...]string{
"Name TEXT NOT NULL",
"HardwareType INTEGER References HardwareType(rowid)",
"Platform TEXT References Platform(rowid)",
"NumberOwned INTEGER",
"NumberBoxed INTEGER",
"Notes TEXT"}
colString := strings.Join(cols[:], ", ")
stmt := fmt.Sprintf("CREATE TABLE Hardware(%s)", colString)
createTableIfNotExist(db, "Hardware", stmt)
}
func createGameTable(db *sql.DB) {
cols := [...]string{
"Title TEXT NOT NULL",
"Genre TEXT References Genre(rowid)",
"Platform TEXT References Platform(rowid)",
"NumberOwned INTEGER",
"NumberBoxed INTEGER",
"NumberOfManuals INTEGER",
"DatePurchased TEXT",
"ApproximatePurchaseDate INTEGER",
"Notes TEXT"}
colString := strings.Join(cols[:], ", ")
stmt := fmt.Sprintf("CREATE TABLE Game(%s)", colString)
createTableIfNotExist(db, "Game", stmt)
}
func createTableIfNotExist(db *sql.DB, tableName string, createStatement string) {
if tableExists(tableName, db) {
return
}
log.Printf("Going to create %s table", tableName)
_, err := db.Exec(createStatement)
if err!=nil {
log.Fatal(err)
}
log.Printf("%s table created successfully", tableName)
}
func tableExists(tableName string, db *sql.DB) bool {
log.Printf("Checking for existence of %s table", tableName)
tx := getDbTransaction(db)
stmt := prepareQuery(tx, "SELECT COUNT(name) FROM sqlite_master WHERE type='table' AND name=?")
defer stmt.Close()
rs, err := stmt.Query(tableName)
if err!= nil {
log.Fatal(err)
}
var count int
for rs.Next() {
rs.Scan(&count)
}
tx.Commit()
return count > 0
}
func populateMasterTables(db *sql.DB) {
populatePlatformsTable(db)
populateGenresTable(db)
populateHardwareTypeTable(db)
}
func populatePlatformsTable(db *sql.DB) {
log.Print("Populating Platform table from masterfiles/platforms.json")
tx := getDbTransaction(db)
existQuery := prepareQuery(tx, "SELECT COUNT(Name) FROM Platform WHERE Name=?")
defer existQuery.Close()
platformData := LoadPlatformsMasterFile()
var platforms []NameDescription
for _, v := range(platformData.Platforms) {
platforms = append(platforms, v)
}
platformsToAdd := getNonExistentNameDescriptionRows(existQuery, platforms)
insertStmt := prepareQuery(tx, "INSERT INTO Platform (Name, Description) VALUES (?, ?)")
defer insertStmt.Close()
insertNameDescriptionTable(insertStmt, platformsToAdd, "platform")
tx.Commit()
}
func populateGenresTable(db *sql.DB) {
log.Print("Populating Genres table from masterfiles/genres.json")
tx := getDbTransaction(db)
existQuery := prepareQuery(tx, "SELECT COUNT(Name) FROM Genre WHERE Name=?")
defer existQuery.Close()
genreData := LoadGenresMasterFile()
var genres []NameDescription
for _, v := range(genreData.Genres) {
genres = append(genres, v)
}
genresToAdd := getNonExistentNameDescriptionRows(existQuery, genres)
insertStmt := prepareQuery(tx, "INSERT INTO Genre (Name, Description) VALUES (?, ?)")
defer insertStmt.Close()
insertNameDescriptionTable(insertStmt, genresToAdd, "genre")
tx.Commit()
}
func populateHardwareTypeTable(db *sql.DB) {
log.Print("Populating HardwareType table from masterfiles/hardwaretypes.json")
tx := getDbTransaction(db)
existQuery := prepareQuery(tx, "SELECT COUNT(Name) FROM HardwareType WHERE Name=?")
defer existQuery.Close()
hardwareTypeData := LoadHardwareTypesMasterFile()
var hardwareTypes []NameDescription
for _, v := range(hardwareTypeData.HardwareTypes) {
hardwareTypes = append(hardwareTypes, v)
}
hardwareTypesToAdd := getNonExistentNameDescriptionRows(existQuery, hardwareTypes)
insertStmt := prepareQuery(tx, "INSERT INTO HardwareType (Name, Description) VALUES (?, ?)")
defer insertStmt.Close()
insertNameDescriptionTable(insertStmt, hardwareTypesToAdd, "hardware type")
tx.Commit()
}
func getNonExistentNameDescriptionRows(existQuery *sql.Stmt, items []NameDescription) []NameDescription {
var out []NameDescription
for _, item := range(items) {
rs, err := existQuery.Query(item.GetName())
if err!=nil {
log.Fatal(err)
}
var count int
for rs.Next() {
rs.Scan(&count)
}
if count==0 {
out = append(out, item)
}
}
return out
}
func insertNameDescriptionTable(insertStmt *sql.Stmt, items []NameDescription, itemName string) {
for _, item := range(items) {
log.Printf("Inserting %s %s", itemName, item)
_, err := insertStmt.Exec(item.GetName(), item.GetDescription())
if err!=nil {
log.Fatal(err)
}
}
}