rows.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. type mysqlRows struct {
  19. mc *mysqlConn
  20. binary bool
  21. columns []mysqlField
  22. eof bool
  23. }
  24. func (rows *mysqlRows) Columns() []string {
  25. columns := make([]string, len(rows.columns))
  26. for i := range columns {
  27. columns[i] = rows.columns[i].name
  28. }
  29. return columns
  30. }
  31. func (rows *mysqlRows) Close() (err error) {
  32. // Remove unread packets from stream
  33. if !rows.eof {
  34. if rows.mc == nil || rows.mc.netConn == nil {
  35. return errInvalidConn
  36. }
  37. err = rows.mc.readUntilEOF()
  38. // explicitly set because readUntilEOF might return early in case of an
  39. // error
  40. rows.eof = true
  41. }
  42. rows.mc = nil
  43. return
  44. }
  45. func (rows *mysqlRows) Next(dest []driver.Value) (err error) {
  46. if rows.eof {
  47. return io.EOF
  48. }
  49. if rows.mc == nil || rows.mc.netConn == nil {
  50. return errInvalidConn
  51. }
  52. // Fetch next row from stream
  53. if rows.binary {
  54. err = rows.readBinaryRow(dest)
  55. } else {
  56. err = rows.readRow(dest)
  57. }
  58. if err == io.EOF {
  59. rows.eof = true
  60. }
  61. return err
  62. }