func (stmt *mysqlStmt) readPrepareResultPacket() (uint16, error) {
	data, err := stmt.mc.readPacket()
	if err == nil {
		if data[0] != iOK {
			return 0, stmt.mc.handleErrorPacket(data)
		}
		stmt.id = binary.LittleEndian.Uint32(data[1:5])
		columnCount := binary.LittleEndian.Uint16(data[5:7])
		stmt.paramCount = int(binary.LittleEndian.Uint16(data[7:9]))
		if !stmt.mc.strict {
			return columnCount, nil
		} else {
			if len(data) >= 12 && binary.LittleEndian.Uint16(data[10:12]) > 0 {
				return columnCount, stmt.mc.getWarnings()
			}
			return columnCount, nil
		}
	}
	return 0, err
}
****************************************************************************
Prepared Statements                               *
****************************************************************************
Prepare Result Packets
http://dev.mysql.com/doc/internals/en/com-stmt-prepare-response.html
package mysql
import (
	"bytes"
	"crypto/tls"
	"database/sql/driver"
	"encoding/binary"
	"fmt"
	"io"
	"math"
	"time"
)
func (mc *mysqlConn) readPacket() ([]byte, error) {
	var payload []byte
	for {
		data, err := mc.buf.readNext(4)
		if err != nil {
			errLog.Print(err)
			mc.Close()
			return nil, driver.ErrBadConn
		}
		pktLen := int(uint32(data[0]) | uint32(data[1])<<8 | uint32(data[2])<<16)
		if pktLen < 1 {
			errLog.Print(ErrMalformPkt)
			mc.Close()
			return nil, driver.ErrBadConn
		}
		if data[3] != mc.sequence {
			if data[3] > mc.sequence {
				return nil, ErrPktSyncMul
			} else {
				return nil, ErrPktSync
			}
		}
		mc.sequence++
		data, err = mc.buf.readNext(pktLen)
		if err != nil {
			errLog.Print(err)
			mc.Close()
			return nil, driver.ErrBadConn
		}
		isLastPacket := (pktLen < maxPacketSize)
		if isLastPacket && payload == nil {
			return data, nil
		}
		payload = append(payload, data...)
		if isLastPacket {
			return payload, nil
		}
	}
}
func (mc *mysqlConn) writePacket(data []byte) error {
	pktLen := len(data) - 4
	if pktLen > mc.maxPacketAllowed {
		return ErrPktTooLarge
	}
	for {
		var size int
		if pktLen >= maxPacketSize {
			data[0] = 0xff
			data[1] = 0xff
			data[2] = 0xff
			size = maxPacketSize
		} else {
			data[0] = byte(pktLen)
			data[1] = byte(pktLen >> 8)
			data[2] = byte(pktLen >> 16)
			size = pktLen
		}
		data[3] = mc.sequence
		n, err := mc.netConn.Write(data[:4+size])
		if err == nil && n == 4+size {
			mc.sequence++
			if size != maxPacketSize {
				return nil
			}
			pktLen -= size
			data = data[size:]
			continue
		}
		if err == nil {  
			errLog.Print(ErrMalformPkt)
		} else {
			errLog.Print(err)
		}
		return driver.ErrBadConn
	}
}
func (mc *mysqlConn) readInitPacket() ([]byte, error) {
	data, err := mc.readPacket()
	if err != nil {
		return nil, err
	}
	if data[0] == iERR {
		return nil, mc.handleErrorPacket(data)
	}
	if data[0] < minProtocolVersion {
		return nil, fmt.Errorf(
			"Unsupported MySQL Protocol Version %d. Protocol Version %d or higher is required",
			data[0],
			minProtocolVersion,
		)
	}
	pos := 1 + bytes.IndexByte(data[1:], 0x00) + 1 + 4
	cipher := data[pos : pos+8]
	pos += 8 + 1
	mc.flags = clientFlag(binary.LittleEndian.Uint16(data[pos : pos+2]))
	if mc.flags&clientProtocol41 == 0 {
		return nil, ErrOldProtocol
	}
	if mc.flags&clientSSL == 0 && mc.cfg.tls != nil {
		return nil, ErrNoTLS
	}
	pos += 2
	if len(data) > pos {
		pos += 1 + 2 + 2 + 1 + 10
		cipher = append(cipher, data[pos:pos+12]...)
		var b [20]byte
		copy(b[:], cipher)
		return b[:], nil
	}
	var b [8]byte
	copy(b[:], cipher)
	return b[:], nil
}
func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
	clientFlags := clientProtocol41 |
		clientSecureConn |
		clientLongPassword |
		clientTransactions |
		clientLocalFiles |
		clientPluginAuth |
		mc.flags&clientLongFlag
	if mc.cfg.clientFoundRows {
		clientFlags |= clientFoundRows
	}
	if mc.cfg.tls != nil {
		clientFlags |= clientSSL
	}
	scrambleBuff := scramblePassword(cipher, []byte(mc.cfg.passwd))
	pktLen := 4 + 4 + 1 + 23 + len(mc.cfg.user) + 1 + 1 + len(scrambleBuff) + 21 + 1
	if n := len(mc.cfg.dbname); n > 0 {
		clientFlags |= clientConnectWithDB
		pktLen += n + 1
	}
	data := mc.buf.takeSmallBuffer(pktLen + 4)
	if data == nil {
		errLog.Print(ErrBusyBuffer)
		return driver.ErrBadConn
	}
	data[4] = byte(clientFlags)
	data[5] = byte(clientFlags >> 8)
	data[6] = byte(clientFlags >> 16)
	data[7] = byte(clientFlags >> 24)
	data[8] = 0x00
	data[9] = 0x00
	data[10] = 0x00
	data[11] = 0x00
	data[12] = mc.cfg.collation
	if mc.cfg.tls != nil {
		if err := mc.writePacket(data[:(4+4+1+23)+4]); err != nil {
			return err
		}
		tlsConn := tls.Client(mc.netConn, mc.cfg.tls)
		if err := tlsConn.Handshake(); err != nil {
			return err
		}
		mc.netConn = tlsConn
		mc.buf.rd = tlsConn
	}
	pos := 13 + 23
	if len(mc.cfg.user) > 0 {
		pos += copy(data[pos:], mc.cfg.user)
	}
	data[pos] = 0x00
	pos++
	data[pos] = byte(len(scrambleBuff))
	pos += 1 + copy(data[pos+1:], scrambleBuff)
	if len(mc.cfg.dbname) > 0 {
		pos += copy(data[pos:], mc.cfg.dbname)
		data[pos] = 0x00
		pos++
	}
	pos += copy(data[pos:], "mysql_native_password")
	data[pos] = 0x00
	return mc.writePacket(data)
}
func (mc *mysqlConn) writeOldAuthPacket(cipher []byte) error {
	scrambleBuff := scrambleOldPassword(cipher, []byte(mc.cfg.passwd))
	pktLen := len(scrambleBuff) + 1
	data := mc.buf.takeSmallBuffer(4 + pktLen)
	if data == nil {
		errLog.Print(ErrBusyBuffer)
		return driver.ErrBadConn
	}
	copy(data[4:], scrambleBuff)
	data[4+pktLen-1] = 0x00
	return mc.writePacket(data)
}
func (mc *mysqlConn) writeClearAuthPacket() error {
	pktLen := len(mc.cfg.passwd) + 1
	data := mc.buf.takeSmallBuffer(4 + pktLen)
	if data == nil {
		errLog.Print(ErrBusyBuffer)
		return driver.ErrBadConn
	}
	copy(data[4:], mc.cfg.passwd)
	data[4+pktLen-1] = 0x00
	return mc.writePacket(data)
}
func (mc *mysqlConn) writeCommandPacket(command byte) error {
	mc.sequence = 0
	data := mc.buf.takeSmallBuffer(4 + 1)
	if data == nil {
		errLog.Print(ErrBusyBuffer)
		return driver.ErrBadConn
	}
	data[4] = command
	return mc.writePacket(data)
}
func (mc *mysqlConn) writeCommandPacketStr(command byte, arg string) error {
	mc.sequence = 0
	pktLen := 1 + len(arg)
	data := mc.buf.takeBuffer(pktLen + 4)
	if data == nil {
		errLog.Print(ErrBusyBuffer)
		return driver.ErrBadConn
	}
	data[4] = command
	copy(data[5:], arg)
	return mc.writePacket(data)
}
func (mc *mysqlConn) writeCommandPacketUint32(command byte, arg uint32) error {
	mc.sequence = 0
	data := mc.buf.takeSmallBuffer(4 + 1 + 4)
	if data == nil {
		errLog.Print(ErrBusyBuffer)
		return driver.ErrBadConn
	}
	data[4] = command
	data[5] = byte(arg)
	data[6] = byte(arg >> 8)
	data[7] = byte(arg >> 16)
	data[8] = byte(arg >> 24)
	return mc.writePacket(data)
}
func (mc *mysqlConn) readResultOK() error {
	data, err := mc.readPacket()
	if err == nil {
		switch data[0] {
		case iOK:
			return mc.handleOkPacket(data)
		case iEOF:
			if len(data) > 1 {
				plugin := string(data[1:bytes.IndexByte(data, 0x00)])
				if plugin == "mysql_old_password" {
					return ErrOldPassword
				} else if plugin == "mysql_clear_password" {
					return ErrCleartextPassword
				} else {
					return ErrUnknownPlugin
				}
			} else {
				return ErrOldPassword
			}
		default:  
			return mc.handleErrorPacket(data)
		}
	}
	return err
}
func (mc *mysqlConn) readResultSetHeaderPacket() (int, error) {
	data, err := mc.readPacket()
	if err == nil {
		switch data[0] {
		case iOK:
			return 0, mc.handleOkPacket(data)
		case iERR:
			return 0, mc.handleErrorPacket(data)
		case iLocalInFile:
			return 0, mc.handleInFileRequest(string(data[1:]))
		}
		num, _, n := readLengthEncodedInteger(data)
		if n-len(data) == 0 {
			return int(num), nil
		}
		return 0, ErrMalformPkt
	}
	return 0, err
}
func (mc *mysqlConn) handleErrorPacket(data []byte) error {
	if data[0] != iERR {
		return ErrMalformPkt
	}
	errno := binary.LittleEndian.Uint16(data[1:3])
	pos := 3
	if data[3] == 0x23 {
		pos = 9
	}
	return &MySQLError{
		Number:  errno,
		Message: string(data[pos:]),
	}
}
func (mc *mysqlConn) handleOkPacket(data []byte) error {
	var n, m int
	mc.affectedRows, _, n = readLengthEncodedInteger(data[1:])
	mc.insertId, _, m = readLengthEncodedInteger(data[1+n:])
	mc.status = statusFlag(data[1+n+m]) | statusFlag(data[1+n+m+1])<<8
	if !mc.strict {
		return nil
	} else {
		pos := 1 + n + m + 2
		if binary.LittleEndian.Uint16(data[pos:pos+2]) > 0 {
			return mc.getWarnings()
		}
		return nil
	}
}
func (mc *mysqlConn) readColumns(count int) ([]mysqlField, error) {
	columns := make([]mysqlField, count)
	for i := 0; ; i++ {
		data, err := mc.readPacket()
		if err != nil {
			return nil, err
		}
		if data[0] == iEOF && (len(data) == 5 || len(data) == 1) {
			if i == count {
				return columns, nil
			}
			return nil, fmt.Errorf("ColumnsCount mismatch n:%d len:%d", count, len(columns))
		}
		pos, err := skipLengthEncodedString(data)
		if err != nil {
			return nil, err
		}
		n, err := skipLengthEncodedString(data[pos:])
		if err != nil {
			return nil, err
		}
		pos += n
		if mc.cfg.columnsWithAlias {
			tableName, _, n, err := readLengthEncodedString(data[pos:])
			if err != nil {
				return nil, err
			}
			pos += n
			columns[i].tableName = string(tableName)
		} else {
			n, err = skipLengthEncodedString(data[pos:])
			if err != nil {
				return nil, err
			}
			pos += n
		}
		n, err = skipLengthEncodedString(data[pos:])
		if err != nil {
			return nil, err
		}
		pos += n
		name, _, n, err := readLengthEncodedString(data[pos:])
		if err != nil {
			return nil, err
		}
		columns[i].name = string(name)
		pos += n
		n, err = skipLengthEncodedString(data[pos:])
		if err != nil {
			return nil, err
		}
		pos += n + 1 + 2 + 4
		columns[i].fieldType = data[pos]
		pos++
		columns[i].flags = fieldFlag(binary.LittleEndian.Uint16(data[pos : pos+2]))
		pos += 2
		columns[i].decimals = data[pos]
	}
}
func (rows *textRows) readRow(dest []driver.Value) error {
	mc := rows.mc
	data, err := mc.readPacket()
	if err != nil {
		return err
	}
	if data[0] == iEOF && len(data) == 5 {
		rows.mc = nil
		return io.EOF
	}
	if data[0] == iERR {
		rows.mc = nil
		return mc.handleErrorPacket(data)
	}
	var n int
	var isNull bool
	pos := 0
	for i := range dest {
		dest[i], isNull, n, err = readLengthEncodedString(data[pos:])
		pos += n
		if err == nil {
			if !isNull {
				if !mc.parseTime {
					continue
				} else {
					switch rows.columns[i].fieldType {
					case fieldTypeTimestamp, fieldTypeDateTime,
						fieldTypeDate, fieldTypeNewDate:
						dest[i], err = parseDateTime(
							string(dest[i].([]byte)),
							mc.cfg.loc,
						)
						if err == nil {
							continue
						}
					default:
						continue
					}
				}
			} else {
				dest[i] = nil
				continue
			}
		}
		return err  
	}
	return nil
}
func (mc *mysqlConn) readUntilEOF() error {
	for {
		data, err := mc.readPacket()
		if err == nil && data[0] != iEOF {
			continue
		}
		return err  
	}
}