connection.go 5.4 KB

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