51 lines
981 B
Go
51 lines
981 B
Go
package mySql
|
|
|
|
import "gorm.io/gorm"
|
|
|
|
type Facade struct {
|
|
tableName string
|
|
dbFunc func() *gorm.DB
|
|
}
|
|
|
|
func NewFacade(tableName string, dbFunc func() *gorm.DB) *Facade {
|
|
return &Facade{tableName: tableName, dbFunc: dbFunc}
|
|
}
|
|
|
|
func (t *Facade) NewQuery() *Query {
|
|
if t.dbFunc != nil {
|
|
return NewQuery(t.tableName).SetDB(t.dbFunc())
|
|
}
|
|
return NewQuery(t.tableName)
|
|
}
|
|
|
|
func (t *Facade) NewUpdate() *Update {
|
|
if t.dbFunc != nil {
|
|
return NewUpdate(t.tableName).SetDB(t.dbFunc())
|
|
}
|
|
return NewUpdate(t.tableName)
|
|
}
|
|
|
|
func (t *Facade) NewInsert() *Insert {
|
|
if t.dbFunc != nil {
|
|
return NewInsert(t.tableName).SetDB(t.dbFunc())
|
|
}
|
|
return NewInsert(t.tableName)
|
|
}
|
|
|
|
func (t *Facade) NewDelete() *Delete {
|
|
if t.dbFunc != nil {
|
|
return NewDelete(t.tableName).SetDB(t.dbFunc())
|
|
}
|
|
return NewDelete(t.tableName)
|
|
}
|
|
|
|
func (t *Facade) Create(val interface{}) error {
|
|
if t.dbFunc != nil {
|
|
db := t.dbFunc()
|
|
if db != nil {
|
|
return Create(val, db)
|
|
}
|
|
}
|
|
return Create(val)
|
|
}
|