rows.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. "database/sql/driver"
  11. "io"
  12. )
  13. type mysqlField struct {
  14. name string
  15. fieldType byte
  16. flags fieldFlag
  17. }
  18. // mysqlRows is the driver-internal Rows struct that is never given to
  19. // the database/sql package. This struct is 40 bytes on 64-bit
  20. // machines and is recycled. Its size isn't very relevant, since we
  21. // recycle it.
  22. //
  23. // Allocate with newMysqlRows (from buffer.go) and return with
  24. // putMySQLRows. See also: mysqlRowsI.
  25. type mysqlRows struct {
  26. mc *mysqlConn
  27. columns []mysqlField
  28. binary bool // Note: packing small bool fields at the end
  29. eof bool
  30. }
  31. // mysqlRowsI implements driver.Rows. Its wrapped *mysqlRows pointer
  32. // becomes nil and recycled on Close. This struct is kept small (8
  33. // bytes) to minimize garbage creation.
  34. type mysqlRowsI struct {
  35. *mysqlRows
  36. }
  37. func (rows *mysqlRows) Columns() []string {
  38. if rows == nil {
  39. return nil
  40. }
  41. columns := make([]string, len(rows.columns))
  42. for i := range columns {
  43. columns[i] = rows.columns[i].name
  44. }
  45. return columns
  46. }
  47. func (ri *mysqlRowsI) Close() error {
  48. if ri.mysqlRows == nil {
  49. return nil // make Close() idempotent
  50. }
  51. err := ri.mysqlRows.close()
  52. putMysqlRows(ri.mysqlRows)
  53. ri.mysqlRows = nil
  54. return err
  55. }
  56. func (rows *mysqlRows) close() (err error) {
  57. // Remove unread packets from stream
  58. if !rows.eof {
  59. if rows.mc == nil || rows.mc.netConn == nil {
  60. return errInvalidConn
  61. }
  62. err = rows.mc.readUntilEOF()
  63. // explicitly set because readUntilEOF might return early in case of an
  64. // error
  65. rows.eof = true
  66. }
  67. rows.mc = nil
  68. if !rows.binary { // Binary rows use cached columns
  69. putFields(rows.columns)
  70. }
  71. rows.columns = nil
  72. return
  73. }
  74. func (rows *mysqlRows) Next(dest []driver.Value) (err error) {
  75. if rows == nil {
  76. return errInvalidConn
  77. }
  78. if rows.eof {
  79. return io.EOF
  80. }
  81. if rows.mc == nil || rows.mc.netConn == nil {
  82. return errInvalidConn
  83. }
  84. // Fetch next row from stream
  85. if rows.binary {
  86. err = rows.readBinaryRow(dest)
  87. } else {
  88. err = rows.readRow(dest)
  89. }
  90. if err == io.EOF {
  91. rows.eof = true
  92. }
  93. return err
  94. }