2017-07-17 18:20:57 +02:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2017-12-19 10:49:42 +01:00
|
|
|
|
|
|
|
"github.com/lib/pq"
|
2017-07-17 18:20:57 +02:00
|
|
|
)
|
|
|
|
|
2017-08-21 17:37:11 +02:00
|
|
|
// A Transaction is something that can be committed or rolledback.
|
|
|
|
type Transaction interface {
|
|
|
|
// Commit the transaction
|
|
|
|
Commit() error
|
|
|
|
// Rollback the transaction.
|
|
|
|
Rollback() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// EndTransaction ends a transaction.
|
|
|
|
// If the transaction succeeded then it is committed, otherwise it is rolledback.
|
|
|
|
func EndTransaction(txn Transaction, succeeded *bool) {
|
|
|
|
if *succeeded {
|
2017-09-20 11:59:19 +02:00
|
|
|
txn.Commit() // nolint: errcheck
|
2017-08-21 17:37:11 +02:00
|
|
|
} else {
|
2017-09-20 11:59:19 +02:00
|
|
|
txn.Rollback() // nolint: errcheck
|
2017-08-21 17:37:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 18:20:57 +02:00
|
|
|
// WithTransaction runs a block of code passing in an SQL transaction
|
|
|
|
// If the code returns an error or panics then the transactions is rolledback
|
|
|
|
// Otherwise the transaction is committed.
|
|
|
|
func WithTransaction(db *sql.DB, fn func(txn *sql.Tx) error) (err error) {
|
|
|
|
txn, err := db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-08-21 17:37:11 +02:00
|
|
|
succeeded := false
|
|
|
|
defer EndTransaction(txn, &succeeded)
|
|
|
|
|
2017-07-17 18:20:57 +02:00
|
|
|
err = fn(txn)
|
2017-08-21 17:37:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
succeeded = true
|
2017-07-17 18:20:57 +02:00
|
|
|
return
|
|
|
|
}
|
2017-08-21 18:20:23 +02:00
|
|
|
|
|
|
|
// TxStmt wraps an SQL stmt inside an optional transaction.
|
|
|
|
// If the transaction is nil then it returns the original statement that will
|
|
|
|
// run outside of a transaction.
|
|
|
|
// Otherwise returns a copy of the statement that will run inside the transaction.
|
|
|
|
func TxStmt(transaction *sql.Tx, statement *sql.Stmt) *sql.Stmt {
|
|
|
|
if transaction != nil {
|
|
|
|
statement = transaction.Stmt(statement)
|
|
|
|
}
|
|
|
|
return statement
|
|
|
|
}
|
2017-12-19 10:49:42 +01:00
|
|
|
|
|
|
|
// IsUniqueConstraintViolationErr returns true if the error is a postgresql unique_violation error
|
|
|
|
func IsUniqueConstraintViolationErr(err error) bool {
|
|
|
|
pqErr, ok := err.(*pq.Error)
|
|
|
|
return ok && pqErr.Code == "23505"
|
|
|
|
}
|