connection.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2012 Julien Schmidt. All rights reserved.
  4. // http://www.julienschmidt.com
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla Public
  7. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  8. // You can obtain one at http://mozilla.org/MPL/2.0/.
  9. package mysql
  10. import (
  11. "database/sql/driver"
  12. "errors"
  13. "net"
  14. "strings"
  15. "time"
  16. )
  17. type mysqlConn struct {
  18. cfg *config
  19. flags clientFlag
  20. charset byte
  21. cipher []byte
  22. netConn net.Conn
  23. buf *buffer
  24. protocol uint8
  25. sequence uint8
  26. affectedRows uint64
  27. insertId uint64
  28. maxPacketAllowed int
  29. maxWriteSize int
  30. parseTime bool
  31. strict bool
  32. }
  33. type config struct {
  34. user string
  35. passwd string
  36. net string
  37. addr string
  38. dbname string
  39. params map[string]string
  40. loc *time.Location
  41. }
  42. // Handles parameters set in DSN
  43. func (mc *mysqlConn) handleParams() (err error) {
  44. for param, val := range mc.cfg.params {
  45. switch param {
  46. // Charset
  47. case "charset":
  48. charsets := strings.Split(val, ",")
  49. for i := range charsets {
  50. // ignore errors here - a charset may not exist
  51. err = mc.exec("SET NAMES " + charsets[i])
  52. if err == nil {
  53. break
  54. }
  55. }
  56. if err != nil {
  57. return
  58. }
  59. // handled elsewhere
  60. case "timeout", "allowAllFiles", "loc":
  61. continue
  62. // time.Time parsing
  63. case "parseTime":
  64. mc.parseTime = readBool(val)
  65. // Strict mode
  66. case "strict":
  67. mc.strict = readBool(val)
  68. // TLS-Encryption
  69. case "tls":
  70. err = errors.New("TLS-Encryption not implemented yet")
  71. return
  72. // Compression
  73. case "compress":
  74. err = errors.New("Compression not implemented yet")
  75. // System Vars
  76. default:
  77. err = mc.exec("SET " + param + "=" + val + "")
  78. if err != nil {
  79. return
  80. }
  81. }
  82. }
  83. return
  84. }
  85. func (mc *mysqlConn) Begin() (driver.Tx, error) {
  86. if mc.netConn == nil {
  87. return nil, errInvalidConn
  88. }
  89. err := mc.exec("START TRANSACTION")
  90. if err == nil {
  91. return &mysqlTx{mc}, err
  92. }
  93. return nil, err
  94. }
  95. func (mc *mysqlConn) Close() (err error) {
  96. // Makes Close idempotent
  97. if mc.netConn != nil {
  98. mc.writeCommandPacket(comQuit)
  99. mc.netConn.Close()
  100. mc.netConn = nil
  101. }
  102. mc.cfg = nil
  103. mc.buf = nil
  104. return
  105. }
  106. func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
  107. if mc.netConn == nil {
  108. return nil, errInvalidConn
  109. }
  110. // Send command
  111. err := mc.writeCommandPacketStr(comStmtPrepare, query)
  112. if err != nil {
  113. return nil, err
  114. }
  115. stmt := &mysqlStmt{
  116. mc: mc,
  117. }
  118. // Read Result
  119. columnCount, err := stmt.readPrepareResultPacket()
  120. if err == nil {
  121. if stmt.paramCount > 0 {
  122. stmt.params, err = stmt.mc.readColumns(stmt.paramCount)
  123. if err != nil {
  124. return nil, err
  125. }
  126. }
  127. if columnCount > 0 {
  128. err = stmt.mc.readUntilEOF()
  129. }
  130. }
  131. return stmt, err
  132. }
  133. func (mc *mysqlConn) Exec(query string, args []driver.Value) (driver.Result, error) {
  134. if mc.netConn == nil {
  135. return nil, errInvalidConn
  136. }
  137. if len(args) == 0 { // no args, fastpath
  138. mc.affectedRows = 0
  139. mc.insertId = 0
  140. err := mc.exec(query)
  141. if err == nil {
  142. return &mysqlResult{
  143. affectedRows: int64(mc.affectedRows),
  144. insertId: int64(mc.insertId),
  145. }, err
  146. }
  147. return nil, err
  148. }
  149. // with args, must use prepared stmt
  150. return nil, driver.ErrSkip
  151. }
  152. // Internal function to execute commands
  153. func (mc *mysqlConn) exec(query string) (err error) {
  154. // Send command
  155. err = mc.writeCommandPacketStr(comQuery, query)
  156. if err != nil {
  157. return
  158. }
  159. // Read Result
  160. var resLen int
  161. resLen, err = mc.readResultSetHeaderPacket()
  162. if err == nil && resLen > 0 {
  163. err = mc.readUntilEOF()
  164. if err != nil {
  165. return
  166. }
  167. err = mc.readUntilEOF()
  168. }
  169. return
  170. }
  171. func (mc *mysqlConn) Query(query string, args []driver.Value) (driver.Rows, error) {
  172. if mc.netConn == nil {
  173. return nil, errInvalidConn
  174. }
  175. if len(args) == 0 { // no args, fastpath
  176. // Send command
  177. err := mc.writeCommandPacketStr(comQuery, query)
  178. if err == nil {
  179. // Read Result
  180. var resLen int
  181. resLen, err = mc.readResultSetHeaderPacket()
  182. if err == nil {
  183. rows := &mysqlRows{mc, false, nil, false}
  184. if resLen > 0 {
  185. // Columns
  186. rows.columns, err = mc.readColumns(resLen)
  187. }
  188. return rows, err
  189. }
  190. }
  191. return nil, err
  192. }
  193. // with args, must use prepared stmt
  194. return nil, driver.ErrSkip
  195. }
  196. // Gets the value of the given MySQL System Variable
  197. // The returned byte slice is only valid until the next read
  198. func (mc *mysqlConn) getSystemVar(name string) (val []byte, err error) {
  199. // Send command
  200. err = mc.writeCommandPacketStr(comQuery, "SELECT @@"+name)
  201. if err == nil {
  202. // Read Result
  203. var resLen int
  204. resLen, err = mc.readResultSetHeaderPacket()
  205. if err == nil {
  206. rows := &mysqlRows{mc, false, nil, false}
  207. if resLen > 0 {
  208. // Columns
  209. rows.columns, err = mc.readColumns(resLen)
  210. }
  211. dest := make([]driver.Value, resLen)
  212. err = rows.readRow(dest)
  213. if err == nil {
  214. val = dest[0].([]byte)
  215. err = mc.readUntilEOF()
  216. }
  217. }
  218. }
  219. return
  220. }