Skip to content

Commit 14ae027

Browse files
author
Philio
committed
added int/float and limited string conversion to row data
1 parent 99465bc commit 14ae027

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

handler.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package mysql
77

88
import (
99
"os"
10+
"strconv"
1011
)
1112

1213
// OK packet handler
@@ -121,15 +122,47 @@ func handleRow(p *packetRowData, c *Client, r *Result) (err os.Error) {
121122
if r == nil || r.mode == RESULT_FREE {
122123
return
123124
}
125+
// Basic type conversion
126+
var row []interface{}
127+
var field interface{}
128+
// Iterate fields to get types
129+
for i, f := range r.fields {
130+
switch f.Type {
131+
// Signed/unsigned ints
132+
case FIELD_TYPE_TINY, FIELD_TYPE_SHORT, FIELD_TYPE_YEAR, FIELD_TYPE_INT24, FIELD_TYPE_LONG, FIELD_TYPE_LONGLONG:
133+
if f.Flags&FLAG_UNSIGNED > 0 {
134+
field, err = strconv.Atoui64(string(p.row[i].([]byte)))
135+
} else {
136+
field, err = strconv.Atoi64(string(p.row[i].([]byte)))
137+
}
138+
if err != nil {
139+
return
140+
}
141+
// Floats and doubles
142+
case FIELD_TYPE_FLOAT, FIELD_TYPE_DOUBLE:
143+
field, err = strconv.Atof64(string(p.row[i].([]byte)))
144+
if err != nil {
145+
return
146+
}
147+
// Strings
148+
case FIELD_TYPE_DECIMAL, FIELD_TYPE_NEWDECIMAL, FIELD_TYPE_VARCHAR:
149+
field = string(p.row[i].([]byte))
150+
// Anything else
151+
default:
152+
field = p.row[i]
153+
}
154+
// Add to row
155+
row = append(row, field)
156+
}
124157
// Stored result
125158
if r.mode == RESULT_STORED {
126159
// Cast and append the row
127-
r.rows = append(r.rows, Row(p.row))
160+
r.rows = append(r.rows, Row(row))
128161
}
129162
// Used result
130163
if r.mode == RESULT_USED {
131164
// Only save 1 row, overwrite previous
132-
r.rows = []Row{Row(p.row)}
165+
r.rows = []Row{Row(row)}
133166
}
134167
return
135168
}

0 commit comments

Comments
 (0)