2021-08-25 13:34:33 +00:00
|
|
|
package bun
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
2021-09-08 19:05:26 +00:00
|
|
|
"time"
|
2021-08-25 13:34:33 +00:00
|
|
|
|
2023-09-11 13:16:52 +00:00
|
|
|
"github.com/uptrace/bun/internal"
|
2021-08-25 13:34:33 +00:00
|
|
|
"github.com/uptrace/bun/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
type structTableModel struct {
|
|
|
|
db *DB
|
|
|
|
table *schema.Table
|
|
|
|
|
|
|
|
rel *schema.Relation
|
2021-09-08 19:05:26 +00:00
|
|
|
joins []relationJoin
|
2021-08-25 13:34:33 +00:00
|
|
|
|
|
|
|
dest interface{}
|
|
|
|
root reflect.Value
|
|
|
|
index []int
|
|
|
|
|
|
|
|
strct reflect.Value
|
|
|
|
structInited bool
|
|
|
|
structInitErr error
|
|
|
|
|
|
|
|
columns []string
|
|
|
|
scanIndex int
|
|
|
|
}
|
|
|
|
|
2021-09-29 13:09:45 +00:00
|
|
|
var _ TableModel = (*structTableModel)(nil)
|
2021-08-25 13:34:33 +00:00
|
|
|
|
|
|
|
func newStructTableModel(db *DB, dest interface{}, table *schema.Table) *structTableModel {
|
|
|
|
return &structTableModel{
|
|
|
|
db: db,
|
|
|
|
table: table,
|
|
|
|
dest: dest,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newStructTableModelValue(db *DB, dest interface{}, v reflect.Value) *structTableModel {
|
|
|
|
return &structTableModel{
|
|
|
|
db: db,
|
|
|
|
table: db.Table(v.Type()),
|
|
|
|
dest: dest,
|
|
|
|
root: v,
|
|
|
|
strct: v,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *structTableModel) Value() interface{} {
|
|
|
|
return m.dest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *structTableModel) Table() *schema.Table {
|
|
|
|
return m.table
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *structTableModel) Relation() *schema.Relation {
|
|
|
|
return m.rel
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *structTableModel) initStruct() error {
|
|
|
|
if m.structInited {
|
|
|
|
return m.structInitErr
|
|
|
|
}
|
|
|
|
m.structInited = true
|
|
|
|
|
|
|
|
switch m.strct.Kind() {
|
|
|
|
case reflect.Invalid:
|
|
|
|
m.structInitErr = errNilModel
|
|
|
|
return m.structInitErr
|
|
|
|
case reflect.Interface:
|
|
|
|
m.strct = m.strct.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.strct.Kind() == reflect.Ptr {
|
|
|
|
if m.strct.IsNil() {
|
|
|
|
m.strct.Set(reflect.New(m.strct.Type().Elem()))
|
|
|
|
m.strct = m.strct.Elem()
|
|
|
|
} else {
|
|
|
|
m.strct = m.strct.Elem()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.mountJoins()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *structTableModel) mountJoins() {
|
|
|
|
for i := range m.joins {
|
|
|
|
j := &m.joins[i]
|
|
|
|
switch j.Relation.Type {
|
|
|
|
case schema.HasOneRelation, schema.BelongsToRelation:
|
2021-09-29 13:09:45 +00:00
|
|
|
j.JoinModel.mount(m.strct)
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-24 11:14:37 +00:00
|
|
|
var _ schema.BeforeAppendModelHook = (*structTableModel)(nil)
|
2021-08-25 13:34:33 +00:00
|
|
|
|
2021-10-24 11:14:37 +00:00
|
|
|
func (m *structTableModel) BeforeAppendModel(ctx context.Context, query Query) error {
|
|
|
|
if !m.table.HasBeforeAppendModelHook() || !m.strct.IsValid() {
|
2021-08-25 13:34:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-10-24 11:14:37 +00:00
|
|
|
return m.strct.Addr().Interface().(schema.BeforeAppendModelHook).BeforeAppendModel(ctx, query)
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 11:14:37 +00:00
|
|
|
var _ schema.BeforeScanRowHook = (*structTableModel)(nil)
|
2021-08-25 13:34:33 +00:00
|
|
|
|
2021-10-24 11:14:37 +00:00
|
|
|
func (m *structTableModel) BeforeScanRow(ctx context.Context) error {
|
|
|
|
if m.table.HasBeforeScanRowHook() {
|
|
|
|
return m.strct.Addr().Interface().(schema.BeforeScanRowHook).BeforeScanRow(ctx)
|
|
|
|
}
|
|
|
|
if m.table.HasBeforeScanHook() {
|
|
|
|
return m.strct.Addr().Interface().(schema.BeforeScanHook).BeforeScan(ctx)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ schema.AfterScanRowHook = (*structTableModel)(nil)
|
|
|
|
|
|
|
|
func (m *structTableModel) AfterScanRow(ctx context.Context) error {
|
|
|
|
if !m.structInited {
|
2021-08-25 13:34:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-24 11:14:37 +00:00
|
|
|
if m.table.HasAfterScanRowHook() {
|
|
|
|
firstErr := m.strct.Addr().Interface().(schema.AfterScanRowHook).AfterScanRow(ctx)
|
|
|
|
|
|
|
|
for _, j := range m.joins {
|
|
|
|
switch j.Relation.Type {
|
|
|
|
case schema.HasOneRelation, schema.BelongsToRelation:
|
|
|
|
if err := j.JoinModel.AfterScanRow(ctx); err != nil && firstErr == nil {
|
|
|
|
firstErr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-25 13:34:33 +00:00
|
|
|
|
2021-10-24 11:14:37 +00:00
|
|
|
return firstErr
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 11:14:37 +00:00
|
|
|
if m.table.HasAfterScanHook() {
|
|
|
|
firstErr := m.strct.Addr().Interface().(schema.AfterScanHook).AfterScan(ctx)
|
|
|
|
|
|
|
|
for _, j := range m.joins {
|
|
|
|
switch j.Relation.Type {
|
|
|
|
case schema.HasOneRelation, schema.BelongsToRelation:
|
|
|
|
if err := j.JoinModel.AfterScanRow(ctx); err != nil && firstErr == nil {
|
|
|
|
firstErr = err
|
|
|
|
}
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-24 11:14:37 +00:00
|
|
|
|
|
|
|
return firstErr
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 11:14:37 +00:00
|
|
|
return nil
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 13:09:45 +00:00
|
|
|
func (m *structTableModel) getJoin(name string) *relationJoin {
|
2021-08-25 13:34:33 +00:00
|
|
|
for i := range m.joins {
|
|
|
|
j := &m.joins[i]
|
|
|
|
if j.Relation.Field.Name == name || j.Relation.Field.GoName == name {
|
|
|
|
return j
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-29 13:09:45 +00:00
|
|
|
func (m *structTableModel) getJoins() []relationJoin {
|
2021-08-25 13:34:33 +00:00
|
|
|
return m.joins
|
|
|
|
}
|
|
|
|
|
2021-09-29 13:09:45 +00:00
|
|
|
func (m *structTableModel) addJoin(j relationJoin) *relationJoin {
|
2021-08-25 13:34:33 +00:00
|
|
|
m.joins = append(m.joins, j)
|
|
|
|
return &m.joins[len(m.joins)-1]
|
|
|
|
}
|
|
|
|
|
2021-09-29 13:09:45 +00:00
|
|
|
func (m *structTableModel) join(name string) *relationJoin {
|
|
|
|
return m._join(m.strct, name)
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 13:09:45 +00:00
|
|
|
func (m *structTableModel) _join(bind reflect.Value, name string) *relationJoin {
|
2021-08-25 13:34:33 +00:00
|
|
|
path := strings.Split(name, ".")
|
|
|
|
index := make([]int, 0, len(path))
|
|
|
|
|
2021-09-08 19:05:26 +00:00
|
|
|
currJoin := relationJoin{
|
2021-08-25 13:34:33 +00:00
|
|
|
BaseModel: m,
|
|
|
|
JoinModel: m,
|
|
|
|
}
|
2021-09-08 19:05:26 +00:00
|
|
|
var lastJoin *relationJoin
|
2021-08-25 13:34:33 +00:00
|
|
|
|
|
|
|
for _, name := range path {
|
|
|
|
relation, ok := currJoin.JoinModel.Table().Relations[name]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
currJoin.Relation = relation
|
|
|
|
index = append(index, relation.Field.Index...)
|
|
|
|
|
2021-09-29 13:09:45 +00:00
|
|
|
if j := currJoin.JoinModel.getJoin(name); j != nil {
|
2021-08-25 13:34:33 +00:00
|
|
|
currJoin.BaseModel = j.BaseModel
|
|
|
|
currJoin.JoinModel = j.JoinModel
|
|
|
|
|
|
|
|
lastJoin = j
|
|
|
|
} else {
|
|
|
|
model, err := newTableModelIndex(m.db, m.table, bind, index, relation)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
currJoin.Parent = lastJoin
|
|
|
|
currJoin.BaseModel = currJoin.JoinModel
|
|
|
|
currJoin.JoinModel = model
|
|
|
|
|
2021-09-29 13:09:45 +00:00
|
|
|
lastJoin = currJoin.BaseModel.addJoin(currJoin)
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastJoin
|
|
|
|
}
|
|
|
|
|
2021-09-29 13:09:45 +00:00
|
|
|
func (m *structTableModel) rootValue() reflect.Value {
|
|
|
|
return m.root
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *structTableModel) parentIndex() []int {
|
|
|
|
return m.index[:len(m.index)-len(m.rel.Field.Index)]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *structTableModel) mount(host reflect.Value) {
|
2023-09-11 13:16:52 +00:00
|
|
|
m.strct = internal.FieldByIndexAlloc(host, m.rel.Field.Index)
|
2021-09-29 13:09:45 +00:00
|
|
|
m.structInited = false
|
|
|
|
}
|
|
|
|
|
2021-09-08 19:05:26 +00:00
|
|
|
func (m *structTableModel) updateSoftDeleteField(tm time.Time) error {
|
2021-09-23 09:13:28 +00:00
|
|
|
if !m.strct.IsValid() {
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-25 13:34:33 +00:00
|
|
|
fv := m.table.SoftDeleteField.Value(m.strct)
|
2021-09-08 19:05:26 +00:00
|
|
|
return m.table.UpdateSoftDeleteField(fv, tm)
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *structTableModel) ScanRows(ctx context.Context, rows *sql.Rows) (int, error) {
|
|
|
|
if !rows.Next() {
|
|
|
|
return 0, rows.Err()
|
|
|
|
}
|
|
|
|
|
2021-09-08 19:05:26 +00:00
|
|
|
var n int
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
if err := m.ScanRow(ctx, rows); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-09-08 19:05:26 +00:00
|
|
|
n++
|
2021-08-25 13:34:33 +00:00
|
|
|
|
2021-09-08 19:05:26 +00:00
|
|
|
// And discard the rest. This is especially important for SQLite3, which can return
|
|
|
|
// a row like it was inserted sucessfully and then return an actual error for the next row.
|
|
|
|
// See issues/100.
|
|
|
|
for rows.Next() {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return 0, err
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 19:05:26 +00:00
|
|
|
return n, nil
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *structTableModel) ScanRow(ctx context.Context, rows *sql.Rows) error {
|
|
|
|
columns, err := rows.Columns()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.columns = columns
|
|
|
|
dest := makeDest(m, len(columns))
|
|
|
|
|
|
|
|
return m.scanRow(ctx, rows, dest)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *structTableModel) scanRow(ctx context.Context, rows *sql.Rows, dest []interface{}) error {
|
2021-10-24 11:14:37 +00:00
|
|
|
if err := m.BeforeScanRow(ctx); err != nil {
|
2021-08-25 13:34:33 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.scanIndex = 0
|
|
|
|
if err := rows.Scan(dest...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-24 11:14:37 +00:00
|
|
|
if err := m.AfterScanRow(ctx); err != nil {
|
2021-08-25 13:34:33 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *structTableModel) Scan(src interface{}) error {
|
|
|
|
column := m.columns[m.scanIndex]
|
|
|
|
m.scanIndex++
|
|
|
|
|
|
|
|
return m.ScanColumn(unquote(column), src)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *structTableModel) ScanColumn(column string, src interface{}) error {
|
|
|
|
if ok, err := m.scanColumn(column, src); ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if column == "" || column[0] == '_' || m.db.flags.Has(discardUnknownColumns) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("bun: %s does not have column %q", m.table.TypeName, column)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *structTableModel) scanColumn(column string, src interface{}) (bool, error) {
|
|
|
|
if src != nil {
|
|
|
|
if err := m.initStruct(); err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if field, ok := m.table.FieldMap[column]; ok {
|
2021-09-08 19:05:26 +00:00
|
|
|
if src == nil && m.isNil() {
|
|
|
|
return true, nil
|
|
|
|
}
|
2021-08-25 13:34:33 +00:00
|
|
|
return true, field.ScanValue(m.strct, src)
|
|
|
|
}
|
|
|
|
|
|
|
|
if joinName, column := splitColumn(column); joinName != "" {
|
2021-09-29 13:09:45 +00:00
|
|
|
if join := m.getJoin(joinName); join != nil {
|
2021-08-25 13:34:33 +00:00
|
|
|
return true, join.JoinModel.ScanColumn(column, src)
|
|
|
|
}
|
2021-09-08 19:05:26 +00:00
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
if m.table.ModelName == joinName {
|
|
|
|
return true, m.ScanColumn(column, src)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2021-09-08 19:05:26 +00:00
|
|
|
func (m *structTableModel) isNil() bool {
|
|
|
|
return m.strct.Kind() == reflect.Ptr && m.strct.IsNil()
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (m *structTableModel) AppendNamedArg(
|
|
|
|
fmter schema.Formatter, b []byte, name string,
|
|
|
|
) ([]byte, bool) {
|
|
|
|
return m.table.AppendNamedArg(fmter, b, name, m.strct)
|
|
|
|
}
|
|
|
|
|
|
|
|
// sqlite3 sometimes does not unquote columns.
|
|
|
|
func unquote(s string) string {
|
|
|
|
if s == "" {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
if s[0] == '"' && s[len(s)-1] == '"' {
|
|
|
|
return s[1 : len(s)-1]
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func splitColumn(s string) (string, string) {
|
|
|
|
if i := strings.Index(s, "__"); i >= 0 {
|
|
|
|
return s[:i], s[i+2:]
|
|
|
|
}
|
|
|
|
return "", s
|
|
|
|
}
|