sqlite_watcher/connection.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
use crate::watcher::{ObservedTableOp, Watcher};
use fixedbitset::FixedBitSet;
use std::error::Error;
use std::future::Future;
use std::ops::{Deref, DerefMut};
use std::sync::Arc;
use tracing::debug;
#[cfg(feature = "rusqlite")]
pub mod rusqlite;
#[cfg(feature = "sqlx")]
pub mod sqlx;
/// Defines an implementation capable of executing SQL statement on a sqlite connection.
///
/// This is required so we can set up the temporary triggers and tables required to
/// track changes.
pub trait SqlExecutor {
type Error: Error;
/// This method will execute a query which returns 0 or N rows with one column of type `usize`.
///
/// # Errors
///
/// Should return error if the query failed.
fn sql_query_values(&mut self, query: &str) -> Result<Vec<usize>, Self::Error>;
/// Execute an sql statement which does not return any rows.
///
/// # Errors
///
/// Should return error if the query failed.
fn sql_execute(&mut self, query: &str) -> Result<(), Self::Error>;
}
/// Defines an implementation of a sqlite connection from which we can create an [`SqlTransaction`].
#[allow(clippy::module_name_repetitions)]
pub trait SqlConnection: SqlExecutor {
/// Create a new transaction for the connection.
///
/// # Errors
///
/// Should return an error if the transaction can't be created.
fn sql_transaction(
&mut self,
) -> Result<impl SqlTransaction<Error = Self::Error> + '_, Self::Error>;
}
/// Defines a transaction on a sqlite connection.
pub trait SqlTransaction: SqlExecutor {
/// Commit the current transaction.
///
/// # Errors
///
/// Should return an error if a transaction can't be committed.
fn sql_commit_transaction(self) -> Result<(), Self::Error>;
}
/// Defines an implementation capable of executing SQL statement on a sqlite connection.
///
/// This is required so we can set up the temporary triggers and tables required to
/// track changes.
pub trait SqlExecutorAsync {
type Error: Error;
/// This method will execute a query which returns 0 or N rows with one column of type `usize`.
///
/// # Errors
///
/// Should return error if the query failed.
fn sql_query_values(
&mut self,
query: &str,
) -> impl Future<Output = Result<Vec<usize>, Self::Error>> + Send;
/// Execute an sql statement which does not return any rows.
///
/// # Errors
///
/// Should return error if the query failed.
fn sql_execute(&mut self, query: &str) -> impl Future<Output = Result<(), Self::Error>> + Send;
}
/// Defines an implementation of a sqlite connection from which we can create an [`crate::connection::SqlTransaction`].
#[allow(clippy::module_name_repetitions)]
pub trait SqlConnectionAsync: SqlExecutorAsync {
/// Create a new transaction for the connection.
///
/// # Errors
///
/// Should return an error if the transaction can't be created.
fn sql_transaction(
&mut self,
) -> impl Future<Output = Result<impl SqlTransactionAsync<Error = Self::Error> + '_, Self::Error>>
+ Send;
}
/// Defines a transaction on a sqlite connection.
pub trait SqlTransactionAsync: SqlExecutorAsync {
/// Commit the current transaction.
///
/// # Errors
///
/// Should return an error if a transaction can't be committed.
fn sql_commit_transaction(self) -> impl Future<Output = Result<(), Self::Error>> + Send;
}
/// Building block to provide tracking capabilities to any type of sqlite connection which
/// implements the [`SqlConnection`] trait.
///
/// # Initialization
///
/// It's recommended to call [`State::set_pragmas()`] to enable in memory temporary tables and recursive
/// triggers. If your connection already has this set up, this can be skipped.
///
/// Next you need to create the infrastructure to track changes. This can be accomplished with
/// [`State::start_tracking()`].
///
/// # Tracking changes
///
/// To make sure we only track required tables always call [`State::sync_tables()`] before a query/statement
/// or a transaction.
///
/// When the query/statement or transaction are completed, call [`State::publish_changes()`] to check
/// which tables have been modified and send this information to the watcher.
///
/// # Disable Tracking
///
/// If you wish to remove all the tracking infrastructure from a connection on which
/// [`State::start_tracking()`] was called, then call [`State::stop_tracking()`].
///
/// # See Also
///
/// The [`Connection`] type provided by this crate provides an example integration implementation.
pub struct State {
tracked_tables: FixedBitSet,
last_sync_version: u64,
}
impl State {
/// Enable required pragmas for execution.
///
/// # Errors
///
/// Returns error if the pragma changes failed.
pub fn set_pragmas<C: SqlConnection>(connection: &mut C) -> Result<(), C::Error> {
connection.sql_execute("PRAGMA temp_store = MEMORY")?;
connection.sql_execute("PRAGMA recursive_triggers='ON'")?;
Ok(())
}
/// Enable required pragmas for execution.
///
/// # Errors
///
/// Returns error if the pragma changes failed.
pub async fn set_pragmas_async<C: SqlConnectionAsync>(
connection: &mut C,
) -> Result<(), C::Error> {
connection.sql_execute("PRAGMA temp_store = MEMORY").await?;
connection
.sql_execute("PRAGMA recursive_triggers='ON'")
.await?;
Ok(())
}
/// Prepare the `connection` for tracking.
///
/// This will create the temporary table used to track change.
///
/// # Errors
///
/// Returns error if the initialization failed.
pub fn start_tracking<C: SqlConnection>(connection: &mut C) -> Result<(), C::Error> {
// create tracking table and cleanup previous data if re-used from a connection pool.
let mut tx = connection.sql_transaction()?;
tx.sql_execute(&create_tracking_table_query())?;
tx.sql_execute(&empty_tracking_table_query())?;
tx.sql_commit_transaction()
}
/// Prepare the `connection` for tracking.
///
/// This will create the temporary table used to track change.
///
/// # Errors
///
/// Returns error if the initialization failed.
pub async fn start_tracking_async<C: SqlConnectionAsync>(
connection: &mut C,
) -> Result<(), C::Error> {
// create tracking table and cleanup previous data if re-used from a connection pool.
let mut tx = connection.sql_transaction().await?;
tx.sql_execute(&create_tracking_table_query()).await?;
tx.sql_execute(&empty_tracking_table_query()).await?;
tx.sql_commit_transaction().await
}
/// Remove all triggers and the tracking table from `connection`.
//
/// # Errors
///
/// Returns error if the initialization failed.
pub fn stop_tracking<C: SqlConnection>(
&self,
connection: &mut C,
watcher: &Watcher,
) -> Result<(), C::Error> {
let tables = watcher.observed_tables();
let mut tx = connection.sql_transaction()?;
for (id, table_name) in tables.into_iter().enumerate() {
drop_triggers(&mut tx, &table_name, id)?;
}
tx.sql_execute(&drop_tracking_table_query())?;
tx.sql_commit_transaction()
}
/// Remove all triggers and the tracking table from `connection`.
//
/// # Errors
///
/// Returns error if the initialization failed.
pub async fn stop_tracking_async<C: SqlConnectionAsync>(
&self,
connection: &mut C,
watcher: &Watcher,
) -> Result<(), C::Error> {
let tables = watcher.observed_tables();
let mut tx = connection.sql_transaction().await?;
for (id, table_name) in tables.into_iter().enumerate() {
drop_triggers_async(&mut tx, &table_name, id).await?;
}
tx.sql_execute(&drop_tracking_table_query()).await?;
tx.sql_commit_transaction().await
}
/// Create a new instance without initializing any connection.
#[must_use]
pub fn new() -> Self {
Self {
tracked_tables: FixedBitSet::new(),
last_sync_version: 0,
}
}
/// Synchronize the table list from the watcher.
///
/// This method will create new triggers for tables that are not being watched over this
/// connection and remove triggers for tables that are no longer observed by the watcher.
///
/// # Errors
///
/// Returns error if creation or removal of triggers failed.
#[tracing::instrument(level=tracing::Level::DEBUG, skip(self, connection, watcher))]
pub fn sync_tables<C: SqlConnection>(
&mut self,
connection: &mut C,
watcher: &Watcher,
) -> Result<(), C::Error> {
let Some(new_version) = self.should_sync(watcher) else {
return Ok(());
};
debug!("Syncing tables from observer");
let Some((new_tracker_state, tracker_changes)) = self.calculate_sync_changes(watcher)
else {
debug!("No changes");
return Ok(());
};
let mut tx = connection.sql_transaction()?;
for change in tracker_changes {
match change {
ObservedTableOp::Add(table_name, id) => {
debug!("Add watcher for table {table_name} id={id}");
create_triggers(&mut tx, &table_name, id)?;
}
ObservedTableOp::Remove(table_name, id) => {
debug!("Remove watcher for table {table_name}");
drop_triggers(&mut tx, &table_name, id)?;
}
}
}
tx.sql_commit_transaction()?;
self.apply_sync_changes(new_tracker_state, new_version);
Ok(())
}
/// Synchronize the table list from the watcher.
///
/// This method will create new triggers for tables that are not being watched over this
/// connection and remove triggers for tables that are no longer observed by the watcher.
///
/// # Errors
///
/// Returns error if creation or removal of triggers failed.
#[tracing::instrument(level=tracing::Level::DEBUG, skip(self, connection, watcher))]
pub async fn sync_tables_async<C: SqlConnectionAsync>(
&mut self,
connection: &mut C,
watcher: &Watcher,
) -> Result<(), C::Error> {
let Some(new_version) = self.should_sync(watcher) else {
return Ok(());
};
debug!("Syncing tables from observer");
let Some((new_tracker_state, tracker_changes)) = self.calculate_sync_changes(watcher)
else {
debug!("No changes");
return Ok(());
};
let mut tx = connection.sql_transaction().await?;
for change in tracker_changes {
match change {
ObservedTableOp::Add(table_name, id) => {
debug!("Add watcher for table {table_name} id={id}");
create_triggers_async(&mut tx, &table_name, id).await?;
}
ObservedTableOp::Remove(table_name, id) => {
debug!("Remove watcher for table {table_name}");
drop_triggers_async(&mut tx, &table_name, id).await?;
}
}
}
tx.sql_commit_transaction().await?;
self.apply_sync_changes(new_tracker_state, new_version);
Ok(())
}
/// Check the tracking table and report finding to the [Watcher].
///
/// The table where the changes are tracked is read and reset. Any
/// table that has been modified will be communicated to the [Watcher], which in turn
/// will notify the respective [TableObserver].
///
/// # Errors
///
/// Returns error if we failed to read from the temporary tables.
///
/// [Watcher]: `crate::watcher::Watcher`
/// [TableObserver]: `crate::watcher::TableObserver`
#[tracing::instrument(level=tracing::Level::DEBUG, skip(self, connection, watcher))]
pub fn publish_changes<C: SqlConnection>(
&mut self,
connection: &mut C,
watcher: &Watcher,
) -> Result<(), C::Error> {
let mut result = FixedBitSet::with_capacity(self.tracked_tables.len());
let query = select_updated_tables_query();
let modified_table_ids = connection.sql_query_values(&query)?;
for id in modified_table_ids {
debug!("Table {} has been modified", id);
result.set(id, true);
}
if !result.is_clear() {
// Reset updated values.
connection.sql_execute(&reset_updated_tables_query())?;
}
watcher.publish_changes(result);
Ok(())
}
/// Check the tracking table and report finding to the [Watcher].
///
/// The table where the changes are tracked is read and reset. Any
/// table that has been modified will be communicated to the [Watcher], which in turn
/// will notify the respective [TableObserver].
///
/// # Errors
///
/// Returns error if we failed to read from the temporary tables.
///
/// [Watcher]: `crate::watcher::Watcher`
/// [TableObserver]: `crate::watcher::TableObserver`
#[tracing::instrument(level=tracing::Level::DEBUG, skip(self, connection, watcher))]
pub async fn publish_changes_async<C: SqlConnectionAsync>(
&mut self,
connection: &mut C,
watcher: &Watcher,
) -> Result<(), C::Error> {
let mut result = FixedBitSet::with_capacity(self.tracked_tables.len());
let query = select_updated_tables_query();
let modified_table_ids = connection.sql_query_values(&query).await?;
for id in modified_table_ids {
debug!("Table {} has been modified", id);
result.set(id, true);
}
if !result.is_clear() {
// Reset updated values.
connection
.sql_execute(&reset_updated_tables_query())
.await?;
}
watcher.publish_changes_async(result).await;
Ok(())
}
fn should_sync(&self, watcher: &Watcher) -> Option<u64> {
let service_version = watcher.tables_version();
if service_version == self.last_sync_version {
None
} else {
Some(service_version)
}
}
/// Determine which tables should start and/or stop being watched.
fn calculate_sync_changes(
&self,
watcher: &Watcher,
) -> Option<(FixedBitSet, Vec<ObservedTableOp>)> {
let (new_tracker_state, tracker_changes) =
watcher.calculate_sync_changes(&self.tracked_tables);
if tracker_changes.is_empty() {
return None;
}
Some((new_tracker_state, tracker_changes))
}
/// Once we are satisfied with the changes, apply the new state.
fn apply_sync_changes(&mut self, new_tracker_state: FixedBitSet, new_version: u64) {
// Update local tracker bitset
self.tracked_tables = new_tracker_state;
self.last_sync_version = new_version;
}
}
/// Connection abstraction that provides on possible implementation which uses the building
/// blocks ([`State`]) provided by this crate.
///
/// For simplicity, it takes ownership of an existing type which implements [`SqlConnection`] and
/// initializes all the tracking infrastructure. The original type can still be accessed as
/// [`Connection`] implements both [`Deref`] and [`DerefMut`].
///
/// # Remarks
///
/// To make sure all changes are capture, it's recommended to always call
/// [`Connection::sync_watcher_tables()`]
/// before any query/statement or transaction.
///
/// # Example
///
/// ## Single Query/Statement
///
/// ```rust
/// use sqlite_watcher::connection::Connection;
/// use sqlite_watcher::connection::SqlConnection;
/// use sqlite_watcher::watcher::Watcher;
///
/// pub fn track_changes<C:SqlConnection>(connection: C) {
/// let watcher = Watcher::new().unwrap();
/// let mut connection = Connection::new(connection, watcher).unwrap();
///
/// // Sync tables so we are up to date.
/// connection.sync_watcher_tables().unwrap();
///
/// connection.sql_execute("sql query here").unwrap();
///
/// // Publish changes to the watcher
/// connection.publish_watcher_changes().unwrap();
/// }
/// ```
///
/// ## Transaction
///
/// ```rust
/// use sqlite_watcher::connection::Connection;
/// use sqlite_watcher::connection::{SqlConnection, SqlTransaction, SqlExecutor};
/// use sqlite_watcher::watcher::Watcher;
///
/// pub fn track_changes<C:SqlConnection>(connection: C) {
/// let watcher = Watcher::new().unwrap();
/// let mut connection = Connection::new(connection, watcher).unwrap();
///
/// // Sync tables so we are up to date.
/// connection.sync_watcher_tables().unwrap();
///
/// let mut tx = connection.sql_transaction().unwrap();
///
/// tx.sql_execute("sql query here").unwrap();
/// tx.sql_execute("sql query here").unwrap();
/// tx.sql_execute("sql query here").unwrap();
///
/// tx.sql_commit_transaction().unwrap();
///
/// // Publish changes to the watcher
/// connection.publish_watcher_changes().unwrap();
/// }
/// ```
pub struct Connection<C: SqlConnection> {
state: State,
watcher: Arc<Watcher>,
connection: C,
}
impl<C: SqlConnection> Connection<C> {
/// Create a new connection with `connection` and `watcher`.
///
/// See [`State::start_tracking()`] for more information about initialization.
///
/// # Errors
///
/// Returns error if the initialization failed.
pub fn new(mut connection: C, watcher: Arc<Watcher>) -> Result<Self, C::Error> {
let state = State::new();
State::set_pragmas(&mut connection)?;
State::start_tracking(&mut connection)?;
Ok(Self {
state,
watcher,
connection,
})
}
/// Sync tables from the [`Watcher`] and update tracking infrastructure.
///
/// See [`State::sync_tables()`] for more information.
///
/// # Errors
///
/// Returns error if we failed to sync the changes to the database.
pub fn sync_watcher_tables(&mut self) -> Result<(), C::Error> {
self.state.sync_tables(&mut self.connection, &self.watcher)
}
/// Check if any tables have changed and notify the [`Watcher`]
///
/// See [`State::publish_changes()`] for more information.
///
/// It is recommended to call this method
///
/// # Errors
///
/// Returns error if we failed to check for changes.
pub fn publish_watcher_changes(&mut self) -> Result<(), C::Error> {
self.state
.publish_changes(&mut self.connection, &self.watcher)
}
/// Disable all tracking on this connection.
///
/// See [`State::stop_tracking`] for more details.
///
/// # Errors
///
/// Returns error if the queries failed.
pub fn stop_tracking(&mut self) -> Result<(), C::Error> {
self.state
.stop_tracking(&mut self.connection, &self.watcher)
}
/// Consume the current connection and take ownership of the real sql connection.
///
/// # Remarks
///
/// This does not stop the tracking infrastructure enabled on the connection.
/// Use [`Self::stop_tracking()`] to disable it first.
pub fn take(self) -> C {
self.connection
}
}
/// Same as [`Connection`] but with an async executor.
#[allow(clippy::module_name_repetitions)]
pub struct ConnectionAsync<C: SqlConnectionAsync> {
state: State,
watcher: Arc<Watcher>,
connection: C,
}
impl<C: SqlConnectionAsync> ConnectionAsync<C> {
/// Create a new connection with `connection` and `watcher`.
///
/// See [`State::start_tracking()`] for more information about initialization.
///
/// # Errors
///
/// Returns error if the initialization failed.
pub async fn new(mut connection: C, watcher: Arc<Watcher>) -> Result<Self, C::Error> {
let state = State::new();
State::set_pragmas_async(&mut connection).await?;
State::start_tracking_async(&mut connection).await?;
Ok(Self {
state,
watcher,
connection,
})
}
/// See [`Connection::sync_watcher_tables`] for more details.
///
/// # Errors
///
/// Returns error if we failed to sync the changes to the database.
pub async fn sync_watcher_tables(&mut self) -> Result<(), C::Error> {
self.state
.sync_tables_async(&mut self.connection, &self.watcher)
.await
}
/// See [`Connection::publish_watcher_changes`] for more details.
///
/// # Errors
///
/// Returns error if we failed to check for changes.
pub async fn publish_watcher_changes(&mut self) -> Result<(), C::Error> {
self.state
.publish_changes_async(&mut self.connection, &self.watcher)
.await
}
/// See [`Connection::stop_tracking`] for more details.
///
/// # Errors
///
/// Returns error if the queries failed.
pub async fn stop_tracking(&mut self) -> Result<(), C::Error> {
self.state
.stop_tracking_async(&mut self.connection, &self.watcher)
.await
}
/// Consume the current connection and take ownership of the real sql connection.
///
/// # Remarks
///
/// This does not stop the tracking infrastructure enabled on the connection.
/// Use [`Self::stop_tracking()`] to disable it first.
pub fn take(self) -> C {
self.connection
}
}
impl<C: SqlConnectionAsync> Deref for ConnectionAsync<C> {
type Target = C;
fn deref(&self) -> &Self::Target {
&self.connection
}
}
impl<C: SqlConnectionAsync> DerefMut for ConnectionAsync<C> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.connection
}
}
impl<C: SqlConnectionAsync> AsRef<C> for ConnectionAsync<C> {
fn as_ref(&self) -> &C {
&self.connection
}
}
impl<C: SqlConnectionAsync> AsMut<C> for ConnectionAsync<C> {
fn as_mut(&mut self) -> &mut C {
&mut self.connection
}
}
impl<C: SqlConnection> Deref for Connection<C> {
type Target = C;
fn deref(&self) -> &Self::Target {
&self.connection
}
}
impl<C: SqlConnection> DerefMut for Connection<C> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.connection
}
}
impl<C: SqlConnection> AsRef<C> for Connection<C> {
fn as_ref(&self) -> &C {
&self.connection
}
}
impl<C: SqlConnection> AsMut<C> for Connection<C> {
fn as_mut(&mut self) -> &mut C {
&mut self.connection
}
}
const TRACKER_TABLE_NAME: &str = "rsqlite_watcher_version_tracker";
const TRIGGER_LIST: [(&str, &str); 3] = [
("INSERT", "insert"),
("UPDATE", "update"),
("DELETE", "delete"),
];
#[inline]
fn create_tracking_table_query() -> String {
format!("CREATE TEMP TABLE IF NOT EXISTS `{TRACKER_TABLE_NAME}` (table_id INTEGER PRIMARY KEY, updated INTEGER)")
}
#[inline]
fn empty_tracking_table_query() -> String {
format!("DELETE FROM `{TRACKER_TABLE_NAME}`")
}
#[inline]
fn drop_tracking_table_query() -> String {
format!("DROP TABLE IF EXISTS `{TRACKER_TABLE_NAME}`")
}
#[inline]
fn create_trigger_query(
writer: &mut impl std::fmt::Write,
table_name: &str,
trigger: &str,
trigger_name: &str,
table_id: usize,
) {
write!(
writer,
r#"
CREATE TEMP TRIGGER IF NOT EXISTS `{TRACKER_TABLE_NAME}_trigger_{table_name}_{trigger_name}` AFTER {trigger} ON `{table_name}`
BEGIN
UPDATE `{TRACKER_TABLE_NAME}` SET updated=1 WHERE table_id={table_id};
END
"#
)
.expect("should not fail");
}
#[inline]
fn insert_table_id_into_tracking_table_query(writer: &mut impl std::fmt::Write, id: usize) {
write!(writer, "INSERT INTO `{TRACKER_TABLE_NAME}` VALUES ({id},0)").expect("Should not fail");
}
#[inline]
fn drop_trigger_query(writer: &mut impl std::fmt::Write, table_name: &str, trigger_name: &str) {
write!(
writer,
"DROP TRIGGER IF EXISTS `{TRACKER_TABLE_NAME}_trigger_{table_name}_{trigger_name}`"
)
.expect("should not fail");
}
#[inline]
fn remove_table_id_from_tracking_table_query(writer: &mut impl std::fmt::Write, table_id: usize) {
write!(
writer,
"DELETE FROM `{TRACKER_TABLE_NAME}` WHERE table_id={table_id}"
)
.expect("should not fail");
}
#[inline]
fn select_updated_tables_query() -> String {
format!("SELECT table_id FROM `{TRACKER_TABLE_NAME}` WHERE updated=1")
}
#[inline]
fn reset_updated_tables_query() -> String {
format!("UPDATE `{TRACKER_TABLE_NAME}` SET updated=0 WHERE updated=1")
}
/// Create tracking triggers for `table` with `id`.
///
/// # Errors
///
/// Return error if the query failed.
fn create_triggers<Ex: SqlExecutor>(
executor: &mut Ex,
table: &str,
id: usize,
) -> Result<(), Ex::Error> {
let mut query = String::with_capacity(64);
for (trigger, trigger_name) in TRIGGER_LIST {
query.clear();
create_trigger_query(&mut query, table, trigger, trigger_name, id);
executor.sql_execute(&query)?;
}
query.clear();
insert_table_id_into_tracking_table_query(&mut query, id);
executor.sql_execute(&query)?;
Ok(())
}
/// Create tracking triggers for `table` with `id`.
///
/// # Errors
///
/// Return error if the query failed.
async fn create_triggers_async<Ex: SqlExecutorAsync>(
executor: &mut Ex,
table: &str,
id: usize,
) -> Result<(), Ex::Error> {
let mut query = String::with_capacity(64);
for (trigger, trigger_name) in TRIGGER_LIST {
query.clear();
create_trigger_query(&mut query, table, trigger, trigger_name, id);
executor.sql_execute(&query).await?;
}
query.clear();
insert_table_id_into_tracking_table_query(&mut query, id);
executor.sql_execute(&query).await?;
Ok(())
}
/// Remove tracking triggers for `table` with `id`.
///
/// # Errors
///
/// Return error if the query failed.
fn drop_triggers<Ex: SqlExecutor>(
executor: &mut Ex,
table: &str,
id: usize,
) -> Result<(), Ex::Error> {
let mut query = String::with_capacity(64);
for (_, trigger_name) in TRIGGER_LIST {
query.clear();
drop_trigger_query(&mut query, table, trigger_name);
executor.sql_execute(&query)?;
}
query.clear();
remove_table_id_from_tracking_table_query(&mut query, id);
executor.sql_execute(&query)?;
Ok(())
}
/// Remove tracking triggers for `table` with `id`.
///
/// # Errors
///
/// Return error if the query failed.
async fn drop_triggers_async<Ex: SqlExecutorAsync>(
executor: &mut Ex,
table: &str,
id: usize,
) -> Result<(), Ex::Error> {
let mut query = String::with_capacity(64);
for (_, trigger_name) in TRIGGER_LIST {
query.clear();
drop_trigger_query(&mut query, table, trigger_name);
executor.sql_execute(&query).await?;
}
query.clear();
remove_table_id_from_tracking_table_query(&mut query, id);
executor.sql_execute(&query).await?;
Ok(())
}
#[cfg(test)]
mod test {
use crate::connection::State;
use crate::watcher::{new_test_observer, ObservedTableOp, TableObserver, Watcher};
use std::collections::BTreeSet;
use std::sync::mpsc::{Receiver, SyncSender};
use std::sync::Mutex;
pub struct TestObserver {
expected: Mutex<Vec<BTreeSet<String>>>,
tables: Vec<String>,
// Channel is here to make sure we don't trigger a merge of multiple pending updates.
checked_channel: SyncSender<()>,
}
impl TestObserver {
pub fn new(
tables: Vec<String>,
expected: impl IntoIterator<Item = BTreeSet<String>>,
) -> (Self, Receiver<()>) {
let (sender, receiver) = std::sync::mpsc::sync_channel::<()>(0);
let mut expected = expected.into_iter().collect::<Vec<_>>();
expected.reverse();
(
Self {
expected: Mutex::new(expected),
tables,
checked_channel: sender,
},
receiver,
)
}
}
impl TableObserver for TestObserver {
fn tables(&self) -> Vec<String> {
self.tables.clone()
}
fn on_tables_changed(&self, tables: &BTreeSet<String>) {
let expected = self.expected.lock().unwrap().pop().unwrap();
assert_eq!(*tables, expected);
self.checked_channel.send(()).unwrap();
}
}
#[test]
fn connection_state() {
let service = Watcher::new().unwrap();
let observer_1 = new_test_observer(["foo", "bar"]);
let observer_2 = new_test_observer(["bar"]);
let observer_3 = new_test_observer(["bar", "omega"]);
let mut local_state = State::new();
assert!(local_state.should_sync(&service).is_none());
let observer_id_1 = service.add_observer(observer_1).unwrap();
let foo_table_id = service.get_table_id("foo").unwrap();
let bar_table_id = service.get_table_id("bar").unwrap();
{
let new_version = local_state
.should_sync(&service)
.expect("Should have new version");
let (tracker, ops) = local_state
.calculate_sync_changes(&service)
.expect("must have changes");
assert!(tracker[foo_table_id]);
assert!(tracker[bar_table_id]);
assert_eq!(ops.len(), 2);
assert_eq!(
ops[0],
ObservedTableOp::Add("foo".to_string(), foo_table_id)
);
assert_eq!(
ops[1],
ObservedTableOp::Add("bar".to_string(), bar_table_id)
);
local_state.apply_sync_changes(tracker, new_version);
}
let observer_id_2 = service.add_observer(observer_2).unwrap();
assert!(local_state.should_sync(&service).is_none());
let observer_id_3 = service.add_observer(observer_3).unwrap();
let omega_table_id = service.get_table_id("omega").unwrap();
{
let new_version = local_state
.should_sync(&service)
.expect("Should have new version");
let (tracker, ops) = local_state
.calculate_sync_changes(&service)
.expect("must have changes");
assert!(tracker[foo_table_id]);
assert!(tracker[bar_table_id]);
assert!(tracker[omega_table_id]);
assert_eq!(ops.len(), 1);
assert_eq!(
ops[0],
ObservedTableOp::Add("omega".to_string(), omega_table_id)
);
local_state.apply_sync_changes(tracker, new_version);
}
service.remove_observer(observer_id_2).unwrap();
assert!(local_state.should_sync(&service).is_none());
service.remove_observer(observer_id_3).unwrap();
{
let new_version = local_state
.should_sync(&service)
.expect("Should have new version");
let (tracker, ops) = local_state
.calculate_sync_changes(&service)
.expect("must have changes");
assert!(tracker[foo_table_id]);
assert!(tracker[bar_table_id]);
assert!(!tracker[omega_table_id]);
assert_eq!(ops.len(), 1);
assert_eq!(
ops[0],
ObservedTableOp::Remove("omega".to_string(), omega_table_id)
);
local_state.apply_sync_changes(tracker, new_version);
}
service.remove_observer(observer_id_1).unwrap();
{
let new_version = local_state
.should_sync(&service)
.expect("Should have new version");
let (tracker, ops) = local_state
.calculate_sync_changes(&service)
.expect("must have changes");
assert!(!tracker[foo_table_id]);
assert!(!tracker[bar_table_id]);
assert!(!tracker[omega_table_id]);
assert_eq!(ops.len(), 2);
assert_eq!(
ops[0],
ObservedTableOp::Remove("foo".to_string(), foo_table_id)
);
assert_eq!(
ops[1],
ObservedTableOp::Remove("bar".to_string(), bar_table_id)
);
local_state.apply_sync_changes(tracker, new_version);
}
}
}