123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- // 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 (
- "database/sql/driver"
- "errors"
- "net"
- "strings"
- "time"
- )
- type mysqlConn struct {
- cfg *config
- flags clientFlag
- charset byte
- cipher []byte
- netConn net.Conn
- buf *buffer
- protocol uint8
- sequence uint8
- affectedRows uint64
- insertId uint64
- maxPacketAllowed int
- maxWriteSize int
- parseTime bool
- strict bool
- }
- type config struct {
- user string
- passwd string
- net string
- addr string
- dbname string
- params map[string]string
- loc *time.Location
- }
- // Handles parameters set in DSN
- func (mc *mysqlConn) handleParams() (err error) {
- for param, val := range mc.cfg.params {
- switch param {
- // Charset
- case "charset":
- charsets := strings.Split(val, ",")
- for i := range charsets {
- // ignore errors here - a charset may not exist
- err = mc.exec("SET NAMES " + charsets[i])
- if err == nil {
- break
- }
- }
- if err != nil {
- return
- }
- // handled elsewhere
- case "timeout", "allowAllFiles", "loc":
- continue
- // time.Time parsing
- case "parseTime":
- mc.parseTime = readBool(val)
- // Strict mode
- case "strict":
- mc.strict = readBool(val)
- // TLS-Encryption
- case "tls":
- err = errors.New("TLS-Encryption not implemented yet")
- return
- // Compression
- case "compress":
- err = errors.New("Compression not implemented yet")
- // System Vars
- default:
- err = mc.exec("SET " + param + "=" + val + "")
- if err != nil {
- return
- }
- }
- }
- return
- }
- func (mc *mysqlConn) Begin() (driver.Tx, error) {
- if mc.netConn == nil {
- return nil, errInvalidConn
- }
- err := mc.exec("START TRANSACTION")
- if err == nil {
- return &mysqlTx{mc}, err
- }
- return nil, err
- }
- func (mc *mysqlConn) Close() (err error) {
- // Makes Close idempotent
- if mc.netConn != nil {
- mc.writeCommandPacket(comQuit)
- mc.netConn.Close()
- mc.netConn = nil
- }
- mc.cfg = nil
- mc.buf = nil
- return
- }
- func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
- if mc.netConn == nil {
- return nil, errInvalidConn
- }
- // Send command
- err := mc.writeCommandPacketStr(comStmtPrepare, query)
- if err != nil {
- return nil, err
- }
- stmt := &mysqlStmt{
- mc: mc,
- }
- // Read Result
- columnCount, err := stmt.readPrepareResultPacket()
- if err == nil {
- if stmt.paramCount > 0 {
- stmt.params, err = stmt.mc.readColumns(stmt.paramCount)
- if err != nil {
- return nil, err
- }
- }
- if columnCount > 0 {
- err = stmt.mc.readUntilEOF()
- }
- }
- return stmt, err
- }
- func (mc *mysqlConn) Exec(query string, args []driver.Value) (driver.Result, error) {
- if mc.netConn == nil {
- return nil, errInvalidConn
- }
- if len(args) == 0 { // no args, fastpath
- mc.affectedRows = 0
- mc.insertId = 0
- err := mc.exec(query)
- if err == nil {
- return &mysqlResult{
- affectedRows: int64(mc.affectedRows),
- insertId: int64(mc.insertId),
- }, err
- }
- return nil, err
- }
- // with args, must use prepared stmt
- return nil, driver.ErrSkip
- }
- // Internal function to execute commands
- func (mc *mysqlConn) exec(query string) (err error) {
- // Send command
- err = mc.writeCommandPacketStr(comQuery, query)
- if err != nil {
- return
- }
- // Read Result
- var resLen int
- resLen, err = mc.readResultSetHeaderPacket()
- if err == nil && resLen > 0 {
- err = mc.readUntilEOF()
- if err != nil {
- return
- }
- err = mc.readUntilEOF()
- }
- return
- }
- func (mc *mysqlConn) Query(query string, args []driver.Value) (driver.Rows, error) {
- if mc.netConn == nil {
- return nil, errInvalidConn
- }
- if len(args) == 0 { // no args, fastpath
- // Send command
- err := mc.writeCommandPacketStr(comQuery, query)
- if err == nil {
- // Read Result
- var resLen int
- resLen, err = mc.readResultSetHeaderPacket()
- if err == nil {
- rows := &mysqlRows{mc, false, nil, false}
- if resLen > 0 {
- // Columns
- rows.columns, err = mc.readColumns(resLen)
- }
- return rows, err
- }
- }
- return nil, err
- }
- // with args, must use prepared stmt
- return nil, driver.ErrSkip
- }
- // Gets the value of the given MySQL System Variable
- // The returned byte slice is only valid until the next read
- func (mc *mysqlConn) getSystemVar(name string) (val []byte, err error) {
- // Send command
- err = mc.writeCommandPacketStr(comQuery, "SELECT @@"+name)
- if err == nil {
- // Read Result
- var resLen int
- resLen, err = mc.readResultSetHeaderPacket()
- if err == nil {
- rows := &mysqlRows{mc, false, nil, false}
- if resLen > 0 {
- // Columns
- rows.columns, err = mc.readColumns(resLen)
- }
- dest := make([]driver.Value, resLen)
- err = rows.readRow(dest)
- if err == nil {
- val = dest[0].([]byte)
- err = mc.readUntilEOF()
- }
- }
- }
- return
- }
|