oopsie/sites/sites.go

35 lines
618 B
Go
Raw Normal View History

2024-05-08 08:00:58 +00:00
package sites
import (
"database/sql"
"fmt"
)
const createSitesTable = `CREATE TABLE IF NOT EXISTS sites (
url TEXT NOT NULL PRIMARY KEY,
2024-05-08 08:00:58 +00:00
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
name TEXT NOT NULL
2024-05-08 08:00:58 +00:00
);`
func CreateTable(db *sql.DB) {
if _, err := db.Exec(createSitesTable); err != nil {
fmt.Println(fmt.Errorf(err.Error()))
}
}
type Site struct {
created_at uint64
Name string
Url string
}
const upsertQuery = `INSERT INTO sites (url, name) VALUES (?, ?)
ON CONFLICT (url) DO UPDATE
SET
name = excluded.name
`
2024-05-08 08:00:58 +00:00
func (s *Site) Save(db *sql.DB) {
db.Exec(upsertQuery, s.Url, s.Name)
2024-05-08 08:00:58 +00:00
}