-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
62 lines (57 loc) · 1.68 KB
/
query.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
package db
import (
"errors"
"fmt"
)
// QueryAllDevices returns every device that is registered in the database
func QueryAllDevices() ([]Device, error) {
var result []Device
rows, err := db.Query("SELECT * FROM devices")
if err != nil {
return result, err
}
for rows.Next() {
var d Device
rows.Scan(&d.ID, &d.Name, &d.Project, &d.LongID, &d.Hostname, &d.Port, &d.Connector, &d.Key, &d.UseKey)
result = append(result, d)
}
return result, nil
}
// QueryDevice by the SQL ID
func QueryDevice(deviceID int64) (Device, error) {
var result Device
rows, err := db.Query(fmt.Sprintf("SELECT * FROM 'devices' WHERE ID=\"%d\"", deviceID))
if err != nil {
return result, err
}
iterated := false
for rows.Next() {
err = rows.Scan(&result.ID, &result.Name, &result.Project, &result.LongID, &result.Hostname, &result.Port, &result.Connector, &result.Key, &result.UseKey)
if iterated {
return result, errors.New("duplicate long ID entries")
}
if err != nil {
return result, err
}
iterated = true
}
return result, nil
}
func QueryUserFromUsername(username string) (User, error) {
var result User
rows, err := db.Query(fmt.Sprintf("SELECT * FROM 'users' WHERE username=\"%s\"", username))
if err != nil {
return result, err
}
err = rows.Scan(result.ID, &result.Name, &result.Username, &result.Password, &result.Created, &result.LastLogin)
return result, nil
}
func QueryUserFromID(id int64) (User, error) {
var result User
rows, err := db.Query(fmt.Sprintf("SELECT * FROM 'users' WHERE id=\"%d\"", id))
if err != nil {
return result, err
}
err = rows.Scan(result.ID, &result.Name, &result.Username, &result.Password, &result.Created, &result.LastLogin)
return result, nil
}