rows.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. handleOk := mc.clearResult()
  107. if err = handleOk.discardResults(); err != nil {
  108. return err
  109. }
  110. }
  111. rows.mc = nil
  112. return err
  113. }
  114. func (rows *mysqlRows) HasNextResultSet() (b bool) {
  115. if rows.mc == nil {
  116. return false
  117. }
  118. return rows.mc.status&statusMoreResultsExists != 0
  119. }
  120. func (rows *mysqlRows) nextResultSet() (int, error) {
  121. if rows.mc == nil {
  122. return 0, io.EOF
  123. }
  124. if err := rows.mc.error(); err != nil {
  125. return 0, err
  126. }
  127. // Remove unread packets from stream
  128. if !rows.rs.done {
  129. if err := rows.mc.readUntilEOF(); err != nil {
  130. return 0, err
  131. }
  132. rows.rs.done = true
  133. }
  134. if !rows.HasNextResultSet() {
  135. rows.mc = nil
  136. return 0, io.EOF
  137. }
  138. rows.rs = resultSet{}
  139. // rows.mc.affectedRows and rows.mc.insertIds accumulate on each call to
  140. // nextResultSet.
  141. resLen, err := rows.mc.resultUnchanged().readResultSetHeaderPacket()
  142. if err != nil {
  143. // Clean up about multi-results flag
  144. rows.rs.done = true
  145. rows.mc.status = rows.mc.status & (^statusMoreResultsExists)
  146. }
  147. return resLen, err
  148. }
  149. func (rows *mysqlRows) nextNotEmptyResultSet() (int, error) {
  150. for {
  151. resLen, err := rows.nextResultSet()
  152. if err != nil {
  153. return 0, err
  154. }
  155. if resLen > 0 {
  156. return resLen, nil
  157. }
  158. rows.rs.done = true
  159. }
  160. }
  161. func (rows *binaryRows) NextResultSet() error {
  162. resLen, err := rows.nextNotEmptyResultSet()
  163. if err != nil {
  164. return err
  165. }
  166. rows.rs.columns, err = rows.mc.readColumns(resLen)
  167. return err
  168. }
  169. func (rows *binaryRows) Next(dest []driver.Value) error {
  170. if mc := rows.mc; mc != nil {
  171. if err := mc.error(); err != nil {
  172. return err
  173. }
  174. // Fetch next row from stream
  175. return rows.readRow(dest)
  176. }
  177. return io.EOF
  178. }
  179. func (rows *textRows) NextResultSet() (err error) {
  180. resLen, err := rows.nextNotEmptyResultSet()
  181. if err != nil {
  182. return err
  183. }
  184. rows.rs.columns, err = rows.mc.readColumns(resLen)
  185. return err
  186. }
  187. func (rows *textRows) Next(dest []driver.Value) error {
  188. if mc := rows.mc; mc != nil {
  189. if err := mc.error(); err != nil {
  190. return err
  191. }
  192. // Fetch next row from stream
  193. return rows.readRow(dest)
  194. }
  195. return io.EOF
  196. }