42 lines
724 B
Go
42 lines
724 B
Go
|
package sites
|
||
|
|
||
|
import (
|
||
|
"database/sql"
|
||
|
"fmt"
|
||
|
)
|
||
|
|
||
|
const createSitesTable = `CREATE TABLE IF NOT EXISTS sites (
|
||
|
id INTEGER NOT NULL PRIMARY KEY,
|
||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||
|
name TEXT NOT NULL,
|
||
|
url TEXT NOT NULL
|
||
|
);`
|
||
|
|
||
|
func CreateTable(db *sql.DB) {
|
||
|
if _, err := db.Exec(createSitesTable); err != nil {
|
||
|
fmt.Println(fmt.Errorf(err.Error()))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type Site struct {
|
||
|
id uint64
|
||
|
created_at uint64
|
||
|
Name string
|
||
|
Url string
|
||
|
}
|
||
|
|
||
|
func (s *Site) Save(db *sql.DB) {
|
||
|
if s.id != 0 {
|
||
|
query := `UPDATE SITES
|
||
|
SET
|
||
|
name=?,
|
||
|
url=?
|
||
|
WHERE id =?
|
||
|
`
|
||
|
db.Exec(query, s.Name, s.Url, s.id)
|
||
|
} else {
|
||
|
query := `INSERT INTO SITES (name, url) VALUES (?, ?)`
|
||
|
db.Exec(query, s.Name, s.Url)
|
||
|
}
|
||
|
}
|