当前位置: 首页>>代码示例>>Golang>>正文


Golang DB.New方法代码示例

本文整理汇总了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
}
开发者ID:NZAOM,项目名称:qor,代码行数:24,代码来源:sorting.go

示例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)
}
开发者ID:kingland,项目名称:qor,代码行数:75,代码来源:transition.go


注:本文中的github.com/jinzhu/gorm.DB.New方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。