-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataTable.go
165 lines (153 loc) · 3.77 KB
/
dataTable.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
package dataSet
import (
"database/sql"
"encoding/xml"
"fmt"
"reflect"
"strconv"
"time"
)
type DataTable struct {
Columns []*ColumnType `json:"columns" xml:"columns"` // 列
Rows []DataRow `json:"rows" xml:"rows"` // 行数据
}
// 从sql.Rows生成DataTable
func newDataTableFromRows(sqlRows *sql.Rows) (dt *DataTable, err error) {
cols, err := newColumnsFromRows(sqlRows)
if err != nil {
return
}
rows, err := newRowsDataFromRows(sqlRows, cols)
if err != nil {
return
}
dt = new(DataTable)
dt.Columns = cols
dt.Rows = rows
return
}
// 从sql.Rows取出字段信息
func newColumnsFromRows(sqlRows *sql.Rows) (cols []*ColumnType, err error) {
cts, err := sqlRows.ColumnTypes()
if err != nil {
return
}
cols = make([]*ColumnType, 0, len(cts))
for index, value := range cts {
tempCol := new(ColumnType)
tempCol.ColumnType = value
tempCol.Index = uint16(index)
cols = append(cols, tempCol)
}
return
}
// 从sql.Rows取出所有行数据
func newRowsDataFromRows(sqlRows *sql.Rows, cols []*ColumnType) (rows []DataRow, err error) {
colCount := len(cols) // 多少字段
scanArgs := make([]interface{}, colCount) // 用于扫描每行数据时作为字段参数
values := make([]interface{}, colCount) // 用于扫描每行数据时临时存储 每个字段的值
for i := range values {
scanArgs[i] = &values[i] // 把字段参数和值指针关联起来
}
for sqlRows.Next() {
err = sqlRows.Scan(scanArgs...)
if err != nil {
return
}
row := make(DataRow, colCount) // 新建空行数据
for index, value := range values { // 循环把取到的值都存到新的空行数据里
//row[index] = value
switch value.(type) {
case []byte:
s := string(value.([]byte))
i2, err := strconv.Atoi(s)
if err == nil {
row[index] = &Data{
column: cols[index],
value: i2,
}
continue
}
f, err := strconv.ParseFloat(s, 64)
if err == nil {
row[index] = &Data{
column: cols[index],
value: f,
}
continue
}
row[index] = &Data{
column: cols[index],
value: value,
}
case time.Time:
row[index] = &Data{
column: cols[index],
value: DateTime{value.(time.Time)},
}
default:
row[index] = &Data{
column: cols[index],
value: value,
}
}
}
rows = append(rows, row)
}
return
}
// MarshalXML 将DataTable转为xml
func (dt DataTable) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if err := e.EncodeToken(start); err != nil {
return err
}
// 序列化列
types := reflect.TypeOf(dt)
colsStart := xml.StartElement{
Name: xml.Name{
Space: "",
Local: types.Field(0).Tag.Get("xml"),
},
Attr: []xml.Attr{},
}
if err := e.EncodeToken(colsStart); err != nil {
return err
}
if err := e.Encode(dt.Columns); err != nil {
return err
}
e.EncodeToken(xml.EndElement{Name: colsStart.Name})
// 序列化行数据
rowsStart := xml.StartElement{
Name: xml.Name{
Space: "",
Local: types.Field(1).Tag.Get("xml"),
},
Attr: []xml.Attr{},
}
if err := e.EncodeToken(rowsStart); err != nil {
return err
}
if err := e.Encode(dt.Rows); err != nil {
return err
}
e.EncodeToken(xml.EndElement{Name: rowsStart.Name})
return e.EncodeToken(xml.EndElement{Name: start.Name})
}
func (dt DataTable) String() string {
var reStr string
reStr = "Columns:"
for _, col := range dt.Columns {
reStr = fmt.Sprintf("%v\n\t%v", reStr, col.String())
}
reStr = fmt.Sprintf("%v\nRows:", reStr)
for rowId, row := range dt.Rows {
reStr = fmt.Sprintf("%v\n[%v]", reStr, rowId)
for colIndex, data := range row {
col := dt.Columns[colIndex].toMarshal() // 对应的列
reStr = fmt.Sprintf("%v\t%v: ", reStr, col.Name)
reStr = fmt.Sprintf("%v%v", reStr, data.value)
}
}
return reStr
}