Documentation ¶
Overview ¶
Thread unsafe engine for MyMySQL
Index ¶
- Constants
- Variables
- func DecodeU16(buf []byte) uint16
- func DecodeU24(buf []byte) uint32
- func DecodeU32(buf []byte) uint32
- func DecodeU64(buf []byte) (rv uint64)
- func EncodeDate(buf []byte, d mysql.Date) int
- func EncodeDuration(buf []byte, d time.Duration) int
- func EncodeTime(buf []byte, t time.Time) int
- func EncodeU16(buf []byte, val uint16)
- func EncodeU24(buf []byte, val uint32)
- func EncodeU32(buf []byte, val uint32)
- func EncodeU64(buf []byte, val uint64)
- func NbinToNstr(nbin *[]byte) *string
- func New(proto, laddr, raddr, user, passwd string, db ...string) mysql.Conn
- func NstrToNbin(nstr *string) *[]byte
- type Conn
- func (my *Conn) Begin() (mysql.Transaction, error)
- func (my *Conn) Clone() mysql.Conn
- func (my *Conn) Close() (err error)
- func (my *Conn) Connect() (err error)
- func (my *Conn) Credentials() (user, passwd string)
- func (my *Conn) Escape(txt string) string
- func (my *Conn) FullFieldInfo(full bool)
- func (my *Conn) IsConnected() bool
- func (my *Conn) NarrowTypeSet(narrow bool)
- func (my *Conn) NetConn() net.Conn
- func (my *Conn) Ping() (err error)
- func (my *Conn) Prepare(sql string) (mysql.Stmt, error)
- func (my *Conn) Query(sql string, params ...interface{}) ([]mysql.Row, mysql.Result, error)
- func (my *Conn) QueryFirst(sql string, params ...interface{}) (mysql.Row, mysql.Result, error)
- func (my *Conn) QueryLast(sql string, params ...interface{}) (mysql.Row, mysql.Result, error)
- func (my *Conn) Reconnect() (err error)
- func (my *Conn) Register(sql string)
- func (my *Conn) SetDialer(d mysql.Dialer)
- func (my *Conn) SetMaxPktSize(new_size int) int
- func (my *Conn) SetTimeout(timeout time.Duration)
- func (my *Conn) Start(sql string, params ...interface{}) (res mysql.Result, err error)
- func (my *Conn) Status() mysql.ConnStatus
- func (my *Conn) ThreadId() uint32
- func (my *Conn) Use(dbname string) (err error)
- type Result
- func (res *Result) AffectedRows() uint64
- func (res *Result) End() error
- func (res *Result) Fields() []*mysql.Field
- func (res *Result) GetFirstRow() (mysql.Row, error)
- func (res *Result) GetLastRow() (mysql.Row, error)
- func (res *Result) GetRow() (mysql.Row, error)
- func (res *Result) GetRows() ([]mysql.Row, error)
- func (res *Result) InsertId() uint64
- func (res *Result) MakeRow() mysql.Row
- func (res *Result) Map(field_name string) int
- func (res *Result) Message() string
- func (res *Result) MoreResults() bool
- func (res *Result) NextResult() (mysql.Result, error)
- func (res *Result) ScanRow(row mysql.Row) error
- func (res *Result) StatusOnly() bool
- func (res *Result) WarnCount() int
- type Stmt
- func (stmt *Stmt) Bind(params ...interface{})
- func (stmt *Stmt) Delete() (err error)
- func (stmt *Stmt) Exec(params ...interface{}) ([]mysql.Row, mysql.Result, error)
- func (stmt *Stmt) ExecFirst(params ...interface{}) (mysql.Row, mysql.Result, error)
- func (stmt *Stmt) ExecLast(params ...interface{}) (mysql.Row, mysql.Result, error)
- func (stmt *Stmt) Fields() []*mysql.Field
- func (stmt *Stmt) NumParam() int
- func (stmt *Stmt) Reset() (err error)
- func (stmt *Stmt) Run(params ...interface{}) (res mysql.Result, err error)
- func (stmt *Stmt) SendLongData(pnum int, data interface{}, pkt_size int) (err error)
- func (stmt *Stmt) WarnCount() int
- type Transaction
Constants ¶
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.
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 ¶
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 NbinToNstr ¶
func New ¶
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 ¶
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) Clone ¶
Creates new (not connected) connection using configuration from current connection.
func (*Conn) Credentials ¶
func (*Conn) Escape ¶
Escapes special characters in the txt, so it is safe to place returned string to Query method.
func (*Conn) FullFieldInfo ¶
func (*Conn) NarrowTypeSet ¶
func (*Conn) QueryFirst ¶
See mysql.QueryFirst
func (*Conn) Reconnect ¶
Close and reopen connection. Ignore unreaded rows, reprepare all prepared statements.
func (*Conn) Register ¶
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) SetMaxPktSize ¶
If new_size > 0 sets maximum packet size. Returns old size.
func (*Conn) SetTimeout ¶
SetTimeout sets timeout for Connect and Reconnect
func (*Conn) Start ¶
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) Status ¶
func (my *Conn) Status() mysql.ConnStatus
type Result ¶
type Result struct {
// contains filtered or unexported fields
}
func (*Result) AffectedRows ¶
func (*Result) GetFirstRow ¶
See mysql.GetFirstRow
func (*Result) GetRow ¶
Like ScanRow but allocates memory for every row. Returns nil row insted of io.EOF error.
func (*Result) MoreResults ¶
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 ¶
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 ¶
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 ¶
Returns true if this is status result that includes no result set
type Stmt ¶
type Stmt struct {
// contains filtered or unexported fields
}
func (*Stmt) Bind ¶
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 ¶
Destroy statement on server side. Client side handler is invalid after this command.
func (*Stmt) Reset ¶
Resets a prepared statement on server: data sent to the server, unbuffered result sets and current errors.
func (*Stmt) Run ¶
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 ¶
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.
type Transaction ¶
type Transaction struct {
*Conn
}
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 ¶
func (tr Transaction) IsValid() bool