mysql

package module
v0.0.0-...-eacef60 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2014 License: BSD-3-Clause Imports: 10 Imported by: 1

README

Note

This driver is only here for historic purposes.

For a more fully fetured mysql driver in go use:

https://github.com/go-sql-driver/mysql

Project Goal

The goal of this project is to implement the MySQL wire protocol in Go, mostly for my own amusement but it might become usable as a client library for other Go projects.

The wire protocol is documented here

Status

  • Most queries work
  • Server side prepared statements work. (With common types)
  • See example/simple.go for simple example
  • See example/prepared.go for example using server side prepared statements

For a more fully fetured mysql driver in go use:

https://github.com/ziutek/mymysql

Install

$ go get github.com/thoj/go-mysqlpure

Use

Three first 2 parameters are passed to Dial. Unix socket: net = unix, raddr = path to mysql.sock

dbh, error = mysql.Connect(net, raddr, username, password, database)

Select database

res, err = dbh.Use(database)

Run simple Query. AffectedRows and InsertId is in res

res, err = dbh.Query(sql)

Prepare server side statement

sth, err = dbh.Prepare(<SQL with ? placeholders>)

Execute prepared statement (Only supports string, int, float parameters):

res, err = sth.Execute(parameters ...)

Fetch row from query with result set

row, err = res.FetchRow()

Fetch one row as map[string]string

rowmap = res.FetchRowMap()

Fetch all rows as []map[string]string

rowsmap = res.FetchAllRowMap()

FAQ

  • Q: I'm getting question marks instead of my Unicode characters
  • A: Run dbh.Query("SET NAMES utf8") before the select query

Documentation

Index

Constants

View Source
const (
	CLIENT_LONG_PASSWORD     ClientFlags = 1      /* new more secure passwords */
	CLIENT_FOUND_ROWS                    = 2      /* Found instead of affected rows */
	CLIENT_LONG_FLAG                     = 4      /* Get all column flags */
	CLIENT_CONNECT_WITH_DB               = 8      /* One can specify db on connect */
	CLIENT_NO_SCHEMA                     = 16     /* Don't allow database.table.column */
	CLIENT_COMPRESS                      = 32     /* Can use compression protocol */
	CLIENT_ODBC                          = 64     /* Odbc client */
	CLIENT_LOCAL_FILES                   = 128    /* Can use LOAD DATA LOCAL */
	CLIENT_IGNORE_SPACE                  = 256    /* Ignore spaces before '(' */
	CLIENT_PROTOCOL_41                   = 512    /* New 4.1 protocol */
	CLIENT_INTERACTIVE                   = 1024   /* This is an interactive client */
	CLIENT_SSL                           = 2048   /* Switch to SSL after handshake */
	CLIENT_IGNORE_SIGPIPE                = 4096   /* IGNORE sigpipes */
	CLIENT_TRANSACTIONS                  = 8192   /* Client knows about transactions */
	CLIENT_RESERVED                      = 16384  /* Old flag for 4.1 protocol  */
	CLIENT_SECURE_CONNECTION             = 32768  /* New 4.1 authentication */
	CLIENT_MULTI_STATEMENTS              = 65536  /* Enable/disable multi-stmt support */
	CLIENT_MULTI_RESULTS                 = 131072 /* Enable/disable multi-results */
)
View Source
const (
	MYSQL_TYPE_DECIMAL uint8 = iota
	MYSQL_TYPE_TINY
	MYSQL_TYPE_SHORT
	MYSQL_TYPE_LONG
	MYSQL_TYPE_FLOAT
	MYSQL_TYPE_DOUBLE
	MYSQL_TYPE_NULL
	MYSQL_TYPE_TIMESTAMP
	MYSQL_TYPE_LONGLONG
	MYSQL_TYPE_INT24
	MYSQL_TYPE_DATE
	MYSQL_TYPE_TIME
	MYSQL_TYPE_DATETIME
	MYSQL_TYPE_YEAR
	MYSQL_TYPE_NEWDATE
	MYSQL_TYPE_VARCHAR
	MYSQL_TYPE_BIT
	MYSQL_TYPE_NEWDECIMAL  = 246
	MYSQL_TYPE_ENUM        = 247
	MYSQL_TYPE_SET         = 248
	MYSQL_TYPE_TINY_BLOB   = 249
	MYSQL_TYPE_MEDIUM_BLOB = 250
	MYSQL_TYPE_LONG_BLOB   = 251
	MYSQL_TYPE_BLOB        = 252
	MYSQL_TYPE_VAR_STRING  = 253
	MYSQL_TYPE_STRING      = 254
	MYSQL_TYPE_GEOMETRY    = 255
)
View Source
const (
	MAX_PACKET_SIZE = (1 << 24)
)
View Source
const (
	PRE_ALLOCATE = 30
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ClientFlags

type ClientFlags uint32

type MySQLCommand

type MySQLCommand uint32
const (
	COM_SLEEP               MySQLCommand = iota //(none, this is an internal thread state)
	COM_QUIT                                    //mysql_close
	COM_INIT_DB                                 //mysql_select_db
	COM_QUERY                                   //mysql_real_query
	COM_FIELD_LIST                              //mysql_list_fields
	COM_CREATE_DB                               //mysql_create_db (deprecated)
	COM_DROP_DB                                 //mysql_drop_db (deprecated)
	COM_REFRESH                                 //mysql_refresh
	COM_SHUTDOWN                                //mysql_shutdown
	COM_STATISTICS                              //mysql_stat
	COM_PROCESS_INFO                            //mysql_list_processes
	COM_CONNECT                                 //(none, this is an internal thread state)
	COM_PROCESS_KILL                            //mysql_kill
	COM_DEBUG                                   //mysql_dump_debug_info
	COM_PING                                    //mysql_ping
	COM_TIME                                    //(none, this is an internal thread state)
	COM_DELAYED_INSERT                          //(none, this is an internal thread state)
	COM_CHANGE_USER                             //mysql_change_user
	COM_BINLOG_DUMP                             //sent by the slave IO thread to request a binlog
	COM_TABLE_DUMP                              //LOAD TABLE ... FROM MASTER (deprecated)
	COM_CONNECT_OUT                             //(none, this is an internal thread state)
	COM_REGISTER_SLAVE                          //sent by the slave to register with the master (optional)
	COM_STMT_PREPARE                            //mysql_stmt_prepare
	COM_STMT_EXECUTE                            //mysql_stmt_execute
	COM_STMT_SEND_LONG_DATA                     //mysql_stmt_send_long_data
	COM_STMT_CLOSE                              //mysql_stmt_close
	COM_STMT_RESET                              //mysql_stmt_reset
	COM_SET_OPTION                              //mysql_set_server_option
	COM_STMT_FETCH                              //mysql_stmt_fetch
)

type MySQLData

type MySQLData struct {
	Data   string
	Length uint64
	IsNull bool
	Type   uint8
}

func (*MySQLData) String

func (d *MySQLData) String() string

type MySQLField

type MySQLField struct {
	Catalog  string
	Db       string
	Table    string
	OrgTable string
	Name     string
	OrgName  string

	Charset  uint16
	Length   uint32
	Type     uint8
	Flags    uint16
	Decimals uint8
	Default  uint64
}

func (*MySQLField) String

func (f *MySQLField) String() string

type MySQLInstance

type MySQLInstance struct {
	ProtocolVersion    uint8  // Protocol version = 0x10
	ServerVersion      string // Server string
	ThreadId           uint32 // Current Thread ID
	ServerCapabilities uint16
	ServerLanguage     uint8
	ServerStatus       uint16

	Connected bool
	// contains filtered or unexported fields
}

func Connect

func Connect(netstr string, raddrstr string, username string, password string, database string) (*MySQLInstance, error)

Connects to mysql server and reads the initial handshake, then tries to login using supplied credentials. The first 2 parameters are passed directly to Dial

func (*MySQLInstance) Prepare

func (dbh *MySQLInstance) Prepare(arg string) (*MySQLStatement, error)

func (*MySQLInstance) Query

func (dbh *MySQLInstance) Query(arg string) (*MySQLResponse, error)

Send query to server and read response. Return response object.

func (*MySQLInstance) Quit

func (dbh *MySQLInstance) Quit()

func (*MySQLInstance) Use

func (dbh *MySQLInstance) Use(arg string) (*MySQLResponse, error)

type MySQLResponse

type MySQLResponse struct {
	FieldCount   uint64
	AffectedRows uint64
	InsertId     uint64
	ServerStatus uint16
	WarningCount uint16
	Message      string
	EOF          bool
	Prepared     bool //Result from prapered statement

	ResultSet *MySQLResultSet
	// contains filtered or unexported fields
}

func (*MySQLResponse) FetchAllRowMap

func (rs *MySQLResponse) FetchAllRowMap() []map[string]string

Fetches all rows from result

func (*MySQLResponse) FetchRow

func (rs *MySQLResponse) FetchRow() (*MySQLRow, error)

Fetch next row.

func (*MySQLResponse) FetchRowMap

func (rs *MySQLResponse) FetchRowMap() map[string]string

Fetch next row map.

func (*MySQLResponse) String

func (r *MySQLResponse) String() string

type MySQLResultSet

type MySQLResultSet struct {
	FieldCount uint64
	Fields     []*MySQLField
	Rows       []*MySQLRow
}

type MySQLRow

type MySQLRow struct {
	Data []*MySQLData
}

type MySQLStatement

type MySQLStatement struct {
	StatementId uint32
	Columns     uint16
	Parameters  uint16
	Warnings    uint16
	FieldCount  uint8

	ResultSet *MySQLResultSet
	// contains filtered or unexported fields
}

func (*MySQLStatement) Execute

func (sth *MySQLStatement) Execute(a ...interface{}) (*MySQLResponse, error)

func (*MySQLStatement) String

func (s *MySQLStatement) String() string

type PacketHeader

type PacketHeader struct {
	Len uint64
	Seq uint8
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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