Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated for Go1 #61

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
package mysql

import (
"io"
"math"
"os"
"strconv"
)

Expand Down Expand Up @@ -121,6 +121,7 @@ func ui32tob(n uint32) (b []byte) {
}
return
}

// bytes to int64
func btoi64(b []byte) int64 {
return int64(btoui64(b))
Expand Down Expand Up @@ -169,7 +170,7 @@ func f64tob(f float64) []byte {
}

// bytes to length
func btolcb(b []byte) (num uint64, n int, err os.Error) {
func btolcb(b []byte) (num uint64, n int, err error) {
switch {
// 0-250 = value of first byte
case b[0] <= 250:
Expand All @@ -193,14 +194,14 @@ func btolcb(b []byte) (num uint64, n int, err os.Error) {
}
// Check there are enough bytes
if len(b) < n {
err = os.EOF
return
err = io.EOF
return num, n, err
}
// Get uint64
t := make([]byte, 8)
copy(t, b[1:n])
num = btoui64(t)
return
return num, n, nil
}

// length to bytes
Expand Down Expand Up @@ -229,7 +230,7 @@ func atoui64(i interface{}) (n uint64) {
return t
case string:
// Convert to int64 first for signing bit
in, err := strconv.Atoi64(t)
in, err := strconv.ParseInt(t, 10, 64)
if err != nil {
panic("Invalid string for integer conversion")
}
Expand All @@ -248,8 +249,8 @@ func atof64(i interface{}) (f float64) {
case float64:
return t
case string:
var err os.Error
f, err = strconv.Atof64(t)
var err error
f, err = strconv.ParseFloat(t, 64)
if err != nil {
panic("Invalid string for floating point conversion")
}
Expand All @@ -263,13 +264,13 @@ func atof64(i interface{}) (f float64) {
func atos(i interface{}) (s string) {
switch t := i.(type) {
case int64:
s = strconv.Itoa64(t)
s = strconv.FormatInt(t, 10)
case uint64:
s = strconv.Uitoa64(t)
s = strconv.FormatUint(t, 10)
case float32:
s = strconv.Ftoa32(t, 'f', -1)
s = strconv.FormatFloat(float64(t), 'f', -1, 32)
case float64:
s = strconv.Ftoa64(t, 'f', -1)
s = strconv.FormatFloat(t, 'f', -1, 64)
case []byte:
s = string(t)
case Date:
Expand Down
18 changes: 12 additions & 6 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,28 @@ const (

// Client error struct
type ClientError struct {
Errno Errno
Error Error
Errno Errno
ErrorS Error
}

// Convert to string
func (e *ClientError) String() string {
return fmt.Sprintf("#%d %s", e.Errno, e.Error)
return fmt.Sprintf("#%d %s", e.Errno, e.ErrorS)
}
func (e *ClientError) Error() string {
return e.String()
}

// Server error struct
type ServerError struct {
Errno Errno
Error Error
Errno Errno
ErrorS Error
}

// Convert to string
func (e *ServerError) String() string {
return fmt.Sprintf("#%d %s", e.Errno, e.Error)
return fmt.Sprintf("#%d %s", e.Errno, e.ErrorS)
}
func (e *ServerError) Error() string {
return e.String()
}
39 changes: 18 additions & 21 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
// license that can be found in the LICENSE file.
package mysql

import (
"os"
"strconv"
)
import "strconv"

// OK packet handler
func handleOK(p *packetOK, c *Client, a, i *uint64, w *uint16) (err os.Error) {
func handleOK(p *packetOK, c *Client, a, i *uint64, w *uint16) (err error) {
// Log OK result
c.log(1, "[%d] Received OK packet", p.sequence)
// Check sequence
Expand All @@ -32,7 +29,7 @@ func handleOK(p *packetOK, c *Client, a, i *uint64, w *uint16) (err os.Error) {
}

// Error packet handler
func handleError(p *packetError, c *Client) (err os.Error) {
func handleError(p *packetError, c *Client) (err error) {
// Log error result
c.log(1, "[%d] Received error packet", p.sequence)
// Check sequence
Expand All @@ -50,7 +47,7 @@ func handleError(p *packetError, c *Client) (err os.Error) {
}

// EOF packet handler
func handleEOF(p *packetEOF, c *Client) (err os.Error) {
func handleEOF(p *packetEOF, c *Client) (err error) {
// Log EOF result
c.log(1, "[%d] Received EOF packet", p.sequence)
// Check sequence
Expand All @@ -70,7 +67,7 @@ func handleEOF(p *packetEOF, c *Client) (err os.Error) {
}

// Result set packet handler
func handleResultSet(p *packetResultSet, c *Client, r *Result) (err os.Error) {
func handleResultSet(p *packetResultSet, c *Client, r *Result) (err error) {
// Log error result
c.log(1, "[%d] Received result set packet", p.sequence)
// Check sequence
Expand All @@ -84,7 +81,7 @@ func handleResultSet(p *packetResultSet, c *Client, r *Result) (err os.Error) {
}

// Field packet handler
func handleField(p *packetField, c *Client, r *Result) (err os.Error) {
func handleField(p *packetField, c *Client, r *Result) (err error) {
// Log field result
c.log(1, "[%d] Received field packet", p.sequence)
// Check sequence
Expand All @@ -110,7 +107,7 @@ func handleField(p *packetField, c *Client, r *Result) (err os.Error) {
}

// Row packet hander
func handleRow(p *packetRowData, c *Client, r *Result) (err os.Error) {
func handleRow(p *packetRowData, c *Client, r *Result) (err error) {
// Log field result
c.log(1, "[%d] Received row packet", p.sequence)
// Check sequence
Expand All @@ -128,23 +125,23 @@ func handleRow(p *packetRowData, c *Client, r *Result) (err os.Error) {
// Iterate fields to get types
for i, f := range r.fields {
// Check null
if len(p.row[i].([]byte)) ==0 {
if len(p.row[i].([]byte)) == 0 {
field = nil
} else {
switch f.Type {
// Signed/unsigned ints
case FIELD_TYPE_TINY, FIELD_TYPE_SHORT, FIELD_TYPE_YEAR, FIELD_TYPE_INT24, FIELD_TYPE_LONG, FIELD_TYPE_LONGLONG:
if f.Flags&FLAG_UNSIGNED > 0 {
field, err = strconv.Atoui64(string(p.row[i].([]byte)))
field, err = strconv.ParseUint(string(p.row[i].([]byte)), 10, 64)
} else {
field, err = strconv.Atoi64(string(p.row[i].([]byte)))
field, err = strconv.ParseInt(string(p.row[i].([]byte)), 10, 64)
}
if err != nil {
return
}
// Floats and doubles
case FIELD_TYPE_FLOAT, FIELD_TYPE_DOUBLE:
field, err = strconv.Atof64(string(p.row[i].([]byte)))
field, err = strconv.ParseFloat(string(p.row[i].([]byte)), 64)
if err != nil {
return
}
Expand Down Expand Up @@ -173,7 +170,7 @@ func handleRow(p *packetRowData, c *Client, r *Result) (err os.Error) {
}

// Prepare OK packet handler
func handlePrepareOK(p *packetPrepareOK, c *Client, s *Statement) (err os.Error) {
func handlePrepareOK(p *packetPrepareOK, c *Client, s *Statement) (err error) {
// Log result
c.log(1, "[%d] Received prepare OK packet", p.sequence)
// Check sequence
Expand All @@ -190,7 +187,7 @@ func handlePrepareOK(p *packetPrepareOK, c *Client, s *Statement) (err os.Error)
}

// Parameter packet handler
func handleParam(p *packetParameter, c *Client) (err os.Error) {
func handleParam(p *packetParameter, c *Client) (err error) {
// Log result
c.log(1, "[%d] Received parameter packet", p.sequence)
// Check sequence
Expand All @@ -203,7 +200,7 @@ func handleParam(p *packetParameter, c *Client) (err os.Error) {
}

// Binary row packet handler
func handleBinaryRow(p *packetRowBinary, c *Client, r *Result) (err os.Error) {
func handleBinaryRow(p *packetRowBinary, c *Client, r *Result) (err error) {
// Log binary row result
c.log(1, "[%d] Received binary row packet", p.sequence)
// Check sequence
Expand Down Expand Up @@ -278,15 +275,15 @@ func handleBinaryRow(p *packetRowBinary, c *Client, r *Result) (err os.Error) {
FIELD_TYPE_VAR_STRING, FIELD_TYPE_STRING, FIELD_TYPE_GEOMETRY:
num, n, err := btolcb(p.data[pos:])
if err != nil {
return
return err
}
field = p.data[pos+uint64(n) : pos+uint64(n)+num]
pos += uint64(n) + num
// Date (From libmysql/libmysql.c read_binary_datetime)
case FIELD_TYPE_DATE:
num, n, err := btolcb(p.data[pos:])
if err != nil {
return
return err
}
// New date
d := Date{}
Expand All @@ -308,7 +305,7 @@ func handleBinaryRow(p *packetRowBinary, c *Client, r *Result) (err os.Error) {
case FIELD_TYPE_TIME:
num, n, err := btolcb(p.data[pos:])
if err != nil {
return
return err
}
// New time
t := Time{}
Expand All @@ -330,7 +327,7 @@ func handleBinaryRow(p *packetRowBinary, c *Client, r *Result) (err os.Error) {
case FIELD_TYPE_TIMESTAMP, FIELD_TYPE_DATETIME:
num, n, err := btolcb(p.data[pos:])
if err != nil {
return
return err
}
// New datetime
d := DateTime{}
Expand Down
Loading