This repository has been archived by the owner on Jan 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.go
67 lines (57 loc) · 1.68 KB
/
fetch.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
package userDB
import (
"database/sql"
"fmt"
)
// Database entry
type Entry struct {
Id int
Name string
BanId int
Ban string
Email string
}
// Fetch a query string (not exec; returns the query)
func fetchQuery(db *sql.DB, query string) ([]Entry, error) {
stmt, err := db.Prepare(query)
if err != nil {
return nil, err
}
defer stmt.Close()
rows, err := stmt.Query()
if err != nil {
return nil, err
}
defer rows.Close()
var entries []Entry
for rows.Next() {
var id int
var name string
var banId int
var ban string
var email string
if err := rows.Scan(&id, &name, &banId, &ban, &email); err != nil {
return nil, err
}
entry := Entry{id, name, banId, ban, email}
entries = append(entries, entry)
}
return entries, nil
}
// Fetch every user from the database
func FetchAllUsers(db *sql.DB) ([]Entry, error) {
query := "SELECT Users.User_Id as id, Users.Name as name, Bannen.Ban_id as ban_id, Bannen.Naam as ban, Emails.Email as email FROM Users " +
"INNER JOIN User_Ban ON User_Ban.User_Id = Users.User_Id " +
"INNER JOIN Bannen ON User_Ban.Ban_Id = Bannen.Ban_Id " +
"INNER JOIN Emails ON Emails.Email_id = Users.User_Id"
return fetchQuery(db, query)
}
// Fetch all users in a `ban`. Use the `ban` enum
func FetchBan(db *sql.DB, ban string) ([]Entry, error) {
query := "SELECT Users.User_Id as id, Users.Name as name, Bannen.Ban_id as ban_id, Bannen.Naam as ban, Emails.Email as email FROM Users " +
"INNER JOIN User_Ban ON User_Ban.User_Id = Users.User_Id " +
"INNER JOIN Bannen ON User_Ban.Ban_Id = Bannen.Ban_Id " +
fmt.Sprintf("AND Bannen.Naam = '%s' ", ban) +
"INNER JOIN Emails ON Emails.Email_id = Users.User_Id"
return fetchQuery(db, query)
}