Пример #1
0
// the result of the query is put in the passed struct.
// returns true if a result was found, false if no result
func FindById(DB db.IDb, table *db.Table, instance interface{}, id int64) (bool, error) {
	logger.CallerAt(1).Debugf("DAOUtils.FindById: %v", id)

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

	return DB.Query(table).
		All().
		Where(keyColumn.Matches(id)).
		SelectTo(instance)
}
Пример #2
0
func DeleteById(DB db.IDb, table *db.Table, id int64) (bool, error) {
	logger.CallerAt(1).Debugf("DAOUtils.deleteById: %v", id)

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

	result, err := DB.Delete(table).
		Where(keyColumn.Matches(id)).
		Execute()

	return result != 0, err
}
Пример #3
0
func FindAll(DB db.IDb, table *db.Table, instance interface{}) (coll.Collection, error) {
	logger.CallerAt(1).Debugf("DAOUtils.FetchAll")

	deletion := table.GetDeletionColumn()

	q := DB.Query(table).All()
	if deletion != nil {
		q.Where(deletion.Matches(NOT_DELETED))
	}

	return q.ListOf(instance)
}
Пример #4
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
}
Пример #5
0
func DeleteByIdAndVersion(DB db.IDb, table *db.Table, id int64, version int64) error {
	logger.CallerAt(1).Debugf("DAOUtils.removeByIdVersion: id: %v, version: %v", id, version)

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

	result, err := DB.Delete(table).
		Where(
			keyColumn.Matches(id),
			versionColumn.Matches(version),
		).
		Execute()

	if err != nil {
		return err
	}

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

	return nil
}
Пример #6
0
// FROM
func (this *GenericTranslator) TableName(table *db.Table) string {
	return table.GetName()
}
Пример #7
0
func (this *FirebirdSQLTranslator) TableName(table *db.Table) string {
	return "\"" + strings.ToUpper(table.GetName()) + "\""
}
Пример #8
0
func (this *MySQL5Translator) TableName(table *db.Table) string {
	return "`" + strings.ToUpper(table.GetName()) + "`"
}
Пример #9
0
func (this *OracleTranslator) TableName(table *db.Table) string {
	return "\"" + strings.ToUpper(table.GetName()) + "\""
}
Пример #10
0
func (this *PostgreSQLTranslator) TableName(table *db.Table) string {
	return strings.ToLower(table.GetName())
}