rows.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. }
  22. type binaryRows struct {
  23. mysqlRows
  24. }
  25. type textRows struct {
  26. mysqlRows
  27. }
  28. func (rows *mysqlRows) Columns() []string {
  29. columns := make([]string, len(rows.columns))
  30. for i := range columns {
  31. columns[i] = rows.columns[i].name
  32. }
  33. return columns
  34. }
  35. func (rows *mysqlRows) Close() error {
  36. mc := rows.mc
  37. if mc == nil {
  38. return nil
  39. }
  40. if mc.netConn == nil {
  41. return ErrInvalidConn
  42. }
  43. // Remove unread packets from stream
  44. err := mc.readUntilEOF()
  45. rows.mc = nil
  46. return err
  47. }
  48. func (rows *binaryRows) Next(dest []driver.Value) error {
  49. if mc := rows.mc; mc != nil {
  50. if mc.netConn == nil {
  51. return ErrInvalidConn
  52. }
  53. // Fetch next row from stream
  54. if err := rows.readRow(dest); err != io.EOF {
  55. return err
  56. }
  57. rows.mc = nil
  58. }
  59. return io.EOF
  60. }
  61. func (rows *textRows) Next(dest []driver.Value) error {
  62. if mc := rows.mc; mc != nil {
  63. if mc.netConn == nil {
  64. return ErrInvalidConn
  65. }
  66. // Fetch next row from stream
  67. if err := rows.readRow(dest); err != io.EOF {
  68. return err
  69. }
  70. rows.mc = nil
  71. }
  72. return io.EOF
  73. }