Select performs the SELECT query for this database (dsn database name is required).
(ctx context.Context, dest interface{}, query string, args ...interface{})
| 58 | |
| 59 | // Select performs the SELECT query for this database (dsn database name is required). |
| 60 | func (db *MySQL) Select(ctx context.Context, dest interface{}, query string, args ...interface{}) error { |
| 61 | rows, err := db.Conn.QueryContext(ctx, query, args...) |
| 62 | if err != nil { |
| 63 | return err |
| 64 | } |
| 65 | defer rows.Close() |
| 66 | |
| 67 | if scannable, ok := dest.(Scannable); ok { |
| 68 | return scannable.Scan(rows) |
| 69 | } |
| 70 | |
| 71 | if !rows.Next() { |
| 72 | return ErrNoRows |
| 73 | } |
| 74 | return rows.Scan(dest) |
| 75 | |
| 76 | /* Uncomment this and pass a slice if u want to see reflection powers <3 |
| 77 | v, ok := dest.(reflect.Value) |
| 78 | if !ok { |
| 79 | v = reflect.Indirect(reflect.ValueOf(dest)) |
| 80 | } |
| 81 | |
| 82 | sliceTyp := v.Type() |
| 83 | |
| 84 | if sliceTyp.Kind() != reflect.Slice { |
| 85 | sliceTyp = reflect.SliceOf(sliceTyp) |
| 86 | } |
| 87 | |
| 88 | sliceElementTyp := deref(sliceTyp.Elem()) |
| 89 | for rows.Next() { |
| 90 | obj := reflect.New(sliceElementTyp) |
| 91 | obj.Interface().(Scannable).Scan(rows) |
| 92 | if err != nil { |
| 93 | return err |
| 94 | } |
| 95 | |
| 96 | v.Set(reflect.Append(v, reflect.Indirect(obj))) |
| 97 | } |
| 98 | */ |
| 99 | } |
| 100 | |
| 101 | // Get same as `Select` but it moves the cursor to the first result. |
| 102 | func (db *MySQL) Get(ctx context.Context, dest interface{}, query string, args ...interface{}) error { |