connection.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. cfg *config
  19. flags clientFlag
  20. netConn net.Conn
  21. buf *buffer
  22. protocol uint8
  23. sequence uint8
  24. affectedRows uint64
  25. insertId uint64
  26. maxPacketAllowed int
  27. maxWriteSize int
  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. // 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. err := mc.exec("START TRANSACTION")
  92. if err == nil {
  93. return &mysqlTx{mc}, err
  94. }
  95. return nil, err
  96. }
  97. func (mc *mysqlConn) Close() (err error) {
  98. // Makes Close idempotent
  99. if mc.netConn != nil {
  100. mc.writeCommandPacket(comQuit)
  101. mc.netConn.Close()
  102. mc.netConn = nil
  103. }
  104. mc.cfg = nil
  105. mc.buf = nil
  106. return
  107. }
  108. func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
  109. // Send command
  110. err := mc.writeCommandPacketStr(comStmtPrepare, query)
  111. if err != nil {
  112. return nil, err
  113. }
  114. stmt := &mysqlStmt{
  115. mc: mc,
  116. }
  117. // Read Result
  118. columnCount, err := stmt.readPrepareResultPacket()
  119. if err == nil {
  120. if stmt.paramCount > 0 {
  121. if err = mc.readUntilEOF(); err != nil {
  122. return nil, err
  123. }
  124. }
  125. if columnCount > 0 {
  126. err = mc.readUntilEOF()
  127. }
  128. }
  129. return stmt, err
  130. }
  131. func (mc *mysqlConn) Exec(query string, args []driver.Value) (driver.Result, error) {
  132. if len(args) == 0 { // no args, fastpath
  133. mc.affectedRows = 0
  134. mc.insertId = 0
  135. err := mc.exec(query)
  136. if err == nil {
  137. return &mysqlResult{
  138. affectedRows: int64(mc.affectedRows),
  139. insertId: int64(mc.insertId),
  140. }, err
  141. }
  142. return nil, err
  143. }
  144. // with args, must use prepared stmt
  145. return nil, driver.ErrSkip
  146. }
  147. // Internal function to execute commands
  148. func (mc *mysqlConn) exec(query string) error {
  149. // Send command
  150. err := mc.writeCommandPacketStr(comQuery, query)
  151. if err != nil {
  152. return err
  153. }
  154. // Read Result
  155. resLen, err := mc.readResultSetHeaderPacket()
  156. if err == nil && resLen > 0 {
  157. if err = mc.readUntilEOF(); err != nil {
  158. return err
  159. }
  160. err = mc.readUntilEOF()
  161. }
  162. return err
  163. }
  164. func (mc *mysqlConn) Query(query string, args []driver.Value) (driver.Rows, error) {
  165. if len(args) == 0 { // no args, fastpath
  166. // Send command
  167. err := mc.writeCommandPacketStr(comQuery, query)
  168. if err == nil {
  169. // Read Result
  170. var resLen int
  171. resLen, err = mc.readResultSetHeaderPacket()
  172. if err == nil {
  173. rows := &mysqlRows{mc, nil, false, false}
  174. if resLen > 0 {
  175. // Columns
  176. rows.columns, err = mc.readColumns(resLen)
  177. }
  178. return rows, err
  179. }
  180. }
  181. return nil, err
  182. }
  183. // with args, must use prepared stmt
  184. return nil, driver.ErrSkip
  185. }
  186. // Gets the value of the given MySQL System Variable
  187. // The returned byte slice is only valid until the next read
  188. func (mc *mysqlConn) getSystemVar(name string) ([]byte, error) {
  189. // Send command
  190. if err := mc.writeCommandPacketStr(comQuery, "SELECT @@"+name); err != nil {
  191. return nil, err
  192. }
  193. // Read Result
  194. resLen, err := mc.readResultSetHeaderPacket()
  195. if err == nil {
  196. rows := &mysqlRows{mc, nil, false, false}
  197. if resLen > 0 {
  198. // Columns
  199. if err := mc.readUntilEOF(); err != nil {
  200. return nil, err
  201. }
  202. }
  203. dest := make([]driver.Value, resLen)
  204. if err = rows.readRow(dest); err == nil {
  205. return dest[0].([]byte), mc.readUntilEOF()
  206. }
  207. }
  208. return nil, err
  209. }