/
paramvalue.go
161 lines (137 loc) · 3.08 KB
/
paramvalue.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
package native
import (
"github.com/ziutek/mymysql/mysql"
"math"
"reflect"
"time"
)
type paramValue struct {
typ uint16
addr reflect.Value
raw bool
length int // >=0 - length of value, <0 - unknown length
}
func (val *paramValue) Len() int {
if !val.addr.IsValid() {
// Invalid Value was binded
return 0
}
// val.addr always points to the pointer - lets dereference it
v := val.addr.Elem()
if v.IsNil() {
// Binded Ptr Value is nil
return 0
}
v = v.Elem()
if val.length >= 0 {
return val.length
}
switch val.typ {
case MYSQL_TYPE_STRING:
return lenStr(v.String())
case MYSQL_TYPE_DATE:
return lenDate(v.Interface().(mysql.Date))
case MYSQL_TYPE_TIMESTAMP:
return lenTime(v.Interface().(mysql.Timestamp).Time)
case MYSQL_TYPE_DATETIME:
return lenTime(v.Interface().(time.Time))
case MYSQL_TYPE_TIME:
return lenDuration(v.Interface().(time.Duration))
case MYSQL_TYPE_TINY: // val.length < 0 so this is bool
return 1
}
// MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_BLOB and type of Raw value
return lenBin(v.Bytes())
}
func (pw *pktWriter) writeValue(val *paramValue) {
if !val.addr.IsValid() {
// Invalid Value was binded
return
}
// val.addr always points to the pointer - lets dereference it
v := val.addr.Elem()
if v.IsNil() {
// Binded Ptr Value is nil
return
}
v = v.Elem()
if val.raw || val.typ == MYSQL_TYPE_VAR_STRING ||
val.typ == MYSQL_TYPE_BLOB {
pw.writeBin(v.Bytes())
return
}
// We don't need unsigned bit to check type
unsign := (val.typ & MYSQL_UNSIGNED_MASK) != 0
switch val.typ & ^MYSQL_UNSIGNED_MASK {
case MYSQL_TYPE_NULL:
// Don't write null values
case MYSQL_TYPE_STRING:
pw.writeBin([]byte(v.String()))
case MYSQL_TYPE_LONG:
i := v.Interface()
if unsign {
l, ok := i.(uint32)
if !ok {
l = uint32(i.(uint))
}
pw.writeU32(l)
} else {
l, ok := i.(int32)
if !ok {
l = int32(i.(int))
}
pw.writeU32(uint32(l))
}
case MYSQL_TYPE_FLOAT:
pw.writeU32(math.Float32bits(v.Interface().(float32)))
case MYSQL_TYPE_SHORT:
if unsign {
pw.writeU16(v.Interface().(uint16))
} else {
pw.writeU16(uint16(v.Interface().(int16)))
}
case MYSQL_TYPE_TINY:
if val.length == -1 {
// Translate bool value to MySQL tiny
if v.Bool() {
pw.writeByte(1)
} else {
pw.writeByte(0)
}
} else {
if unsign {
pw.writeByte(v.Interface().(uint8))
} else {
pw.writeByte(uint8(v.Interface().(int8)))
}
}
case MYSQL_TYPE_LONGLONG:
i := v.Interface()
if unsign {
l, ok := i.(uint64)
if !ok {
l = uint64(i.(uint))
}
pw.writeU64(l)
} else {
l, ok := i.(int64)
if !ok {
l = int64(i.(int))
}
pw.writeU64(uint64(l))
}
case MYSQL_TYPE_DOUBLE:
pw.writeU64(math.Float64bits(v.Interface().(float64)))
case MYSQL_TYPE_DATE:
pw.writeDate(v.Interface().(mysql.Date))
case MYSQL_TYPE_TIMESTAMP:
pw.writeTime(v.Interface().(mysql.Timestamp).Time)
case MYSQL_TYPE_DATETIME:
pw.writeTime(v.Interface().(time.Time))
case MYSQL_TYPE_TIME:
pw.writeDuration(v.Interface().(time.Duration))
default:
panic(mysql.ErrBindUnkType)
}
return
}