2016-12-08 21:20:40 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import(
|
|
|
|
"database/sql"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Page struct {
|
|
|
|
ID int64
|
2016-12-10 13:16:05 +00:00
|
|
|
Hostname string
|
2016-12-08 21:20:40 +00:00
|
|
|
Path string
|
|
|
|
Title string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Page) Save(conn *sql.DB) error {
|
|
|
|
// prepare statement for inserting data
|
|
|
|
stmt, err := conn.Prepare(`INSERT INTO pages(
|
2016-12-10 13:16:05 +00:00
|
|
|
hostname,
|
2016-12-08 21:20:40 +00:00
|
|
|
path,
|
|
|
|
title
|
|
|
|
) VALUES( ?, ?, ? )`)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
2016-12-10 13:16:05 +00:00
|
|
|
result, err := stmt.Exec(p.Hostname, p.Path, p.Title)
|
2016-12-08 21:20:40 +00:00
|
|
|
p.ID, _ = result.LastInsertId()
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|