startpage/db.go

95 lines
3.4 KiB
Go

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)
DeleteSessionsByExpirationDateBefore = advsql.Delete(Database, "DELETE FROM sessions WHERE expiration_date <= ?")
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)
GetVisibleBookmarksBySessionIdOrdered = advsql.QueryMany(Database, "SELECT * FROM bookmarks WHERE session_id = ? AND hide = 0 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 = ?, bookmark_size = ? 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,
&u.Hide,
)
}
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.Hide,
&u.SessionID,
&u.Title,
)
}
func ScanSettingsPkFirst(u *Settings, encode advsql.ScanFunc) error {
return encode(&u.SessionID, &u.Background, &u.Foreground, &u.Search, &u.BorderRadius, &u.BookmarkSize)
}
func ScanSettingsPkLast(u *Settings, encode advsql.ScanFunc) error {
return encode(&u.Background, &u.Foreground, &u.Search, &u.BorderRadius, &u.BookmarkSize, &u.SessionID)
}