本文整理汇总了Golang中github.com/jinzhu/gorm.DB.New方法的典型用法代码示例。如果您正苦于以下问题:Golang DB.New方法的具体用法?Golang DB.New怎么用?Golang DB.New使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/jinzhu/gorm.DB
的用法示例。
在下文中一共展示了DB.New方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: createPublishEvent
func createPublishEvent(db *gorm.DB, value interface{}) (err error) {
// Create Publish Event in Draft Mode
if publish.IsDraftMode(db) && publish.IsPublishableModel(value) {
scope := db.NewScope(value)
var sortingPublishEvent = changedSortingPublishEvent{
Table: scope.TableName(),
}
for _, field := range scope.PrimaryFields() {
sortingPublishEvent.PrimaryKeys = append(sortingPublishEvent.PrimaryKeys, field.DBName)
}
var result []byte
if result, err = json.Marshal(sortingPublishEvent); err == nil {
err = db.New().Where("publish_status = ?", publish.DIRTY).Where(map[string]interface{}{
"name": "changed_sorting",
"argument": string(result),
}).Attrs(map[string]interface{}{
"publish_status": publish.DIRTY,
"description": "Changed sort order for " + scope.GetModelStruct().ModelType.Name(),
}).FirstOrCreate(&publish.PublishEvent{}).Error
}
}
return
}
示例2: Trigger
func (sm *StateMachine) Trigger(name string, value Stater, tx *gorm.DB, notes ...string) error {
stateWas := value.GetState()
if stateWas == "" {
stateWas = sm.initialState
}
newTx := tx.New()
if event := sm.events[name]; event != nil {
var matchedTransitions []*EventTransition
for _, transition := range event.transitions {
var validFrom = len(transition.froms) == 0
if len(transition.froms) > 0 {
for _, from := range transition.froms {
if from == stateWas {
validFrom = true
}
}
}
if validFrom {
matchedTransitions = append(matchedTransitions, transition)
}
}
if len(matchedTransitions) == 1 {
transition := matchedTransitions[0]
// State: exit
if state, ok := sm.states[stateWas]; ok {
for _, exit := range state.exits {
if err := exit(value, newTx); err != nil {
return err
}
}
}
// Transition: before
for _, before := range transition.befores {
if err := before(value, newTx); err != nil {
return err
}
}
value.SetState(transition.to)
// State: enter
if state, ok := sm.states[transition.to]; ok {
for _, enter := range state.enters {
if err := enter(value, newTx); err != nil {
return err
}
}
}
// Transition: after
for _, after := range transition.afters {
if err := after(value, newTx); err != nil {
return err
}
}
scope := newTx.NewScope(value)
primaryKey := fmt.Sprintf("%v", scope.PrimaryKeyValue())
log := StateChangeLog{
ReferTable: scope.TableName(),
ReferId: primaryKey,
From: stateWas,
To: transition.to,
Note: strings.Join(notes, ""),
}
return newTx.Save(&log).Error
}
}
return fmt.Errorf("failed to perform event %s from state %s", name, stateWas)
}