Exemple #1
0
// SaveOrUpdate
func (s Xorm) SaveOrUpdate(model interface{}) (int64, error) {
	// id == ""; insert with new uuid.
	id := mmodel.GetModelId(model)
	if id == "" {
		logger.Trace("uuid save")
		return s.UuidSave(model)
	}

	// id!=null && id in db, update
	tableName := mmodel.GetModelTableName(model)
	q := fmt.Sprintf("select Id from %s where Id = ? ", tableName)
	row, _ := s.Engine.Query(q, id)
	if len(row) > 0 {
		logger.Trace("update")
		n, err := s.Engine.Id(id).Update(model)
		logger.CheckError(err)
		return n, err
	}

	// id != null && id not in db, insert with provide id
	logger.Trace("normal insert.")
	n, err := s.Engine.Insert(model)
	logger.CheckError(err)
	return n, err
}
Exemple #2
0
// Id is uuid value.
// model is a pointer.
func (s Xorm) UuidSave(model interface{}) (int64, error) {
	logger.Trace("UuidSave modell")
	if mmodel.GetModelId(model) == "" {
		mmodel.AddModelUuid(model)
	}
	n, err := s.Engine.Insert(model)
	logger.CheckError(err)
	return n, err
}
Exemple #3
0
// model must have field Id and Id not null.
func (s Xorm) Update(model interface{}) (int64, error) {
	id := mmodel.GetModelId(model)
	if id == "" {
		err := fmt.Errorf("Cannot update model without Id")
		return 0, err
	}
	n, err := s.Engine.Id(id).Update(model)
	logger.CheckError(err)
	return n, err
}
Exemple #4
0
func Demo(m1 interface{}) {
	fmt.Println(mmodel.GetModelId(m1))
	m2 := mmodel.AddModelUuid(m1)
	fmt.Printf("%+v\n", m1)
	fmt.Printf("%+v\n", m2)
}