-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdb.go
65 lines (61 loc) · 1.54 KB
/
db.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
"log"
)
func Db(dbConfig DbConfig) *sql.DB {
db, err := sql.Open("postgres",
fmt.Sprintf("user=%s password=%s host=%s dbname=%s", dbConfig.User,
dbConfig.Pass, dbConfig.Host, dbConfig.Name))
if err != nil {
log.Fatal("Unable to connect to the database: ", err)
}
return db
}
func SetPageViews(db *sql.DB, pageViews []PageView) {
if len(pageViews) < 1 {
return
}
tx, _ := db.Begin()
stmt, err := db.Prepare("INSERT INTO page_view(timestamp, url, ip_address, user_agent, screen_height, screen_width) VALUES (NOW(), $1, $2, $3, $4, $5)")
if err != nil {
log.Println("Unable to prepare statment for PageView: ", err)
}
for k := range pageViews {
_, err = tx.Stmt(stmt).Exec(
pageViews[k].URL,
pageViews[k].IPAddress,
pageViews[k].UserAgent,
pageViews[k].ScreenHeight,
pageViews[k].ScreenWidth,
)
if err != nil {
log.Fatal(err)
}
}
tx.Commit()
}
func SetHrefClicks(db *sql.DB, hrefClicks []HrefClick) {
if len(hrefClicks) < 1 {
return
}
tx, _ := db.Begin()
stmt, err := db.Prepare("INSERT INTO href_click(timestamp, url, ip_address, href, href_rectangle) VALUES (NOW(), $1, $2, $3, box(point($4,$5), point($6,$7)))")
if err != nil {
log.Println("Unable to prepare statment for HrefClick: ", err)
}
for k := range hrefClicks {
tx.Stmt(stmt).Exec(
hrefClicks[k].URL,
hrefClicks[k].IPAddress,
hrefClicks[k].Href,
hrefClicks[k].HrefTop,
hrefClicks[k].HrefRight,
hrefClicks[k].HrefBottom,
hrefClicks[k].HrefLeft,
)
}
tx.Commit()
}