MCPcopy
hub / github.com/micro/go-micro / List

Method List

model/sqlite/sqlite.go:167–237  ·  view source on GitHub ↗
(ctx context.Context, result interface{}, opts ...model.QueryOption)

Source from the content-addressed store, hash-verified

165}
166
167func (d *sqliteModel) List(ctx context.Context, result interface{}, opts ...model.QueryOption) error {
168 // result must be *[]*T
169 rv := reflect.ValueOf(result)
170 if rv.Kind() != reflect.Ptr || rv.Elem().Kind() != reflect.Slice {
171 return fmt.Errorf("model/sqlite: result must be a pointer to a slice")
172 }
173 sliceVal := rv.Elem()
174 elemType := sliceVal.Type().Elem()
175 structType := elemType
176 if structType.Kind() == reflect.Ptr {
177 structType = structType.Elem()
178 }
179
180 d.mu.RLock()
181 schema, ok := d.types[structType]
182 d.mu.RUnlock()
183 if !ok {
184 return model.ErrNotRegistered
185 }
186
187 q := model.ApplyQueryOptions(opts...)
188 cols := columnList(schema)
189
190 query := fmt.Sprintf("SELECT %s FROM %q", cols, schema.Table)
191 var args []any
192
193 if len(q.Filters) > 0 {
194 where, fArgs := buildWhere(q.Filters)
195 query += " WHERE " + where
196 args = append(args, fArgs...)
197 }
198
199 if q.OrderBy != "" {
200 dir := "ASC"
201 if q.Desc {
202 dir = "DESC"
203 }
204 query += fmt.Sprintf(" ORDER BY %q %s", q.OrderBy, dir)
205 }
206
207 if q.Limit > 0 {
208 query += fmt.Sprintf(" LIMIT %d", q.Limit)
209 }
210 if q.Offset > 0 {
211 query += fmt.Sprintf(" OFFSET %d", q.Offset)
212 }
213
214 rows, err := d.db.QueryContext(ctx, query, args...)
215 if err != nil {
216 return fmt.Errorf("model/sqlite: list: %w", err)
217 }
218 defer rows.Close()
219
220 fieldMaps, err := scanRows(schema, rows)
221 if err != nil {
222 return err
223 }
224

Callers

nothing calls this directly

Calls 9

ApplyQueryOptionsFunction · 0.92
MapToStructFunction · 0.92
TypeMethod · 0.80
columnListFunction · 0.70
buildWhereFunction · 0.70
scanRowsFunction · 0.70
CloseMethod · 0.65
SetMethod · 0.65
ErrorfMethod · 0.45

Tested by

no test coverage detected