native

package
v1.5.4 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2015 License: BSD-3-Clause, BSD-3-Clause Imports: 16 Imported by: 209

Documentation

Overview

Thread unsafe engine for MyMySQL

Index

Constants

View Source
const (
	MYSQL_TYPE_DECIMAL     = 0x00
	MYSQL_TYPE_TINY        = 0x01 // int8, uint8, bool
	MYSQL_TYPE_SHORT       = 0x02 // int16, uint16
	MYSQL_TYPE_LONG        = 0x03 // int32, uint32
	MYSQL_TYPE_FLOAT       = 0x04 // float32
	MYSQL_TYPE_DOUBLE      = 0x05 // float64
	MYSQL_TYPE_NULL        = 0x06 // nil
	MYSQL_TYPE_TIMESTAMP   = 0x07 // Timestamp
	MYSQL_TYPE_LONGLONG    = 0x08 // int64, uint64
	MYSQL_TYPE_INT24       = 0x09
	MYSQL_TYPE_DATE        = 0x0a // Date
	MYSQL_TYPE_TIME        = 0x0b // Time
	MYSQL_TYPE_DATETIME    = 0x0c // time.Time
	MYSQL_TYPE_YEAR        = 0x0d
	MYSQL_TYPE_NEWDATE     = 0x0e
	MYSQL_TYPE_VARCHAR     = 0x0f
	MYSQL_TYPE_BIT         = 0x10
	MYSQL_TYPE_NEWDECIMAL  = 0xf6
	MYSQL_TYPE_ENUM        = 0xf7
	MYSQL_TYPE_SET         = 0xf8
	MYSQL_TYPE_TINY_BLOB   = 0xf9
	MYSQL_TYPE_MEDIUM_BLOB = 0xfa
	MYSQL_TYPE_LONG_BLOB   = 0xfb
	MYSQL_TYPE_BLOB        = 0xfc // Blob
	MYSQL_TYPE_VAR_STRING  = 0xfd // []byte
	MYSQL_TYPE_STRING      = 0xfe // string
	MYSQL_TYPE_GEOMETRY    = 0xff

	MYSQL_UNSIGNED_MASK = uint16(1 << 15)
)

MySQL protocol types.

mymysql uses only some of them for send data to the MySQL server. Used MySQL types are marked with a comment contains mymysql type that uses it.

View Source
const (
	// Client send and receive, mymysql representation for send / receive
	TINYINT   = MYSQL_TYPE_TINY      // int8 / int8
	SMALLINT  = MYSQL_TYPE_SHORT     // int16 / int16
	INT       = MYSQL_TYPE_LONG      // int32 / int32
	BIGINT    = MYSQL_TYPE_LONGLONG  // int64 / int64
	FLOAT     = MYSQL_TYPE_FLOAT     // float32 / float32
	DOUBLE    = MYSQL_TYPE_DOUBLE    // float64 / float32
	TIME      = MYSQL_TYPE_TIME      // Time / Time
	DATE      = MYSQL_TYPE_DATE      // Date / Date
	DATETIME  = MYSQL_TYPE_DATETIME  // time.Time / time.Time
	TIMESTAMP = MYSQL_TYPE_TIMESTAMP // Timestamp / time.Time
	CHAR      = MYSQL_TYPE_STRING    // string / []byte
	BLOB      = MYSQL_TYPE_BLOB      // Blob / []byte
	NULL      = MYSQL_TYPE_NULL      // nil

	// Client send only, mymysql representation for send
	OUT_TEXT      = MYSQL_TYPE_STRING // string
	OUT_VARCHAR   = MYSQL_TYPE_STRING // string
	OUT_BINARY    = MYSQL_TYPE_BLOB   // Blob
	OUT_VARBINARY = MYSQL_TYPE_BLOB   // Blob

	// Client receive only, mymysql representation for receive
	IN_MEDIUMINT  = MYSQL_TYPE_LONG        // int32
	IN_YEAR       = MYSQL_TYPE_SHORT       // int16
	IN_BINARY     = MYSQL_TYPE_STRING      // []byte
	IN_VARCHAR    = MYSQL_TYPE_VAR_STRING  // []byte
	IN_VARBINARY  = MYSQL_TYPE_VAR_STRING  // []byte
	IN_TINYBLOB   = MYSQL_TYPE_TINY_BLOB   // []byte
	IN_TINYTEXT   = MYSQL_TYPE_TINY_BLOB   // []byte
	IN_TEXT       = MYSQL_TYPE_BLOB        // []byte
	IN_MEDIUMBLOB = MYSQL_TYPE_MEDIUM_BLOB // []byte
	IN_MEDIUMTEXT = MYSQL_TYPE_MEDIUM_BLOB // []byte
	IN_LONGBLOB   = MYSQL_TYPE_LONG_BLOB   // []byte
	IN_LONGTEXT   = MYSQL_TYPE_LONG_BLOB   // []byte

	// MySQL 5.x specific
	IN_DECIMAL = MYSQL_TYPE_NEWDECIMAL // TODO
	IN_BIT     = MYSQL_TYPE_BIT        // []byte
)

Mapping of MySQL types to (prefered) protocol types. Use it if you create your own Raw value.

Comments contains corresponding types used by mymysql. string type may be replaced by []byte type and vice versa. []byte type is native for sending on a network, so any string is converted to it before sending. Than for better preformance use []byte.

Variables

View Source
var DefaultDialer mysql.Dialer = func(proto, laddr, raddr string,
	timeout time.Duration) (net.Conn, error) {

	if proto == "" {
		proto = "unix"
		if strings.IndexRune(raddr, ':') != -1 {
			proto = "tcp"
		}
	}

	d := &net.Dialer{Timeout: timeout}
	if laddr != "" {
		var err error
		switch proto {
		case "tcp", "tcp4", "tcp6":
			d.LocalAddr, err = net.ResolveTCPAddr(proto, laddr)
		case "unix":
			d.LocalAddr, err = net.ResolveTCPAddr(proto, laddr)
		default:
			err = net.UnknownNetworkError(proto)
		}
		if err != nil {
			return nil, err
		}
	}
	return d.Dial(proto, raddr)
}

Functions

func DecodeU16

func DecodeU16(buf []byte) uint16

func DecodeU24

func DecodeU24(buf []byte) uint32

func DecodeU32

func DecodeU32(buf []byte) uint32

func DecodeU64

func DecodeU64(buf []byte) (rv uint64)

func EncodeDate

func EncodeDate(buf []byte, d mysql.Date) int

func EncodeDuration added in v0.4.2

func EncodeDuration(buf []byte, d time.Duration) int

func EncodeTime

func EncodeTime(buf []byte, t time.Time) int

func EncodeU16

func EncodeU16(buf []byte, val uint16)

func EncodeU24

func EncodeU24(buf []byte, val uint32)

func EncodeU32

func EncodeU32(buf []byte, val uint32)

func EncodeU64

func EncodeU64(buf []byte, val uint64)

func NbinToNstr

func NbinToNstr(nbin *[]byte) *string

func New

func New(proto, laddr, raddr, user, passwd string, db ...string) mysql.Conn

Create new MySQL handler. The first three arguments are passed to net.Bind for create connection. user and passwd are for authentication. Optional db is database name (you may not specify it and use Use() method later).

func NstrToNbin

func NstrToNbin(nstr *string) *[]byte

Types

type Conn

type Conn struct {

	// Debug logging. You may change it at any time.
	Debug bool
	// contains filtered or unexported fields
}

MySQL connection handler

func (*Conn) Begin

func (my *Conn) Begin() (mysql.Transaction, error)

Starts a new transaction

func (*Conn) Clone added in v0.4.10

func (my *Conn) Clone() mysql.Conn

Creates new (not connected) connection using configuration from current connection.

func (*Conn) Close

func (my *Conn) Close() (err error)

Close connection to the server

func (*Conn) Connect

func (my *Conn) Connect() (err error)

Establishes a connection with MySQL server version 4.1 or later.

func (*Conn) Escape added in v1.2.1

func (my *Conn) Escape(txt string) string

Escapes special characters in the txt, so it is safe to place returned string to Query method.

func (*Conn) FullFieldInfo added in v1.4.1

func (my *Conn) FullFieldInfo(full bool)

func (*Conn) IsConnected

func (my *Conn) IsConnected() bool

Check if connection is established

func (*Conn) NarrowTypeSet added in v1.2.1

func (my *Conn) NarrowTypeSet(narrow bool)

func (*Conn) NetConn added in v1.5.1

func (my *Conn) NetConn() net.Conn

NetConn return internall net.Conn

func (*Conn) Ping

func (my *Conn) Ping() (err error)

Send MySQL PING to the server.

func (*Conn) Prepare

func (my *Conn) Prepare(sql string) (mysql.Stmt, error)

Prepare server side statement. Return statement handler.

func (*Conn) Query

func (my *Conn) Query(sql string, params ...interface{}) ([]mysql.Row, mysql.Result, error)

See mysql.Query

func (*Conn) QueryFirst added in v0.4.8

func (my *Conn) QueryFirst(sql string, params ...interface{}) (mysql.Row, mysql.Result, error)

See mysql.QueryFirst

func (*Conn) QueryLast added in v0.4.8

func (my *Conn) QueryLast(sql string, params ...interface{}) (mysql.Row, mysql.Result, error)

See mysql.QueryLast

func (*Conn) Reconnect

func (my *Conn) Reconnect() (err error)

Close and reopen connection. Ignore unreaded rows, reprepare all prepared statements.

func (*Conn) Register

func (my *Conn) Register(sql string)

Register MySQL command/query to be executed immediately after connecting to the server. You may register multiple commands. They will be executed in the order of registration. Yhis method is mainly useful for reconnect.

func (*Conn) SetDialer added in v1.5.1

func (my *Conn) SetDialer(d mysql.Dialer)

func (*Conn) SetMaxPktSize

func (my *Conn) SetMaxPktSize(new_size int) int

If new_size > 0 sets maximum packet size. Returns old size.

func (*Conn) SetTimeout added in v1.1.3

func (my *Conn) SetTimeout(timeout time.Duration)

SetTimeout sets timeout for Connect and Reconnect

func (*Conn) Start

func (my *Conn) Start(sql string, params ...interface{}) (res mysql.Result, err error)

Start new query.

If you specify the parameters, the SQL string will be a result of fmt.Sprintf(sql, params...). You must get all result rows (if they exists) before next query.

func (*Conn) ThreadId

func (my *Conn) ThreadId() uint32

Returns the thread ID of the current connection.

func (*Conn) Use

func (my *Conn) Use(dbname string) (err error)

Change database

type Result

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

func (*Result) AffectedRows

func (res *Result) AffectedRows() uint64

func (*Result) End

func (res *Result) End() error

See mysql.End

func (*Result) Fields

func (res *Result) Fields() []*mysql.Field

Returns a table containing descriptions of the columns

func (*Result) GetFirstRow added in v0.4.8

func (res *Result) GetFirstRow() (mysql.Row, error)

See mysql.GetFirstRow

func (*Result) GetLastRow added in v0.4.8

func (res *Result) GetLastRow() (mysql.Row, error)

See mysql.GetLastRow

func (*Result) GetRow

func (res *Result) GetRow() (mysql.Row, error)

Like ScanRow but allocates memory for every row. Returns nil row insted of io.EOF error.

func (*Result) GetRows

func (res *Result) GetRows() ([]mysql.Row, error)

See mysql.GetRows

func (*Result) InsertId

func (res *Result) InsertId() uint64

func (*Result) MakeRow added in v0.4.7

func (res *Result) MakeRow() mysql.Row

func (*Result) Map

func (res *Result) Map(field_name string) int

Returns index for given name or -1 if field of that name doesn't exist

func (*Result) Message

func (res *Result) Message() string

func (*Result) MoreResults

func (res *Result) MoreResults() bool

Returns true if more results exixts. You don't have to call it before NextResult method (NextResult returns nil if there is no more results).

func (*Result) NextResult

func (res *Result) NextResult() (mysql.Result, error)

This function is used when last query was the multi result query or procedure call. Returns the next result or nil if no more resuts exists.

Statements within the procedure may produce unknown number of result sets. The final result from the procedure is a status result that includes no result set (Result.StatusOnly() == true) .

func (*Result) ScanRow added in v0.4.7

func (res *Result) ScanRow(row mysql.Row) error

Get the data row from server. This method reads one row of result set directly from network connection (without rows buffering on client side). Returns io.EOF if there is no more rows in current result set.

func (*Result) StatusOnly added in v0.4.6

func (res *Result) StatusOnly() bool

Returns true if this is status result that includes no result set

func (*Result) WarnCount

func (res *Result) WarnCount() int

type Stmt

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

func (*Stmt) Bind added in v0.4.2

func (stmt *Stmt) Bind(params ...interface{})

Bind input data for the parameter markers in the SQL statement that was passed to Prepare.

params may be a parameter list (slice), a struct or a pointer to the struct. A struct field can by value or pointer to value. A parameter (slice element) can be value, pointer to value or pointer to pointer to value. Values may be of the folowind types: intXX, uintXX, floatXX, bool, []byte, Blob, string, Time, Date, Time, Timestamp, Raw.

func (*Stmt) Delete

func (stmt *Stmt) Delete() (err error)

Destroy statement on server side. Client side handler is invalid after this command.

func (*Stmt) Exec

func (stmt *Stmt) Exec(params ...interface{}) ([]mysql.Row, mysql.Result, error)

See mysql.Exec

func (*Stmt) ExecFirst added in v0.4.8

func (stmt *Stmt) ExecFirst(params ...interface{}) (mysql.Row, mysql.Result, error)

See mysql.ExecFirst

func (*Stmt) ExecLast added in v0.4.8

func (stmt *Stmt) ExecLast(params ...interface{}) (mysql.Row, mysql.Result, error)

See mysql.ExecLast

func (*Stmt) Fields added in v1.4.1

func (stmt *Stmt) Fields() []*mysql.Field

func (*Stmt) NumParam

func (stmt *Stmt) NumParam() int

func (*Stmt) Reset

func (stmt *Stmt) Reset() (err error)

Resets a prepared statement on server: data sent to the server, unbuffered result sets and current errors.

func (*Stmt) Run

func (stmt *Stmt) Run(params ...interface{}) (res mysql.Result, err error)

Execute prepared statement. If statement requires parameters you may bind them first or specify directly. After this command you may use GetRow to retrieve data.

func (*Stmt) SendLongData

func (stmt *Stmt) SendLongData(pnum int, data interface{}, pkt_size int) (err error)

Send long data to MySQL server in chunks. You can call this method after Bind and before Exec. It can be called multiple times for one parameter to send TEXT or BLOB data in chunks.

pnum - Parameter number to associate the data with.

data - Data source string, []byte or io.Reader.

pkt_size - It must be must be greater than 6 and less or equal to MySQL max_allowed_packet variable. You can obtain value of this variable using such query: SHOW variables WHERE Variable_name = 'max_allowed_packet' If data source is io.Reader then (pkt_size - 6) is size of a buffer that will be allocated for reading.

If you have data source of type string or []byte in one piece you may properly set pkt_size and call this method once. If you have data in multiple pieces you can call this method multiple times. If data source is io.Reader you should properly set pkt_size. Data will be readed from io.Reader and send in pieces to the server until EOF.

func (*Stmt) WarnCount

func (stmt *Stmt) WarnCount() int

type Transaction

type Transaction struct {
	*Conn
}

func (Transaction) Commit

func (tr Transaction) Commit() error

Commit a transaction

func (Transaction) Do

func (tr Transaction) Do(st mysql.Stmt) mysql.Stmt

Binds statement to the context of transaction. For native engine this is identity function.

func (Transaction) IsValid added in v1.0.1

func (tr Transaction) IsValid() bool

func (Transaction) Rollback

func (tr Transaction) Rollback() error

Rollback a transaction

Jump to

Keyboard shortcuts

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