-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdb.go
More file actions
84 lines (69 loc) · 1.83 KB
/
db.go
File metadata and controls
84 lines (69 loc) · 1.83 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
package main
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
// DB is a DB instance for running queries against the bouncer database
type DB struct {
*sql.DB
}
// NewDB returns a new database instance.
func NewDB(dsn string) (*DB, error) {
db, err := sql.Open("mysql", dsn)
if err != nil {
return nil, err
}
err = db.Ping()
if err != nil {
return nil, err
}
return &DB{
DB: db,
}, nil
}
// AliasFor returns the alias for a product
//
// For example firefox-latest will resolve to the latest version of firefox.
func (d *DB) AliasFor(product string) (related string, err error) {
err = d.QueryRow(
"SELECT related_product FROM mirror_aliases WHERE alias = ?",
product).Scan(&related)
if err != nil {
if err == sql.ErrNoRows {
return product, nil
}
return "", err
}
return
}
// OSID returns the id of an operation system, by name
func (d *DB) OSID(name string) (id string, err error) {
err = d.QueryRow(
"SELECT id FROM mirror_os WHERE name = ?",
name).Scan(&id)
return
}
// ProductForLanguage returns the product ID given a product name and language.
func (d *DB) ProductForLanguage(product, lang string) (productID string, sslOnly bool, err error) {
sslInt := 0
err = d.QueryRow(
`SELECT prod.id, prod.ssl_only FROM mirror_products AS prod
LEFT JOIN mirror_product_langs AS langs ON (prod.id = langs.product_id)
WHERE prod.name LIKE ?
AND (langs.language LIKE ? OR langs.language IS NULL)`,
product, lang).Scan(&productID, &sslInt)
if sslInt == 1 {
sslOnly = true
} else {
sslOnly = false
}
return
}
// Location returns the path of the product/os combination
func (d *DB) Location(productID, osID string) (id, path string, err error) {
err = d.QueryRow(
`SELECT id, path FROM mirror_locations
WHERE product_id = ? AND os_id = ?`,
productID, osID).Scan(&id, &path)
return
}