connection.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. package mysql
  9. import (
  10. "crypto/tls"
  11. "database/sql/driver"
  12. "errors"
  13. "net"
  14. "strings"
  15. "time"
  16. )
  17. type mysqlConn struct {
  18. buf *buffer
  19. netConn net.Conn
  20. affectedRows uint64
  21. insertId uint64
  22. cfg *config
  23. maxPacketAllowed int
  24. maxWriteSize int
  25. flags clientFlag
  26. sequence uint8
  27. collation byte
  28. parseTime bool
  29. strict bool
  30. }
  31. type config struct {
  32. user string
  33. passwd string
  34. net string
  35. addr string
  36. dbname string
  37. params map[string]string
  38. loc *time.Location
  39. timeout time.Duration
  40. tls *tls.Config
  41. allowAllFiles bool
  42. allowOldPasswords bool
  43. clientFoundRows bool
  44. }
  45. // Handles parameters set in DSN
  46. func (mc *mysqlConn) handleParams() (err error) {
  47. for param, val := range mc.cfg.params {
  48. switch param {
  49. // Collation
  50. case "collation":
  51. collation, ok := collations[val]
  52. if !ok {
  53. // Note possibility for false negatives:
  54. // could be caused although the collation is valid
  55. // if the collations map does not contain entries
  56. // the server supports.
  57. err = errors.New("unknown collation")
  58. return
  59. }
  60. mc.collation = collation
  61. break
  62. // Charset
  63. case "charset":
  64. charsets := strings.Split(val, ",")
  65. for i := range charsets {
  66. // ignore errors here - a charset may not exist
  67. err = mc.exec("SET NAMES " + charsets[i])
  68. if err == nil {
  69. break
  70. }
  71. }
  72. if err != nil {
  73. return
  74. }
  75. // time.Time parsing
  76. case "parseTime":
  77. var isBool bool
  78. mc.parseTime, isBool = readBool(val)
  79. if !isBool {
  80. return errors.New("Invalid Bool value: " + val)
  81. }
  82. // Strict mode
  83. case "strict":
  84. var isBool bool
  85. mc.strict, isBool = readBool(val)
  86. if !isBool {
  87. return errors.New("Invalid Bool value: " + val)
  88. }
  89. // Compression
  90. case "compress":
  91. err = errors.New("Compression not implemented yet")
  92. return
  93. // System Vars
  94. default:
  95. err = mc.exec("SET " + param + "=" + val + "")
  96. if err != nil {
  97. return
  98. }
  99. }
  100. }
  101. return
  102. }
  103. func (mc *mysqlConn) Begin() (driver.Tx, error) {
  104. if mc.netConn == nil {
  105. errLog.Print(ErrInvalidConn)
  106. return nil, driver.ErrBadConn
  107. }
  108. err := mc.exec("START TRANSACTION")
  109. if err == nil {
  110. return &mysqlTx{mc}, err
  111. }
  112. return nil, err
  113. }
  114. func (mc *mysqlConn) Close() (err error) {
  115. // Makes Close idempotent
  116. if mc.netConn != nil {
  117. err = mc.writeCommandPacket(comQuit)
  118. if err == nil {
  119. err = mc.netConn.Close()
  120. } else {
  121. mc.netConn.Close()
  122. }
  123. mc.netConn = nil
  124. }
  125. mc.cfg = nil
  126. mc.buf = nil
  127. return
  128. }
  129. func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
  130. if mc.netConn == nil {
  131. errLog.Print(ErrInvalidConn)
  132. return nil, driver.ErrBadConn
  133. }
  134. // Send command
  135. err := mc.writeCommandPacketStr(comStmtPrepare, query)
  136. if err != nil {
  137. return nil, err
  138. }
  139. stmt := &mysqlStmt{
  140. mc: mc,
  141. }
  142. // Read Result
  143. columnCount, err := stmt.readPrepareResultPacket()
  144. if err == nil {
  145. if stmt.paramCount > 0 {
  146. if err = mc.readUntilEOF(); err != nil {
  147. return nil, err
  148. }
  149. }
  150. if columnCount > 0 {
  151. err = mc.readUntilEOF()
  152. }
  153. }
  154. return stmt, err
  155. }
  156. func (mc *mysqlConn) Exec(query string, args []driver.Value) (driver.Result, error) {
  157. if mc.netConn == nil {
  158. errLog.Print(ErrInvalidConn)
  159. return nil, driver.ErrBadConn
  160. }
  161. if len(args) == 0 { // no args, fastpath
  162. mc.affectedRows = 0
  163. mc.insertId = 0
  164. err := mc.exec(query)
  165. if err == nil {
  166. return &mysqlResult{
  167. affectedRows: int64(mc.affectedRows),
  168. insertId: int64(mc.insertId),
  169. }, err
  170. }
  171. return nil, err
  172. }
  173. // with args, must use prepared stmt
  174. return nil, driver.ErrSkip
  175. }
  176. // Internal function to execute commands
  177. func (mc *mysqlConn) exec(query string) error {
  178. // Send command
  179. err := mc.writeCommandPacketStr(comQuery, query)
  180. if err != nil {
  181. return err
  182. }
  183. // Read Result
  184. resLen, err := mc.readResultSetHeaderPacket()
  185. if err == nil && resLen > 0 {
  186. if err = mc.readUntilEOF(); err != nil {
  187. return err
  188. }
  189. err = mc.readUntilEOF()
  190. }
  191. return err
  192. }
  193. func (mc *mysqlConn) Query(query string, args []driver.Value) (driver.Rows, error) {
  194. if mc.netConn == nil {
  195. errLog.Print(ErrInvalidConn)
  196. return nil, driver.ErrBadConn
  197. }
  198. if len(args) == 0 { // no args, fastpath
  199. // Send command
  200. err := mc.writeCommandPacketStr(comQuery, query)
  201. if err == nil {
  202. // Read Result
  203. var resLen int
  204. resLen, err = mc.readResultSetHeaderPacket()
  205. if err == nil {
  206. rows := new(textRows)
  207. rows.mc = mc
  208. if resLen > 0 {
  209. // Columns
  210. rows.columns, err = mc.readColumns(resLen)
  211. }
  212. return rows, err
  213. }
  214. }
  215. return nil, err
  216. }
  217. // with args, must use prepared stmt
  218. return nil, driver.ErrSkip
  219. }
  220. // Gets the value of the given MySQL System Variable
  221. // The returned byte slice is only valid until the next read
  222. func (mc *mysqlConn) getSystemVar(name string) ([]byte, error) {
  223. // Send command
  224. if err := mc.writeCommandPacketStr(comQuery, "SELECT @@"+name); err != nil {
  225. return nil, err
  226. }
  227. // Read Result
  228. resLen, err := mc.readResultSetHeaderPacket()
  229. if err == nil {
  230. rows := new(textRows)
  231. rows.mc = mc
  232. if resLen > 0 {
  233. // Columns
  234. if err := mc.readUntilEOF(); err != nil {
  235. return nil, err
  236. }
  237. }
  238. dest := make([]driver.Value, resLen)
  239. if err = rows.readRow(dest); err == nil {
  240. return dest[0].([]byte), mc.readUntilEOF()
  241. }
  242. }
  243. return nil, err
  244. }