-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth_sqlite.go
42 lines (36 loc) · 1.12 KB
/
auth_sqlite.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
package mtdb
import (
"database/sql"
)
type sqliteAuthRepository struct {
db *sql.DB
}
func (repo *sqliteAuthRepository) GetByUsername(username string) (*AuthEntry, error) {
rows, err := repo.db.Query("select id,name,password,last_login from auth where name = $1", username)
if err != nil {
return nil, err
}
if !rows.Next() {
return nil, nil
}
entry := &AuthEntry{}
err = rows.Scan(&entry.ID, &entry.Name, &entry.Password, &entry.LastLogin)
return entry, err
}
func (repo *sqliteAuthRepository) Create(entry *AuthEntry) error {
res, err := repo.db.Exec("insert into auth(name,password,last_login) values($1,$2,$3)", entry.Name, entry.Password, entry.LastLogin)
if err != nil {
return err
}
id, err := res.LastInsertId()
entry.ID = &id
return err
}
func (repo *sqliteAuthRepository) Update(entry *AuthEntry) error {
_, err := repo.db.Exec("update auth set name = $1, password = $2, last_login = $3 where id = $4", entry.Name, entry.Password, entry.LastLogin, entry.ID)
return err
}
func (repo *sqliteAuthRepository) Delete(id int64) error {
_, err := repo.db.Exec("delete from auth where id = $1", id)
return err
}