driver

package
v0.0.0-...-584552c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 21, 2018 License: Apache-2.0 Imports: 14 Imported by: 1

Documentation

Index

Constants

View Source
const (
	DEFAULT_RECEIVE_BUFFER_SIZE = 16 * 1024 //16k
	DEFAULT_CONNECT_TIMEOUT     = 10 * time.Second
	DEFAULT_CHARSET             = "utf8" //utf8
	MaxPacketSize               = 1<<24 - 1
)
View Source
const (
	NULL_TERMINATED_STRING_DELIMITER      = 0x00
	MinProtocolVersion               byte = 10
	NATIVE_CLIENT_NAME                    = "mysql_native_password"
	COM_SEMI_MARK                         = 0xef
	SERVER_MORE_RESULTS_EXISTS            = 0x0008
)
View Source
const (
	COM_SLEEP byte = iota
	COM_QUIT
	COM_INIT_DB
	COM_QUERY
	COM_FIELD_LIST
	COM_CREATE_DB
	COM_DROP_DB
	COM_REFRESH
	COM_SHUTDOWN
	COM_STATISTICS
	COM_PROCESS_INFO
	COM_CONNECT
	COM_PROCESS_KILL
	COM_DEBUG
	COM_PING
	COM_TIME
	COM_DELAYED_INSERT
	COM_CHANGE_USER
	COM_BINLOG_DUMP
	COM_TABLE_DUMP
	COM_CONNECT_OUT
	COM_REGISTER_SLAVE
	COM_STMT_PREPARE
	COM_STMT_EXECUTE
	COM_STMT_SEND_LONG_DATA
	COM_STMT_CLOSE
	COM_STMT_RESET
	COM_SET_OPTION
	COM_STMT_FETCH
	COM_DAEMON
	COM_BINLOG_DUMP_GTID
	COM_RESET_CONNECTION
)

Commands

View Source
const (
	BINLOG_DUMP_NEVER_STOP  uint16 = 0x0
	BINLOG_DUMP_NON_BLOCK   uint16 = 0x1
	BINLOG_THROUGH_POSITION uint16 = 0x2
	BINLOG_THROUGH_GTID     uint16 = 0x4
)

Listening options

View Source
const (
	CLIENT_LONG_PASSWORD uint32 = 1 << iota
	CLIENT_FOUND_ROWS
	CLIENT_LONG_FLAG
	CLIENT_CONNECT_WITH_DB
	CLIENT_NO_SCHEMA
	CLIENT_COMPRESS
	CLIENT_ODBC
	CLIENT_LOCAL_FILES
	CLIENT_IGNORE_SPACE
	CLIENT_PROTOCOL_41
	CLIENT_INTERACTIVE
	CLIENT_SSL
	CLIENT_IGNORE_SIGPIPE
	CLIENT_TRANSACTIONS
	CLIENT_RESERVED
	CLIENT_SECURE_CONNECTION
	CLIENT_MULTI_STATEMENTS
	CLIENT_MULTI_RESULTS
	CLIENT_PS_MULTI_RESULTS
	CLIENT_PLUGIN_AUTH
	CLIENT_CONNECT_ATTRS
	CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA
)
View Source
const (
	NOT_NULL_FLAG       = 1
	PRI_KEY_FLAG        = 2
	UNIQUE_KEY_FLAG     = 4
	BLOB_FLAG           = 16
	UNSIGNED_FLAG       = 32
	ZEROFILL_FLAG       = 64
	BINARY_FLAG         = 128
	ENUM_FLAG           = 256
	AUTO_INCREMENT_FLAG = 512
	TIMESTAMP_FLAG      = 1024
	SET_FLAG            = 2048
	NUM_FLAG            = 32768
	PART_KEY_FLAG       = 16384
	GROUP_FLAG          = 32768
	UNIQUE_FLAG         = 65536
)

Flags

View Source
const (
	// CharacterSetUtf8 is for UTF8. We use this by default.
	CharacterSetUtf8 = 33

	// CharacterSetBinary is for binary. Use by integer fields for instance.
	CharacterSetBinary = 63
)

A few interesting character set values. See http://dev.mysql.com/doc/internals/en/character-set.html#packet-Protocol::CharacterSet

Variables

View Source
var (
	ErrInvalidConn = errors.New("Invalid Connection")
	ErrMalformPkt  = errors.New("Malformed Packet")
	ErrBadConn     = errors.New("driver: bad connection")
)
View Source
var CharacterSetMap = map[string]uint8{
	"big5":     1,
	"dec8":     3,
	"cp850":    4,
	"hp8":      6,
	"koi8r":    7,
	"latin1":   8,
	"latin2":   9,
	"swe7":     10,
	"ascii":    11,
	"ujis":     12,
	"sjis":     13,
	"hebrew":   16,
	"tis620":   18,
	"euckr":    19,
	"koi8u":    22,
	"gb2312":   24,
	"greek":    25,
	"cp1250":   26,
	"gbk":      28,
	"latin5":   30,
	"armscii8": 32,
	"utf8":     CharacterSetUtf8,
	"ucs2":     35,
	"cp866":    36,
	"keybcs2":  37,
	"macce":    38,
	"macroman": 39,
	"cp852":    40,
	"latin7":   41,
	"utf8mb4":  45,
	"cp1251":   51,
	"utf16":    54,
	"utf16le":  56,
	"cp1256":   57,
	"cp1257":   59,
	"utf32":    60,
	"binary":   CharacterSetBinary,
	"geostd8":  92,
	"cp932":    95,
	"eucjpms":  97,
}

CharacterSetMap maps the charset name (used in ConnParams) to the integer value. Interesting ones have their own constant above.

Functions

This section is empty.

Types

type Conn

type Conn struct {
	net.Conn
	// contains filtered or unexported fields
}

func (*Conn) ReadPacket

func (c *Conn) ReadPacket() ([]byte, error)

reads a raw packet from the MySQL connection. A raw packet is (1) a binary log event sent from a master to a slave <- what we care about (2) a single statement sent to the MySQL server, or (3) a single row sent to the client. This method returns a generic error, not a SQLError.

type FieldData

type FieldData []byte

type FieldPacket

type FieldPacket struct {
	Data               FieldData
	Schema             []byte
	Table              []byte
	OrgTable           []byte
	Name               []byte
	OrgName            []byte
	Charset            uint16
	ColumnLength       uint32
	Type               uint8
	Flag               uint16
	Decimal            uint8
	DefaultValueLength uint64
	DefaultValue       []byte
}

EventBody Name ----- ---- n (Length Coded String) catalog n (Length Coded String) db n (Length Coded String) table n (Length Coded String) org_table n (Length Coded String) name n (Length Coded String) org_name 1 (filler) 2 charsetnr 4 length 1 type 2 flags 1 decimals 2 (filler), always 0x00 n (Length Coded Binary) default

type HandshakeInitPacket

type HandshakeInitPacket struct {
	// contains filtered or unexported fields
}

|Type |Name | |1 |protocol_version | |n (Null-Terminated String)|server_version | |4 |thread_id | |8 |scramble_buff | |1 |(filler) always 0x00 | |2 |server_capabilities | |1 |server_language | |2 |server_status | |13 |(filler) always 0x00 ... | |13 |rest of scramble_buff (4.1)|

type MySQLConnector

type MySQLConnector struct {
	*Conn

	//handshake packet
	*HandshakeInitPacket
	// contains filtered or unexported fields
}

func NewMySQLConnector

func NewMySQLConnector(address, username, password, dbName string) *MySQLConnector

func (*MySQLConnector) Close

func (mc *MySQLConnector) Close() error

func (*MySQLConnector) Connect

func (mc *MySQLConnector) Connect() error

Start a background connection routine. It first establishes a network connection, returns it on the channel, then starts the negotiation, and returns the result on the channel. It can send on the channel, before closing it:

  • a connectChan with an error and nothing else (when dial fails).
  • a connectChan with a *Conn and no error, then another one with possibly an error.

func (*MySQLConnector) ConnectionId

func (mc *MySQLConnector) ConnectionId() uint32

func (*MySQLConnector) HandleERRPacket

func (mc *MySQLConnector) HandleERRPacket(data []byte) error

|Type |Name |Description | |int<1> |header |0xFF ERR packet header | |int<2> |error_code |error-code | |if capabilities & CLIENT_PROTOCOL_41 { | |string[1] |sql_state_marker |# marker of the SQL state | |string[5] |sql_state |SQL state | |} | |string<EOF>|error_message |human readable error message| https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_basic_err_packet.html

func (*MySQLConnector) IsConnected

func (mc *MySQLConnector) IsConnected() bool

func (*MySQLConnector) Query

func (mc *MySQLConnector) Query(query string) (*Result, error)

func (*MySQLConnector) QueryMulti

func (mc *MySQLConnector) QueryMulti(query string) ([]*Result, error)

func (*MySQLConnector) SetCharSet

func (mc *MySQLConnector) SetCharSet(charset string)

func (*MySQLConnector) SetConnTimeout

func (mc *MySQLConnector) SetConnTimeout(timeout int)

func (*MySQLConnector) SetReadTimeout

func (mc *MySQLConnector) SetReadTimeout(timeout int)

func (*MySQLConnector) Update

func (mc *MySQLConnector) Update(update string) (*Result, error)

func (*MySQLConnector) WriteBinlogDumpPacket

func (mc *MySQLConnector) WriteBinlogDumpPacket(binlogName string, slaveId, position uint32) error

|Type |Name | |4 |binlog position to start at (little endian)| |2 |binlog flags (currently not used; always 0)| |4 |server_id of the slave (little endian) | |n |binlog file name (optional) |

func (*MySQLConnector) WriteRegisterSlavePacket

func (mc *MySQLConnector) WriteRegisterSlavePacket(slaveId uint32) error

func (*MySQLConnector) WriteSemiAckComPacket

func (mc *MySQLConnector) WriteSemiAckComPacket(p *SemiAckComPacket) error

type Result

type Result struct {
	Status       uint16
	InsertId     uint64
	AffectedRows uint64
	*ResultSet
}

type ResultSet

type ResultSet struct {
	Fields     []*FieldPacket
	FieldNames map[string]int
	Values     [][]interface{}
	RowDatas   []RowData
}

func (*ResultSet) GetInt64

func (r *ResultSet) GetInt64(row, column int) (int64, error)

func (*ResultSet) GetString

func (r *ResultSet) GetString(row, column int) (string, error)

func (*ResultSet) GetUint64

func (r *ResultSet) GetUint64(row, column int) (uint64, error)

func (*ResultSet) GetValue

func (r *ResultSet) GetValue(row, column int) (interface{}, error)

func (*ResultSet) RowNumber

func (r *ResultSet) RowNumber() int

type RowData

type RowData []byte

type SemiAckComPacket

type SemiAckComPacket struct {
	Position   uint64
	BinlogName string
}

|Type |Name | |1 |semi mark | |8 |binlog position to start at (little endian)| |n |binlog file name |

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL