-
Notifications
You must be signed in to change notification settings - Fork 0
/
xq_inf_query_binder.go
151 lines (139 loc) · 3.35 KB
/
xq_inf_query_binder.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
package xqi
import (
"reflect"
)
type TQueryColDef struct {
// 字段名称
ColName string
// 扫描数据类型
ColType FieldDataType
}
// 查询字段信息
type TQueryColInfo struct {
TQueryColDef
// 字段序号
ColIndex int
// 数据库定义类型
DbType string
}
type TQueryColValue struct {
*TQueryColInfo
// 字段值
ColValue interface{}
}
type QueryColValue = *TQueryColValue
// 数据访问方法
type QueryBinderVisit = func(row int, values ...interface{}) (interface{}, bool)
type DbQueryBinder interface {
SetOptions(options map[string]interface{}) DbQueryBinder
// 使用定义参数创建新实例
NewInstance(options ...map[string]interface{}) DbQueryBinder
// 开始绑定,返回false时结束绑定
StartBuild(colInfos ...*TQueryColInfo) bool
// 开始创建行,返回false忽略该行
// rowIndex: 行序号
// colCount: 总列数
StartBuildRow(rowIndex int, colCount int) bool
// 创建行
// row:当前行号
// colInfos行中的列信息
// 返回:result行数据结构,exit: 返回true结束绑定
BuildRow(row int, colInfos *[]QueryColValue) (result interface{}, exit bool)
// 行创建完成
// rowData为BuildRow所创建的行数据
EndBuildRow(rowData interface{})
// 绑定完成,返回所有行数据
EndBuild() interface{}
//// 字段映射名
// @param qryName 查询字段名
// @return 返回输出需要的字段名
FieldName(colIndex int, qryName string) string
// 字段数据转换
// @param qryName 字段名
// @param val 原数据
// @return 返回转换后的数据
FieldConvert(colIndex int, qryName string, val interface{}) interface{}
}
func NewQueryColDefByType(name string, gType reflect.Type, dbType ...string) *TQueryColDef {
s := ""
if len(dbType) > 0 {
s = dbType[0]
}
ft := QueryDataType2FieldType(gType, s)
return NewQueryColDef(name, ft)
}
func NewQueryColDef(name string, fieldType FieldDataType) *TQueryColDef {
return &TQueryColDef{
ColName: name,
ColType: fieldType,
}
}
func QueryDataType2FieldType(gType reflect.Type, dbType string) FieldDataType {
for gType.Kind() == reflect.Ptr {
gType = gType.Elem()
}
var fieldType = FDTUnknown
switch gType.Kind() {
case reflect.String:
fieldType = FDTString
break
case reflect.Int:
fieldType = FDTInt
break
case reflect.Int8:
fieldType = FDTInt8
break
case reflect.Int16:
fieldType = FDTInt16
break
case reflect.Int32:
fieldType = FDTInt32
break
case reflect.Int64:
fieldType = FDTInt64
break
case reflect.Uint:
fieldType = FDTUint
break
case reflect.Uint8:
fieldType = FDTUint8
break
case reflect.Uint16:
fieldType = FDTUint16
break
case reflect.Uint32:
fieldType = FDTUint32
break
case reflect.Uint64:
fieldType = FDTInt64
break
case reflect.Float32:
fieldType = FDTFloat
break
case reflect.Float64:
fieldType = FDTDouble
break
case reflect.Bool:
fieldType = FDTBool
break
default:
if dbType == "DATE" || dbType == "TIME" || dbType == "DATETIME" ||
dbType == "TIMESTAMP" {
fieldType = FDTDatetime
} else if dbType == "BINARY" {
fieldType = FDTBinary
}
}
return fieldType
}
func NewQueryColInfo(index int, name string, gType reflect.Type, dbType string) *TQueryColInfo {
fieldType := QueryDataType2FieldType(gType, dbType)
return &TQueryColInfo{
ColIndex: index,
DbType: dbType,
TQueryColDef: TQueryColDef{
ColName: name,
ColType: fieldType,
},
}
}