rows.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 {
  40. return errors.New("Invalid Connection")
  41. }
  42. err = rows.mc.readUntilEOF()
  43. }
  44. return
  45. }
  46. func (rows *mysqlRows) Next(dest []driver.Value) error {
  47. if rows.eof {
  48. return io.EOF
  49. }
  50. if rows.mc == nil {
  51. return errors.New("Invalid Connection")
  52. }
  53. // Fetch next row from stream
  54. var err error
  55. if rows.binary {
  56. err = rows.readBinaryRow(dest)
  57. } else {
  58. err = rows.readRow(dest)
  59. }
  60. if err == io.EOF {
  61. rows.eof = true
  62. }
  63. return err
  64. }