This repository has been archived by the owner on Jun 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
137 lines (104 loc) · 2.37 KB
/
run.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
package mysqlann
import (
"database/sql"
"strconv"
)
var db *sql.DB
func SetDB(new_db *sql.DB) {
db = new_db
}
func exec(q Query) (sql.Result, error) {
if db == nil {
panic("connection not initialized")
}
/*
stmt, _ := db.Prepare(q.Sql())
defer stmt.Close()
return stmt.Exec()
*/
return db.Exec(q.Sql())
}
func query(q Query) (*sql.Rows, error) {
if db == nil {
panic("connection not initialized")
}
/*
stmt, _ := db.Prepare(q.Sql())
defer stmt.Close()
return stmt.Query()
*/
return db.Query(q.Sql())
}
func readRow(rows *sql.Rows) (row []Anything) {
column_types, _ := rows.ColumnTypes()
row = make([]Anything, len(column_types))
str_data := make([]string, len(column_types))
pointers := make([]interface{}, len(column_types))
for i, _ := range column_types {
pointers[i] = &str_data[i]
}
rows.Scan(pointers...)
for i, columnType := range column_types {
row[i] = strToValue(str_data[i], columnType)
}
return row
}
func readSingleColumnByIndex(rows *sql.Rows, columnIndex int) (value Anything) {
column_types, _ := rows.ColumnTypes()
var column_type *sql.ColumnType
if len(column_types) <= columnIndex {
return nil //no such column
} else {
column_type = column_types[columnIndex]
}
var str_data string
rows.Scan(&str_data)
value = strToValue(str_data, column_type)
return value
}
func readRowMap(rows *sql.Rows) (row map[string]interface{}) {
column_types, _ := rows.ColumnTypes()
row = make(map[string]interface{}, len(column_types))
str_data := make([]string, len(column_types))
pointers := make([]interface{}, len(column_types))
for i, _ := range column_types {
pointers[i] = &str_data[i]
}
rows.Scan(pointers...)
for i, ct := range column_types {
row[ct.Name()] = strToValue(str_data[i], ct)
}
return row
}
func strToValue(str string, ct *sql.ColumnType) (r interface{}) {
switch t := ct.DatabaseTypeName(); t {
case "NULL":
r = nil
case "VARCHAR":
r = str
case "INT":
v, err := strconv.Atoi(str)
if err == nil {
r = v
} else {
r = "[ERROR: " + err.Error() + "]"
}
case "BIGINT":
v, err := strconv.ParseInt(str, 10, 64)
if err == nil {
r = v
} else {
r = "[ERROR: " + err.Error() + "]"
}
case "DECIMAL":
v, err := strconv.ParseFloat(str, 64)
if err == nil {
r = v
} else {
r = "[ERROR: " + err.Error() + "]"
}
default:
r = "[UNKNOWN DATATYPE: " + t + "]"
}
return r
}