rows.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. "math"
  13. "reflect"
  14. )
  15. type resultSet struct {
  16. columns []mysqlField
  17. columnNames []string
  18. done bool
  19. }
  20. type mysqlRows struct {
  21. mc *mysqlConn
  22. rs resultSet
  23. finish func()
  24. }
  25. type binaryRows struct {
  26. mysqlRows
  27. }
  28. type textRows struct {
  29. mysqlRows
  30. }
  31. func (rows *mysqlRows) Columns() []string {
  32. if rows.rs.columnNames != nil {
  33. return rows.rs.columnNames
  34. }
  35. columns := make([]string, len(rows.rs.columns))
  36. if rows.mc != nil && rows.mc.cfg.ColumnsWithAlias {
  37. for i := range columns {
  38. if tableName := rows.rs.columns[i].tableName; len(tableName) > 0 {
  39. columns[i] = tableName + "." + rows.rs.columns[i].name
  40. } else {
  41. columns[i] = rows.rs.columns[i].name
  42. }
  43. }
  44. } else {
  45. for i := range columns {
  46. columns[i] = rows.rs.columns[i].name
  47. }
  48. }
  49. rows.rs.columnNames = columns
  50. return columns
  51. }
  52. func (rows *mysqlRows) ColumnTypeDatabaseTypeName(i int) string {
  53. return rows.rs.columns[i].typeDatabaseName()
  54. }
  55. // func (rows *mysqlRows) ColumnTypeLength(i int) (length int64, ok bool) {
  56. // return int64(rows.rs.columns[i].length), true
  57. // }
  58. func (rows *mysqlRows) ColumnTypeNullable(i int) (nullable, ok bool) {
  59. return rows.rs.columns[i].flags&flagNotNULL == 0, true
  60. }
  61. func (rows *mysqlRows) ColumnTypePrecisionScale(i int) (int64, int64, bool) {
  62. column := rows.rs.columns[i]
  63. decimals := int64(column.decimals)
  64. switch column.fieldType {
  65. case fieldTypeDecimal, fieldTypeNewDecimal:
  66. if decimals > 0 {
  67. return int64(column.length) - 2, decimals, true
  68. }
  69. return int64(column.length) - 1, decimals, true
  70. case fieldTypeTimestamp, fieldTypeDateTime, fieldTypeTime:
  71. return decimals, decimals, true
  72. case fieldTypeFloat, fieldTypeDouble:
  73. if decimals == 0x1f {
  74. return math.MaxInt64, math.MaxInt64, true
  75. }
  76. return math.MaxInt64, decimals, true
  77. }
  78. return 0, 0, false
  79. }
  80. func (rows *mysqlRows) ColumnTypeScanType(i int) reflect.Type {
  81. return rows.rs.columns[i].scanType()
  82. }
  83. func (rows *mysqlRows) Close() (err error) {
  84. if f := rows.finish; f != nil {
  85. f()
  86. rows.finish = nil
  87. }
  88. mc := rows.mc
  89. if mc == nil {
  90. return nil
  91. }
  92. if err := mc.error(); err != nil {
  93. return err
  94. }
  95. // flip the buffer for this connection if we need to drain it.
  96. // note that for a successful query (i.e. one where rows.next()
  97. // has been called until it returns false), `rows.mc` will be nil
  98. // by the time the user calls `(*Rows).Close`, so we won't reach this
  99. // see: https://github.com/golang/go/commit/651ddbdb5056ded455f47f9c494c67b389622a47
  100. mc.buf.flip()
  101. // Remove unread packets from stream
  102. if !rows.rs.done {
  103. err = mc.readUntilEOF()
  104. }
  105. if err == nil {
  106. if err = mc.discardResults(); err != nil {
  107. return err
  108. }
  109. }
  110. rows.mc = nil
  111. return err
  112. }
  113. func (rows *mysqlRows) HasNextResultSet() (b bool) {
  114. if rows.mc == nil {
  115. return false
  116. }
  117. return rows.mc.status&statusMoreResultsExists != 0
  118. }
  119. func (rows *mysqlRows) nextResultSet() (int, error) {
  120. if rows.mc == nil {
  121. return 0, io.EOF
  122. }
  123. if err := rows.mc.error(); err != nil {
  124. return 0, err
  125. }
  126. // Remove unread packets from stream
  127. if !rows.rs.done {
  128. if err := rows.mc.readUntilEOF(); err != nil {
  129. return 0, err
  130. }
  131. rows.rs.done = true
  132. }
  133. if !rows.HasNextResultSet() {
  134. rows.mc = nil
  135. return 0, io.EOF
  136. }
  137. rows.rs = resultSet{}
  138. return rows.mc.readResultSetHeaderPacket()
  139. }
  140. func (rows *mysqlRows) nextNotEmptyResultSet() (int, error) {
  141. for {
  142. resLen, err := rows.nextResultSet()
  143. if err != nil {
  144. return 0, err
  145. }
  146. if resLen > 0 {
  147. return resLen, nil
  148. }
  149. rows.rs.done = true
  150. }
  151. }
  152. func (rows *binaryRows) NextResultSet() error {
  153. resLen, err := rows.nextNotEmptyResultSet()
  154. if err != nil {
  155. return err
  156. }
  157. rows.rs.columns, err = rows.mc.readColumns(resLen)
  158. return err
  159. }
  160. func (rows *binaryRows) Next(dest []driver.Value) error {
  161. if mc := rows.mc; mc != nil {
  162. if err := mc.error(); err != nil {
  163. return err
  164. }
  165. // Fetch next row from stream
  166. return rows.readRow(dest)
  167. }
  168. return io.EOF
  169. }
  170. func (rows *textRows) NextResultSet() (err error) {
  171. resLen, err := rows.nextNotEmptyResultSet()
  172. if err != nil {
  173. return err
  174. }
  175. rows.rs.columns, err = rows.mc.readColumns(resLen)
  176. return err
  177. }
  178. func (rows *textRows) Next(dest []driver.Value) error {
  179. if mc := rows.mc; mc != nil {
  180. if err := mc.error(); err != nil {
  181. return err
  182. }
  183. // Fetch next row from stream
  184. return rows.readRow(dest)
  185. }
  186. return io.EOF
  187. }