54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
package orm
|
|
|
|
import (
|
|
"github.com/jackc/pgx/v5"
|
|
"reflect"
|
|
"strings"
|
|
)
|
|
|
|
func buildScanDest(val reflect.Value, model *Model, fk *Relationship, cols []string, anonymousCols map[string]map[string]*Field, fkDest any) ([]any, error) {
|
|
var dest []any
|
|
|
|
for _, col := range cols {
|
|
bcol := col
|
|
if strings.Contains(bcol, ".") {
|
|
_, bcol, _ = strings.Cut(bcol, ".")
|
|
}
|
|
field := model.FieldsByColumnName[bcol]
|
|
if field != nil && !field.isAnonymous() {
|
|
dest = append(dest, val.FieldByName(field.Name).Addr().Interface())
|
|
}
|
|
}
|
|
for fn, a := range anonymousCols {
|
|
iv := val.FieldByName(fn)
|
|
for _, field := range a {
|
|
dest = append(dest, iv.FieldByName(field.Name).Addr().Interface())
|
|
}
|
|
}
|
|
|
|
if fk.Type != BelongsTo {
|
|
dest = append(dest, fkDest)
|
|
}
|
|
|
|
return dest, nil
|
|
}
|
|
func scanRow(row pgx.Row, cols []string, anonymousCols map[string][]string, destVal reflect.Value, m *Model) error {
|
|
var scanDest []any
|
|
for _, col := range cols {
|
|
f := m.FieldsByColumnName[col]
|
|
if f != nil && f.ColumnType != "" && !f.isAnonymous() {
|
|
scanDest = append(scanDest, destVal.FieldByIndex(f.Original.Index).Addr().Interface())
|
|
}
|
|
}
|
|
for kcol := range anonymousCols {
|
|
f := m.FieldsByColumnName[kcol]
|
|
if f != nil {
|
|
for _, ef := range f.embeddedFields {
|
|
scanDest = append(scanDest, destVal.FieldByIndex(f.Original.Index).FieldByName(ef.Name).Addr().Interface())
|
|
}
|
|
}
|
|
}
|
|
|
|
return row.Scan(scanDest...)
|
|
}
|