-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathdatabase_notd_sqlite.v
More file actions
61 lines (53 loc) · 1.34 KB
/
database_notd_sqlite.v
File metadata and controls
61 lines (53 loc) · 1.34 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
module main
import config
import db.pg
import os
type GitlyDb = pg.DB
fn connect_db(conf config.Config) !GitlyDb {
if conninfo := first_env_opt(['GITLY_DB_CONNINFO', 'DATABASE_URL'], conf.pg.conninfo) {
return GitlyDb(pg.connect_with_conninfo(conninfo)!)
}
return GitlyDb(pg.connect(
host: first_env(['GITLY_DB_HOST', 'PGHOST'], conf.pg.host)
port: first_int_env(['GITLY_DB_PORT', 'PGPORT'], conf.pg.port)
dbname: first_env(['GITLY_DB_NAME', 'PGDATABASE'], conf.pg.dbname)
user: first_env(['GITLY_DB_USER', 'PGUSER'], conf.pg.user)
password: first_env(['GITLY_DB_PASSWORD', 'PGPASSWORD'], conf.pg.password)
)!)
}
fn db_backend_name() string {
return 'postgres'
}
fn db_exec_values(db &GitlyDb, query string) ![][]string {
rows := db.exec_no_null(query)!
mut values := [][]string{cap: rows.len}
for row in rows {
values << row.vals.clone()
}
return values
}
fn first_env(keys []string, fallback string) string {
for key in keys {
value := os.getenv(key)
if value != '' {
return value
}
}
return fallback
}
fn first_env_opt(keys []string, fallback string) ?string {
value := first_env(keys, fallback)
if value == '' {
return none
}
return value
}
fn first_int_env(keys []string, fallback int) int {
for key in keys {
value := os.getenv(key)
if value != '' {
return value.int()
}
}
return fallback
}