-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
209 lines (184 loc) · 3.89 KB
/
utils.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
206
207
208
209
package sqlpipe
import (
"bytes"
"errors"
"html/template"
"net/url"
"strings"
"database/sql"
_ "github.com/ClickHouse/clickhouse-go"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
var ErrDupMapKey = errors.New("duplicate map key error")
func StrListHas(l []string, v string) bool {
for _, d := range l {
if d == v {
return true
}
}
return false
}
func Panic(err error) {
if err != nil {
panic(err)
}
}
func Open(dsn string) (*sqlx.DB, error) {
u, err := url.Parse(dsn)
if err != nil {
return nil, err
}
if u.Scheme == "postgres" || u.Scheme == "postgresql" {
db, err := sqlx.Connect("postgres", dsn)
if err != nil {
return nil, err
}
db = db.Unsafe()
return db, nil
}
if u.Scheme == "mysql" {
dsn = strings.TrimPrefix(dsn, "mysql://")
db, err := sqlx.Connect("mysql", dsn)
if err != nil {
return nil, err
}
db = db.Unsafe()
return db, nil
}
if u.Scheme == "clickhouse" {
q := u.Query()
if database := strings.Trim(u.Path, "/"); database != "" {
q.Add("database", database)
}
dsn = "tcp://" + u.Host + "?" + q.Encode()
db, err := sqlx.Connect("clickhouse", dsn)
if err != nil {
return nil, err
}
db = db.Unsafe()
return db, nil
}
return nil, errors.New("unsurpport Scheme")
}
// Rows函数解析sql.Rows,返回列名、map[string]string的切片,其中key为列名,value为对应的值
func Rows(sqlrows *sql.Rows) ([]string, []map[string]string, error) {
defer sqlrows.Close()
cols, err := sqlrows.Columns()
if err != nil {
return nil, nil, err
}
n := len(cols)
row, err := getRows(sqlrows, n)
if err != nil {
return nil, nil, err
}
ret := make([]map[string]string, 0, len(row))
for _, row := range row {
m := make(map[string]string, n)
for i, s := range row {
m[cols[i]] = s
}
ret = append(ret, m)
}
return cols, ret, nil
}
func GetRows(rows *sql.Rows) ([][]string, error) {
defer rows.Close()
columns, err := rows.Columns()
if err != nil {
return nil, err
}
n := len(columns)
return getRows(rows, n)
}
func GetRowsInterface(rows *sql.Rows) ([][]interface{}, error) {
defer rows.Close()
columns, err := rows.Columns()
if err != nil {
return nil, err
}
n := len(columns)
return getRowsInterface(rows, n)
}
func getRows(rows *sql.Rows, n int) ([][]string, error) {
var l [][]string
for rows.Next() {
r, err := scanRow(rows, n)
if err != nil {
return l, err
}
l = append(l, r)
}
return l, nil
}
func getRowsInterface(rows *sql.Rows, n int) ([][]interface{}, error) {
var l [][]interface{}
for rows.Next() {
r, err := scanRowInterface(rows, n)
if err != nil {
return l, err
}
l = append(l, r)
}
return l, nil
}
func scanRow(rows *sql.Rows, n int) ([]string, error) {
row := make([]string, n)
ref := make([]interface{}, n)
for i := 0; i < n; i++ {
ref[i] = &row[i]
}
err := rows.Scan(ref...)
return row, err
}
func scanRowInterface(rows *sql.Rows, n int) ([]interface{}, error) {
row := make([]interface{}, n)
ref := make([]interface{}, n)
for i := 0; i < n; i++ {
ref[i] = &row[i]
}
err := rows.Scan(ref...)
return row, err
}
// 仅取一列
func getOneColFromRows(col string, rows []map[string]string) []interface{} {
rs := []interface{}{}
for _, row := range rows {
rs = append(rs, row[col])
}
return rs
}
func genPlaceholders(n int) string {
buf := new(bytes.Buffer)
for i := 0; i < n; i++ {
if i != 0 {
buf.WriteString(",")
}
buf.WriteString("?")
}
return buf.String()
}
func Tpl(s string, d interface{}) (string, error) {
t, err := template.New("").Parse(s)
if err != nil {
return "", err
}
buf := new(bytes.Buffer)
err = t.Execute(buf, d)
return buf.String(), err
}
func MergeMapWithNoDupKey(ms ...map[string]string) (res map[string]string, err error) {
res = map[string]string{}
for _, m := range ms {
for k, v := range m {
_, ok := res[k]
if ok {
err = ErrDupMapKey
return
}
res[k] = v
}
}
return
}