本文整理汇总了Golang中github.com/jinzhu/gorm.DB.Unscoped方法的典型用法代码示例。如果您正苦于以下问题:Golang DB.Unscoped方法的具体用法?Golang DB.Unscoped怎么用?Golang DB.Unscoped使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/jinzhu/gorm.DB
的用法示例。
在下文中一共展示了DB.Unscoped方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Monit
// Monit monitors the database and file system to remove old entries
func Monit(db *gorm.DB) {
log.Println("[INFO][System]\tStarted monitoring of files and db entries")
tc := time.NewTicker(1 * time.Minute)
for {
res := []models.ResourceEntry{}
db.Find(&res, "created_at < ?", time.Now().Add(-timeLimit))
db.Unscoped().Where("created_at < ?", time.Now().Add(-timeLimit)).Delete(&models.ResourceEntry{})
if len(res) > 0 {
log.Printf("[INFO][System]\tFlushing %d DB entries and files.\n", len(res))
}
for _, re := range res {
err := os.Remove(path.Join(conf.C.UploadDir, re.Key))
if err != nil {
log.Printf("[ERROR][System]\tWhile deleting : %v", err)
}
}
<-tc.C
}
}
示例2: Delete
func (p *Point) Delete(db gorm.DB) {
db.Unscoped().Delete(p)
}
示例3: New
// New initialize a publish instance
func New(db *gorm.DB) *Publish {
tableHandler := gorm.DefaultTableNameHandler
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
tableName := tableHandler(db, defaultTableName)
if db != nil {
if IsPublishableModel(db.Value) {
// Set join table handler
typ := utils.ModelType(db.Value)
if !injectedJoinTableHandler[typ] {
injectedJoinTableHandler[typ] = true
scope := db.NewScope(db.Value)
for _, field := range scope.GetModelStruct().StructFields {
if many2many := utils.ParseTagOption(field.Tag.Get("gorm"))["MANY2MANY"]; many2many != "" {
db.SetJoinTableHandler(db.Value, field.Name, &publishJoinTableHandler{})
db.AutoMigrate(db.Value)
}
}
}
var forceDraftTable bool
if forceDraft, ok := db.Get("publish:force_draft_table"); ok {
if forceMode, ok := forceDraft.(bool); ok && forceMode {
forceDraftTable = true
}
}
if IsDraftMode(db) || forceDraftTable {
return DraftTableName(tableName)
}
}
}
return tableName
}
db.AutoMigrate(&PublishEvent{})
db.Callback().Create().Before("gorm:begin_transaction").Register("publish:set_table_to_draft", setTableAndPublishStatus(true))
db.Callback().Create().Before("gorm:commit_or_rollback_transaction").
Register("publish:sync_to_production_after_create", syncCreateFromProductionToDraft)
db.Callback().Create().Before("gorm:commit_or_rollback_transaction").Register("gorm:create_publish_event", createPublishEvent)
db.Callback().Delete().Before("gorm:begin_transaction").Register("publish:set_table_to_draft", setTableAndPublishStatus(true))
deleteCallback := db.Callback().Delete().Get("gorm:delete")
db.Callback().Delete().Replace("gorm:delete", deleteScope)
db.Callback().Delete().Before("gorm:commit_or_rollback_transaction").
Register("publish:sync_to_production_after_delete", syncDeleteFromProductionToDraft)
db.Callback().Delete().Before("gorm:commit_or_rollback_transaction").Register("gorm:create_publish_event", createPublishEvent)
db.Callback().Update().Before("gorm:begin_transaction").Register("publish:set_table_to_draft", setTableAndPublishStatus(true))
db.Callback().Update().Before("gorm:commit_or_rollback_transaction").
Register("publish:sync_to_production", syncUpdateFromProductionToDraft)
db.Callback().Update().Before("gorm:commit_or_rollback_transaction").Register("gorm:create_publish_event", createPublishEvent)
db.Callback().RowQuery().Register("publish:set_table_in_draft_mode", setTableAndPublishStatus(false))
db.Callback().Query().Before("gorm:query").Register("publish:set_table_in_draft_mode", setTableAndPublishStatus(false))
searchHandler := func(db *gorm.DB, context *qor.Context) *gorm.DB {
return db.Unscoped()
}
return &Publish{SearchHandler: searchHandler, DB: db, deleteCallback: deleteCallback, logger: Logger}
}