rows.go 1.5 KB

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