conn.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package client
  2. import (
  3. "crypto/tls"
  4. "fmt"
  5. "net"
  6. "strings"
  7. "time"
  8. "github.com/juju/errors"
  9. . "github.com/siddontang/go-mysql/mysql"
  10. "github.com/siddontang/go-mysql/packet"
  11. )
  12. type Conn struct {
  13. *packet.Conn
  14. user string
  15. password string
  16. db string
  17. TLSConfig *tls.Config
  18. capability uint32
  19. status uint16
  20. charset string
  21. salt []byte
  22. connectionID uint32
  23. }
  24. func getNetProto(addr string) string {
  25. proto := "tcp"
  26. if strings.Contains(addr, "/") {
  27. proto = "unix"
  28. }
  29. return proto
  30. }
  31. // Connect to a MySQL server, addr can be ip:port, or a unix socket domain like /var/sock.
  32. // Accepts a series of configuration functions as a variadic argument.
  33. func Connect(addr string, user string, password string, dbName string, options ...func(*Conn)) (*Conn, error) {
  34. proto := getNetProto(addr)
  35. c := new(Conn)
  36. var err error
  37. conn, err := net.DialTimeout(proto, addr, 10*time.Second)
  38. if err != nil {
  39. return nil, errors.Trace(err)
  40. }
  41. c.Conn = packet.NewConn(conn)
  42. c.user = user
  43. c.password = password
  44. c.db = dbName
  45. //use default charset here, utf-8
  46. c.charset = DEFAULT_CHARSET
  47. // Apply configuration functions.
  48. for i := range options {
  49. options[i](c)
  50. }
  51. if err = c.handshake(); err != nil {
  52. return nil, errors.Trace(err)
  53. }
  54. return c, nil
  55. }
  56. func (c *Conn) handshake() error {
  57. var err error
  58. if err = c.readInitialHandshake(); err != nil {
  59. c.Close()
  60. return errors.Trace(err)
  61. }
  62. if err := c.writeAuthHandshake(); err != nil {
  63. c.Close()
  64. return errors.Trace(err)
  65. }
  66. if _, err := c.readOK(); err != nil {
  67. c.Close()
  68. return errors.Trace(err)
  69. }
  70. return nil
  71. }
  72. func (c *Conn) Close() error {
  73. return c.Conn.Close()
  74. }
  75. func (c *Conn) Ping() error {
  76. if err := c.writeCommand(COM_PING); err != nil {
  77. return errors.Trace(err)
  78. }
  79. if _, err := c.readOK(); err != nil {
  80. return errors.Trace(err)
  81. }
  82. return nil
  83. }
  84. func (c *Conn) UseDB(dbName string) error {
  85. if c.db == dbName {
  86. return nil
  87. }
  88. if err := c.writeCommandStr(COM_INIT_DB, dbName); err != nil {
  89. return errors.Trace(err)
  90. }
  91. if _, err := c.readOK(); err != nil {
  92. return errors.Trace(err)
  93. }
  94. c.db = dbName
  95. return nil
  96. }
  97. func (c *Conn) GetDB() string {
  98. return c.db
  99. }
  100. func (c *Conn) Execute(command string, args ...interface{}) (*Result, error) {
  101. if len(args) == 0 {
  102. return c.exec(command)
  103. } else {
  104. if s, err := c.Prepare(command); err != nil {
  105. return nil, errors.Trace(err)
  106. } else {
  107. var r *Result
  108. r, err = s.Execute(args...)
  109. s.Close()
  110. return r, err
  111. }
  112. }
  113. }
  114. func (c *Conn) Begin() error {
  115. _, err := c.exec("BEGIN")
  116. return errors.Trace(err)
  117. }
  118. func (c *Conn) Commit() error {
  119. _, err := c.exec("COMMIT")
  120. return errors.Trace(err)
  121. }
  122. func (c *Conn) Rollback() error {
  123. _, err := c.exec("ROLLBACK")
  124. return errors.Trace(err)
  125. }
  126. func (c *Conn) SetCharset(charset string) error {
  127. if c.charset == charset {
  128. return nil
  129. }
  130. if _, err := c.exec(fmt.Sprintf("SET NAMES %s", charset)); err != nil {
  131. return errors.Trace(err)
  132. } else {
  133. c.charset = charset
  134. return nil
  135. }
  136. }
  137. func (c *Conn) FieldList(table string, wildcard string) ([]*Field, error) {
  138. if err := c.writeCommandStrStr(COM_FIELD_LIST, table, wildcard); err != nil {
  139. return nil, errors.Trace(err)
  140. }
  141. data, err := c.ReadPacket()
  142. if err != nil {
  143. return nil, errors.Trace(err)
  144. }
  145. fs := make([]*Field, 0, 4)
  146. var f *Field
  147. if data[0] == ERR_HEADER {
  148. return nil, c.handleErrorPacket(data)
  149. } else {
  150. for {
  151. if data, err = c.ReadPacket(); err != nil {
  152. return nil, errors.Trace(err)
  153. }
  154. // EOF Packet
  155. if c.isEOFPacket(data) {
  156. return fs, nil
  157. }
  158. if f, err = FieldData(data).Parse(); err != nil {
  159. return nil, errors.Trace(err)
  160. }
  161. fs = append(fs, f)
  162. }
  163. }
  164. return nil, fmt.Errorf("field list error")
  165. }
  166. func (c *Conn) SetAutoCommit() error {
  167. if !c.IsAutoCommit() {
  168. if _, err := c.exec("SET AUTOCOMMIT = 1"); err != nil {
  169. return errors.Trace(err)
  170. }
  171. }
  172. return nil
  173. }
  174. func (c *Conn) IsAutoCommit() bool {
  175. return c.status&SERVER_STATUS_AUTOCOMMIT > 0
  176. }
  177. func (c *Conn) IsInTransaction() bool {
  178. return c.status&SERVER_STATUS_IN_TRANS > 0
  179. }
  180. func (c *Conn) GetCharset() string {
  181. return c.charset
  182. }
  183. func (c *Conn) GetConnectionID() uint32 {
  184. return c.connectionID
  185. }
  186. func (c *Conn) HandleOKPacket(data []byte) *Result {
  187. r, _ := c.handleOKPacket(data)
  188. return r
  189. }
  190. func (c *Conn) HandleErrorPacket(data []byte) error {
  191. return c.handleErrorPacket(data)
  192. }
  193. func (c *Conn) ReadOKPacket() (*Result, error) {
  194. return c.readOK()
  195. }
  196. func (c *Conn) exec(query string) (*Result, error) {
  197. if err := c.writeCommandStr(COM_QUERY, query); err != nil {
  198. return nil, errors.Trace(err)
  199. }
  200. return c.readResult(false)
  201. }