-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
105 lines (93 loc) · 2.05 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"database/sql"
"encoding/json"
"log"
_ "github.com/lib/pq"
)
var db *sql.DB = nil
var err error = nil
func Init() {
}
/*Inicializar base de datos*/
func Start() {
db, err = sql.Open("postgres", "postgresql://jhordy@localhost:26257/truora?sslmode=disable")
if err != nil {
log.Fatal("error connecting to the database: ", err)
}
if _, err := db.Exec(
"CREATE TABLE IF NOT EXISTS truora_whois (url varchar(300) PRIMARY KEY, data text)"); err != nil {
log.Fatal(err)
}
}
/*Obtener todos los registros */
func GetAll() (outList []string, out []Out) {
if db == nil {
Start()
}
counter, err := db.Query("SELECT count(url) as size FROM truora_whois")
if err != nil {
log.Fatal(err)
}
var size int
if counter.Next() {
if err := counter.Scan(&size); err != nil {
log.Fatal(err)
}
}
defer counter.Close()
rows, err := db.Query("SELECT url,data FROM truora_whois")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
outList = make([]string, size)
out = make([]Out, size)
for rows.Next() {
var url, data string
if err := rows.Scan(&url, &data); err != nil {
log.Fatal(err)
}
size = size - 1
out[size] = Out{}
outList[size] = url
json.Unmarshal([]byte(data), &out[size])
}
return
}
/*Obtener un registro*/
func Get(url string) (out Out, errOut bool) {
if db == nil {
Start()
}
rows, err := db.Query("SELECT url,data FROM truora_whois WHERE url LIKE '" + url + "'")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
errOut = true
if rows.Next() {
var url, data string
if err := rows.Scan(&url, &data); err != nil {
log.Fatal(err)
}
errOut = data == ""
if !errOut {
json.Unmarshal([]byte(data), &out)
}
}
return
}
/*Insertar o alterar un registro*/
func Insert(url string, data string) {
if db == nil {
Start()
}
if _, err := db.Exec(
"INSERT INTO truora_whois (url, data) VALUES ('" + url + "', '" + data + "')"); err != nil {
if _, err := db.Exec(
"UPDATE truora_whois SET data = '" + data + "' WHERE url LIKE '" + url + "'"); err != nil {
log.Fatal(err)
}
}
}