1 Star 1 Fork 0

bigbase / pg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
model_table.go 2.30 KB
一键复制 编辑 原始数据 按行查看 历史
package orm
import (
"errors"
"fmt"
"reflect"
)
type tableModel interface {
Table() *Table
Model
Join(string) *join
GetJoin(string) *join
GetJoins() []join
AddJoin(join) *join
Root() reflect.Value
Path() []int
Bind(reflect.Value)
Value() reflect.Value
scanColumn(int, string, []byte) (bool, error)
}
func newTableModel(v interface{}) (tableModel, error) {
switch v := v.(type) {
case tableModel:
return v, nil
case reflect.Value:
return newTableModelValue(v)
default:
vv := reflect.ValueOf(v)
if !vv.IsValid() {
return nil, errors.New("pg: Model(nil)")
}
if vv.Kind() != reflect.Ptr {
return nil, fmt.Errorf("pg: Model(nonsettable %T)", v)
}
return newTableModelValue(vv.Elem())
}
}
func newTableModelValue(v reflect.Value) (tableModel, error) {
if !v.IsValid() {
return nil, errors.New("pg: Model(nil)")
}
v = reflect.Indirect(v)
switch v.Kind() {
case reflect.Struct:
return newStructTableModel(v)
case reflect.Slice:
elType := indirectType(v.Type().Elem())
if elType.Kind() == reflect.Interface && v.Len() > 0 {
elType = reflect.Indirect(v.Index(0).Elem()).Type()
}
if elType.Kind() == reflect.Struct {
return &sliceTableModel{
structTableModel: structTableModel{
table: Tables.Get(elType),
root: v,
},
slice: v,
}, nil
}
}
return nil, fmt.Errorf("pg: Model(unsupported %s)", v.Type())
}
func newTableModelPath(root reflect.Value, path []int, table *Table) (tableModel, error) {
v := fieldByPath(root, path)
v = reflect.Indirect(v)
if v.Kind() == reflect.Struct {
return &structTableModel{
table: Tables.Get(v.Type()),
root: root,
path: path,
}, nil
}
if v.Kind() == reflect.Slice {
elType := indirectType(v.Type().Elem())
if elType.Kind() == reflect.Struct {
return &sliceTableModel{
structTableModel: structTableModel{
table: Tables.Get(elType),
root: root,
path: path,
},
}, nil
}
}
return nil, fmt.Errorf("pg: newTableModelPath(path %s on %s)", path, root.Type())
}
func fieldByPath(v reflect.Value, path []int) reflect.Value {
for _, index := range path {
if v.Kind() == reflect.Slice {
v = reflect.Zero(v.Type().Elem())
}
v = v.Field(index)
if v.Kind() == reflect.Ptr {
if v.IsNil() {
v = reflect.New(v.Type().Elem())
}
v = v.Elem()
}
}
return v
}
Go
1
https://gitee.com/bigbase/pg.git
git@gitee.com:bigbase/pg.git
bigbase
pg
pg
v4.5.4

搜索帮助