代码拉取完成,页面将自动刷新
同步操作将从 springrain/zorm 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package zorm
import (
"context"
"errors"
"log"
"time"
)
type Engine struct {
dbDao *DBDao
ctx context.Context
}
func NewEngine(dataSourceConfig *DataSourceConfig) (*Engine,error) {
db, err :=newDBDao(dataSourceConfig)
newCtx, err := db.BindContextDBConnection(context.Background())
if err != nil {
return nil, err
}
return &Engine{
dbDao: db,
ctx: newCtx,
},nil
}
func (engine *Engine) SelectTable(tableNameOrBean string, selectCols ...string) *Session {
return &Session{
finderTable: NewSelectFinder(wrapTableName(tableNameOrBean), selectCols...),
page: nil,
engine: engine,
}
}
func (engine *Engine) SelectMap(tableNameOrBean string, entity IEntityStruct) *Session {
return &Session{
finderTable: NewSelectFinder(wrapTableName(tableNameOrBean), wrapMapSelectCols(entity)),
page: nil,
engine: engine,
}
}
func (engine *Engine) DeleteTable(tableNameOrBean string) *Session {
return &Session{
finderTable: NewDeleteFinder(wrapTableName(tableNameOrBean)),
page: nil,
engine: engine,
}
}
func (engine *Engine) UpdateTable(tableNameOrBean string) *Session {
return &Session{
finderTable: NewUpdateFinder(wrapTableName(tableNameOrBean)),
page: nil,
engine: engine,
}
}
func (engine *Engine) SelectSql(sql string, args ...interface{}) *Session {
finder := NewFinder()
finder.Append(sql, args)
return &Session{
finderTable: finder,
page: nil,
engine: engine,
}
}
func (engine *Engine) Insert(entity IEntityStruct) error {
var err error
_, err = Transaction(engine.ctx, func(ctx context.Context) (interface{}, error) {
autoSetTime(entity, TagMap{"created", time.Now()})
_, err := Insert(engine.ctx, entity)
return nil, err
})
return err
}
func (engine *Engine) InsertMap(entity IEntityStruct) error {
var err error
_, err = Transaction(engine.ctx, func(ctx context.Context) (interface{}, error) {
autoSetTime(entity, TagMap{"created", time.Now()}, TagMap{"updated", time.Now()})
insertFinder, err := genInsertSql(entity)
if err != nil {
return nil, err
}
_, err = UpdateFinder(engine.ctx, insertFinder)
return nil, err
})
return err
}
func (engine *Engine) InsertSlice(entityStruct interface{}) error {
entityStructSlice, ok := entityStruct.([]IEntityStruct)
if !ok {
return errors.New("插入对象非[]IEntityStruct类型")
}
var err error
if entityStructSlice == nil || len(entityStructSlice) == 0 {
return nil
}
_, err = Transaction(engine.ctx, func(ctx context.Context) (interface{}, error) {
for _, entityStruct := range entityStructSlice {
autoSetTime(entityStruct, TagMap{"created", time.Now()}, TagMap{"updated", time.Now()})
}
_, err := InsertSlice(engine.ctx, entityStructSlice)
return nil, err
})
return err
}
func (engine *Engine) Exec(sql string, args ...interface{}) error {
var err error
_, err = Transaction(engine.ctx, func(ctx context.Context) (interface{}, error) {
finder := NewFinder()
finder.SelectTotalCount = false
finder.Append(sql, args)
_, err := UpdateFinder(engine.ctx, finder)
return nil, err
})
return err
}
func (engine *Engine) NewEngine() (*Engine, error) {
newCtx, err := engine.dbDao.BindContextDBConnection(context.Background())
if err != nil {
log.Fatalf("change ctx error: %s", err)
return nil, err
}
return &Engine{
dbDao: engine.dbDao,
ctx: newCtx,
}, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。