rows.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 FieldType
  17. flags FieldFlag
  18. }
  19. type rowsContent struct {
  20. columns []mysqlField
  21. rows []*[]*[]byte
  22. }
  23. type mysqlRows struct {
  24. content *rowsContent
  25. }
  26. func (rows mysqlRows) Columns() (columns []string) {
  27. columns = make([]string, len(rows.content.columns))
  28. for i := 0; i < cap(columns); i++ {
  29. columns[i] = rows.content.columns[i].name
  30. }
  31. return
  32. }
  33. func (rows mysqlRows) Close() error {
  34. rows.content = nil
  35. return nil
  36. }
  37. // Next returns []driver.Value filled with either nil values for NULL entries
  38. // or []byte's for all other entries. Type conversion is done on rows.scan(),
  39. // when the dest. type is know, which makes type conversion easier and avoids
  40. // unnecessary conversions.
  41. func (rows mysqlRows) Next(dest []driver.Value) error {
  42. if len(rows.content.rows) > 0 {
  43. for i := 0; i < cap(dest); i++ {
  44. if (*rows.content.rows[0])[i] == nil {
  45. dest[i] = nil
  46. } else {
  47. dest[i] = *(*rows.content.rows[0])[i]
  48. }
  49. }
  50. rows.content.rows = rows.content.rows[1:]
  51. } else {
  52. return io.EOF
  53. }
  54. return nil
  55. }