-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase.go
More file actions
128 lines (103 loc) · 2.99 KB
/
database.go
File metadata and controls
128 lines (103 loc) · 2.99 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (C) 2016 Eneo Tecnologia S.L.
// Diego Fernández Barrera <bigomby@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"database/sql"
"github.com/sirupsen/logrus"
_ "github.com/mattn/go-sqlite3"
)
const (
sqlCreateTable = "CREATE TABLE IF NOT EXISTS Devices (Hash varchar(255) PRIMARY KEY, Uuid varchar(255))"
sqlInsertEntry = "INSERT INTO Devices (Hash, Uuid) values (?, ?)"
sqlSelectDevices = "SELECT * FROM Devices"
)
// Database handles the connection with a SQL Database
type Database struct {
config DatabaseConfig
}
// NewDatabase creates a new instance of a database
func NewDatabase(config DatabaseConfig) *Database {
db := &Database{
config: config,
}
if db.config.Logger == nil {
db.config.Logger = logrus.New()
}
logger := db.config.Logger
if len(db.config.dbFile) <= 0 {
return nil
}
var err error
db.config.sqldb, err = sql.Open("sqlite3", db.config.dbFile)
if err != nil {
logger.Fatal(err)
}
// Ping db to check if db is available
if err := db.config.sqldb.Ping(); err != nil {
logger.Error(err)
return nil
}
// Init connection with db
if _, err := db.config.sqldb.Begin(); err != nil {
logger.Error(err)
return nil
}
// Create table if no exists
if _, err := db.config.sqldb.Exec(sqlCreateTable); err != nil {
logger.Error(err)
return nil
}
return db
}
// LoadUUID loads from the database the UUID used along with a previous HASH
func (db *Database) LoadUUID(hash string) (uuid string, err error) {
logger := db.config.Logger
// Get all the rows and load data
rows, err := db.config.sqldb.Query(sqlSelectDevices)
if err != nil {
return
}
for rows.Next() {
var devHash sql.NullString
var devUUID sql.NullString
if err = rows.Scan(&devHash, &devUUID); err != nil {
return
}
// If hash is in database load the associated UUID
if devHash.String == hash {
uuid = devUUID.String
break
}
}
if len(uuid) > 0 {
logger.Debugf("Loaded UUID from DB: %s", uuid)
}
return
}
// StoreUUID save the UUD to a database
func (db *Database) StoreUUID(hash, uuid string) error {
logger := db.config.Logger
// Insert data into DB
if _, err := db.config.sqldb.Exec(sqlInsertEntry, hash, uuid); err != nil {
return err
}
logger.Infof("Stored UUID on DB: %s", uuid)
return nil
}
// Close closes the connection with the database
func (db *Database) Close() {
db.config.sqldb.Close()
}