-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
205 lines (171 loc) · 3.88 KB
/
table.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package argp
import (
"fmt"
"os"
"reflect"
"strings"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"github.com/pelletier/go-toml"
)
type TableSource interface {
Has(string) bool
Get(string) string
Close() error
}
// Table is an option that loads key-value map from a source (such as mysql).
type Table struct {
TableSource
Values []string
}
func (table *Table) Help() (string, string) {
return strings.Join(table.Values, " "), "type:table"
}
func (table *Table) Scan(name string, s []string) (int, error) {
if len(s) == 0 {
return 0, fmt.Errorf("missing value")
}
vals, _, split := truncEnd(s)
if len(vals) == 0 || split {
return 0, fmt.Errorf("invalid value")
}
colon := strings.IndexByte(vals[0], ':')
if colon == -1 || (vals[0][0] < 'a' || 'z' < vals[0][0]) && (vals[0][0] < 'A' || 'Z' < vals[0][0]) {
return 0, fmt.Errorf("invalid value, expected type:table where type is e.g. mysql")
}
table.Values = vals
var err error
typ := vals[0][:colon]
vals[0] = vals[0][colon+1:]
switch typ {
case "static":
table.TableSource, err = newStaticTable(vals)
case "inline":
table.TableSource, err = newInlineTable(vals)
case "sqlite":
table.TableSource, err = newSQLiteTable(vals)
case "mysql":
table.TableSource, err = newMySQLTable(vals)
default:
return 0, fmt.Errorf("unknown table type: %s", typ)
}
if err != nil {
return 0, err
}
return len(vals), nil
}
type staticTable struct {
value string
}
func newStaticTable(s []string) (TableSource, error) {
return &staticTable{strings.Join(s, " ")}, nil
}
func (t *staticTable) Has(key string) bool {
return true
}
func (t *staticTable) Get(key string) string {
return t.value
}
func (t *staticTable) Close() error {
return nil
}
type inlineTable struct {
table map[string]string
}
func newInlineTable(s []string) (TableSource, error) {
table := map[string]string{}
if 0 < len(s) {
if _, err := scanValue(reflect.ValueOf(&table).Elem(), s); err != nil {
return nil, err
}
}
return &inlineTable{table}, nil
}
func (t *inlineTable) Has(key string) bool {
_, ok := t.table[key]
return ok
}
func (t *inlineTable) Get(key string) string {
return t.table[key]
}
func (t *inlineTable) Close() error {
return nil
}
type sqlTable struct {
db *sqlx.DB
stmt *sqlx.Stmt
}
func (t *sqlTable) Has(key string) bool {
return t.stmt.QueryRow(key).Err() == nil
}
func (t *sqlTable) Get(key string) string {
var val string // TODO: does this work for ints? Or should we use interface{}?
if err := t.stmt.QueryRow(key).Scan(&val); err != nil {
return ""
}
return val
}
func (t *sqlTable) Close() error {
return t.db.Close()
}
type sqliteTable struct {
Path string // can be :memory:
Query string
}
func newSQLiteTable(s []string) (TableSource, error) {
if len(s) != 1 {
return nil, fmt.Errorf("invalid path")
}
b, err := os.ReadFile(s[0])
if err != nil {
return nil, err
}
t := sqliteTable{}
if err := toml.Unmarshal(b, &t); err != nil {
return nil, err
}
db, err := sqlx.Open("sqlite", t.Path)
if err != nil {
return nil, err
}
stmt, err := db.Preparex(t.Query)
if err != nil {
return nil, err
}
return &sqlTable{db, stmt}, nil
}
type mysqlTable struct {
Host string
User string
Password string
Dbname string
Query string
}
func newMySQLTable(s []string) (TableSource, error) {
if len(s) != 1 {
return nil, fmt.Errorf("invalid path")
}
b, err := os.ReadFile(s[0])
if err != nil {
return nil, err
}
t := mysqlTable{}
if err := toml.Unmarshal(b, &t); err != nil {
return nil, err
}
uri := fmt.Sprintf("%s:%s@%s/%s", t.User, t.Password, t.Host, t.Dbname)
db, err := sqlx.Open("mysql", uri)
if err != nil {
return nil, err
}
db.SetConnMaxLifetime(time.Minute)
db.SetConnMaxIdleTime(time.Minute)
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10)
stmt, err := db.Preparex(t.Query)
if err != nil {
return nil, err
}
return &sqlTable{db, stmt}, nil
}