Skip to content

Commit c7566f9

Browse files
committed
Improve SQL
Cleanup syntax for clarity. Added update clause for insert raw tx blob. Added update clause for insert txout. Remove "replace" style queries in favor of "upsert" for columns that are allowed to change, e.g. keychain last-revealed, and otherwise ignore if the column should not change.
1 parent 3befb95 commit c7566f9

2 files changed

Lines changed: 25 additions & 23 deletions

File tree

src/async_store.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,28 +74,30 @@ impl Store {
7474

7575
for tx in txs {
7676
let txid = tx.compute_txid();
77-
sqlx::query("insert into tx(txid, tx) values($1, $2)")
78-
.bind(txid.to_string())
79-
.bind(consensus::encode::serialize(tx))
80-
.execute(&self.pool)
81-
.await?;
77+
sqlx::query(
78+
"INSERT INTO tx(txid, tx) VALUES($1, $2) ON CONFLICT DO UPDATE SET tx = $2",
79+
)
80+
.bind(txid.to_string())
81+
.bind(consensus::encode::serialize(tx))
82+
.execute(&self.pool)
83+
.await?;
8284
}
8385
for (txid, t) in first_seen {
84-
sqlx::query("insert into tx(txid, first_seen) values($1, $2) on conflict do update set first_seen = $2")
86+
sqlx::query("INSERT INTO tx(txid, first_seen) VALUES($1, $2) ON CONFLICT DO UPDATE SET first_seen = $2")
8587
.bind(txid.to_string())
8688
.bind(i64::try_from(*t)?)
8789
.execute(&self.pool)
8890
.await?;
8991
}
9092
for (txid, t) in last_seen {
91-
sqlx::query("insert into tx(txid, last_seen) values($1, $2) on conflict do update set last_seen = $2")
93+
sqlx::query("INSERT INTO tx(txid, last_seen) VALUES($1, $2) ON CONFLICT DO UPDATE SET last_seen = $2")
9294
.bind(txid.to_string())
9395
.bind(i64::try_from(*t)?)
9496
.execute(&self.pool)
9597
.await?;
9698
}
9799
for (txid, t) in last_evicted {
98-
sqlx::query("insert into tx(txid, last_evicted) values($1, $2) on conflict do update set last_evicted = $2")
100+
sqlx::query("INSERT INTO tx(txid, last_evicted) VALUES($1, $2) ON CONFLICT DO UPDATE SET last_evicted = $2")
99101
.bind(txid.to_string())
100102
.bind(i64::try_from(*t)?)
101103
.execute(&self.pool)
@@ -107,7 +109,7 @@ impl Store {
107109
value,
108110
script_pubkey,
109111
} = txout;
110-
sqlx::query("insert into txout(txid, vout, value, script) values($1, $2, $3, $4)")
112+
sqlx::query("INSERT INTO txout(txid, vout, value, script) VALUES($1, $2, $3, $4) ON CONFLICT DO UPDATE SET value = $3, script = $4")
111113
.bind(txid.to_string())
112114
.bind(vout)
113115
.bind(i64::try_from(value.to_sat())?)
@@ -118,7 +120,7 @@ impl Store {
118120
for (anchor, txid) in anchors {
119121
let BlockId { height, hash } = anchor.block_id;
120122
let confirmation_time = anchor.confirmation_time;
121-
sqlx::query("insert into anchor(block_height, block_hash, txid, confirmation_time) values($1, $2, $3, $4)")
123+
sqlx::query("INSERT OR IGNORE INTO anchor(block_height, block_hash, txid, confirmation_time) VALUES($1, $2, $3, $4)")
122124
.bind(height)
123125
.bind(hash.to_string())
124126
.bind(txid.to_string())
@@ -138,14 +140,14 @@ impl Store {
138140
for (&height, hash) in &local_chain.blocks {
139141
match hash {
140142
Some(hash) => {
141-
sqlx::query("insert or replace into block(height, hash) values($1, $2)")
143+
sqlx::query("INSERT OR IGNORE INTO block(height, hash) VALUES($1, $2)")
142144
.bind(height)
143145
.bind(hash.to_string())
144146
.execute(&self.pool)
145147
.await?;
146148
}
147149
None => {
148-
sqlx::query("delete from block where height = $1")
150+
sqlx::query("DELETE FROM block WHERE height = $1")
149151
.bind(height)
150152
.execute(&self.pool)
151153
.await?;
@@ -163,7 +165,7 @@ impl Store {
163165
) -> Result<(), Error> {
164166
for (descriptor_id, last_revealed) in &keychain_txout.last_revealed {
165167
sqlx::query(
166-
"insert or replace into keychain_last_revealed(descriptor_id, last_revealed) values($1, $2)",
168+
"INSERT INTO keychain_last_revealed(descriptor_id, last_revealed) VALUES($1, $2) ON CONFLICT DO UPDATE SET last_revealed = $2",
167169
)
168170
.bind(descriptor_id.to_string())
169171
.bind(last_revealed)
@@ -173,7 +175,7 @@ impl Store {
173175
for (descriptor_id, spk_cache) in &keychain_txout.spk_cache {
174176
for (derivation_index, script) in spk_cache {
175177
sqlx::query(
176-
"insert or replace into keychain_script_pubkey(descriptor_id, derivation_index, script) values($1, $2, $3)",
178+
"INSERT OR IGNORE INTO keychain_script_pubkey(descriptor_id, derivation_index, script) VALUES($1, $2, $3)",
177179
)
178180
.bind(descriptor_id.to_string())
179181
.bind(*derivation_index)
@@ -190,7 +192,7 @@ impl Store {
190192
pub async fn read_tx_graph(&self) -> Result<tx_graph::ChangeSet<ConfirmationBlockTime>, Error> {
191193
let mut changeset = tx_graph::ChangeSet::default();
192194

193-
let rows = sqlx::query("select txid, tx, first_seen, last_seen, last_evicted from tx")
195+
let rows = sqlx::query("SELECT txid, tx, first_seen, last_seen, last_evicted FROM tx")
194196
.fetch_all(&self.pool)
195197
.await?;
196198
for row in rows {
@@ -228,7 +230,7 @@ impl Store {
228230
}
229231

230232
let rows =
231-
sqlx::query("select block_height, block_hash, txid, confirmation_time from anchor")
233+
sqlx::query("SELECT block_height, block_hash, txid, confirmation_time FROM anchor")
232234
.fetch_all(&self.pool)
233235
.await?;
234236
for row in rows {
@@ -252,7 +254,7 @@ impl Store {
252254
pub async fn read_local_chain(&self) -> Result<local_chain::ChangeSet, Error> {
253255
let mut changeset = local_chain::ChangeSet::default();
254256

255-
let rows = sqlx::query("select height, hash from block")
257+
let rows = sqlx::query("SELECT height, hash FROM block")
256258
.fetch_all(&self.pool)
257259
.await?;
258260
for row in rows {
@@ -269,7 +271,7 @@ impl Store {
269271
pub async fn read_keychain_txout(&self) -> Result<keychain_txout::ChangeSet, Error> {
270272
let mut changeset = keychain_txout::ChangeSet::default();
271273

272-
let rows = sqlx::query("select descriptor_id, last_revealed from keychain_last_revealed")
274+
let rows = sqlx::query("SELECT descriptor_id, last_revealed FROM keychain_last_revealed")
273275
.fetch_all(&self.pool)
274276
.await?;
275277
for row in rows {
@@ -280,7 +282,7 @@ impl Store {
280282
}
281283

282284
let rows = sqlx::query(
283-
"select descriptor_id, derivation_index, script from keychain_script_pubkey",
285+
"SELECT descriptor_id, derivation_index, script FROM keychain_script_pubkey",
284286
)
285287
.fetch_all(&self.pool)
286288
.await?;

src/wallet.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl Store {
3737

3838
/// Write network.
3939
pub async fn write_network(&self, network: Network) -> Result<(), Error> {
40-
sqlx::query("insert into network(network) values($1)")
40+
sqlx::query("INSERT OR IGNORE INTO network(network) VALUES($1)")
4141
.bind(network.to_string())
4242
.execute(&self.pool)
4343
.await?;
@@ -55,7 +55,7 @@ impl Store {
5555
KeychainKind::External => 0u8,
5656
KeychainKind::Internal => 1,
5757
};
58-
sqlx::query("insert into keychain(keychain, descriptor) values($1, $2)")
58+
sqlx::query("INSERT OR IGNORE INTO keychain(keychain, descriptor) VALUES($1, $2)")
5959
.bind(keychain)
6060
.bind(descriptor.to_string())
6161
.execute(&self.pool)
@@ -89,7 +89,7 @@ impl Store {
8989

9090
/// Read network.
9191
pub async fn read_network(&self) -> Result<Option<Network>, Error> {
92-
let row = sqlx::query("select network from network")
92+
let row = sqlx::query("SELECT network FROM network")
9393
.fetch_optional(&self.pool)
9494
.await?;
9595

@@ -106,7 +106,7 @@ impl Store {
106106
) -> Result<BTreeMap<KeychainKind, Descriptor<DescriptorPublicKey>>, Error> {
107107
let mut descriptors = BTreeMap::new();
108108

109-
let rows = sqlx::query("select keychain, descriptor from keychain")
109+
let rows = sqlx::query("SELECT keychain, descriptor FROM keychain")
110110
.fetch_all(&self.pool)
111111
.await?;
112112
for row in rows {

0 commit comments

Comments
 (0)