mirror of
https://github.com/ClusterCockpit/cc-backend
synced 2024-11-10 08:57:25 +01:00
Prepare transaction API for general usage
This commit is contained in:
parent
39c09f8565
commit
6568b6d723
@ -27,7 +27,7 @@ func InitDB() error {
|
||||
starttime := time.Now()
|
||||
log.Print("Building job table...")
|
||||
|
||||
t, err := r.TransactionInit()
|
||||
t, err := r.TransactionInit(repository.NamedJobInsert)
|
||||
if err != nil {
|
||||
log.Warn("Error while initializing SQL transactions")
|
||||
return err
|
||||
|
@ -15,20 +15,19 @@ type Transaction struct {
|
||||
stmt *sqlx.NamedStmt
|
||||
}
|
||||
|
||||
func (r *JobRepository) TransactionInit() (*Transaction, error) {
|
||||
func (r *JobRepository) TransactionInit(sqlStmt string) (*Transaction, error) {
|
||||
var err error
|
||||
t := new(Transaction)
|
||||
// Inserts are bundled into transactions because in sqlite,
|
||||
// that speeds up inserts A LOT.
|
||||
|
||||
t.tx, err = r.DB.Beginx()
|
||||
if err != nil {
|
||||
log.Warn("Error while bundling transactions")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
t.stmt, err = t.tx.PrepareNamed(NamedJobInsert)
|
||||
t.stmt, err = t.tx.PrepareNamed(sqlStmt)
|
||||
if err != nil {
|
||||
log.Warn("Error while preparing namedJobInsert")
|
||||
log.Warn("Error while preparing SQL statement in transaction")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@ -63,8 +62,8 @@ func (r *JobRepository) TransactionEnd(t *Transaction) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *JobRepository) TransactionAdd(t *Transaction, job schema.Job) (int64, error) {
|
||||
res, err := t.stmt.Exec(job)
|
||||
func (r *JobRepository) TransactionAdd(t *Transaction, obj interface{}) (int64, error) {
|
||||
res, err := t.stmt.Exec(obj)
|
||||
if err != nil {
|
||||
log.Errorf("repository initDB(): %v", err)
|
||||
return 0, err
|
||||
|
Loading…
Reference in New Issue
Block a user