-
-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathdatabase_d_sqlite.v
More file actions
62 lines (53 loc) · 1.35 KB
/
database_d_sqlite.v
File metadata and controls
62 lines (53 loc) · 1.35 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
module main
import config
import db.sqlite
import os
type GitlyDb = sqlite.DB
fn connect_db(conf config.Config) !GitlyDb {
path := first_env(['GITLY_SQLITE_PATH', 'GITLY_DB_PATH'], conf.sqlite.path)
mut db := sqlite.connect(path)!
if db.busy_timeout(10000) != 0 {
return error('failed to configure sqlite busy timeout')
}
db.exec('pragma journal_mode = WAL;') or { eprintln('cannot enable sqlite WAL mode: ${err}') }
return GitlyDb(db)
}
fn db_backend_name() string {
return 'sqlite'
}
fn db_exec_values(db &GitlyDb, query string) ![][]string {
rows := db.exec(query)!
mut values := [][]string{cap: rows.len}
for row in rows {
values << row.vals.clone()
}
return values
}
fn db_last_insert_id(db &GitlyDb) int {
rows := db.exec('select last_insert_rowid()') or { return 0 }
if rows.len > 0 && rows[0].vals.len > 0 {
return rows[0].vals[0].int()
}
return 0
}
fn db_column_exists(db &GitlyDb, table_name string, column_name string) !bool {
rows := db_exec_values(db, 'pragma table_info(${sql_table(table_name)})')!
for row in rows {
if row.len > 1 && row[1] == column_name {
return true
}
}
return false
}
fn db_bool_column_type() string {
return 'INTEGER NOT NULL DEFAULT 0'
}
fn first_env(keys []string, fallback string) string {
for key in keys {
value := os.getenv(key)
if value != '' {
return value
}
}
return fallback
}