2020-08-19 09:38:27 -05:00
|
|
|
package sqlutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
)
|
|
|
|
|
2020-08-21 04:42:08 -05:00
|
|
|
// DummyWriter implements sqlutil.Writer.
|
|
|
|
// The DummyWriter is designed to allow reuse of the sqlutil.Writer
|
|
|
|
// interface but, unlike ExclusiveWriter, it will not guarantee
|
|
|
|
// writer exclusivity. This is fine in PostgreSQL where overlapping
|
|
|
|
// transactions and writes are acceptable.
|
|
|
|
type DummyWriter struct {
|
2020-08-19 09:38:27 -05:00
|
|
|
}
|
|
|
|
|
2020-08-21 04:42:08 -05:00
|
|
|
// NewDummyWriter returns a new dummy writer.
|
|
|
|
func NewDummyWriter() Writer {
|
|
|
|
return &DummyWriter{}
|
2020-08-19 09:38:27 -05:00
|
|
|
}
|
|
|
|
|
2020-08-21 04:42:08 -05:00
|
|
|
func (w *DummyWriter) Do(db *sql.DB, txn *sql.Tx, f func(txn *sql.Tx) error) error {
|
|
|
|
if db != nil && txn == nil {
|
2020-08-19 09:38:27 -05:00
|
|
|
return WithTransaction(db, func(txn *sql.Tx) error {
|
|
|
|
return f(txn)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return f(txn)
|
|
|
|
}
|
|
|
|
}
|