2024-05-08 08:00:58 +00:00
|
|
|
package sites
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
const createSitesTable = `CREATE TABLE IF NOT EXISTS sites (
|
2024-05-08 08:26:26 +00:00
|
|
|
url TEXT NOT NULL PRIMARY KEY,
|
2024-05-08 08:00:58 +00:00
|
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
2024-05-08 08:26:26 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-05-08 08:26:26 +00:00
|
|
|
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) {
|
2024-05-08 08:26:26 +00:00
|
|
|
db.Exec(upsertQuery, s.Url, s.Name)
|
2024-05-08 08:00:58 +00:00
|
|
|
}
|