@@ -56,7 +56,8 @@ use crate::{
56
56
/// A vector with accounts, or an error.
57
57
#[ cfg( test) ]
58
58
pub fn select_all_accounts ( conn : & mut Connection ) -> Result < Vec < AccountInfo > > {
59
- let mut stmt = conn. prepare_cached (
59
+ let transaction = conn. transaction ( ) ?;
60
+ let mut stmt = transaction. prepare_cached (
60
61
"
61
62
SELECT
62
63
account_id,
@@ -84,7 +85,8 @@ pub fn select_all_accounts(conn: &mut Connection) -> Result<Vec<AccountInfo>> {
84
85
///
85
86
/// The vector with the account id and corresponding hash, or an error.
86
87
pub fn select_all_account_hashes ( conn : & mut Connection ) -> Result < Vec < ( AccountId , RpoDigest ) > > {
87
- let mut stmt = conn
88
+ let transaction = conn. transaction ( ) ?;
89
+ let mut stmt = transaction
88
90
. prepare_cached ( "SELECT account_id, account_hash FROM accounts ORDER BY block_num ASC;" ) ?;
89
91
let mut rows = stmt. query ( [ ] ) ?;
90
92
@@ -112,7 +114,8 @@ pub fn select_accounts_by_block_range(
112
114
block_end : BlockNumber ,
113
115
account_ids : & [ AccountId ] ,
114
116
) -> Result < Vec < AccountSummary > > {
115
- let mut stmt = conn. prepare_cached (
117
+ let transaction = conn. transaction ( ) ?;
118
+ let mut stmt = transaction. prepare_cached (
116
119
"
117
120
SELECT
118
121
account_id,
@@ -151,7 +154,8 @@ pub fn select_accounts_by_block_range(
151
154
///
152
155
/// The latest account details, or an error.
153
156
pub fn select_account ( conn : & mut Connection , account_id : AccountId ) -> Result < AccountInfo > {
154
- let mut stmt = conn. prepare_cached (
157
+ let transaction = conn. transaction ( ) ?;
158
+ let mut stmt = transaction. prepare_cached (
155
159
"
156
160
SELECT
157
161
account_id,
@@ -181,7 +185,8 @@ pub fn select_accounts_by_ids(
181
185
conn : & mut Connection ,
182
186
account_ids : & [ AccountId ] ,
183
187
) -> Result < Vec < AccountInfo > > {
184
- let mut stmt = conn. prepare_cached (
188
+ let transaction = conn. transaction ( ) ?;
189
+ let mut stmt = transaction. prepare_cached (
185
190
"
186
191
SELECT
187
192
account_id,
@@ -227,7 +232,8 @@ pub fn select_account_delta(
227
232
block_start : BlockNumber ,
228
233
block_end : BlockNumber ,
229
234
) -> Result < Option < AccountDelta > > {
230
- let mut select_nonce_stmt = conn. prepare_cached (
235
+ let transaction = conn. transaction ( ) ?;
236
+ let mut select_nonce_stmt = transaction. prepare_cached (
231
237
"
232
238
SELECT
233
239
nonce
@@ -241,7 +247,7 @@ pub fn select_account_delta(
241
247
" ,
242
248
) ?;
243
249
244
- let mut select_slot_updates_stmt = conn . prepare_cached (
250
+ let mut select_slot_updates_stmt = transaction . prepare_cached (
245
251
"
246
252
SELECT
247
253
slot, value
@@ -263,7 +269,7 @@ pub fn select_account_delta(
263
269
" ,
264
270
) ?;
265
271
266
- let mut select_storage_map_updates_stmt = conn . prepare_cached (
272
+ let mut select_storage_map_updates_stmt = transaction . prepare_cached (
267
273
"
268
274
SELECT
269
275
slot, key, value
@@ -286,7 +292,7 @@ pub fn select_account_delta(
286
292
" ,
287
293
) ?;
288
294
289
- let mut select_fungible_asset_deltas_stmt = conn . prepare_cached (
295
+ let mut select_fungible_asset_deltas_stmt = transaction . prepare_cached (
290
296
"
291
297
SELECT
292
298
faucet_id, SUM(delta)
@@ -301,7 +307,7 @@ pub fn select_account_delta(
301
307
" ,
302
308
) ?;
303
309
304
- let mut select_non_fungible_asset_updates_stmt = conn . prepare_cached (
310
+ let mut select_non_fungible_asset_updates_stmt = transaction . prepare_cached (
305
311
"
306
312
SELECT
307
313
block_num, vault_key, is_remove
@@ -418,10 +424,10 @@ pub fn upsert_accounts(
418
424
block_num : BlockNumber ,
419
425
) -> Result < usize > {
420
426
let mut upsert_stmt = transaction. prepare_cached (
421
- "INSERT OR REPLACE INTO accounts (account_id, account_hash, block_num, details) VALUES (?1, ?2, ?3, ?4); " ,
427
+ "INSERT OR REPLACE INTO accounts (account_id, account_hash, block_num, details) VALUES (?1, ?2, ?3, ?4)" ,
422
428
) ?;
423
429
let mut select_details_stmt =
424
- transaction. prepare_cached ( "SELECT details FROM accounts WHERE account_id = ?1; " ) ?;
430
+ transaction. prepare_cached ( "SELECT details FROM accounts WHERE account_id = ?1" ) ?;
425
431
426
432
let mut count = 0 ;
427
433
for update in accounts {
@@ -601,7 +607,7 @@ pub fn insert_nullifiers_for_block(
601
607
let mut count = stmt. execute ( params ! [ serialized_nullifiers] ) ?;
602
608
603
609
let mut stmt = transaction. prepare_cached (
604
- "INSERT INTO nullifiers (nullifier, nullifier_prefix, block_num) VALUES (?1, ?2, ?3); " ,
610
+ "INSERT INTO nullifiers (nullifier, nullifier_prefix, block_num) VALUES (?1, ?2, ?3)" ,
605
611
) ?;
606
612
607
613
for ( nullifier, bytes) in nullifiers. iter ( ) . zip ( serialized_nullifiers. iter ( ) ) {
@@ -617,8 +623,9 @@ pub fn insert_nullifiers_for_block(
617
623
///
618
624
/// A vector with nullifiers and the block height at which they were created, or an error.
619
625
pub fn select_all_nullifiers ( conn : & mut Connection ) -> Result < Vec < ( Nullifier , BlockNumber ) > > {
620
- let mut stmt =
621
- conn. prepare_cached ( "SELECT nullifier, block_num FROM nullifiers ORDER BY block_num ASC;" ) ?;
626
+ let transaction = conn. transaction ( ) ?;
627
+ let mut stmt = transaction
628
+ . prepare_cached ( "SELECT nullifier, block_num FROM nullifiers ORDER BY block_num ASC" ) ?;
622
629
let mut rows = stmt. query ( [ ] ) ?;
623
630
624
631
let mut result = vec ! [ ] ;
@@ -652,7 +659,8 @@ pub fn select_nullifiers_by_prefix(
652
659
let nullifier_prefixes: Vec < Value > =
653
660
nullifier_prefixes. iter ( ) . copied ( ) . map ( Into :: into) . collect ( ) ;
654
661
655
- let mut stmt = conn. prepare_cached (
662
+ let transaction = conn. transaction ( ) ?;
663
+ let mut stmt = transaction. prepare_cached (
656
664
"
657
665
SELECT
658
666
nullifier,
@@ -689,7 +697,8 @@ pub fn select_nullifiers_by_prefix(
689
697
/// A vector with notes, or an error.
690
698
#[ cfg( test) ]
691
699
pub fn select_all_notes ( conn : & mut Connection ) -> Result < Vec < NoteRecord > > {
692
- let mut stmt = conn. prepare_cached ( & format ! (
700
+ let transaction = conn. transaction ( ) ?;
701
+ let mut stmt = transaction. prepare_cached ( & format ! (
693
702
"SELECT {} FROM notes ORDER BY block_num ASC" ,
694
703
NoteRecord :: SELECT_COLUMNS ,
695
704
) ) ?;
@@ -735,7 +744,7 @@ pub fn insert_notes(
735
744
736
745
let mut count = 0 ;
737
746
for ( note, nullifier) in notes {
738
- let details = note. details . as_ref ( ) . map ( miden_objects :: utils :: Serializable :: to_bytes) ;
747
+ let details = note. details . as_ref ( ) . map ( Serializable :: to_bytes) ;
739
748
count += stmt. execute ( params ! [
740
749
note. block_num. as_u32( ) ,
741
750
note. note_index. batch_idx( ) ,
@@ -777,7 +786,8 @@ pub fn select_notes_since_block_by_tag_and_sender(
777
786
account_ids : & [ AccountId ] ,
778
787
block_num : BlockNumber ,
779
788
) -> Result < Vec < NoteSyncRecord > > {
780
- let mut stmt = conn
789
+ let transaction = conn. transaction ( ) ?;
790
+ let mut stmt = transaction
781
791
. prepare_cached ( include_str ! ( "queries/select_notes_since_block_by_tag_and_sender.sql" ) ) ?;
782
792
783
793
let tags: Vec < Value > = tags. iter ( ) . copied ( ) . map ( Into :: into) . collect ( ) ;
@@ -835,7 +845,8 @@ pub fn select_notes_since_block_by_tag_and_sender(
835
845
pub fn select_notes_by_id ( conn : & mut Connection , note_ids : & [ NoteId ] ) -> Result < Vec < NoteRecord > > {
836
846
let note_ids: Vec < Value > = note_ids. iter ( ) . map ( |id| id. to_bytes ( ) . into ( ) ) . collect ( ) ;
837
847
838
- let mut stmt = conn. prepare_cached ( & format ! (
848
+ let transaction = conn. transaction ( ) ?;
849
+ let mut stmt = transaction. prepare_cached ( & format ! (
839
850
"SELECT {} FROM notes WHERE note_id IN rarray(?1)" ,
840
851
NoteRecord :: SELECT_COLUMNS
841
852
) ) ?;
@@ -861,7 +872,8 @@ pub fn select_note_inclusion_proofs(
861
872
) -> Result < BTreeMap < NoteId , NoteInclusionProof > > {
862
873
let note_ids: Vec < Value > = note_ids. into_iter ( ) . map ( |id| id. to_bytes ( ) . into ( ) ) . collect ( ) ;
863
874
864
- let mut select_notes_stmt = conn. prepare_cached (
875
+ let transaction = conn. transaction ( ) ?;
876
+ let mut select_notes_stmt = transaction. prepare_cached (
865
877
"
866
878
SELECT
867
879
block_num,
@@ -983,13 +995,14 @@ pub fn select_block_header_by_block_num(
983
995
conn : & mut Connection ,
984
996
block_number : Option < BlockNumber > ,
985
997
) -> Result < Option < BlockHeader > > {
998
+ let transaction = conn. transaction ( ) ?;
986
999
let mut stmt;
987
1000
let mut rows = if let Some ( block_number) = block_number {
988
- stmt =
989
- conn . prepare_cached ( "SELECT block_header FROM block_headers WHERE block_num = ?1" ) ?;
1001
+ stmt = transaction
1002
+ . prepare_cached ( "SELECT block_header FROM block_headers WHERE block_num = ?1" ) ?;
990
1003
stmt. query ( [ block_number. as_u32 ( ) ] ) ?
991
1004
} else {
992
- stmt = conn . prepare_cached (
1005
+ stmt = transaction . prepare_cached (
993
1006
"SELECT block_header FROM block_headers ORDER BY block_num DESC LIMIT 1" ,
994
1007
) ?;
995
1008
stmt. query ( [ ] ) ?
@@ -1020,7 +1033,8 @@ pub fn select_block_headers(
1020
1033
let blocks: Vec < Value > = blocks. map ( |b| b. as_u32 ( ) . into ( ) ) . collect ( ) ;
1021
1034
1022
1035
let mut headers = Vec :: with_capacity ( blocks. len ( ) ) ;
1023
- let mut stmt = conn
1036
+ let transaction = conn. transaction ( ) ?;
1037
+ let mut stmt = transaction
1024
1038
. prepare_cached ( "SELECT block_header FROM block_headers WHERE block_num IN rarray(?1);" ) ?;
1025
1039
let mut rows = stmt. query ( params ! [ Rc :: new( blocks) ] ) ?;
1026
1040
@@ -1039,8 +1053,9 @@ pub fn select_block_headers(
1039
1053
///
1040
1054
/// A vector of [`BlockHeader`] or an error.
1041
1055
pub fn select_all_block_headers ( conn : & mut Connection ) -> Result < Vec < BlockHeader > > {
1042
- let mut stmt =
1043
- conn. prepare_cached ( "SELECT block_header FROM block_headers ORDER BY block_num ASC;" ) ?;
1056
+ let transaction = conn. transaction ( ) ?;
1057
+ let mut stmt = transaction
1058
+ . prepare_cached ( "SELECT block_header FROM block_headers ORDER BY block_num ASC;" ) ?;
1044
1059
let mut rows = stmt. query ( [ ] ) ?;
1045
1060
let mut result = vec ! [ ] ;
1046
1061
while let Some ( row) = rows. next ( ) ? {
@@ -1105,7 +1120,8 @@ pub fn select_transactions_by_accounts_and_block_range(
1105
1120
. map ( |account_id| account_id. to_bytes ( ) . into ( ) )
1106
1121
. collect ( ) ;
1107
1122
1108
- let mut stmt = conn. prepare_cached (
1123
+ let transaction = conn. transaction ( ) ?;
1124
+ let mut stmt = transaction. prepare_cached (
1109
1125
"
1110
1126
SELECT
1111
1127
account_id,
0 commit comments