rows.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2012 Julien Schmidt. All rights reserved.
  4. // http://www.julienschmidt.com
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla Public
  7. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  8. // You can obtain one at http://mozilla.org/MPL/2.0/.
  9. package mysql
  10. import (
  11. "database/sql/driver"
  12. "errors"
  13. "io"
  14. )
  15. type mysqlField struct {
  16. name string
  17. fieldType byte
  18. flags fieldFlag
  19. }
  20. type mysqlRows struct {
  21. mc *mysqlConn
  22. binary bool
  23. columns []mysqlField
  24. eof bool
  25. }
  26. func (rows *mysqlRows) Columns() (columns []string) {
  27. columns = make([]string, len(rows.columns))
  28. for i := range columns {
  29. columns[i] = rows.columns[i].name
  30. }
  31. return
  32. }
  33. func (rows *mysqlRows) Close() (err error) {
  34. defer func() {
  35. rows.mc = nil
  36. }()
  37. // Remove unread packets from stream
  38. if !rows.eof {
  39. if rows.mc == nil || rows.mc.netConn == nil {
  40. return errors.New("Invalid Connection")
  41. }
  42. err = rows.mc.readUntilEOF()
  43. // explicitly set because readUntilEOF might return early in case of an
  44. // error
  45. rows.eof = true
  46. }
  47. return
  48. }
  49. func (rows *mysqlRows) Next(dest []driver.Value) error {
  50. if rows.eof {
  51. return io.EOF
  52. }
  53. if rows.mc == nil || rows.mc.netConn == nil {
  54. return errors.New("Invalid Connection")
  55. }
  56. // Fetch next row from stream
  57. var err error
  58. if rows.binary {
  59. err = rows.readBinaryRow(dest)
  60. } else {
  61. err = rows.readRow(dest)
  62. }
  63. if err == io.EOF {
  64. rows.eof = true
  65. }
  66. return err
  67. }