-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
113 lines (93 loc) · 2.43 KB
/
storage.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
package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
)
var storage = NewImageDataStorage(imageDataDriverName, imageDataDataSourceName)
func NewImageDataStorage(driverName string, dataSourceName string) *ImageDataStorage {
return &ImageDataStorage{
DriverName: driverName,
DataSourceName: dataSourceName,
}
}
func (s *ImageDataStorage) Init() error {
db, err := sql.Open(s.DriverName, s.DataSourceName)
if err != nil {
return err
}
s.Client = db
sqlStmt := fmt.Sprintf(`
create table if not exists %s (
fileName text not null primary key,
spriteType text,
hue integer,
saturation float
);
`, imageDataTableName)
_, err = s.Client.Exec(sqlStmt)
if err != nil {
return err
}
return nil
}
func (s *ImageDataStorage) GetAll() ([]ImageData, error) {
rows, err := s.Client.Query(fmt.Sprintf("select * from %s", imageDataTableName))
if err != nil {
return []ImageData{}, err
}
defer rows.Close()
var allImageData = []ImageData{}
for rows.Next() {
i, err := scanIntoImageData(rows)
if err != nil {
return []ImageData{}, err
}
allImageData = append(allImageData, *i)
}
// Check for errors from iterating over rows
if err := rows.Err(); err != nil {
return []ImageData{}, err
}
return allImageData, nil
}
func (s *ImageDataStorage) GetOne(st SpriteType, fileName string) (*ImageData, error) {
rows, err := s.Client.Query(
fmt.Sprintf(`select * from %s where spriteType = "%s" and fileName = "%s"`, imageDataTableName, st, fileName),
)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
return scanIntoImageData(rows)
}
return nil, fmt.Errorf("imageData with spriteType %s and fileName %s not found", st, fileName)
}
func (s *ImageDataStorage) UpsertOne(i ImageData) error {
sqlStmt := fmt.Sprintf(`
insert or replace into %s (fileName, spriteType, hue, saturation)
values (?, ?, ?, ?);
`, imageDataTableName)
_, err := s.Client.Exec(sqlStmt, i.fileName, i.spriteType, i.Hue, i.Saturation)
if err != nil {
return err
}
return nil
}
func (s *ImageDataStorage) DeleteOne(st SpriteType, fileName string) error {
_, err := s.Client.Query(
fmt.Sprintf(`delete from %s where spriteType = "%s" and fileName = "%s"`, imageDataTableName, st, fileName),
)
return err
}
func scanIntoImageData(rows *sql.Rows) (*ImageData, error) {
i := new(ImageData)
err := rows.Scan(
&i.fileName,
&i.spriteType,
&i.Hue,
&i.Saturation,
)
return i, err
}