lib.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //! SQL-backed Store implementation for SQLite and PostgreSQL.
  2. //!
  3. //! Uses `sqlx::Any` for database-agnostic queries. Enable features
  4. //! `sqlite` or `postgres` to select the backend.
  5. //!
  6. //! ```text
  7. //! kuatia_storage_sql::install_drivers()?;
  8. //! let pool = sqlx::any::AnyPoolOptions::new()
  9. //! .connect("sqlite::memory:").await?;
  10. //! let store = SqlStore::new(pool);
  11. //! store.migrate().await?;
  12. //! ```
  13. //!
  14. //! [`install_drivers`] replaces `sqlx::any::install_default_drivers` and has to
  15. //! run before the pool connects; see its docs for why.
  16. //!
  17. //! The [`Store`](kuatia_storage::store::Store) sub-traits are each implemented in
  18. //! their own module (`account`, `posting`, `transfer`, `commit`, `event`,
  19. //! `book`, `projection`); shared row mappers and codecs live in `row`, the schema
  20. //! migrations in `migrate`, the SQLite/PostgreSQL divergence behind the
  21. //! `Dialect` seam in `dialect`, and the SQLite money functions in `sqlite_ext`.
  22. use sqlx::{Any, Pool};
  23. use kuatia_storage::error::StoreError;
  24. use kuatia_types::autoid::AutoId;
  25. use crate::dialect::Dialect;
  26. mod account;
  27. mod asset;
  28. mod book;
  29. mod commit;
  30. mod dialect;
  31. mod event;
  32. mod migrate;
  33. mod posting;
  34. mod projection;
  35. mod row;
  36. #[cfg(feature = "sqlite")]
  37. mod sqlite_ext;
  38. mod transfer;
  39. /// Install the `sqlx::Any` drivers and, when the `sqlite` feature is on, the
  40. /// SQLite money functions.
  41. ///
  42. /// Call this instead of [`sqlx::any::install_default_drivers`], and call it
  43. /// **before** connecting any pool: the SQLite functions are registered as an
  44. /// auto-extension, so a connection opened earlier will not have them and every
  45. /// statement naming one fails to prepare.
  46. pub fn install_drivers() -> Result<(), StoreError> {
  47. sqlx::any::install_default_drivers();
  48. install_sqlite_extensions()
  49. }
  50. /// Install just the SQLite money functions, for callers that install the
  51. /// `sqlx::Any` drivers themselves. Idempotent; a no-op without the `sqlite`
  52. /// feature. See [`install_drivers`] for the ordering requirement.
  53. pub fn install_sqlite_extensions() -> Result<(), StoreError> {
  54. #[cfg(feature = "sqlite")]
  55. {
  56. sqlite_ext::install()?;
  57. }
  58. Ok(())
  59. }
  60. /// SQL-backed [`Store`](kuatia_storage::store::Store) implementation.
  61. pub struct SqlStore {
  62. pool: Pool<Any>,
  63. autoid: AutoId,
  64. /// Which backend this store talks to; resolved once at construction.
  65. dialect: Dialect,
  66. }
  67. impl SqlStore {
  68. /// Create a new SQL store wrapping an existing connection pool. The backend
  69. /// dialect is resolved from the pool's connection URL, so no query is issued
  70. /// here; call [`migrate`](Self::migrate) next to apply the schema.
  71. pub fn new(pool: Pool<Any>) -> Self {
  72. let dialect = Dialect::from_pool(&pool);
  73. Self {
  74. pool,
  75. autoid: AutoId::new(),
  76. dialect,
  77. }
  78. }
  79. }