-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
79 lines (69 loc) · 2.16 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"database/sql"
"fmt"
"os"
"time"
_ "github.com/mattn/go-sqlite3"
)
func NewDbConn() *sql.DB {
database, err := sql.Open("sqlite3", "./db/database.db")
if err != nil {
fmt.Println("Open database error")
}
//defer database.Close()
return database
}
func Select(database *sql.DB) []Source {
query := "SELECT * FROM status;"
rows, err := database.Query(query)
if err != nil {
fmt.Println("Query error")
}
defer rows.Close()
var source Source
var sources []Source
for rows.Next() {
rows.Scan(&source.Host, &source.Desired, &source.Interval, &source.Method, &source.Proxy, &source.LastCode)
sources = append(sources, source)
}
return sources
}
func Insert(database *sql.DB, source Source) string {
statement, err := database.Prepare("INSERT INTO status (host,desired,interval,method,proxy,lastCode) VALUES (?,?,?,?,?,?)")
if err != nil {
fmt.Println(err)
fmt.Println("prepare statement error")
return `{"status":"FAIL"}`
}
statement.Exec(source.Host, source.Desired, source.Interval, source.Method, source.Proxy, source.LastCode)
restart = true
time.Sleep(time.Second)
restart = false
return `{"status":"OK"}`
}
func Delete(database *sql.DB, source Source) string {
statement, err := database.Prepare("DELETE FROM status where host=? AND desired=? AND interval=? AND method=? AND proxy=? ;")
if err != nil {
fmt.Println("prepare statement error")
return `{"status":"FAIL"}`
}
statement.Exec(source.Host, source.Desired, source.Interval, source.Method, source.Proxy)
restart = true
time.Sleep(time.Second)
restart = false
return `{"status":"OK"}`
}
func Update(database *sql.DB, source Source) string {
if os.Getenv("DEBUG") == "true" {
fmt.Println(source)
}
statement, err := database.Prepare("UPDATE status SET lastCode = ? WHERE host=? AND desired=? AND interval=? AND method=? AND proxy=? ;")
if err != nil {
fmt.Println("prepare statement error")
return `{"status":"FAIL"}`
}
time.Sleep(time.Second)
statement.Exec(source.LastCode, source.Host, source.Desired, source.Interval, source.Method, source.Proxy)
return `{"status":"OK"}`
}