Skip to content

Commit

Permalink
add all remaining packet types, empty functions, some other small cha…
Browse files Browse the repository at this point in the history
…nges
  • Loading branch information
Phil Bayfield committed Feb 10, 2011
1 parent 2e6c836 commit 07a2520
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 11 deletions.
8 changes: 3 additions & 5 deletions mysql.go
Expand Up @@ -782,6 +782,7 @@ func (c *Client) getFields() (err os.Error) {
return
}
if eof {
c.result.fieldPos = 0
break
}
}
Expand Down Expand Up @@ -970,15 +971,12 @@ func (c *Client) processRowResult(p *packetRowData) (err os.Error) {
// Stored result
if c.result.mode == RESULT_STORED {
// Cast and append the row
c.result.rows = append(c.result.rows, Row(p.values))
c.result.rows = append(c.result.rows, Row(p.row))
}
// Used result
if c.result.mode == RESULT_USED {
// Only save 1 row, overwrite previous
if len(c.result.rows) == 0 {
c.result.rows = make([]Row, 1)
}
c.result.rows[0] = Row(p.values)
c.result.rows = []Row{Row(p.row)}
}
return
}
118 changes: 112 additions & 6 deletions packet.go
Expand Up @@ -24,7 +24,7 @@ const (
PACKET_FIELD
PACKET_ROW
PACKET_EOF
PACKET_OK_PREPARED
PACKET_PREPARE_OK
PACKET_PARAM
PACKET_LONG_DATA
PACKET_EXECUTE
Expand Down Expand Up @@ -635,7 +635,7 @@ func (p *packetField) read(data []byte) (err os.Error) {
// Row data struct
type packetRowData struct {
packetBase
values []interface{}
row []interface{}
}

// Row data packet reader
Expand All @@ -656,11 +656,10 @@ func (p *packetRowData) read(data []byte) (err os.Error) {
return
}
// Add to slice
if len(p.values) == 0 {
p.values = make([]interface{}, 1)
p.values[0] = str
if len(p.row) == 0 {
p.row = []interface{}{str}
} else {
p.values = append(p.values, str)
p.row = append(p.row, str)
}
// Increment position and check for end of packet
pos += n
Expand All @@ -670,3 +669,110 @@ func (p *packetRowData) read(data []byte) (err os.Error) {
}
return
}

// Prepare ok struct
type packetPrepareOK struct {
packetBase
statementId uint32
columnCount uint16
paramCount uint16
warningCount uint16
}

// Prepare ok packet reader
func (p *packetPrepareOK) read(data []byte) (err os.Error) {
// Recover errors
defer func() {
if e := recover(); e != nil {
err = &ClientError{CR_MALFORMED_PACKET, CR_MALFORMED_PACKET_STR}
}
}()

return
}

// Parameter struct
type packetParameter struct {
packetBase
paramType []byte
flags uint16
decimals uint8
length uint32
}

// Parameter packet reader
func (p *packetParameter) read(data []byte) (err os.Error) {
// Recover errors
defer func() {
if e := recover(); e != nil {
err = &ClientError{CR_MALFORMED_PACKET, CR_MALFORMED_PACKET_STR}
}
}()
return
}

// Long data struct
type packetLongData struct {
packetBase
command byte
statementId uint32
paramNumber uint16
data string
}

// Lond data packet writer
func (p *packetLongData) write() (data []byte, err os.Error) {
// Recover errors
defer func() {
if e := recover(); e != nil {
err = &ClientError{CR_MALFORMED_PACKET, CR_MALFORMED_PACKET_STR}
}
}()
// Add the packet header
data = p.addHeader(data)
return
}

// Execute struct
type packetExecute struct {
packetBase
command byte
statementId uint32
flags uint8
iterationCount uint32
nullBitMap []byte
newParamBound uint8
paramType [][]byte
paramData [][]byte
paramLength uint32
}

// Execute packet writer
func (p *packetExecute) write() (data []byte, err os.Error) {
// Recover errors
defer func() {
if e := recover(); e != nil {
err = &ClientError{CR_MALFORMED_PACKET, CR_MALFORMED_PACKET_STR}
}
}()
// Add the packet header
data = p.addHeader(data)
return
}

// Binary row struct
type packetRowBinary struct {
packetBase
row []interface{}
}

// Row binary packet reader
func (p *packetRowBinary) read(data []byte) (err os.Error) {
// Recover errors
defer func() {
if e := recover(); e != nil {
err = &ClientError{CR_MALFORMED_PACKET, CR_MALFORMED_PACKET_STR}
}
}()
return
}
15 changes: 15 additions & 0 deletions reader.go
Expand Up @@ -107,6 +107,21 @@ func (r *reader) readPacket(types packetType) (p packetReadable, err os.Error) {
pk := new(packetRowData)
pk.sequence = uint8(pktSeq)
return pk, pk.read(pktData)
// Prepare ok packet
case types&PACKET_PREPARE_OK != 0 && pktData[0] == 0x0:
pk := new(packetPrepareOK)
pk.sequence = uint8(pktSeq)
return pk, pk.read(pktData)
// Param packet
case types&PACKET_PARAM != 0 && pktData[0] < 0xfe:
pk := new(packetParameter)
pk.sequence = uint8(pktSeq)
return pk, pk.read(pktData)
// Binary row packet
case types&PACKET_ROW_BINARY != 0 && pktData[0] < 0xfe:
pk := new(packetRowBinary)
pk.sequence = uint8(pktSeq)
return pk, pk.read(pktData)
}
return
}
Expand Down
19 changes: 19 additions & 0 deletions result.go
Expand Up @@ -46,6 +46,17 @@ func (r *Result) FieldCount() uint64 {
return r.fieldCount
}

// Fetch the next field
func (r *Result) FetchField() *Field {
// Check if all fields have been fetched
if r.fieldPos < uint64(len(r.fields)) {
// Increment and return current field
r.fieldPos ++
return r.fields[r.fieldPos-1]
}
return nil
}

// Fetch all fields
func (r *Result) FetchFields() []*Field {
return r.fields
Expand Down Expand Up @@ -102,6 +113,14 @@ func (r *Result) FetchMap() Map {
return nil
}

// Fetch all rows
func (r *Result) FetchRows() []Row {
if r.mode == RESULT_STORED {
return r.rows
}
return nil
}

// Free the result
func (r *Result) Free() (err os.Error) {
err = r.c.FreeResult()
Expand Down

0 comments on commit 07a2520

Please sign in to comment.