Exemplo n.º 1
0
// Insert store given articles to database
func (dao *BaseDAO) Insert(tx db.TX, entity interface{}) (int64, error) {
	var (
		res sql.Result
		err error
	)
	if res, err = tx.NamedExec(dao.Table.insertSQL, entity); err != nil {
		return 0, err
	}
	return res.LastInsertId()
}
Exemplo n.º 2
0
// Delete remove specyfic record
func (dao *BaseDAO) Delete(tx db.TX, id int64) error {
	var (
		res   sql.Result
		err   error
		count int64
	)
	if res, err = tx.NamedExec(dao.Table.deleteByIDSQL, &IDContainer{id}); err != nil {
		return err
	}
	if count, err = res.RowsAffected(); err != nil {
		return err
	}
	if count != 1 {
		return fmt.Errorf("Delete more than one record (%v records deleted)", count)
	}
	return nil
}
Exemplo n.º 3
0
// Update data of article
func (dao *BaseDAO) Update(tx db.TX, entity interface{}) error {
	var (
		res   sql.Result
		err   error
		count int64
	)
	if res, err = tx.NamedExec(dao.Table.updateByIDSQL, entity); err != nil {
		return err
	}
	if count, err = res.RowsAffected(); err != nil {
		return err
	}
	if count != 1 {
		return fmt.Errorf("Update modified more then one record (%v records modyfieds)", count)
	}
	return nil
}
Exemplo n.º 4
0
// InsertWithID store given articles to database (It persist with id from entity)
func (dao *BaseDAO) InsertWithID(tx db.TX, entity interface{}) error {
	if _, err := tx.NamedExec(dao.Table.insertWithIDSQL, entity); err != nil {
		return err
	}
	return nil
}