mirror of
https://github.com/ClusterCockpit/cc-backend
synced 2024-12-26 13:29:05 +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()
|
starttime := time.Now()
|
||||||
log.Print("Building job table...")
|
log.Print("Building job table...")
|
||||||
|
|
||||||
t, err := r.TransactionInit()
|
t, err := r.TransactionInit(repository.NamedJobInsert)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("Error while initializing SQL transactions")
|
log.Warn("Error while initializing SQL transactions")
|
||||||
return err
|
return err
|
||||||
|
@ -15,20 +15,19 @@ type Transaction struct {
|
|||||||
stmt *sqlx.NamedStmt
|
stmt *sqlx.NamedStmt
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *JobRepository) TransactionInit() (*Transaction, error) {
|
func (r *JobRepository) TransactionInit(sqlStmt string) (*Transaction, error) {
|
||||||
var err error
|
var err error
|
||||||
t := new(Transaction)
|
t := new(Transaction)
|
||||||
// Inserts are bundled into transactions because in sqlite,
|
|
||||||
// that speeds up inserts A LOT.
|
|
||||||
t.tx, err = r.DB.Beginx()
|
t.tx, err = r.DB.Beginx()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("Error while bundling transactions")
|
log.Warn("Error while bundling transactions")
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
t.stmt, err = t.tx.PrepareNamed(NamedJobInsert)
|
t.stmt, err = t.tx.PrepareNamed(sqlStmt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("Error while preparing namedJobInsert")
|
log.Warn("Error while preparing SQL statement in transaction")
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,8 +62,8 @@ func (r *JobRepository) TransactionEnd(t *Transaction) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *JobRepository) TransactionAdd(t *Transaction, job schema.Job) (int64, error) {
|
func (r *JobRepository) TransactionAdd(t *Transaction, obj interface{}) (int64, error) {
|
||||||
res, err := t.stmt.Exec(job)
|
res, err := t.stmt.Exec(obj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("repository initDB(): %v", err)
|
log.Errorf("repository initDB(): %v", err)
|
||||||
return 0, err
|
return 0, err
|
||||||
|
Loading…
Reference in New Issue
Block a user