-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdo_db_query.go
93 lines (71 loc) · 1.68 KB
/
do_db_query.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
package main
import (
"database/sql"
"encoding/base64"
"errors"
"reflect"
)
type query_response map[string]interface{}
func do_db_select_query(db *sql.DB, query_string string, base64_columns []string) ([]query_response, error) {
rows, err := db.Query(query_string)
if err != nil {
return nil, err
}
defer rows.Close()
query_results := []query_response{}
for rows.Next() {
r := make(query_response)
cols, _ := rows.Columns()
values := make([]interface{}, len(cols))
valuePointers := make([]interface{}, len(cols))
for i := range cols {
valuePointers[i] = &values[i]
}
if err := rows.Scan(valuePointers...); err != nil {
return nil, err
}
for i, col := range cols {
var query_value interface{}
val := values[i]
// fmt.Printf("%T\n", val)
// fmt.Println(reflect.TypeOf(val))
if contains_in_slice(base64_columns, col) {
slice_type, ok := val.([]byte)
if reflect.TypeOf(val) == nil {
val = nil
} else {
if ok {
val = base64.StdEncoding.EncodeToString(slice_type)
}
if !ok {
if reflect.TypeOf(val).Kind() == reflect.String {
slice_type := []byte(val.(string))
val = base64.StdEncoding.EncodeToString(slice_type)
}
}
}
}
b, ok := val.([]byte)
if ok {
query_value = string(b)
} else {
query_value = val
}
r[col] = query_value
}
query_results = append(query_results, r)
}
if len(query_results) < 1 {
err = errors.New("empty response")
return nil, err
}
// defer db.Close()
return query_results, nil
}
func do_db_query_exec(db *sql.DB, query_string string) error {
_, err := db.Query(query_string)
if err != nil {
return err
}
return nil
}