Skip to content

Commit

Permalink
Merge remote-tracking branch 'patrickxb/master'
Browse files Browse the repository at this point in the history
Conflicts:
	handler.go
	mysql_test.go
	packet.go
	result.go
  • Loading branch information
Frederic Donckels committed Feb 12, 2012
2 parents 2f9185f + a4d75c7 commit ae79470
Show file tree
Hide file tree
Showing 11 changed files with 289 additions and 259 deletions.
29 changes: 18 additions & 11 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
package mysql

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

Expand Down Expand Up @@ -121,6 +122,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 +171,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,7 +195,7 @@ func btolcb(b []byte) (num uint64, n int, err os.Error) {
}
// Check there are enough bytes
if len(b) < n {
err = os.EOF
err = io.EOF
return
}
// Get uint64
Expand Down Expand Up @@ -229,13 +231,15 @@ 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")
}
n = uint64(in)
case nil:
return 0
default:
panic("Not a numeric type")
panic(fmt.Sprintf("Not a numeric type: %v", t))
}
return
}
Expand All @@ -248,12 +252,15 @@ 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")
}
case nil:
f = 0.0
default:
fmt.Printf("type: %v\n", t)
panic("Not a floating point type")
}
return
Expand All @@ -263,13 +270,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
218 changes: 113 additions & 105 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,127 +9,135 @@ import "fmt"

// Client error types
type Errno int
type Error string
type ErrorMsg string

const (
CR_UNKNOWN_ERROR Errno = 2000
CR_UNKNOWN_ERROR_STR Error = "Unknown MySQL error"
CR_SOCKET_CREATE_ERROR Errno = 2001
CR_SOCKET_CREATE_ERROR_STR Error = "Can't create UNIX socket (%d)"
CR_CONNECTION_ERROR Errno = 2002
CR_CONNECTION_ERROR_STR Error = "Can't connect to local MySQL server through socket '%s'"
CR_CONN_HOST_ERROR Errno = 2003
CR_CONN_HOST_ERROR_STR Error = "Can't connect to MySQL server on '%s'"
CR_IPSOCK_ERROR Errno = 2004
CR_IPSOCK_ERROR_STR Error = "Can't create TCP/IP socket (%d)"
CR_UNKNOWN_HOST Errno = 2005
CR_UNKNOWN_HOST_STR Error = "Uknown MySQL server host '%s' (%d)"
CR_SERVER_GONE_ERROR Errno = 2006
CR_SERVER_GONE_ERROR_STR Error = "MySQL server has gone away"
CR_VERSION_ERROR Errno = 2007
CR_VERSION_ERROR_STR Error = "Protocol mismatch; server version = %d, client version = %d"
CR_OUT_OF_MEMORY Errno = 2008
CR_OUT_OF_MEMORY_STR Error = "MySQL client ran out of memory"
CR_WRONG_HOST_INFO Errno = 2009
CR_WRONG_HOST_INFO_STR Error = "Wrong host info"
CR_LOCALHOST_CONNECTION Errno = 2010
CR_LOCALHOST_CONNECTION_STR Error = "Localhost via UNIX socket"
CR_TCP_CONNECTION Errno = 2011
CR_TCP_CONNECTION_STR Error = "%s via TCP/IP"
CR_SERVER_HANDSHAKE_ERR Errno = 2012
CR_SERVER_HANDSHAKE_ERR_STR Error = "Error in server handshake"
CR_SERVER_LOST Errno = 2013
CR_SERVER_LOST_STR Error = "Lost connection to MySQL server during query"
CR_COMMANDS_OUT_OF_SYNC Errno = 2014
CR_COMMANDS_OUT_OF_SYNC_STR Error = "Commands out of sync; you can't run this command now"
CR_NAMEDPIPE_CONNECTION Errno = 2015
CR_NAMEDPIPE_CONNECTION_STR Error = "Named pipe: %s"
CR_NAMEDPIPEWAIT_ERROR Errno = 2016
CR_NAMEDPIPEWAIT_ERROR_STR Error = "Can't wait for named pipe to host: %s pipe: %s (%lu)"
CR_NAMEDPIPEOPEN_ERROR Errno = 2017
CR_NAMEDPIPEOPEN_ERROR_STR Error = "Can't open named pipe to host: %s pipe: %s (%lu)"
CR_NAMEDPIPESETSTATE_ERROR Errno = 2018
CR_NAMEDPIPESETSTATE_ERROR_STR Error = "Can't set state of named pipe to host: %s pipe: %s (%lu)"
CR_CANT_READ_CHARSET Errno = 2019
CR_CANT_READ_CHARSET_STR Error = "Can't initialize character set %s (path: %s)"
CR_NET_PACKET_TOO_LARGE Errno = 2020
CR_NET_PACKET_TOO_LARGE_STR Error = "Got packet bigger than 'max_allowed_packet' bytes"
CR_EMBEDDED_CONNECTION Errno = 2021
CR_EMBEDDED_CONNECTION_STR Error = "Embedded server"
CR_PROBE_SLAVE_STATUS Errno = 2022
CR_PROBE_SLAVE_STATUS_STR Error = "Error on SHOW SLAVE STATUS:"
CR_PROBE_SLAVE_HOSTS Errno = 2023
CR_PROBE_SLAVE_HOSTS_STR Error = "Error on SHOW SLAVE HOSTS:"
CR_PROBE_SLAVE_CONNECT Errno = 2024
CR_PROBE_SLAVE_CONNECT_STR Error = "Error connecting to slave:"
CR_PROBE_MASTER_CONNECT Errno = 2025
CR_PROBE_MASTER_CONNECT_STR Error = "Error connecting to master:"
CR_SSL_CONNECTION_ERROR Errno = 2026
CR_SSL_CONNECTION_ERROR_STR Error = "SSL connection error"
CR_MALFORMED_PACKET Errno = 2027
CR_MALFORMED_PACKET_STR Error = "Malformed Packet"
CR_WRONG_LICENSE Errno = 2028
CR_WRONG_LICENSE_STR Error = "This client library is licensed only for use with MySQL servers having '%s' license"
CR_NULL_POINTER Errno = 2029
CR_NULL_POINTER_STR Error = "Invalid use of null pointer"
CR_NO_PREPARE_STMT Errno = 2030
CR_NO_PREPARE_STMT_STR Error = "Statement not prepared"
CR_PARAMS_NOT_BOUND Errno = 2031
CR_PARAMS_NOT_BOUND_STR Error = "No data supplied for parameters in prepared statement"
CR_DATA_TRUNCATED Errno = 2032
CR_DATA_TRUNCATED_STR Error = "Data truncated"
CR_NO_PARAMETERS_EXISTS Errno = 2033
CR_NO_PARAMETERS_EXISTS_STR Error = "No parameters exist in the statement"
CR_INVALID_PARAMETER_NO Errno = 2034
CR_INVALID_PARAMETER_NO_STR Error = "Invalid parameter number"
CR_INVALID_BUFFER_USE Errno = 2035
CR_INVALID_BUFFER_USE_STR Error = "Can't send long data for non-string/non-binary data types (parameter: %d)"
CR_UNSUPPORTED_PARAM_TYPE Errno = 2036
CR_UNSUPPORTED_PARAM_TYPE_STR Error = "Using unsupported parameter type: %s (parameter: %d)"
CR_CONN_UNKNOW_PROTOCOL Errno = 2047
CR_CONN_UNKNOW_PROTOCOL_STR Error = "Wrong or unknown protocol"
CR_SECURE_AUTH Errno = 2049
CR_SECURE_AUTH_STR Error = "Connection using old (pre-4.1.1) authentication protocol refused (client option 'secure_auth' enabled)"
CR_FETCH_CANCELED Errno = 2050
CR_FETCH_CANCELED_STR Error = "Row retrieval was canceled by mysql_stmt_close() call"
CR_NO_DATA Errno = 2051
CR_NO_DATA_STR Error = "Attempt to read column without prior row fetch"
CR_NO_STMT_METADATA Errno = 2052
CR_NO_STMT_METADATA_STR Error = "Prepared statement contains no metadata"
CR_NO_RESULT_SET Errno = 2053
CR_NO_RESULT_SET_STR Error = "Attempt to read a row while there is no result set associated with the statement"
CR_NOT_IMPLEMENTED Errno = 2054
CR_NOT_IMPLEMENTED_STR Error = "This feature is not implemented yet"
CR_SERVER_LOST_EXTENDED Errno = 2055
CR_SERVER_LOST_EXTENDED_STR Error = "Lost connection to MySQL server at '%s', system error: %d"
CR_STMT_CLOSED Errno = 2056
CR_STMT_CLOSED_STR Error = "Statement closed indirectly because of a preceeding %s() call"
CR_NEW_STMT_METADATA Errno = 2057
CR_NEW_STMT_METADATA_STR Error = "The number of columns in the result set differs from the number of bound buffers. You must reset the statement, rebind the result set columns, and execute the statement again"
CR_ALREADY_CONNECTED Errno = 2058
CR_ALREADY_CONNECTED_STR Error = "This handle is already connected"
CR_AUTH_PLUGIN_CANNOT_LOAD Errno = 2059
CR_AUTH_PLUGIN_CANNOT_LOAD_STR Error = "Authentication plugin '%s' cannot be loaded: %s"
CR_UNKNOWN_ERROR Errno = 2000
CR_UNKNOWN_ERROR_STR ErrorMsg = "Unknown MySQL error"
CR_SOCKET_CREATE_ERROR Errno = 2001
CR_SOCKET_CREATE_ERROR_STR ErrorMsg = "Can't create UNIX socket (%d)"
CR_CONNECTION_ERROR Errno = 2002
CR_CONNECTION_ERROR_STR ErrorMsg = "Can't connect to local MySQL server through socket '%s'"
CR_CONN_HOST_ERROR Errno = 2003
CR_CONN_HOST_ERROR_STR ErrorMsg = "Can't connect to MySQL server on '%s'"
CR_IPSOCK_ERROR Errno = 2004
CR_IPSOCK_ERROR_STR ErrorMsg = "Can't create TCP/IP socket (%d)"
CR_UNKNOWN_HOST Errno = 2005
CR_UNKNOWN_HOST_STR ErrorMsg = "Uknown MySQL server host '%s' (%d)"
CR_SERVER_GONE_ERROR Errno = 2006
CR_SERVER_GONE_ERROR_STR ErrorMsg = "MySQL server has gone away"
CR_VERSION_ERROR Errno = 2007
CR_VERSION_ERROR_STR ErrorMsg = "Protocol mismatch; server version = %d, client version = %d"
CR_OUT_OF_MEMORY Errno = 2008
CR_OUT_OF_MEMORY_STR ErrorMsg = "MySQL client ran out of memory"
CR_WRONG_HOST_INFO Errno = 2009
CR_WRONG_HOST_INFO_STR ErrorMsg = "Wrong host info"
CR_LOCALHOST_CONNECTION Errno = 2010
CR_LOCALHOST_CONNECTION_STR ErrorMsg = "Localhost via UNIX socket"
CR_TCP_CONNECTION Errno = 2011
CR_TCP_CONNECTION_STR ErrorMsg = "%s via TCP/IP"
CR_SERVER_HANDSHAKE_ERR Errno = 2012
CR_SERVER_HANDSHAKE_ERR_STR ErrorMsg = "ErrorMsg in server handshake"
CR_SERVER_LOST Errno = 2013
CR_SERVER_LOST_STR ErrorMsg = "Lost connection to MySQL server during query"
CR_COMMANDS_OUT_OF_SYNC Errno = 2014
CR_COMMANDS_OUT_OF_SYNC_STR ErrorMsg = "Commands out of sync; you can't run this command now"
CR_NAMEDPIPE_CONNECTION Errno = 2015
CR_NAMEDPIPE_CONNECTION_STR ErrorMsg = "Named pipe: %s"
CR_NAMEDPIPEWAIT_ERROR Errno = 2016
CR_NAMEDPIPEWAIT_ERROR_STR ErrorMsg = "Can't wait for named pipe to host: %s pipe: %s (%lu)"
CR_NAMEDPIPEOPEN_ERROR Errno = 2017
CR_NAMEDPIPEOPEN_ERROR_STR ErrorMsg = "Can't open named pipe to host: %s pipe: %s (%lu)"
CR_NAMEDPIPESETSTATE_ERROR Errno = 2018
CR_NAMEDPIPESETSTATE_ERROR_STR ErrorMsg = "Can't set state of named pipe to host: %s pipe: %s (%lu)"
CR_CANT_READ_CHARSET Errno = 2019
CR_CANT_READ_CHARSET_STR ErrorMsg = "Can't initialize character set %s (path: %s)"
CR_NET_PACKET_TOO_LARGE Errno = 2020
CR_NET_PACKET_TOO_LARGE_STR ErrorMsg = "Got packet bigger than 'max_allowed_packet' bytes"
CR_EMBEDDED_CONNECTION Errno = 2021
CR_EMBEDDED_CONNECTION_STR ErrorMsg = "Embedded server"
CR_PROBE_SLAVE_STATUS Errno = 2022
CR_PROBE_SLAVE_STATUS_STR ErrorMsg = "Error on SHOW SLAVE STATUS:"
CR_PROBE_SLAVE_HOSTS Errno = 2023
CR_PROBE_SLAVE_HOSTS_STR ErrorMsg = "Error on SHOW SLAVE HOSTS:"
CR_PROBE_SLAVE_CONNECT Errno = 2024
CR_PROBE_SLAVE_CONNECT_STR ErrorMsg = "Error connecting to slave:"
CR_PROBE_MASTER_CONNECT Errno = 2025
CR_PROBE_MASTER_CONNECT_STR ErrorMsg = "Error connecting to master:"
CR_SSL_CONNECTION_ERROR Errno = 2026
CR_SSL_CONNECTION_ERROR_STR ErrorMsg = "SSL connection error"
CR_MALFORMED_PACKET Errno = 2027
CR_MALFORMED_PACKET_STR ErrorMsg = "Malformed Packet"
CR_WRONG_LICENSE Errno = 2028
CR_WRONG_LICENSE_STR ErrorMsg = "This client library is licensed only for use with MySQL servers having '%s' license"
CR_NULL_POINTER Errno = 2029
CR_NULL_POINTER_STR ErrorMsg = "Invalid use of null pointer"
CR_NO_PREPARE_STMT Errno = 2030
CR_NO_PREPARE_STMT_STR ErrorMsg = "Statement not prepared"
CR_PARAMS_NOT_BOUND Errno = 2031
CR_PARAMS_NOT_BOUND_STR ErrorMsg = "No data supplied for parameters in prepared statement"
CR_DATA_TRUNCATED Errno = 2032
CR_DATA_TRUNCATED_STR ErrorMsg = "Data truncated"
CR_NO_PARAMETERS_EXISTS Errno = 2033
CR_NO_PARAMETERS_EXISTS_STR ErrorMsg = "No parameters exist in the statement"
CR_INVALID_PARAMETER_NO Errno = 2034
CR_INVALID_PARAMETER_NO_STR ErrorMsg = "Invalid parameter number"
CR_INVALID_BUFFER_USE Errno = 2035
CR_INVALID_BUFFER_USE_STR ErrorMsg = "Can't send long data for non-string/non-binary data types (parameter: %d)"
CR_UNSUPPORTED_PARAM_TYPE Errno = 2036
CR_UNSUPPORTED_PARAM_TYPE_STR ErrorMsg = "Using unsupported parameter type: %s (parameter: %d)"
CR_CONN_UNKNOW_PROTOCOL Errno = 2047
CR_CONN_UNKNOW_PROTOCOL_STR ErrorMsg = "Wrong or unknown protocol"
CR_SECURE_AUTH Errno = 2049
CR_SECURE_AUTH_STR ErrorMsg = "Connection using old (pre-4.1.1) authentication protocol refused (client option 'secure_auth' enabled)"
CR_FETCH_CANCELED Errno = 2050
CR_FETCH_CANCELED_STR ErrorMsg = "Row retrieval was canceled by mysql_stmt_close() call"
CR_NO_DATA Errno = 2051
CR_NO_DATA_STR ErrorMsg = "Attempt to read column without prior row fetch"
CR_NO_STMT_METADATA Errno = 2052
CR_NO_STMT_METADATA_STR ErrorMsg = "Prepared statement contains no metadata"
CR_NO_RESULT_SET Errno = 2053
CR_NO_RESULT_SET_STR ErrorMsg = "Attempt to read a row while there is no result set associated with the statement"
CR_NOT_IMPLEMENTED Errno = 2054
CR_NOT_IMPLEMENTED_STR ErrorMsg = "This feature is not implemented yet"
CR_SERVER_LOST_EXTENDED Errno = 2055
CR_SERVER_LOST_EXTENDED_STR ErrorMsg = "Lost connection to MySQL server at '%s', system error: %d"
CR_STMT_CLOSED Errno = 2056
CR_STMT_CLOSED_STR ErrorMsg = "Statement closed indirectly because of a preceeding %s() call"
CR_NEW_STMT_METADATA Errno = 2057
CR_NEW_STMT_METADATA_STR ErrorMsg = "The number of columns in the result set differs from the number of bound buffers. You must reset the statement, rebind the result set columns, and execute the statement again"
CR_ALREADY_CONNECTED Errno = 2058
CR_ALREADY_CONNECTED_STR ErrorMsg = "This handle is already connected"
CR_AUTH_PLUGIN_CANNOT_LOAD Errno = 2059
CR_AUTH_PLUGIN_CANNOT_LOAD_STR ErrorMsg = "Authentication plugin '%s' cannot be loaded: %s"
)

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

// 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.ErrorMsg)
}

func (e *ClientError) Error() string {
return string(e.ErrorMsg)
}

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

// 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.ErrorMsg)
}

func (e *ServerError) Error() string {
return string(e.ErrorMsg)
}
Loading

0 comments on commit ae79470

Please sign in to comment.