@@ -16,6 +16,7 @@ import (
16
16
"github.com/lightningnetwork/lnd/lnrpc"
17
17
"github.com/lightningnetwork/lnd/lntypes"
18
18
"github.com/lightningnetwork/lnd/lnwire"
19
+ "github.com/lightningnetwork/lnd/sqldb/v2"
19
20
)
20
21
21
22
const (
@@ -33,6 +34,8 @@ const (
33
34
//
34
35
//nolint:lll
35
36
type SQLQueries interface {
37
+ sqldb.BaseQuerier
38
+
36
39
AddAccountInvoice (ctx context.Context , arg sqlc.AddAccountInvoiceParams ) error
37
40
DeleteAccount (ctx context.Context , id int64 ) error
38
41
DeleteAccountPayment (ctx context.Context , arg sqlc.DeleteAccountPaymentParams ) error
@@ -53,12 +56,13 @@ type SQLQueries interface {
53
56
GetAccountInvoice (ctx context.Context , arg sqlc.GetAccountInvoiceParams ) (sqlc.AccountInvoice , error )
54
57
}
55
58
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.
58
62
type BatchedSQLQueries interface {
59
63
SQLQueries
60
64
61
- db .BatchedTx [SQLQueries ]
65
+ sqldb .BatchedTx [SQLQueries ]
62
66
}
63
67
64
68
// SQLStore represents a storage backend.
@@ -68,19 +72,37 @@ type SQLStore struct {
68
72
db BatchedSQLQueries
69
73
70
74
// BaseDB represents the underlying database connection.
71
- * db .BaseDB
75
+ * sqldb .BaseDB
72
76
73
77
clock clock.Clock
74
78
}
75
79
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 )
82
92
},
83
93
)
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 )
84
106
85
107
return & SQLStore {
86
108
db : executor ,
@@ -157,7 +179,7 @@ func (s *SQLStore) NewAccount(ctx context.Context, balance lnwire.MilliSatoshi,
157
179
}
158
180
159
181
return nil
160
- })
182
+ }, sqldb . NoOpReset )
161
183
if err != nil {
162
184
return nil , err
163
185
}
@@ -299,7 +321,7 @@ func (s *SQLStore) AddAccountInvoice(ctx context.Context, alias AccountID,
299
321
}
300
322
301
323
return s .markAccountUpdated (ctx , db , acctID )
302
- })
324
+ }, sqldb . NoOpReset )
303
325
}
304
326
305
327
func getAccountIDByAlias (ctx context.Context , db SQLQueries , alias AccountID ) (
@@ -377,7 +399,7 @@ func (s *SQLStore) UpdateAccountBalanceAndExpiry(ctx context.Context,
377
399
}
378
400
379
401
return s .markAccountUpdated (ctx , db , id )
380
- })
402
+ }, sqldb . NoOpReset )
381
403
}
382
404
383
405
// 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,
412
434
}
413
435
414
436
return s .markAccountUpdated (ctx , db , id )
415
- })
437
+ }, sqldb . NoOpReset )
416
438
}
417
439
418
440
// 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,
453
475
}
454
476
455
477
return s .markAccountUpdated (ctx , db , id )
456
- })
478
+ }, sqldb . NoOpReset )
457
479
}
458
480
459
481
// 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) (
475
497
476
498
account , err = getAndMarshalAccount (ctx , db , id )
477
499
return err
478
- })
500
+ }, sqldb . NoOpReset )
479
501
480
502
return account , err
481
503
}
@@ -507,7 +529,7 @@ func (s *SQLStore) Accounts(ctx context.Context) ([]*OffChainBalanceAccount,
507
529
}
508
530
509
531
return nil
510
- })
532
+ }, sqldb . NoOpReset )
511
533
512
534
return accounts , err
513
535
}
@@ -524,7 +546,7 @@ func (s *SQLStore) RemoveAccount(ctx context.Context, alias AccountID) error {
524
546
}
525
547
526
548
return db .DeleteAccount (ctx , id )
527
- })
549
+ }, sqldb . NoOpReset )
528
550
}
529
551
530
552
// UpsertAccountPayment updates or inserts a payment entry for the given
@@ -634,7 +656,7 @@ func (s *SQLStore) UpsertAccountPayment(ctx context.Context, alias AccountID,
634
656
}
635
657
636
658
return s .markAccountUpdated (ctx , db , id )
637
- })
659
+ }, sqldb . NoOpReset )
638
660
}
639
661
640
662
// DeleteAccountPayment removes a payment entry from the account with the given
@@ -677,7 +699,7 @@ func (s *SQLStore) DeleteAccountPayment(ctx context.Context, alias AccountID,
677
699
}
678
700
679
701
return s .markAccountUpdated (ctx , db , id )
680
- })
702
+ }, sqldb . NoOpReset )
681
703
}
682
704
683
705
// LastIndexes returns the last invoice add and settle index or
@@ -704,7 +726,7 @@ func (s *SQLStore) LastIndexes(ctx context.Context) (uint64, uint64, error) {
704
726
}
705
727
706
728
return err
707
- })
729
+ }, sqldb . NoOpReset )
708
730
709
731
return uint64 (addIndex ), uint64 (settleIndex ), err
710
732
}
@@ -729,7 +751,7 @@ func (s *SQLStore) StoreLastIndexes(ctx context.Context, addIndex,
729
751
Name : settleIndexName ,
730
752
Value : int64 (settleIndex ),
731
753
})
732
- })
754
+ }, sqldb . NoOpReset )
733
755
}
734
756
735
757
// Close closes the underlying store.
0 commit comments