Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
julienschmidt committed Feb 28, 2013
1 parent a48f79b commit 9454eee
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 22 deletions.
7 changes: 4 additions & 3 deletions connection.go
Expand Up @@ -95,6 +95,7 @@ func (mc *mysqlConn) Begin() (driver.Tx, error) {

func (mc *mysqlConn) Close() (err error) {
mc.writeCommandPacket(COM_QUIT)
mc.cfg = nil
mc.bufReader = nil
mc.netConn.Close()
mc.netConn = nil
Expand Down Expand Up @@ -150,9 +151,9 @@ func (mc *mysqlConn) Exec(query string, args []driver.Value) (driver.Result, err
}

return &mysqlResult{
affectedRows: int64(mc.affectedRows),
insertId: int64(mc.insertId)},
err
affectedRows: int64(mc.affectedRows),
insertId: int64(mc.insertId),
}, err
}

// Internal function to execute commands
Expand Down
8 changes: 4 additions & 4 deletions const.go
Expand Up @@ -2,22 +2,22 @@
//
// Copyright 2012 Julien Schmidt. All rights reserved.
// http://www.julienschmidt.com
//
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.

package mysql

// Constants documentation:
// http://dev.mysql.com/doc/internals/en/client-server-protocol.html

const (
MIN_PROTOCOL_VERSION = 10
MAX_PACKET_SIZE = 1<<24 - 1
TIME_FORMAT = "2006-01-02 15:04:05"
)

// MySQL constants documentation:
// http://dev.mysql.com/doc/internals/en/client-server-protocol.html

type ClientFlag uint32

const (
Expand Down
22 changes: 8 additions & 14 deletions packets.go
Expand Up @@ -330,10 +330,10 @@ func (mc *mysqlConn) writeCommandPacket(command commandType, args ...interface{}
******************************************************************************/

// Returns error if Packet is not an 'Result OK'-Packet
func (mc *mysqlConn) readResultOK() (err error) {
func (mc *mysqlConn) readResultOK() error {
data, err := mc.readPacket()
if err != nil {
return
return err
}

switch data[0] {
Expand All @@ -342,17 +342,13 @@ func (mc *mysqlConn) readResultOK() (err error) {
return mc.handleOkPacket(data)
// EOF, someone is using old_passwords
case 254:
err = errors.New("It seems like you are using old_passwords, which is unsupported. See https://github.com/Go-SQL-Driver/MySQL/wiki/old_passwords")
return
return errors.New("It seems like you are using old_passwords, which is unsupported. See https://github.com/Go-SQL-Driver/MySQL/wiki/old_passwords")
// ERROR
case 255:
return mc.handleErrorPacket(data)
default:
err = errors.New("Invalid Result Packet-Type")
return
}

return
return errors.New("Invalid Result Packet-Type")
}

/* Error Packet
Expand All @@ -364,10 +360,9 @@ Bytes Name
5 sqlstate (5 characters)
n message
*/
func (mc *mysqlConn) handleErrorPacket(data []byte) (err error) {
func (mc *mysqlConn) handleErrorPacket(data []byte) error {
if data[0] != 255 {
err = errors.New("Wrong Packet-Type: Not an Error-Packet")
return
return errors.New("Wrong Packet-Type: Not an Error-Packet")
}

pos := 1
Expand All @@ -383,8 +378,7 @@ func (mc *mysqlConn) handleErrorPacket(data []byte) (err error) {
// Error Message [string]
message := string(data[pos:])

err = fmt.Errorf("Error %d: %s", errno, message)
return
return fmt.Errorf("Error %d: %s", errno, message)
}

/* Ok Packet
Expand Down Expand Up @@ -604,7 +598,7 @@ func (mc *mysqlConn) readRow(columnsCount int) (*[]*[]byte, error) {
return &row, nil
}

// Reads Packets Packets until EOF-Packet or an Error appears. Returns count of Packets read
// Reads Packets until EOF-Packet or an Error appears. Returns count of Packets read
func (mc *mysqlConn) readUntilEOF() (count uint64, err error) {
var data []byte

Expand Down
2 changes: 1 addition & 1 deletion utils_test.go
Expand Up @@ -2,7 +2,7 @@
//
// Copyright 2013 Julien Schmidt. All rights reserved.
// http://www.julienschmidt.com
//
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
Expand Down

0 comments on commit 9454eee

Please sign in to comment.