rows.go 1.7 KB

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