| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
- //
- // 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
- import (
- "bufio"
- "database/sql/driver"
- "errors"
- "net"
- "strconv"
- "strings"
- "time"
- )
- type mysqlConn struct {
- cfg *config
- server *serverSettings
- netConn net.Conn
- bufReader *bufio.Reader
- protocol uint8
- sequence uint8
- affectedRows uint64
- insertId uint64
- lastCmdTime time.Time
- keepaliveTimer *time.Timer
- }
- type config struct {
- user string
- passwd string
- net string
- addr string
- dbname string
- params map[string]string
- }
- type serverSettings struct {
- protocol byte
- version string
- flags ClientFlag
- charset uint8
- scrambleBuff []byte
- threadID uint32
- keepalive int64
- }
- // Handles parameters set in DSN
- func (mc *mysqlConn) handleParams() (e error) {
- for param, val := range mc.cfg.params {
- switch param {
- // Charset
- case "charset":
- charsets := strings.Split(val, ",")
- for _, charset := range charsets {
- e = mc.exec("SET NAMES " + charset)
- if e == nil {
- break
- }
- }
- if e != nil {
- return
- }
- // TLS-Encryption
- case "tls":
- dbgLog.Print("TLS-Encryption not implemented yet")
- // Compression
- case "compress":
- dbgLog.Print("Compression not implemented yet")
- // We don't want to set keepalive as system var
- case "keepalive":
- continue
- // System Vars
- default:
- e = mc.exec("SET " + param + "=" + val + "")
- if e != nil {
- return
- }
- }
- }
- // KeepAlive
- if val, param := mc.cfg.params["keepalive"]; param {
- mc.server.keepalive, e = strconv.ParseInt(val, 10, 64)
- if e != nil {
- return errors.New("Invalid keepalive time")
- }
- // Get keepalive time by MySQL system var wait_timeout
- if mc.server.keepalive == 1 {
- val, e = mc.getSystemVar("wait_timeout")
- mc.server.keepalive, e = strconv.ParseInt(val, 10, 64)
- if e != nil {
- return errors.New("Error getting wait_timeout")
- }
- // Trigger 1min BEFORE wait_timeout
- if mc.server.keepalive > 60 {
- mc.server.keepalive -= 60
- }
- }
- if mc.server.keepalive > 0 {
- mc.lastCmdTime = time.Now()
- // Ping-Timer to avoid timeout
- mc.keepaliveTimer = time.AfterFunc(
- time.Duration(mc.server.keepalive)*time.Second, func() {
- var diff time.Duration
- for {
- // Fires only if diff > keepalive. Makes it collision safe
- for mc.netConn != nil &&
- mc.lastCmdTime.Unix()+mc.server.keepalive > time.Now().Unix() {
- diff = mc.lastCmdTime.Sub(time.Unix(time.Now().Unix()-mc.server.keepalive, 0))
- time.Sleep(diff)
- }
- if mc.netConn != nil {
- if e := mc.Ping(); e != nil {
- break
- }
- } else {
- return
- }
- }
- })
- }
- }
- return
- }
- func (mc *mysqlConn) Begin() (driver.Tx, error) {
- e := mc.exec("START TRANSACTION")
- if e != nil {
- return nil, e
- }
- return &mysqlTx{mc}, e
- }
- func (mc *mysqlConn) Close() (e error) {
- if mc.server.keepalive > 0 {
- mc.keepaliveTimer.Stop()
- }
- mc.writeCommandPacket(COM_QUIT)
- mc.bufReader = nil
- mc.netConn.Close()
- mc.netConn = nil
- return
- }
- func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
- // Send command
- e := mc.writeCommandPacket(COM_STMT_PREPARE, query)
- if e != nil {
- return nil, e
- }
- stmt := mysqlStmt{new(stmtContent)}
- stmt.mc = mc
- // Read Result
- var columnCount uint16
- columnCount, e = stmt.readPrepareResultPacket()
- if e != nil {
- return nil, e
- }
- if stmt.paramCount > 0 {
- stmt.params, e = stmt.mc.readColumns(stmt.paramCount)
- if e != nil {
- return nil, e
- }
- }
- if columnCount > 0 {
- _, e = stmt.mc.readUntilEOF()
- if e != nil {
- return nil, e
- }
- }
- return stmt, e
- }
- func (mc *mysqlConn) Exec(query string, args []driver.Value) (driver.Result, error) {
- if len(args) > 0 {
- return nil, driver.ErrSkip
- }
- mc.affectedRows = 0
- mc.insertId = 0
- e := mc.exec(query)
- if e != nil {
- return nil, e
- }
- return &mysqlResult{
- affectedRows: int64(mc.affectedRows),
- insertId: int64(mc.insertId)},
- e
- }
- // Internal function to execute commands
- func (mc *mysqlConn) exec(query string) (e error) {
- // Send command
- e = mc.writeCommandPacket(COM_QUERY, query)
- if e != nil {
- return
- }
- // Read Result
- var resLen int
- resLen, e = mc.readResultSetHeaderPacket()
- if e != nil {
- return
- }
- if resLen > 0 {
- _, e = mc.readUntilEOF()
- if e != nil {
- return
- }
- mc.affectedRows, e = mc.readUntilEOF()
- if e != nil {
- return
- }
- }
- return
- }
- func (mc *mysqlConn) Query(query string, args []driver.Value) (driver.Rows, error) {
- if len(args) > 0 {
- return nil, driver.ErrSkip
- }
- // Send command
- e := mc.writeCommandPacket(COM_QUERY, query)
- if e != nil {
- return nil, e
- }
- // Read Result
- var resLen int
- resLen, e = mc.readResultSetHeaderPacket()
- if e != nil {
- return nil, e
- }
- rows := mysqlRows{&rowsContent{mc, false, nil, false}}
- if resLen > 0 {
- // Columns
- rows.content.columns, e = mc.readColumns(resLen)
- if e != nil {
- return nil, e
- }
- }
- return rows, e
- }
- // Gets the value of the given MySQL System Variable
- func (mc *mysqlConn) getSystemVar(name string) (val string, e error) {
- // Send command
- e = mc.writeCommandPacket(COM_QUERY, "SELECT @@"+name)
- if e != nil {
- return
- }
- // Read Result
- resLen, e := mc.readResultSetHeaderPacket()
- if e != nil {
- return
- }
- if resLen > 0 {
- var n uint64
- n, e = mc.readUntilEOF()
- if e != nil {
- return
- }
- var row *[]*[]byte
- row, e = mc.readRow(int(n))
- if e != nil {
- return
- }
- _, e = mc.readUntilEOF()
- if e != nil {
- return
- }
- val = string(*(*row)[0])
- }
- return
- }
- // *** DEPRECATED ***
- // Executes a simple Ping-CMD to test or keepalive the connection
- func (mc *mysqlConn) Ping() (e error) {
- // Send command
- e = mc.writeCommandPacket(COM_PING)
- if e != nil {
- return
- }
- // Read Result
- e = mc.readResultOK()
- return
- }
|