1 Star 1 Fork 0

bigbase/pg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
model_table_m2m.go 2.33 KB
一键复制 编辑 原始数据 按行查看 历史
package orm
import (
"fmt"
"reflect"
)
type m2mModel struct {
*sliceTableModel
baseTable *Table
rel *Relation
buf []byte
zeroStruct reflect.Value
dstValues map[string][]reflect.Value
columns map[string]string
}
var _ tableModel = (*m2mModel)(nil)
func newM2MModel(join *join) *m2mModel {
baseTable := join.BaseModel.Table()
joinModel := join.JoinModel.(*sliceTableModel)
dstValues := dstValues(joinModel.Root(), joinModel.Index(), baseTable.PKs)
m := &m2mModel{
sliceTableModel: joinModel,
baseTable: baseTable,
rel: join.Rel,
dstValues: dstValues,
columns: make(map[string]string),
}
if !m.sliceOfPtr {
m.strct = reflect.New(m.table.Type).Elem()
m.zeroStruct = reflect.Zero(m.table.Type)
}
return m
}
func (m *m2mModel) NewModel() ColumnScanner {
if m.sliceOfPtr {
m.strct = reflect.New(m.table.Type).Elem()
} else {
m.strct.Set(m.zeroStruct)
}
m.structTableModel.NewModel()
return m
}
func (m *m2mModel) AddModel(model ColumnScanner) error {
m.buf = modelIdMap(m.buf[:0], m.columns, m.baseTable.ModelName+"_", m.baseTable.PKs)
dstValues, ok := m.dstValues[string(m.buf)]
if !ok {
return fmt.Errorf("pg: can't find dst value for model id=%q", m.buf)
}
for _, v := range dstValues {
if m.sliceOfPtr {
v.Set(reflect.Append(v, m.strct.Addr()))
} else {
v.Set(reflect.Append(v, m.strct))
}
}
return nil
}
func (m *m2mModel) AfterQuery(db DB) error {
if !m.rel.JoinTable.Has(AfterQueryHookFlag) {
return nil
}
var retErr error
for _, slices := range m.dstValues {
for _, slice := range slices {
err := callAfterQueryHookSlice(slice, m.sliceOfPtr, db)
if err != nil && retErr == nil {
retErr = err
}
}
}
return retErr
}
func (m *m2mModel) AfterSelect(db DB) error {
return nil
}
func (m *m2mModel) BeforeInsert(db DB) error {
return nil
}
func (m *m2mModel) AfterInsert(db DB) error {
return nil
}
func (m *m2mModel) BeforeUpdate(db DB) error {
return nil
}
func (m *m2mModel) AfterUpdate(db DB) error {
return nil
}
func (m *m2mModel) BeforeDelete(db DB) error {
return nil
}
func (m *m2mModel) AfterDelete(db DB) error {
return nil
}
func (m *m2mModel) ScanColumn(colIdx int, colName string, b []byte) error {
ok, err := m.sliceTableModel.scanColumn(colIdx, colName, b)
if ok {
return err
}
m.columns[colName] = string(b)
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/bigbase/pg.git
git@gitee.com:bigbase/pg.git
bigbase
pg
pg
v5.0.12

搜索帮助