-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsql.go
192 lines (153 loc) · 4.11 KB
/
sql.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
package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"maunium.net/go/mautrix/id"
)
type sqlDB struct {
db *sql.DB
stmtFetchCalendars *sql.Stmt
stmtFetchAllCalendars *sql.Stmt
stmtAddCalendar *sql.Stmt
stmtRemoveCalendar *sql.Stmt
stmtFetchAllUsers *sql.Stmt
stmtAddUser *sql.Stmt
stmtUpdateUserRoomID *sql.Stmt
}
func initSQLDB(path string) (*sqlDB, error) {
db, err := sql.Open("sqlite3", path)
if err != nil {
return nil, err
}
d := &sqlDB{db: db}
err = d.createTables()
if err != nil {
return d, err
}
d.stmtFetchCalendars, err = db.Prepare("SELECT id, user_id, name, cal_type, uri FROM calendar WHERE user_id = ?;")
if err != nil {
return d, err
}
d.stmtFetchAllCalendars, err = db.Prepare("SELECT id, user_id, name, cal_type, uri FROM calendar;")
if err != nil {
return d, err
}
d.stmtAddCalendar, err = db.Prepare("INSERT INTO calendar (user_id, name, cal_type, uri) VALUES (?, ?, ?, ?);")
if err != nil {
return d, err
}
d.stmtRemoveCalendar, err = db.Prepare("DELETE FROM calendar WHERE user_id = ? AND name = ?;")
if err != nil {
return d, err
}
d.stmtFetchAllUsers, err = db.Prepare("SELECT user_id, room_id FROM user;")
if err != nil {
return d, err
}
d.stmtAddUser, err = db.Prepare("INSERT INTO user (user_id, room_id) VALUES (?, ?);")
if err != nil {
return d, err
}
d.stmtUpdateUserRoomID, err = db.Prepare("UPDATE user SET room_id = ? WHERE user_id = ?;")
return d, err
}
func (d *sqlDB) createTables() error {
userSQL := `CREATE TABLE IF NOT EXISTS user (
"user_id" TEXT NOT NULL PRIMARY KEY,
"room_ID" TEXT,
"created" datetime default current_timestamp);`
_, err := d.db.Exec(userSQL)
if err != nil {
return err
}
// TODO: Make calendar have relation with user
calendarSQL := `CREATE TABLE IF NOT EXISTS calendar (
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"user_id" TEXT,
"name" TEXT,
"cal_type" TEXT,
"uri" TEXT,
"created" datetime default current_timestamp);`
_, err = d.db.Exec(calendarSQL)
return err
}
func (d *sqlDB) fetchAllUsers() ([]*user, error) {
rows, err := d.stmtFetchAllUsers.Query()
defer rows.Close()
if err != nil {
return nil, err
}
users := []*user{}
for rows.Next() {
user := &user{}
var roomID string
err = rows.Scan(&user.userID, &roomID)
if err != nil {
return users, err
}
user.roomID = id.RoomID(roomID)
users = append(users, user)
}
return users, nil
}
func (d *sqlDB) fetchAllCalendars() ([]*userCalendar, error) {
rows, err := d.stmtFetchAllCalendars.Query()
defer rows.Close()
if err != nil {
return nil, err
}
return rowsToCalendars(rows)
}
func (d *sqlDB) fetchCalendars(userID id.UserID) ([]*userCalendar, error) {
rows, err := d.stmtFetchCalendars.Query(userID)
defer rows.Close()
if err != nil {
return nil, err
}
return rowsToCalendars(rows)
}
func rowsToCalendars(rows *sql.Rows) ([]*userCalendar, error) {
cals := []*userCalendar{}
for rows.Next() {
cal := &userCalendar{}
var userID string
var calTypeStr string
err := rows.Scan(&cal.DBID, &userID, &cal.Name, &calTypeStr, &cal.URI)
if err != nil {
return cals, err
}
cal.UserID = id.UserID(userID)
switch calTypeStr {
case "caldav":
cal.CalType = calendarTypeCalDav
case "ical":
cal.CalType = calendarTypeICal
default:
fmt.Printf("unknown caltype in database: %q, row id: %d\n", calTypeStr, cal.DBID)
continue
}
cals = append(cals, cal)
}
return cals, nil
}
func (d *sqlDB) addCalendar(userID id.UserID, name string, calType calendarType, uri string) (int64, error) {
res, err := d.stmtAddCalendar.Exec(userID, name, string(calType), uri)
if err != nil {
return 0, err
}
id, err := res.LastInsertId()
return id, err
}
func (d *sqlDB) removeCalendar(userID id.UserID, name string) error {
_, err := d.stmtRemoveCalendar.Exec(userID, name)
return err
}
func (d *sqlDB) updateUserRoomID(userID id.UserID, roomID id.RoomID) error {
_, err := d.stmtUpdateUserRoomID.Exec(roomID, userID)
return err
}
func (d *sqlDB) addUser(userID id.UserID, roomID id.RoomID) error {
_, err := d.stmtAddUser.Exec(userID, roomID)
return err
}