Пример #1
0
func SoftDeleteByIdAndVersion(DB db.IDb, table *db.Table, id int64, version int64) error {
	deletion := table.GetDeletionColumn()
	if deletion == nil {
		return dbx.NewPersistenceFail("DAOUtils.SoftDeleteByIdAndVersion", "Table "+table.GetName()+" does not have a deletion type column to do a soft delete.")
	}

	keyColumn := table.GetKeyColumns().Enumerator().Next().(*db.Column)
	versionColumn := table.GetVersionColumn()

	result, err := DB.Update(table).
		Set(deletion, tk.Milliseconds()).
		Set(versionColumn, version+1).
		Where(
			keyColumn.Matches(id),
			versionColumn.Matches(version),
		).
		Execute()

	if err != nil {
		return err
	}

	if result == 0 {
		return dbx.NewOptimisticLockFail("DAOUtils.removeByIdVersion: Unable to soft delete by id and version for the table " + table.GetName())
	}

	return nil
}
Пример #2
0
func Save(DB db.IDb, table *db.Table, entity IEntity) error {
	logger.CallerAt(1).Debugf("DAOUtils.Save: %s", entity)

	if entity.GetVersion() == nil {
		entity.SetVersion(Int64(1))
		//entity.SetCreation(NOW()) -> PreInsert
		id, err := DB.Insert(table).Submit(entity)
		if err != nil {
			return err
		}
		entity.SetId(&id)
	} else {
		// TODO should check the deletion column in the where clause
		//entity.SetModification(NOW()) -> PreUpdate
		_, err := DB.Update(table).Submit(entity)
		if err != nil {
			return err
		}
	}
	return nil
}