pub const ANCHORS_TABLE_NAME: &'static str = "bdk_anchors";
/// Initialize sqlite tables.
- fn init_sqlite_tables(db_tx: &rusqlite::Transaction) -> rusqlite::Result<()> {
+ pub fn init_sqlite_tables(db_tx: &rusqlite::Transaction) -> rusqlite::Result<()> {
let schema_v0: &[&str] = &[
// full transactions
&format!(
}
/// Construct a [`TxGraph`] from an sqlite database.
+ ///
+ /// Remember to call [`Self::init_sqlite_tables`] beforehand.
pub fn from_sqlite(db_tx: &rusqlite::Transaction) -> rusqlite::Result<Self> {
- Self::init_sqlite_tables(db_tx)?;
-
let mut changeset = Self::default();
let mut statement = db_tx.prepare(&format!(
}
/// Persist `changeset` to the sqlite database.
+ ///
+ /// Remember to call [`Self::init_sqlite_tables`] beforehand.
pub fn persist_to_sqlite(&self, db_tx: &rusqlite::Transaction) -> rusqlite::Result<()> {
- Self::init_sqlite_tables(db_tx)?;
-
let mut statement = db_tx.prepare_cached(&format!(
"INSERT INTO {}(txid, raw_tx) VALUES(:txid, :raw_tx) ON CONFLICT(txid) DO UPDATE SET raw_tx=:raw_tx",
Self::TXS_TABLE_NAME,
pub const BLOCKS_TABLE_NAME: &'static str = "bdk_blocks";
/// Initialize sqlite tables for persisting [`local_chain::LocalChain`].
- fn init_sqlite_tables(db_tx: &rusqlite::Transaction) -> rusqlite::Result<()> {
+ pub fn init_sqlite_tables(db_tx: &rusqlite::Transaction) -> rusqlite::Result<()> {
let schema_v0: &[&str] = &[
// blocks
&format!(
}
/// Construct a [`LocalChain`](local_chain::LocalChain) from sqlite database.
+ ///
+ /// Remember to call [`Self::init_sqlite_tables`] beforehand.
pub fn from_sqlite(db_tx: &rusqlite::Transaction) -> rusqlite::Result<Self> {
- Self::init_sqlite_tables(db_tx)?;
-
let mut changeset = Self::default();
let mut statement = db_tx.prepare(&format!(
}
/// Persist `changeset` to the sqlite database.
+ ///
+ /// Remember to call [`Self::init_sqlite_tables`] beforehand.
pub fn persist_to_sqlite(&self, db_tx: &rusqlite::Transaction) -> rusqlite::Result<()> {
- Self::init_sqlite_tables(db_tx)?;
-
let mut replace_statement = db_tx.prepare_cached(&format!(
"REPLACE INTO {}(block_height, block_hash) VALUES(:block_height, :block_hash)",
Self::BLOCKS_TABLE_NAME,
/// Initialize sqlite tables for persisting
/// [`KeychainTxOutIndex`](keychain_txout::KeychainTxOutIndex).
- fn init_sqlite_tables(db_tx: &rusqlite::Transaction) -> rusqlite::Result<()> {
+ pub fn init_sqlite_tables(db_tx: &rusqlite::Transaction) -> rusqlite::Result<()> {
let schema_v0: &[&str] = &[
// last revealed
&format!(
/// Construct [`KeychainTxOutIndex`](keychain_txout::KeychainTxOutIndex) from sqlite database
/// and given parameters.
+ ///
+ /// Remember to call [`Self::init_sqlite_tables`] beforehand.
pub fn from_sqlite(db_tx: &rusqlite::Transaction) -> rusqlite::Result<Self> {
- Self::init_sqlite_tables(db_tx)?;
-
let mut changeset = Self::default();
let mut statement = db_tx.prepare(&format!(
}
/// Persist `changeset` to the sqlite database.
+ ///
+ /// Remember to call [`Self::init_sqlite_tables`] beforehand.
pub fn persist_to_sqlite(&self, db_tx: &rusqlite::Transaction) -> rusqlite::Result<()> {
- Self::init_sqlite_tables(db_tx)?;
-
let mut statement = db_tx.prepare_cached(&format!(
"REPLACE INTO {}(descriptor_id, last_revealed) VALUES(:descriptor_id, :last_revealed)",
Self::LAST_REVEALED_TABLE_NAME,
/// Name of table to store wallet descriptors and network.
pub const WALLET_TABLE_NAME: &'static str = "bdk_wallet";
- /// Initialize sqlite tables for wallet schema & table.
- fn init_wallet_sqlite_tables(
- db_tx: &chain::rusqlite::Transaction,
- ) -> chain::rusqlite::Result<()> {
+ /// Initialize sqlite tables for wallet tables.
+ pub fn init_sqlite_tables(db_tx: &chain::rusqlite::Transaction) -> chain::rusqlite::Result<()> {
let schema_v0: &[&str] = &[&format!(
"CREATE TABLE {} ( \
id INTEGER PRIMARY KEY NOT NULL CHECK (id = 0), \
) STRICT;",
Self::WALLET_TABLE_NAME,
)];
- crate::rusqlite_impl::migrate_schema(db_tx, Self::WALLET_SCHEMA_NAME, &[schema_v0])
+ crate::rusqlite_impl::migrate_schema(db_tx, Self::WALLET_SCHEMA_NAME, &[schema_v0])?;
+
+ bdk_chain::local_chain::ChangeSet::init_sqlite_tables(db_tx)?;
+ bdk_chain::tx_graph::ChangeSet::<ConfirmationBlockTime>::init_sqlite_tables(db_tx)?;
+ bdk_chain::keychain_txout::ChangeSet::init_sqlite_tables(db_tx)?;
+
+ Ok(())
}
/// Recover a [`ChangeSet`] from sqlite database.
pub fn from_sqlite(db_tx: &chain::rusqlite::Transaction) -> chain::rusqlite::Result<Self> {
- Self::init_wallet_sqlite_tables(db_tx)?;
use chain::rusqlite::OptionalExtension;
use chain::Impl;
&self,
db_tx: &chain::rusqlite::Transaction,
) -> chain::rusqlite::Result<()> {
- Self::init_wallet_sqlite_tables(db_tx)?;
use chain::rusqlite::named_params;
use chain::Impl;
type Error = bdk_chain::rusqlite::Error;
fn initialize(persister: &mut Self) -> Result<ChangeSet, Self::Error> {
+ ChangeSet::init_sqlite_tables(&*persister)?;
ChangeSet::from_sqlite(persister)
}
fn initialize(persister: &mut Self) -> Result<ChangeSet, Self::Error> {
let db_tx = persister.transaction()?;
+ ChangeSet::init_sqlite_tables(&db_tx)?;
let changeset = ChangeSet::from_sqlite(&db_tx)?;
db_tx.commit()?;
Ok(changeset)
}
fn persist(persister: &mut Self, changeset: &ChangeSet) -> Result<(), Self::Error> {
- persister.append_changeset(changeset).map_err(FileStoreError::Write)
+ persister
+ .append_changeset(changeset)
+ .map_err(FileStoreError::Write)
}
}