-
Notifications
You must be signed in to change notification settings - Fork 1
/
getCommon.go
117 lines (95 loc) · 2.97 KB
/
getCommon.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) 2016 OpenM++
// This code is licensed under the MIT license (see LICENSE.txt for details)
package db
import (
"database/sql"
"strconv"
)
// GetProfileList return profile names: profile_lst table rows.
//
// Profile is a named group of (key, value) options, similar to ini-file.
// Default model options has profile_name = model_name.
func GetProfileList(dbConn *sql.DB) ([]string, error) {
var rs []string
err := SelectRows(dbConn,
"SELECT profile_name FROM profile_lst ORDER BY 1",
func(rows *sql.Rows) error {
var r string
if err := rows.Scan(&r); err != nil {
return err
}
rs = append(rs, r)
return nil
})
if err != nil {
return nil, err
}
return rs, nil
}
// GetProfile return profile_option table rows as (key, value) map.
//
// Profile is a named group of (key, value) options, similar to ini-file.
// Default model options has profile_name = model_name.
func GetProfile(dbConn *sql.DB, name string) (*ProfileMeta, error) {
meta := ProfileMeta{Name: name}
kv, err := getOpts(dbConn,
"SELECT option_key, option_value FROM profile_option WHERE profile_name = "+ToQuoted(name))
if err != nil {
return nil, err
}
meta.Opts = kv
return &meta, nil
}
// GetRunOptions return run_option table rows as (key, value) map.
func GetRunOptions(dbConn *sql.DB, runId int) (map[string]string, error) {
return getOpts(dbConn,
"SELECT option_key, option_value FROM run_option WHERE run_id = "+strconv.Itoa(runId))
}
// getOpts return option table (profile_option or run_option) rows as (key, value) map.
func getOpts(dbConn *sql.DB, query string) (map[string]string, error) {
kv := make(map[string]string)
err := SelectRows(dbConn, query,
func(rows *sql.Rows) error {
var key, val string
if err := rows.Scan(&key, &val); err != nil {
return err
}
kv[key] = val
return nil
})
if err != nil {
return nil, err
}
return kv, nil
}
// GetModelOptions return model run_option table rows as map of maps: map(run_id, map(key, value)).
func GetModelRunOptions(dbConn *sql.DB, modelId int) (map[int]map[string]string, error) {
return getRunOpts(dbConn,
"SELECT"+
" M.run_id, M.option_key, M.option_value"+
" FROM run_option M"+
" INNER JOIN run_lst H ON (H.run_id = M.run_id)"+
" WHERE H.model_id = "+strconv.Itoa(modelId)+
" ORDER BY 1, 2")
}
// getRunOpts return run_option rows as map of maps: map(run_id, map(key, value)).
func getRunOpts(dbConn *sql.DB, query string) (map[int]map[string]string, error) {
rkv := make(map[int]map[string]string)
err := SelectRows(dbConn, query,
func(rows *sql.Rows) error {
var runId int
var key, val string
if err := rows.Scan(&runId, &key, &val); err != nil {
return err
}
if _, ok := rkv[runId]; !ok {
rkv[runId] = make(map[string]string)
}
rkv[runId][key] = val
return nil
})
if err != nil {
return nil, err
}
return rkv, nil
}