|
| 1 | +// Go MySQL Driver - A MySQL-Driver for Go's database/sql package |
| 2 | +// |
| 3 | +// Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved. |
| 4 | +// |
| 5 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | +// License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 7 | +// You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | + |
| 9 | +package mysql |
| 10 | + |
| 11 | +import ( |
| 12 | + "crypto/tls" |
| 13 | + "database/sql/driver" |
| 14 | + "errors" |
| 15 | + "net" |
| 16 | + "strings" |
| 17 | + "time" |
| 18 | +) |
| 19 | + |
| 20 | +type mysqlConn struct { |
| 21 | + buf *buffer |
| 22 | + netConn net.Conn |
| 23 | + affectedRows uint64 |
| 24 | + insertId uint64 |
| 25 | + cfg *config |
| 26 | + maxPacketAllowed int |
| 27 | + maxWriteSize int |
| 28 | + flags clientFlag |
| 29 | + sequence uint8 |
| 30 | + parseTime bool |
| 31 | + strict bool |
| 32 | + startTx bool |
| 33 | +} |
| 34 | + |
| 35 | +type config struct { |
| 36 | + user string |
| 37 | + passwd string |
| 38 | + net string |
| 39 | + addr string |
| 40 | + dbname string |
| 41 | + params map[string]string |
| 42 | + loc *time.Location |
| 43 | + timeout time.Duration |
| 44 | + tls *tls.Config |
| 45 | + allowAllFiles bool |
| 46 | + allowOldPasswords bool |
| 47 | + clientFoundRows bool |
| 48 | +} |
| 49 | + |
| 50 | +// Handles parameters set in DSN |
| 51 | +func (mc *mysqlConn) handleParams() (err error) { |
| 52 | + for param, val := range mc.cfg.params { |
| 53 | + switch param { |
| 54 | + // Charset |
| 55 | + case "charset": |
| 56 | + charsets := strings.Split(val, ",") |
| 57 | + for i := range charsets { |
| 58 | + // ignore errors here - a charset may not exist |
| 59 | + err = mc.exec("SET NAMES " + charsets[i]) |
| 60 | + if err == nil { |
| 61 | + break |
| 62 | + } |
| 63 | + } |
| 64 | + if err != nil { |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + // time.Time parsing |
| 69 | + case "parseTime": |
| 70 | + var isBool bool |
| 71 | + mc.parseTime, isBool = readBool(val) |
| 72 | + if !isBool { |
| 73 | + return errors.New("Invalid Bool value: " + val) |
| 74 | + } |
| 75 | + |
| 76 | + // Strict mode |
| 77 | + case "strict": |
| 78 | + var isBool bool |
| 79 | + mc.strict, isBool = readBool(val) |
| 80 | + if !isBool { |
| 81 | + return errors.New("Invalid Bool value: " + val) |
| 82 | + } |
| 83 | + |
| 84 | + // Compression |
| 85 | + case "compress": |
| 86 | + err = errors.New("Compression not implemented yet") |
| 87 | + return |
| 88 | + |
| 89 | + // System Vars |
| 90 | + default: |
| 91 | + err = mc.exec("SET " + param + "=" + val + "") |
| 92 | + if err != nil { |
| 93 | + return |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return |
| 99 | +} |
| 100 | + |
| 101 | +func (mc *mysqlConn) Begin() (driver.Tx, error) { |
| 102 | + if mc.netConn == nil { |
| 103 | + errLog.Print(errInvalidConn) |
| 104 | + return nil, driver.ErrBadConn |
| 105 | + } |
| 106 | + /* |
| 107 | + err := mc.exec("START TRANSACTION") |
| 108 | + if err == nil { |
| 109 | + return &mysqlTx{mc}, err |
| 110 | + } |
| 111 | + |
| 112 | + return nil, err |
| 113 | + */ |
| 114 | + mc.startTx = true |
| 115 | + return &mysqlTx{mc}, nil |
| 116 | +} |
| 117 | + |
| 118 | +func (mc *mysqlConn) Close() (err error) { |
| 119 | + // Makes Close idempotent |
| 120 | + if mc.netConn != nil { |
| 121 | + mc.writeCommandPacket(comQuit) |
| 122 | + mc.netConn.Close() |
| 123 | + mc.netConn = nil |
| 124 | + } |
| 125 | + |
| 126 | + mc.cfg = nil |
| 127 | + mc.buf = nil |
| 128 | + mc.startTx = false |
| 129 | + |
| 130 | + return |
| 131 | +} |
| 132 | + |
| 133 | +func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) { |
| 134 | + if mc.netConn == nil { |
| 135 | + errLog.Print(errInvalidConn) |
| 136 | + return nil, driver.ErrBadConn |
| 137 | + } |
| 138 | + |
| 139 | + // Start tx lazily |
| 140 | + if mc.startTx { |
| 141 | + mc.startTx = false |
| 142 | + err := mc.exec("START TRANSACTION") |
| 143 | + if err != nil { |
| 144 | + return nil, err |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + // Send command |
| 149 | + err := mc.writeCommandPacketStr(comStmtPrepare, query) |
| 150 | + if err != nil { |
| 151 | + return nil, err |
| 152 | + } |
| 153 | + |
| 154 | + stmt := &mysqlStmt{ |
| 155 | + mc: mc, |
| 156 | + } |
| 157 | + |
| 158 | + // Read Result |
| 159 | + columnCount, err := stmt.readPrepareResultPacket() |
| 160 | + if err == nil { |
| 161 | + if stmt.paramCount > 0 { |
| 162 | + if err = mc.readUntilEOF(); err != nil { |
| 163 | + return nil, err |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + if columnCount > 0 { |
| 168 | + err = mc.readUntilEOF() |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + return stmt, err |
| 173 | +} |
| 174 | + |
| 175 | +func (mc *mysqlConn) Exec(query string, args []driver.Value) (driver.Result, error) { |
| 176 | + if mc.netConn == nil { |
| 177 | + errLog.Print(errInvalidConn) |
| 178 | + return nil, driver.ErrBadConn |
| 179 | + } |
| 180 | + |
| 181 | + // Start tx lazily |
| 182 | + if mc.startTx { |
| 183 | + mc.startTx = false |
| 184 | + var err error |
| 185 | + if strings.HasPrefix(strings.ToUpper(query), "SET TRANSACTION ISOLATION LEVEL") { |
| 186 | + err = mc.exec(query) |
| 187 | + if err != nil { |
| 188 | + return nil, err |
| 189 | + } |
| 190 | + err = mc.exec("START TRANSACTION") |
| 191 | + return nil, err |
| 192 | + } else { |
| 193 | + err = mc.exec("START TRANSACTION") |
| 194 | + if err != nil { |
| 195 | + return nil, err |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + if len(args) == 0 { // no args, fastpath |
| 201 | + mc.affectedRows = 0 |
| 202 | + mc.insertId = 0 |
| 203 | + |
| 204 | + err := mc.exec(query) |
| 205 | + if err == nil { |
| 206 | + return &mysqlResult{ |
| 207 | + affectedRows: int64(mc.affectedRows), |
| 208 | + insertId: int64(mc.insertId), |
| 209 | + }, err |
| 210 | + } |
| 211 | + return nil, err |
| 212 | + } |
| 213 | + |
| 214 | + // with args, must use prepared stmt |
| 215 | + return nil, driver.ErrSkip |
| 216 | + |
| 217 | +} |
| 218 | + |
| 219 | +// Internal function to execute commands |
| 220 | +func (mc *mysqlConn) exec(query string) error { |
| 221 | + // Send command |
| 222 | + err := mc.writeCommandPacketStr(comQuery, query) |
| 223 | + if err != nil { |
| 224 | + return err |
| 225 | + } |
| 226 | + |
| 227 | + // Read Result |
| 228 | + resLen, err := mc.readResultSetHeaderPacket() |
| 229 | + if err == nil && resLen > 0 { |
| 230 | + if err = mc.readUntilEOF(); err != nil { |
| 231 | + return err |
| 232 | + } |
| 233 | + |
| 234 | + err = mc.readUntilEOF() |
| 235 | + } |
| 236 | + |
| 237 | + return err |
| 238 | +} |
| 239 | + |
| 240 | +func (mc *mysqlConn) Query(query string, args []driver.Value) (driver.Rows, error) { |
| 241 | + if mc.netConn == nil { |
| 242 | + errLog.Print(errInvalidConn) |
| 243 | + return nil, driver.ErrBadConn |
| 244 | + } |
| 245 | + |
| 246 | + // Start tx lazily |
| 247 | + if mc.startTx { |
| 248 | + mc.startTx = false |
| 249 | + err := mc.exec("START TRANSACTION") |
| 250 | + if err != nil { |
| 251 | + return nil, err |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + if len(args) == 0 { // no args, fastpath |
| 256 | + // Send command |
| 257 | + err := mc.writeCommandPacketStr(comQuery, query) |
| 258 | + if err == nil { |
| 259 | + // Read Result |
| 260 | + var resLen int |
| 261 | + resLen, err = mc.readResultSetHeaderPacket() |
| 262 | + if err == nil { |
| 263 | + rows := new(textRows) |
| 264 | + rows.mc = mc |
| 265 | + |
| 266 | + if resLen > 0 { |
| 267 | + // Columns |
| 268 | + rows.columns, err = mc.readColumns(resLen) |
| 269 | + } |
| 270 | + return rows, err |
| 271 | + } |
| 272 | + } |
| 273 | + return nil, err |
| 274 | + } |
| 275 | + |
| 276 | + // with args, must use prepared stmt |
| 277 | + return nil, driver.ErrSkip |
| 278 | +} |
| 279 | + |
| 280 | +// Gets the value of the given MySQL System Variable |
| 281 | +// The returned byte slice is only valid until the next read |
| 282 | +func (mc *mysqlConn) getSystemVar(name string) ([]byte, error) { |
| 283 | + // Send command |
| 284 | + if err := mc.writeCommandPacketStr(comQuery, "SELECT @@"+name); err != nil { |
| 285 | + return nil, err |
| 286 | + } |
| 287 | + |
| 288 | + // Read Result |
| 289 | + resLen, err := mc.readResultSetHeaderPacket() |
| 290 | + if err == nil { |
| 291 | + rows := new(textRows) |
| 292 | + rows.mc = mc |
| 293 | + |
| 294 | + if resLen > 0 { |
| 295 | + // Columns |
| 296 | + if err := mc.readUntilEOF(); err != nil { |
| 297 | + return nil, err |
| 298 | + } |
| 299 | + } |
| 300 | + |
| 301 | + dest := make([]driver.Value, resLen) |
| 302 | + if err = rows.readRow(dest); err == nil { |
| 303 | + return dest[0].([]byte), mc.readUntilEOF() |
| 304 | + } |
| 305 | + } |
| 306 | + return nil, err |
| 307 | +} |
0 commit comments