multi: add a re-usable TxOptions type

Add a re-usable implementation of the sqldb.TxOptions interface and make
use of this in the various spots (invoices, batch and graph/db) where we
had previously defined individual implementations that were all doing
the same thing.
This commit is contained in:
Elle Mouton
2025-05-27 18:53:31 +02:00
parent 8e96bd0308
commit c4e6f23c5b
9 changed files with 79 additions and 134 deletions

View File

@@ -39,6 +39,35 @@ type TxOptions interface {
ReadOnly() bool
}
// txOptions is a concrete implementation of the TxOptions interface.
type txOptions struct {
// readOnly indicates if the transaction should be read-only.
readOnly bool
}
// ReadOnly returns true if the transaction should be read only.
//
// NOTE: This is part of the TxOptions interface.
func (t *txOptions) ReadOnly() bool {
return t.readOnly
}
// WriteTxOpt returns a TxOptions that indicates that the transaction
// should be a write transaction.
func WriteTxOpt() TxOptions {
return &txOptions{
readOnly: false,
}
}
// ReadTxOpt returns a TxOptions that indicates that the transaction
// should be a read-only transaction.
func ReadTxOpt() TxOptions {
return &txOptions{
readOnly: true,
}
}
// BatchedTx is a generic interface that represents the ability to execute
// several operations to a given storage interface in a single atomic
// transaction. Typically, Q here will be some subset of the main sqlc.Querier