startpage/db.go

92 lines
3.2 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 (
2022-09-06 12:36:21 +02:00
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 <= ?")
2022-09-06 11:22:28 +02:00
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 = ?")
2022-09-07 11:27:35 +02:00
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)
2022-09-06 11:22:28 +02:00
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 {
2022-09-07 11:27:35 +02:00
return encode(&u.SessionID, &u.Background, &u.Foreground, &u.Search, &u.BorderRadius, &u.BookmarkSize)
2022-09-06 11:22:28 +02:00
}
func ScanSettingsPkLast(u *Settings, encode advsql.ScanFunc) error {
2022-09-07 11:27:35 +02:00
return encode(&u.Background, &u.Foreground, &u.Search, &u.BorderRadius, &u.BookmarkSize, &u.SessionID)
2022-09-06 11:22:28 +02:00
}