MCPcopy Index your code
hub / github.com/upper/db / fetchRows

Function fetchRows

internal/sqlbuilder/fetch.go:95–140  ·  view source on GitHub ↗

fetchRows receives a *sql.Rows value and tries to map all the rows into a slice of structs given by the pointer `dst`.

(iter *iterator, dst interface{})

Source from the content-addressed store, hash-verified

93// fetchRows receives a *sql.Rows value and tries to map all the rows into a
94// slice of structs given by the pointer `dst`.
95func fetchRows(iter *iterator, dst interface{}) error {
96 var err error
97 rows := iter.cursor
98 defer rows.Close()
99
100 // Destination.
101 dstv := reflect.ValueOf(dst)
102
103 if dstv.IsNil() || dstv.Kind() != reflect.Ptr {
104 return ErrExpectingPointer
105 }
106
107 if dstv.Elem().Kind() != reflect.Slice {
108 return ErrExpectingSlicePointer
109 }
110
111 if dstv.Kind() != reflect.Ptr || dstv.Elem().Kind() != reflect.Slice || dstv.IsNil() {
112 return ErrExpectingSliceMapStruct
113 }
114
115 var columns []string
116 if columns, err = rows.Columns(); err != nil {
117 return err
118 }
119
120 slicev := dstv.Elem()
121 itemT := slicev.Type().Elem()
122
123 reset(dst)
124
125 for rows.Next() {
126 item, err := fetchResult(iter, itemT, columns)
127 if err != nil {
128 return err
129 }
130 if itemT.Kind() == reflect.Ptr {
131 slicev = reflect.Append(slicev, item)
132 } else {
133 slicev = reflect.Append(slicev, reflect.Indirect(item))
134 }
135 }
136
137 dstv.Elem().Set(slicev)
138
139 return rows.Err()
140}
141
142func fetchResult(iter *iterator, itemT reflect.Type, columns []string) (reflect.Value, error) {
143

Callers 1

AllMethod · 0.85

Calls 9

resetFunction · 0.85
fetchResultFunction · 0.85
KindMethod · 0.80
CloseMethod · 0.65
ColumnsMethod · 0.65
NextMethod · 0.65
SetMethod · 0.65
ErrMethod · 0.65
AppendMethod · 0.45

Tested by

no test coverage detected