@@ -18,57 +18,22 @@ var CSVColumnParser = func(s string) []byte {
1818 return []byte (s )
1919}
2020
21- // Rows interface allows to construct rows
22- // which also satisfies database/sql/driver.Rows interface
23- type Rows interface {
24- // composed interface, supports sql driver.Rows
25- driver.Rows
26-
27- // AddRow composed from database driver.Value slice
28- // return the same instance to perform subsequent actions.
29- // Note that the number of values must match the number
30- // of columns
31- AddRow (columns ... driver.Value ) Rows
32-
33- // FromCSVString build rows from csv string.
34- // return the same instance to perform subsequent actions.
35- // Note that the number of values must match the number
36- // of columns
37- FromCSVString (s string ) Rows
38-
39- // RowError allows to set an error
40- // which will be returned when a given
41- // row number is read
42- RowError (row int , err error ) Rows
43-
44- // CloseError allows to set an error
45- // which will be returned by rows.Close
46- // function.
47- //
48- // The close error will be triggered only in cases
49- // when rows.Next() EOF was not yet reached, that is
50- // a default sql library behavior
51- CloseError (err error ) Rows
21+ type rowSets struct {
22+ sets []* Rows
23+ pos int
5224}
5325
54- type rows struct {
55- cols []string
56- rows [][]driver.Value
57- pos int
58- nextErr map [int ]error
59- closeErr error
60- }
61-
62- func (r * rows ) Columns () []string {
63- return r .cols
26+ func (rs * rowSets ) Columns () []string {
27+ return rs .sets [rs .pos ].cols
6428}
6529
66- func (r * rows ) Close () error {
67- return r .closeErr
30+ func (rs * rowSets ) Close () error {
31+ return rs . sets [ rs . pos ] .closeErr
6832}
6933
7034// advances to next row
71- func (r * rows ) Next (dest []driver.Value ) error {
35+ func (rs * rowSets ) Next (dest []driver.Value ) error {
36+ r := rs .sets [rs .pos ]
7237 r .pos ++
7338 if r .pos > len (r .rows ) {
7439 return io .EOF // per interface spec
@@ -81,24 +46,48 @@ func (r *rows) Next(dest []driver.Value) error {
8146 return r .nextErr [r .pos - 1 ]
8247}
8348
49+ // Rows is a mocked collection of rows to
50+ // return for Query result
51+ type Rows struct {
52+ cols []string
53+ rows [][]driver.Value
54+ pos int
55+ nextErr map [int ]error
56+ closeErr error
57+ }
58+
8459// NewRows allows Rows to be created from a
8560// sql driver.Value slice or from the CSV string and
8661// to be used as sql driver.Rows
87- func NewRows (columns []string ) Rows {
88- return & rows {cols : columns , nextErr : make (map [int ]error )}
62+ func NewRows (columns []string ) * Rows {
63+ return & Rows {cols : columns , nextErr : make (map [int ]error )}
8964}
9065
91- func (r * rows ) CloseError (err error ) Rows {
66+ // CloseError allows to set an error
67+ // which will be returned by rows.Close
68+ // function.
69+ //
70+ // The close error will be triggered only in cases
71+ // when rows.Next() EOF was not yet reached, that is
72+ // a default sql library behavior
73+ func (r * Rows ) CloseError (err error ) * Rows {
9274 r .closeErr = err
9375 return r
9476}
9577
96- func (r * rows ) RowError (row int , err error ) Rows {
78+ // RowError allows to set an error
79+ // which will be returned when a given
80+ // row number is read
81+ func (r * Rows ) RowError (row int , err error ) * Rows {
9782 r .nextErr [row ] = err
9883 return r
9984}
10085
101- func (r * rows ) AddRow (values ... driver.Value ) Rows {
86+ // AddRow composed from database driver.Value slice
87+ // return the same instance to perform subsequent actions.
88+ // Note that the number of values must match the number
89+ // of columns
90+ func (r * Rows ) AddRow (values ... driver.Value ) * Rows {
10291 if len (values ) != len (r .cols ) {
10392 panic ("Expected number of values to match number of columns" )
10493 }
@@ -112,7 +101,11 @@ func (r *rows) AddRow(values ...driver.Value) Rows {
112101 return r
113102}
114103
115- func (r * rows ) FromCSVString (s string ) Rows {
104+ // FromCSVString build rows from csv string.
105+ // return the same instance to perform subsequent actions.
106+ // Note that the number of values must match the number
107+ // of columns
108+ func (r * Rows ) FromCSVString (s string ) * Rows {
116109 res := strings .NewReader (strings .TrimSpace (s ))
117110 csvReader := csv .NewReader (res )
118111
0 commit comments