@@ -16,6 +16,7 @@ import (
1616 "github.com/lightningnetwork/lnd/lnrpc"
1717 "github.com/lightningnetwork/lnd/lntypes"
1818 "github.com/lightningnetwork/lnd/lnwire"
19+ "github.com/lightningnetwork/lnd/sqldb/v2"
1920)
2021
2122const (
@@ -33,6 +34,8 @@ const (
3334//
3435//nolint:lll
3536type SQLQueries interface {
37+ sqldb.BaseQuerier
38+
3639 AddAccountInvoice (ctx context.Context , arg sqlc.AddAccountInvoiceParams ) error
3740 DeleteAccount (ctx context.Context , id int64 ) error
3841 DeleteAccountPayment (ctx context.Context , arg sqlc.DeleteAccountPaymentParams ) error
@@ -53,12 +56,13 @@ type SQLQueries interface {
5356 GetAccountInvoice (ctx context.Context , arg sqlc.GetAccountInvoiceParams ) (sqlc.AccountInvoice , error )
5457}
5558
56- // BatchedSQLQueries is a version of the SQLQueries that's capable
57- // of batched database operations.
59+ // BatchedSQLQueries combines the SQLQueries interface with the BatchedTx
60+ // interface, allowing for multiple queries to be executed in single SQL
61+ // transaction.
5862type BatchedSQLQueries interface {
5963 SQLQueries
6064
61- db .BatchedTx [SQLQueries ]
65+ sqldb .BatchedTx [SQLQueries ]
6266}
6367
6468// SQLStore represents a storage backend.
@@ -68,19 +72,37 @@ type SQLStore struct {
6872 db BatchedSQLQueries
6973
7074 // BaseDB represents the underlying database connection.
71- * db .BaseDB
75+ * sqldb .BaseDB
7276
7377 clock clock.Clock
7478}
7579
76- // NewSQLStore creates a new SQLStore instance given an open BatchedSQLQueries
77- // storage backend.
78- func NewSQLStore (sqlDB * db.BaseDB , clock clock.Clock ) * SQLStore {
79- executor := db .NewTransactionExecutor (
80- sqlDB , func (tx * sql.Tx ) SQLQueries {
81- return sqlDB .WithTx (tx )
80+ type SQLQueriesExecutor [T sqldb.BaseQuerier ] struct {
81+ * sqldb.TransactionExecutor [T ]
82+
83+ SQLQueries
84+ }
85+
86+ func NewSQLQueriesExecutor (baseDB * sqldb.BaseDB ,
87+ queries * sqlc.Queries ) * SQLQueriesExecutor [SQLQueries ] {
88+
89+ executor := sqldb .NewTransactionExecutor (
90+ baseDB , func (tx * sql.Tx ) SQLQueries {
91+ return queries .WithTx (tx )
8292 },
8393 )
94+ return & SQLQueriesExecutor [SQLQueries ]{
95+ TransactionExecutor : executor ,
96+ SQLQueries : queries ,
97+ }
98+ }
99+
100+ // NewSQLStore creates a new SQLStore instance given an open BatchedSQLQueries
101+ // storage backend.
102+ func NewSQLStore (sqlDB * sqldb.BaseDB , queries * sqlc.Queries ,
103+ clock clock.Clock ) * SQLStore {
104+
105+ executor := NewSQLQueriesExecutor (sqlDB , queries )
84106
85107 return & SQLStore {
86108 db : executor ,
@@ -157,7 +179,7 @@ func (s *SQLStore) NewAccount(ctx context.Context, balance lnwire.MilliSatoshi,
157179 }
158180
159181 return nil
160- })
182+ }, sqldb . NoOpReset )
161183 if err != nil {
162184 return nil , err
163185 }
@@ -299,7 +321,7 @@ func (s *SQLStore) AddAccountInvoice(ctx context.Context, alias AccountID,
299321 }
300322
301323 return s .markAccountUpdated (ctx , db , acctID )
302- })
324+ }, sqldb . NoOpReset )
303325}
304326
305327func getAccountIDByAlias (ctx context.Context , db SQLQueries , alias AccountID ) (
@@ -377,7 +399,7 @@ func (s *SQLStore) UpdateAccountBalanceAndExpiry(ctx context.Context,
377399 }
378400
379401 return s .markAccountUpdated (ctx , db , id )
380- })
402+ }, sqldb . NoOpReset )
381403}
382404
383405// CreditAccount increases the balance of the account with the given alias by
@@ -412,7 +434,7 @@ func (s *SQLStore) CreditAccount(ctx context.Context, alias AccountID,
412434 }
413435
414436 return s .markAccountUpdated (ctx , db , id )
415- })
437+ }, sqldb . NoOpReset )
416438}
417439
418440// DebitAccount decreases the balance of the account with the given alias by the
@@ -453,7 +475,7 @@ func (s *SQLStore) DebitAccount(ctx context.Context, alias AccountID,
453475 }
454476
455477 return s .markAccountUpdated (ctx , db , id )
456- })
478+ }, sqldb . NoOpReset )
457479}
458480
459481// Account retrieves an account from the SQL store and un-marshals it. If the
@@ -475,7 +497,7 @@ func (s *SQLStore) Account(ctx context.Context, alias AccountID) (
475497
476498 account , err = getAndMarshalAccount (ctx , db , id )
477499 return err
478- })
500+ }, sqldb . NoOpReset )
479501
480502 return account , err
481503}
@@ -507,7 +529,7 @@ func (s *SQLStore) Accounts(ctx context.Context) ([]*OffChainBalanceAccount,
507529 }
508530
509531 return nil
510- })
532+ }, sqldb . NoOpReset )
511533
512534 return accounts , err
513535}
@@ -524,7 +546,7 @@ func (s *SQLStore) RemoveAccount(ctx context.Context, alias AccountID) error {
524546 }
525547
526548 return db .DeleteAccount (ctx , id )
527- })
549+ }, sqldb . NoOpReset )
528550}
529551
530552// UpsertAccountPayment updates or inserts a payment entry for the given
@@ -634,7 +656,7 @@ func (s *SQLStore) UpsertAccountPayment(ctx context.Context, alias AccountID,
634656 }
635657
636658 return s .markAccountUpdated (ctx , db , id )
637- })
659+ }, sqldb . NoOpReset )
638660}
639661
640662// DeleteAccountPayment removes a payment entry from the account with the given
@@ -677,7 +699,7 @@ func (s *SQLStore) DeleteAccountPayment(ctx context.Context, alias AccountID,
677699 }
678700
679701 return s .markAccountUpdated (ctx , db , id )
680- })
702+ }, sqldb . NoOpReset )
681703}
682704
683705// LastIndexes returns the last invoice add and settle index or
@@ -704,7 +726,7 @@ func (s *SQLStore) LastIndexes(ctx context.Context) (uint64, uint64, error) {
704726 }
705727
706728 return err
707- })
729+ }, sqldb . NoOpReset )
708730
709731 return uint64 (addIndex ), uint64 (settleIndex ), err
710732}
@@ -729,7 +751,7 @@ func (s *SQLStore) StoreLastIndexes(ctx context.Context, addIndex,
729751 Name : settleIndexName ,
730752 Value : int64 (settleIndex ),
731753 })
732- })
754+ }, sqldb . NoOpReset )
733755}
734756
735757// Close closes the underlying store.
0 commit comments