Beispiel #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
}
Beispiel #2
0
func DemoTableName() {
	m := Model{}
	fmt.Printf("%s\n", mmodel.GetModelTableName(m))
	m2 := &Model{}
	fmt.Printf("%s\n", mmodel.GetModelTableName(m2))
}
Beispiel #3
0
// TableName
func TableName(model interface{}) string {
	return mmodel.GetModelTableName(model)
}
Beispiel #4
0
// GetModelFieldLabel
// returns model locale filed name message. Parameters:
// locale
// model can be a slice? no. do as name.
// field
// Field name save as column name.
// table name should be follow some rules.
func GetModelFieldLabel(locale string, model interface{}, field string) string {
	//table := mcore.GetTypeName(model)
	table := mmodel.GetModelTableName(model)
	column := field
	return GetTableColumnLabel(locale, table, column)
}