Esempio 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()
}
Esempio 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
}
Esempio 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
}
Esempio n. 4
0
// CreateTable add new table to a database
func (dao *BaseDAO) CreateTable(tx db.TX) error {
	tx.MustExec(dao.Table.createSQL)
	return nil
}
Esempio n. 5
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
}
Esempio n. 6
0
// FindByID obtain article of given ID from database
func (dao *BaseDAO) FindByID(tx db.TX, id int64) *sqlx.Row {
	return tx.QueryRowx(dao.Table.selectByIDSQL, id)
}
Esempio n. 7
0
// FindAll obtain all articles from database
func (dao *BaseDAO) FindAll(tx db.TX) (*sqlx.Rows, error) {
	return tx.Queryx(dao.Table.selectSQL)
}