mirror of
https://github.com/lightninglabs/lightning-terminal.git
synced 2026-08-13 12:33:36 +02:00
This commit adds a helper struct that creates a `sqldb/v2` transaction executor that wraps the `sqlcmig6.Queries` type.
44 lines
891 B
Go
44 lines
891 B
Go
package sqlcmig6
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"github.com/lightningnetwork/lnd/sqldb/v2"
|
|
)
|
|
|
|
type DBTX interface {
|
|
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
|
|
PrepareContext(context.Context, string) (*sql.Stmt, error)
|
|
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
|
|
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
|
|
}
|
|
|
|
func New(db DBTX) *Queries {
|
|
return &Queries{db: db}
|
|
}
|
|
|
|
type Queries struct {
|
|
db DBTX
|
|
}
|
|
|
|
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
|
|
return &Queries{
|
|
db: tx,
|
|
}
|
|
}
|
|
|
|
type TxExecutor struct {
|
|
*sqldb.TransactionExecutor[*Queries]
|
|
}
|
|
|
|
func NewTxExecutor(baseDB *sqldb.BaseDB, queries *Queries) *TxExecutor {
|
|
executor := sqldb.NewTransactionExecutor(
|
|
baseDB, func(tx *sql.Tx) *Queries {
|
|
return queries.WithTx(tx)
|
|
},
|
|
)
|
|
|
|
return &TxExecutor{
|
|
TransactionExecutor: executor,
|
|
}
|
|
}
|