rows.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. fieldType byte
  15. flags fieldFlag
  16. name string
  17. }
  18. type mysqlRows struct {
  19. mc *mysqlConn
  20. columns []mysqlField
  21. binary bool
  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) ColumnsCount() int {
  32. return len(rows.columns)
  33. }
  34. func (rows *mysqlRows) Close() (err error) {
  35. // Remove unread packets from stream
  36. if !rows.eof {
  37. if rows.mc == nil || rows.mc.netConn == nil {
  38. return errInvalidConn
  39. }
  40. err = rows.mc.readUntilEOF()
  41. // explicitly set because readUntilEOF might return early in case of an
  42. // error
  43. rows.eof = true
  44. }
  45. rows.mc = nil
  46. return
  47. }
  48. func (rows *mysqlRows) Next(dest []driver.Value) (err 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. if rows.binary {
  57. err = rows.readBinaryRow(dest)
  58. } else {
  59. err = rows.readRow(dest)
  60. }
  61. if err == io.EOF {
  62. rows.eof = true
  63. }
  64. return err
  65. }