startpage/db.go

91 lines
2.9 KiB
Go
Raw Normal View History

2022-09-06 11:22:28 +02:00
package main
import (
"time"
"git.milar.in/milarin/advsql"
)
var Database = &advsql.Database{}
var (
InsertSession = advsql.Insert(Database, "INSERT INTO sessions VALUES (?, ?, ?)", ScanSessionPkFirst)
UpdateSession = advsql.Update(Database, "UPDATE sessions SET expiration_date = ?, creation_date = ? WHERE id = ?", ScanSessionPkLast)
GetSessionByID = advsql.QueryOne(Database, "SELECT * FROM sessions WHERE id = ?", ScanSessionPkFirst)
InsertBookmark = advsql.Insert(Database, "INSERT INTO bookmarks VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", ScanBookmarkPkFirst)
UpdateBookmark = advsql.Update(Database, "UPDATE bookmarks SET link = ?, image = ?, color = ?, image_size = ?, icon_padding = ?, hide_border = ?, order_priority = ? WHERE session_id = ? AND title = ?", ScanBookmarkPkLast)
GetBookmarkBySessionIDAndTitle = advsql.QueryOne(Database, "SELECT * FROM bookmarks WHERE session_id = ? AND title = ?", ScanBookmarkPkFirst)
GetBookmarksBySessionIdOrdered = advsql.QueryMany(Database, "SELECT * FROM bookmarks WHERE session_id = ? ORDER BY order_priority", ScanBookmarkPkFirst)
DeleteBookmarksBySessionID = advsql.Delete(Database, "DELETE FROM bookmarks WHERE session_id = ?")
InsertSettings = advsql.Insert(Database, "INSERT INTO settings VALUES (?, ?, ?, ?, ?)", ScanSettingsPkFirst)
UpdateSettings = advsql.Update(Database, "UPDATE settings SET background_color = ?, foreground_color = ?, search_query = ?, border_radius = ? WHERE session_id = ?", ScanSettingsPkLast)
GetSettingsBySessionID = advsql.QueryOne(Database, "SELECT * FROM settings WHERE session_id = ?", ScanSettingsPkFirst)
)
type Session struct {
ID string
ExpirationDate time.Time
CreationDate time.Time
}
func ScanSessionPkFirst(u *Session, encode advsql.ScanFunc) error {
return encode(&u.ID, &u.ExpirationDate, &u.CreationDate)
}
func ScanSessionPkLast(u *Session, encode advsql.ScanFunc) error {
return encode(&u.ExpirationDate, &u.CreationDate, &u.ID)
}
func NewSession() (*Session, error) {
id, err := generateRandomID(32)
if err != nil {
return nil, err
}
return &Session{
ID: id,
CreationDate: time.Now(),
ExpirationDate: time.Now().AddDate(1, 0, 0),
}, nil
}
func ScanBookmarkPkFirst(u *Bookmark, encode advsql.ScanFunc) error {
return encode(
&u.SessionID,
&u.Title,
&u.Link,
&u.Image,
&u.Color,
&u.ImageSize,
&u.IconPadding,
&u.HideBorder,
&u.Order,
)
}
func ScanBookmarkPkLast(u *Bookmark, encode advsql.ScanFunc) error {
return encode(
&u.Link,
&u.Image,
&u.Color,
&u.ImageSize,
&u.IconPadding,
&u.HideBorder,
&u.Order,
&u.SessionID,
&u.Title,
)
}
func ScanSettingsPkFirst(u *Settings, encode advsql.ScanFunc) error {
return encode(&u.SessionID, &u.Background, &u.Foreground, &u.Search, &u.BorderRadius)
}
func ScanSettingsPkLast(u *Settings, encode advsql.ScanFunc) error {
return encode(&u.Background, &u.Foreground, &u.Search, &u.BorderRadius, &u.SessionID)
}