-
Notifications
You must be signed in to change notification settings - Fork 22
/
database.go
43 lines (33 loc) · 822 Bytes
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package pokepaste
import (
"database/sql"
"os"
_ "github.com/go-sql-driver/mysql"
)
var db, _ = sql.Open("mysql", os.Getenv("POKEPASTE_DB"))
func getPaste(id uint64) (paste, title, author, notes []byte, err error) {
row := db.QueryRow("SELECT paste, title, author, notes FROM pastes WHERE id = ?", id)
err = row.Scan(&paste, &title, &author, ¬es)
return
}
func postPaste(paste, title, author, notes *string) (id uint64, err error) {
if len(*title) == 0 {
title = nil
}
if len(*author) == 0 {
author = nil
}
if len(*notes) == 0 {
notes = nil
}
res, err := db.Exec("INSERT INTO pastes(paste, title, author, notes) VALUES(?, ?, ?, ?)", paste, title, author, notes)
if err != nil {
return
}
idSigned, err := res.LastInsertId()
if err != nil {
return
}
id = uint64(idSigned)
return
}