2021-08-25 13:34:33 +00:00
|
|
|
package bun
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"reflect"
|
2021-09-08 19:05:26 +00:00
|
|
|
"time"
|
2021-08-25 13:34:33 +00:00
|
|
|
|
2023-01-22 11:26:47 +00:00
|
|
|
"github.com/uptrace/bun/internal"
|
2021-08-25 13:34:33 +00:00
|
|
|
"github.com/uptrace/bun/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
type sliceTableModel struct {
|
|
|
|
structTableModel
|
|
|
|
|
|
|
|
slice reflect.Value
|
|
|
|
sliceLen int
|
|
|
|
sliceOfPtr bool
|
|
|
|
nextElem func() reflect.Value
|
|
|
|
}
|
|
|
|
|
2021-09-29 13:09:45 +00:00
|
|
|
var _ TableModel = (*sliceTableModel)(nil)
|
2021-08-25 13:34:33 +00:00
|
|
|
|
|
|
|
func newSliceTableModel(
|
|
|
|
db *DB, dest interface{}, slice reflect.Value, elemType reflect.Type,
|
|
|
|
) *sliceTableModel {
|
|
|
|
m := &sliceTableModel{
|
|
|
|
structTableModel: structTableModel{
|
|
|
|
db: db,
|
|
|
|
table: db.Table(elemType),
|
|
|
|
dest: dest,
|
|
|
|
root: slice,
|
|
|
|
},
|
|
|
|
|
|
|
|
slice: slice,
|
|
|
|
sliceLen: slice.Len(),
|
2023-01-22 11:26:47 +00:00
|
|
|
nextElem: internal.MakeSliceNextElemFunc(slice),
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
m.init(slice.Type())
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *sliceTableModel) init(sliceType reflect.Type) {
|
|
|
|
switch sliceType.Elem().Kind() {
|
|
|
|
case reflect.Ptr, reflect.Interface:
|
|
|
|
m.sliceOfPtr = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-29 13:09:45 +00:00
|
|
|
func (m *sliceTableModel) join(name string) *relationJoin {
|
|
|
|
return m._join(m.slice, name)
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *sliceTableModel) ScanRows(ctx context.Context, rows *sql.Rows) (int, error) {
|
|
|
|
columns, err := rows.Columns()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.columns = columns
|
|
|
|
dest := makeDest(m, len(columns))
|
|
|
|
|
|
|
|
if m.slice.IsValid() && m.slice.Len() > 0 {
|
|
|
|
m.slice.Set(m.slice.Slice(0, 0))
|
|
|
|
}
|
|
|
|
|
|
|
|
var n int
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
m.strct = m.nextElem()
|
|
|
|
m.structInited = false
|
|
|
|
|
|
|
|
if err := m.scanRow(ctx, rows, dest); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2021-10-24 11:14:37 +00:00
|
|
|
var _ schema.BeforeAppendModelHook = (*sliceTableModel)(nil)
|
|
|
|
|
|
|
|
func (m *sliceTableModel) BeforeAppendModel(ctx context.Context, query Query) error {
|
2022-04-24 10:26:22 +00:00
|
|
|
if !m.table.HasBeforeAppendModelHook() || !m.slice.IsValid() {
|
2021-10-24 11:14:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sliceLen := m.slice.Len()
|
|
|
|
for i := 0; i < sliceLen; i++ {
|
|
|
|
strct := m.slice.Index(i)
|
|
|
|
if !m.sliceOfPtr {
|
|
|
|
strct = strct.Addr()
|
|
|
|
}
|
|
|
|
err := strct.Interface().(schema.BeforeAppendModelHook).BeforeAppendModel(ctx, query)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
// Inherit these hooks from structTableModel.
|
|
|
|
var (
|
2021-10-24 11:14:37 +00:00
|
|
|
_ schema.BeforeScanRowHook = (*sliceTableModel)(nil)
|
|
|
|
_ schema.AfterScanRowHook = (*sliceTableModel)(nil)
|
2021-08-25 13:34:33 +00:00
|
|
|
)
|
|
|
|
|
2021-09-08 19:05:26 +00:00
|
|
|
func (m *sliceTableModel) updateSoftDeleteField(tm time.Time) error {
|
2021-08-25 13:34:33 +00:00
|
|
|
sliceLen := m.slice.Len()
|
|
|
|
for i := 0; i < sliceLen; i++ {
|
|
|
|
strct := indirect(m.slice.Index(i))
|
|
|
|
fv := m.table.SoftDeleteField.Value(strct)
|
2021-09-08 19:05:26 +00:00
|
|
|
if err := m.table.UpdateSoftDeleteField(fv, tm); err != nil {
|
2021-08-25 13:34:33 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|