]>
code.octet-stream.net Git - broadcaster/blob - server/database.go
11 type Database
struct {
18 sqldb
, err
:= sql
. Open ( "sqlite" , config
. SqliteDB
)
24 _
, err
= db
. sqldb
. Exec ( "PRAGMA journal_mode = WAL" )
29 _
, err
= db
. sqldb
. Exec ( "PRAGMA foreign_keys = ON" )
34 _
, err
= db
. sqldb
. Exec ( "PRAGMA busy_timeout = 5000" )
40 CREATE TABLE IF NOT EXISTS sessions (id INTEGER PRIMARY KEY AUTOINCREMENT, token TEXT, username TEXT, created TIMESTAMP, expiry TIMESTAMP);
41 CREATE TABLE IF NOT EXISTS playlists (id INTEGER PRIMARY KEY AUTOINCREMENT, enabled INTEGER, name TEXT, start_time TEXT);
42 CREATE TABLE IF NOT EXISTS playlist_entries (id INTEGER PRIMARY KEY AUTOINCREMENT, playlist_id INTEGER, position INTEGER, filename TEXT, delay_seconds INTEGER, is_relative INTEGER, CONSTRAINT fk_playlists FOREIGN KEY (playlist_id) REFERENCES playlists(id) ON DELETE CASCADE);
43 CREATE TABLE IF NOT EXISTS radios (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, token TEXT);
44 CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT UNIQUE, password_hash TEXT, is_admin INTEGER);
46 _
, err
= db
. sqldb
. Exec ( sqlStmt
)
48 log
. Printf ( " %q : %s \n " , err
, sqlStmt
)
53 func ( d
* Database
) CloseDatabase () {
57 func ( d
* Database
) InsertSession ( user
string , token
string , expiry time
. Time
) {
58 _
, err
:= d
. sqldb
. Exec ( "INSERT INTO sessions (token, username, created, expiry) values (?, ?, CURRENT_TIMESTAMP, ?)" , token
, user
, expiry
)
64 func ( d
* Database
) GetUserNameForSession ( token
string ) ( string , error
) {
66 err
:= d
. sqldb
. QueryRow ( "SELECT username FROM sessions WHERE token = ? AND expiry > CURRENT_TIMESTAMP" , token
). Scan (& username
)
68 return "" , errors
. New ( "no matching token" )
73 func ( d
* Database
) GetUser ( username
string ) ( User
, error
) {
75 err
:= d
. sqldb
. QueryRow ( "SELECT id, username, password_hash, is_admin FROM users WHERE username = ?" , username
). Scan (& user
. Id
, & user
. Username
, & user
. PasswordHash
, & user
. IsAdmin
)
77 return User
{}, errors
. New ( "no user with that username" )
82 func ( d
* Database
) GetUsers () [] User
{
83 ret
:= make ([] User
, 0 )
84 rows
, err
:= d
. sqldb
. Query ( "SELECT id, username, password_hash, is_admin FROM users ORDER BY username ASC" )
91 if err
:= rows
. Scan (& u
. Id
, & u
. Username
, & u
. PasswordHash
, & u
. IsAdmin
); err
!= nil {
99 func ( d
* Database
) SetUserPassword ( username
string , passwordHash
string ) {
100 d
. sqldb
. Exec ( "UPDATE users SET password_hash = ? WHERE username = ?" , passwordHash
, username
)
103 func ( d
* Database
) ClearOtherSessions ( username
string , token
string ) {
104 d
. sqldb
. Exec ( "DELETE FROM sessions WHERE username = ? AND token != ?" , username
, token
)
107 func ( d
* Database
) SetUserIsAdmin ( username
string , isAdmin
bool ) {
108 d
. sqldb
. Exec ( "UPDATE users SET is_admin = ? WHERE username = ?" , isAdmin
, username
)
111 func ( d
* Database
) CreateUser ( user User
) error
{
112 _
, err
:= d
. sqldb
. Exec ( "INSERT INTO users (username, password_hash, is_admin) values (?, ?, ?)" , user
. Username
, user
. PasswordHash
, user
. IsAdmin
)
116 func ( d
* Database
) DeleteUser ( username
string ) error
{
117 _
, err
:= d
. sqldb
. Exec ( "DELETE FROM users WHERE username = ?" , username
)
121 func ( d
* Database
) CreatePlaylist ( playlist Playlist
) int {
123 tx
, _
:= d
. sqldb
. Begin ()
124 _
, err
:= tx
. Exec ( "INSERT INTO playlists (enabled, name, start_time) values (?, ?, ?)" , playlist
. Enabled
, playlist
. Name
, playlist
. StartTime
)
128 err
= tx
. QueryRow ( "SELECT last_insert_rowid()" ). Scan (& id
)
139 func ( d
* Database
) DeletePlaylist ( playlistId
int ) {
140 d
. sqldb
. Exec ( "DELETE FROM playlists WHERE id = ?" , playlistId
)
143 func ( d
* Database
) GetPlaylists () [] Playlist
{
144 ret
:= make ([] Playlist
, 0 )
145 rows
, err
:= d
. sqldb
. Query ( "SELECT id, enabled, name, start_time FROM playlists ORDER BY id ASC" )
152 if err
:= rows
. Scan (& p
. Id
, & p
. Enabled
, & p
. Name
, & p
. StartTime
); err
!= nil {
160 func ( d
* Database
) GetPlaylist ( playlistId
int ) ( Playlist
, error
) {
162 err
:= d
. sqldb
. QueryRow ( "SELECT id, enabled, name, start_time FROM playlists WHERE id = ?" , playlistId
). Scan (& p
. Id
, & p
. Enabled
, & p
. Name
, & p
. StartTime
)
169 func ( d
* Database
) UpdatePlaylist ( playlist Playlist
) {
170 d
. sqldb
. Exec ( "UPDATE playlists SET enabled = ?, name = ?, start_time = ? WHERE id = ?" , playlist
. Enabled
, playlist
. Name
, playlist
. StartTime
, playlist
. Id
)
173 func ( d
* Database
) SetEntriesForPlaylist ( entries
[] PlaylistEntry
, playlistId
int ) {
174 tx
, _
:= d
. sqldb
. Begin ()
175 _
, err
:= tx
. Exec ( "DELETE FROM playlist_entries WHERE playlist_id = ?" , playlistId
)
176 for _
, e
:= range entries
{
177 _
, err
= tx
. Exec ( "INSERT INTO playlist_entries (playlist_id, position, filename, delay_seconds, is_relative) values (?, ?, ?, ?, ?)" , playlistId
, e
. Position
, e
. Filename
, e
. DelaySeconds
, e
. IsRelative
)
182 tx
. Commit () // ignore errors
185 func ( d
* Database
) GetEntriesForPlaylist ( playlistId
int ) [] PlaylistEntry
{
186 ret
:= make ([] PlaylistEntry
, 0 )
187 rows
, err
:= d
. sqldb
. Query ( "SELECT id, position, filename, delay_seconds, is_relative FROM playlist_entries WHERE playlist_id = ? ORDER by position ASC" , playlistId
)
193 var entry PlaylistEntry
194 if err
:= rows
. Scan (& entry
. Id
, & entry
. Position
, & entry
. Filename
, & entry
. DelaySeconds
, & entry
. IsRelative
); err
!= nil {
197 ret
= append ( ret
, entry
)
202 func ( d
* Database
) GetRadio ( radioId
int ) ( Radio
, error
) {
204 err
:= d
. sqldb
. QueryRow ( "SELECT id, name, token FROM radios WHERE id = ?" , radioId
). Scan (& r
. Id
, & r
. Name
, & r
. Token
)
211 func ( d
* Database
) GetRadioByToken ( token
string ) ( Radio
, error
) {
213 err
:= d
. sqldb
. QueryRow ( "SELECT id, name, token FROM radios WHERE token = ?" , token
). Scan (& r
. Id
, & r
. Name
, & r
. Token
)
220 func ( d
* Database
) GetRadios () [] Radio
{
221 ret
:= make ([] Radio
, 0 )
222 rows
, err
:= d
. sqldb
. Query ( "SELECT id, name, token FROM radios ORDER BY id ASC" )
229 if err
:= rows
. Scan (& r
. Id
, & r
. Name
, & r
. Token
); err
!= nil {
237 func ( d
* Database
) DeleteRadio ( radioId
int ) {
238 d
. sqldb
. Exec ( "DELETE FROM radios WHERE id = ?" , radioId
)
241 func ( d
* Database
) CreateRadio ( radio Radio
) {
242 d
. sqldb
. Exec ( "INSERT INTO radios (name, token) values (?, ?)" , radio
. Name
, radio
. Token
)
245 func ( d
* Database
) UpdateRadio ( radio Radio
) {
246 d
. sqldb
. Exec ( "UPDATE radios SET name = ?, token = ? WHERE id = ?" , radio
. Name
, radio
. Token
, radio
. Id
)