rows.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. tableName string
  15. name string
  16. flags fieldFlag
  17. fieldType byte
  18. decimals byte
  19. }
  20. type mysqlRows struct {
  21. mc *mysqlConn
  22. columns []mysqlField
  23. }
  24. type binaryRows struct {
  25. mysqlRows
  26. }
  27. type textRows struct {
  28. mysqlRows
  29. }
  30. type emptyRows struct{}
  31. func (rows *mysqlRows) Columns() []string {
  32. columns := make([]string, len(rows.columns))
  33. if rows.mc != nil && rows.mc.cfg.ColumnsWithAlias {
  34. for i := range columns {
  35. if tableName := rows.columns[i].tableName; len(tableName) > 0 {
  36. columns[i] = tableName + "." + rows.columns[i].name
  37. } else {
  38. columns[i] = rows.columns[i].name
  39. }
  40. }
  41. } else {
  42. for i := range columns {
  43. columns[i] = rows.columns[i].name
  44. }
  45. }
  46. return columns
  47. }
  48. func (rows *mysqlRows) Close() error {
  49. mc := rows.mc
  50. if mc == nil {
  51. return nil
  52. }
  53. if mc.netConn == nil {
  54. return ErrInvalidConn
  55. }
  56. // Remove unread packets from stream
  57. err := mc.readUntilEOF()
  58. if err == nil {
  59. if err = mc.discardResults(); err != nil {
  60. return err
  61. }
  62. }
  63. rows.mc = nil
  64. return err
  65. }
  66. func (rows *binaryRows) Next(dest []driver.Value) error {
  67. if mc := rows.mc; mc != nil {
  68. if mc.netConn == nil {
  69. return ErrInvalidConn
  70. }
  71. // Fetch next row from stream
  72. return rows.readRow(dest)
  73. }
  74. return io.EOF
  75. }
  76. func (rows *textRows) Next(dest []driver.Value) error {
  77. if mc := rows.mc; mc != nil {
  78. if mc.netConn == nil {
  79. return ErrInvalidConn
  80. }
  81. // Fetch next row from stream
  82. return rows.readRow(dest)
  83. }
  84. return io.EOF
  85. }
  86. func (rows emptyRows) Columns() []string {
  87. return nil
  88. }
  89. func (rows emptyRows) Close() error {
  90. return nil
  91. }
  92. func (rows emptyRows) Next(dest []driver.Value) error {
  93. return io.EOF
  94. }