2021-08-25 14:34:33 +01:00
|
|
|
package bun
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"reflect"
|
2021-10-24 12:14:37 +01:00
|
|
|
|
|
|
|
"github.com/uptrace/bun/schema"
|
2021-08-25 14:34:33 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type scanModel struct {
|
|
|
|
db *DB
|
|
|
|
|
|
|
|
dest []interface{}
|
|
|
|
scanIndex int
|
|
|
|
}
|
|
|
|
|
2021-09-29 14:09:45 +01:00
|
|
|
var _ Model = (*scanModel)(nil)
|
2021-08-25 14:34:33 +01:00
|
|
|
|
|
|
|
func newScanModel(db *DB, dest []interface{}) *scanModel {
|
|
|
|
return &scanModel{
|
|
|
|
db: db,
|
|
|
|
dest: dest,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *scanModel) Value() interface{} {
|
|
|
|
return m.dest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *scanModel) ScanRows(ctx context.Context, rows *sql.Rows) (int, error) {
|
|
|
|
if !rows.Next() {
|
|
|
|
return 0, rows.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
dest := makeDest(m, len(m.dest))
|
|
|
|
|
|
|
|
m.scanIndex = 0
|
|
|
|
if err := rows.Scan(dest...); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *scanModel) ScanRow(ctx context.Context, rows *sql.Rows) error {
|
|
|
|
return rows.Scan(m.dest...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *scanModel) Scan(src interface{}) error {
|
|
|
|
dest := reflect.ValueOf(m.dest[m.scanIndex])
|
|
|
|
m.scanIndex++
|
|
|
|
|
2021-10-24 12:14:37 +01:00
|
|
|
scanner := schema.Scanner(dest.Type())
|
2021-08-25 14:34:33 +01:00
|
|
|
return scanner(dest, src)
|
|
|
|
}
|