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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
|
package native
import (
"errors"
"github.com/ziutek/mymysql/mysql"
"log"
"math"
"strconv"
)
type Result struct {
my *Conn
status_only bool // true if result doesn't contain result set
binary bool // Binary result expected
field_count int
fields []*mysql.Field // Fields table
fc_map map[string]int // Maps field name to column number
message []byte
affected_rows uint64
// Primary key value (useful for AUTO_INCREMENT primary keys)
insert_id uint64
// Number of warinigs during command execution
// You can use the SHOW WARNINGS query for details.
warning_count int
// MySQL server status immediately after the query execution
status mysql.ConnStatus
// Seted by GetRow if it returns nil row
eor_returned bool
}
// Returns true if this is status result that includes no result set
func (res *Result) StatusOnly() bool {
return res.status_only
}
// Returns a table containing descriptions of the columns
func (res *Result) Fields() []*mysql.Field {
return res.fields
}
// Returns index for given name or -1 if field of that name doesn't exist
func (res *Result) Map(field_name string) int {
if fi, ok := res.fc_map[field_name]; ok {
return fi
}
return -1
}
func (res *Result) Message() string {
return string(res.message)
}
func (res *Result) AffectedRows() uint64 {
return res.affected_rows
}
func (res *Result) InsertId() uint64 {
return res.insert_id
}
func (res *Result) WarnCount() int {
return res.warning_count
}
func (res *Result) MakeRow() mysql.Row {
return make(mysql.Row, res.field_count)
}
func (my *Conn) getResult(res *Result, row mysql.Row) *Result {
loop:
pr := my.newPktReader() // New reader for next packet
pkt0 := pr.readByte()
if pkt0 == 255 {
// Error packet
my.getErrorPacket(pr)
}
if res == nil {
switch {
case pkt0 == 0:
// OK packet
return my.getOkPacket(pr)
case pkt0 > 0 && pkt0 < 251:
// Result set header packet
res = my.getResSetHeadPacket(pr)
// Read next packet
goto loop
case pkt0 == 251:
// Load infile response
// Handle response
goto loop
case pkt0 == 254:
// EOF packet (without body)
return nil
}
} else {
switch {
case pkt0 == 254:
// EOF packet
res.warning_count, res.status = my.getEofPacket(pr)
my.status = res.status
return res
case pkt0 > 0 && pkt0 < 251 && res.field_count < len(res.fields):
// Field packet
field := my.getFieldPacket(pr)
res.fields[res.field_count] = field
res.fc_map[field.Name] = res.field_count
// Increment field count
res.field_count++
// Read next packet
goto loop
case pkt0 < 254 && res.field_count == len(res.fields):
// Row Data Packet
if len(row) != res.field_count {
panic(mysql.ErrRowLength)
}
if res.binary {
my.getBinRowPacket(pr, res, row)
} else {
my.getTextRowPacket(pr, res, row)
}
return nil
}
}
panic(mysql.ErrUnkResultPkt)
}
func (my *Conn) getOkPacket(pr *pktReader) (res *Result) {
if my.Debug {
log.Printf("[%2d ->] OK packet:", my.seq-1)
}
res = new(Result)
res.status_only = true
res.my = my
// First byte was readed by getResult
res.affected_rows = pr.readLCB()
res.insert_id = pr.readLCB()
res.status = mysql.ConnStatus(pr.readU16())
my.status = res.status
res.warning_count = int(pr.readU16())
res.message = pr.readAll()
pr.checkEof()
if my.Debug {
log.Printf(tab8s+"AffectedRows=%d InsertId=0x%x Status=0x%x "+
"WarningCount=%d Message=\"%s\"", res.affected_rows, res.insert_id,
res.status, res.warning_count, res.message,
)
}
return
}
func (my *Conn) getErrorPacket(pr *pktReader) {
if my.Debug {
log.Printf("[%2d ->] Error packet:", my.seq-1)
}
var err mysql.Error
err.Code = pr.readU16()
if pr.readByte() != '#' {
panic(mysql.ErrPkt)
}
pr.skipN(5)
err.Msg = pr.readAll()
pr.checkEof()
if my.Debug {
log.Printf(tab8s+"code=0x%x msg=\"%s\"", err.Code, err.Msg)
}
panic(&err)
}
func (my *Conn) getEofPacket(pr *pktReader) (warn_count int, status mysql.ConnStatus) {
if my.Debug {
if pr.eof() {
log.Printf("[%2d ->] EOF packet without body", my.seq-1)
} else {
log.Printf("[%2d ->] EOF packet:", my.seq-1)
}
}
if pr.eof() {
return
}
warn_count = int(pr.readU16())
if pr.eof() {
return
}
status = mysql.ConnStatus(pr.readU16())
pr.checkEof()
if my.Debug {
log.Printf(tab8s+"WarningCount=%d Status=0x%x", warn_count, status)
}
return
}
func (my *Conn) getResSetHeadPacket(pr *pktReader) (res *Result) {
if my.Debug {
log.Printf("[%2d ->] Result set header packet:", my.seq-1)
}
pr.unreadByte()
field_count := int(pr.readLCB())
pr.checkEof()
res = &Result{
my: my,
fields: make([]*mysql.Field, field_count),
fc_map: make(map[string]int),
}
if my.Debug {
log.Printf(tab8s+"FieldCount=%d", field_count)
}
return
}
func (my *Conn) getFieldPacket(pr *pktReader) (field *mysql.Field) {
if my.Debug {
log.Printf("[%2d ->] Field packet:", my.seq-1)
}
pr.unreadByte()
field = new(mysql.Field)
if my.fullFieldInfo {
field.Catalog = string(pr.readBin())
field.Db = string(pr.readBin())
field.Table = string(pr.readBin())
field.OrgTable = string(pr.readBin())
} else {
pr.skipBin()
pr.skipBin()
pr.skipBin()
pr.skipBin()
}
field.Name = string(pr.readBin())
if my.fullFieldInfo {
field.OrgName = string(pr.readBin())
} else {
pr.skipBin()
}
pr.skipN(1 + 2)
//field.Charset= pr.readU16()
field.DispLen = pr.readU32()
field.Type = pr.readByte()
field.Flags = pr.readU16()
field.Scale = pr.readByte()
pr.skipN(2)
pr.checkEof()
if my.Debug {
log.Printf(tab8s+"Name=\"%s\" Type=0x%x", field.Name, field.Type)
}
return
}
func (my *Conn) getTextRowPacket(pr *pktReader, res *Result, row mysql.Row) {
if my.Debug {
log.Printf("[%2d ->] Text row data packet", my.seq-1)
}
pr.unreadByte()
for ii := 0; ii < res.field_count; ii++ {
bin, null := pr.readNullBin()
if null {
row[ii] = nil
} else {
row[ii] = bin
}
}
pr.checkEof()
}
func (my *Conn) getBinRowPacket(pr *pktReader, res *Result, row mysql.Row) {
if my.Debug {
log.Printf("[%2d ->] Binary row data packet", my.seq-1)
}
// First byte was readed by getResult
null_bitmap := make([]byte, (res.field_count+7+2)>>3)
pr.readFull(null_bitmap)
for ii, field := range res.fields {
null_byte := (ii + 2) >> 3
null_mask := byte(1) << uint(2+ii-(null_byte<<3))
if null_bitmap[null_byte]&null_mask != 0 {
// Null field
row[ii] = nil
continue
}
unsigned := (field.Flags & _FLAG_UNSIGNED) != 0
if my.narrowTypeSet {
row[ii] = readValueNarrow(pr, field.Type, unsigned)
} else {
row[ii] = readValue(pr, field.Type, unsigned)
}
}
}
func readValue(pr *pktReader, typ byte, unsigned bool) interface{} {
switch typ {
case MYSQL_TYPE_STRING, MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VARCHAR,
MYSQL_TYPE_BIT, MYSQL_TYPE_BLOB, MYSQL_TYPE_TINY_BLOB,
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_SET,
MYSQL_TYPE_ENUM, MYSQL_TYPE_GEOMETRY:
return pr.readBin()
case MYSQL_TYPE_TINY:
if unsigned {
return pr.readByte()
} else {
return int8(pr.readByte())
}
case MYSQL_TYPE_SHORT, MYSQL_TYPE_YEAR:
if unsigned {
return pr.readU16()
} else {
return int16(pr.readU16())
}
case MYSQL_TYPE_LONG, MYSQL_TYPE_INT24:
if unsigned {
return pr.readU32()
} else {
return int32(pr.readU32())
}
case MYSQL_TYPE_LONGLONG:
if unsigned {
return pr.readU64()
} else {
return int64(pr.readU64())
}
case MYSQL_TYPE_FLOAT:
return math.Float32frombits(pr.readU32())
case MYSQL_TYPE_DOUBLE:
return math.Float64frombits(pr.readU64())
case MYSQL_TYPE_DECIMAL, MYSQL_TYPE_NEWDECIMAL:
dec := string(pr.readBin())
r, err := strconv.ParseFloat(dec, 64)
if err != nil {
panic(errors.New("MySQL server returned wrong decimal value: " + dec))
}
return r
case MYSQL_TYPE_DATE, MYSQL_TYPE_NEWDATE:
return pr.readDate()
case MYSQL_TYPE_DATETIME, MYSQL_TYPE_TIMESTAMP:
return pr.readTime()
case MYSQL_TYPE_TIME:
return pr.readDuration()
}
panic(mysql.ErrUnkMySQLType)
}
func readValueNarrow(pr *pktReader, typ byte, unsigned bool) interface{} {
switch typ {
case MYSQL_TYPE_STRING, MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VARCHAR,
MYSQL_TYPE_BIT, MYSQL_TYPE_BLOB, MYSQL_TYPE_TINY_BLOB,
MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_SET,
MYSQL_TYPE_ENUM, MYSQL_TYPE_GEOMETRY:
return pr.readBin()
case MYSQL_TYPE_TINY:
if unsigned {
return int64(pr.readByte())
}
return int64(int8(pr.readByte()))
case MYSQL_TYPE_SHORT, MYSQL_TYPE_YEAR:
if unsigned {
return int64(pr.readU16())
}
return int64(int16(pr.readU16()))
case MYSQL_TYPE_LONG, MYSQL_TYPE_INT24:
if unsigned {
return int64(pr.readU32())
}
return int64(int32(pr.readU32()))
case MYSQL_TYPE_LONGLONG:
v := pr.readU64()
if unsigned && v > math.MaxInt64 {
panic(errors.New("Value to large for int64 type"))
}
return int64(v)
case MYSQL_TYPE_FLOAT:
return float64(math.Float32frombits(pr.readU32()))
case MYSQL_TYPE_DOUBLE:
return math.Float64frombits(pr.readU64())
case MYSQL_TYPE_DECIMAL, MYSQL_TYPE_NEWDECIMAL:
dec := string(pr.readBin())
r, err := strconv.ParseFloat(dec, 64)
if err != nil {
panic("MySQL server returned wrong decimal value: " + dec)
}
return r
case MYSQL_TYPE_DATETIME, MYSQL_TYPE_TIMESTAMP, MYSQL_TYPE_DATE, MYSQL_TYPE_NEWDATE:
return pr.readTime()
case MYSQL_TYPE_TIME:
return int64(pr.readDuration())
}
panic(mysql.ErrUnkMySQLType)
}
|