Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ hypr-data = { path = "crates/data", package = "data" }
hypr-db-app = { path = "crates/db-app", package = "db-app" }
hypr-db-core2 = { path = "crates/db-core2", package = "db-core2" }
hypr-db-live-query = { path = "crates/db-live-query", package = "db-live-query" }
hypr-db-migrate = { path = "crates/db-migrate", package = "db-migrate" }
hypr-denoise = { path = "crates/denoise", package = "denoise" }
hypr-detect = { path = "crates/detect", package = "detect" }
hypr-device-monitor = { path = "crates/device-monitor", package = "device-monitor" }
Expand Down
175 changes: 175 additions & 0 deletions crates/cloudsync/src/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
use sqlx::{Executor, Sqlite};

use crate::error::Error;

/// https://docs.sqlitecloud.io/docs/sqlite-sync-api-cloudsync-version
pub async fn version<'e, E>(executor: E) -> Result<String, Error>
where
E: Executor<'e, Database = Sqlite>,
{
Ok(sqlx::query_scalar("SELECT cloudsync_version()")
.fetch_one(executor)
.await?)
}

/// https://docs.sqlitecloud.io/docs/sqlite-sync-api-cloudsync-init
pub async fn init<'e, E>(
executor: E,
table_name: &str,
crdt_algo: Option<&str>,
force: Option<bool>,
) -> Result<(), Error>
where
E: Executor<'e, Database = Sqlite> + Copy,
{
match (crdt_algo, force) {
(None, None) => {
sqlx::query("SELECT cloudsync_init(?)")
.bind(table_name)
.fetch_optional(executor)
.await?;
}
(Some(crdt_algo), None) => {
sqlx::query("SELECT cloudsync_init(?, ?)")
.bind(table_name)
.bind(crdt_algo)
.fetch_optional(executor)
.await?;
}
(None, Some(force)) => {
sqlx::query("SELECT cloudsync_init(?, NULL, ?)")
.bind(table_name)
.bind(force)
.fetch_optional(executor)
.await?;
}
(Some(crdt_algo), Some(force)) => {
sqlx::query("SELECT cloudsync_init(?, ?, ?)")
.bind(table_name)
.bind(crdt_algo)
.bind(force)
.fetch_optional(executor)
.await?;
}
}

Ok(())
}

/// https://docs.sqlitecloud.io/docs/sqlite-sync-api-cloudsync-begin-alter
pub async fn begin_alter<'e, E>(executor: E, table_name: &str) -> Result<(), Error>
where
E: Executor<'e, Database = Sqlite>,
{
sqlx::query("SELECT cloudsync_begin_alter(?)")
.bind(table_name)
.fetch_optional(executor)
.await?;

Ok(())
}

/// https://docs.sqlitecloud.io/docs/sqlite-sync-api-cloudsync-enable
pub async fn enable<'e, E>(executor: E, table_name: &str) -> Result<(), Error>
where
E: Executor<'e, Database = Sqlite>,
{
sqlx::query("SELECT cloudsync_enable(?)")
.bind(table_name)
.fetch_optional(executor)
.await?;

Ok(())
}

/// https://docs.sqlitecloud.io/docs/sqlite-sync-api-cloudsync-disable
pub async fn disable<'e, E>(executor: E, table_name: &str) -> Result<(), Error>
where
E: Executor<'e, Database = Sqlite>,
{
sqlx::query("SELECT cloudsync_disable(?)")
.bind(table_name)
.fetch_optional(executor)
.await?;

Ok(())
}

/// https://docs.sqlitecloud.io/docs/sqlite-sync-api-cloudsync-is-enabled
pub async fn is_enabled<'e, E>(executor: E, table_name: &str) -> Result<bool, Error>
where
E: Executor<'e, Database = Sqlite>,
{
Ok(sqlx::query_scalar("SELECT cloudsync_is_enabled(?)")
.bind(table_name)
.fetch_one(executor)
.await?)
}

/// https://docs.sqlitecloud.io/docs/sqlite-sync-api-cloudsync-commit-alter
pub async fn commit_alter<'e, E>(executor: E, table_name: &str) -> Result<(), Error>
where
E: Executor<'e, Database = Sqlite>,
{
sqlx::query("SELECT cloudsync_commit_alter(?)")
.bind(table_name)
.fetch_optional(executor)
.await?;

Ok(())
}

/// https://docs.sqlitecloud.io/docs/sqlite-sync-api-cloudsync-cleanup
pub async fn cleanup<'e, E>(executor: E, table_name: &str) -> Result<(), Error>
where
E: Executor<'e, Database = Sqlite>,
{
sqlx::query("SELECT cloudsync_cleanup(?)")
.bind(table_name)
.fetch_optional(executor)
.await?;

Ok(())
}

/// https://docs.sqlitecloud.io/docs/sqlite-sync-api-cloudsync-siteid
pub async fn siteid<'e, E>(executor: E) -> Result<Vec<u8>, Error>
where
E: Executor<'e, Database = Sqlite>,
{
Ok(sqlx::query_scalar("SELECT cloudsync_siteid()")
.fetch_one(executor)
.await?)
}

/// https://docs.sqlitecloud.io/docs/sqlite-sync-api-cloudsync-db-version
pub async fn db_version<'e, E>(executor: E) -> Result<i64, Error>
where
E: Executor<'e, Database = Sqlite>,
{
Ok(sqlx::query_scalar("SELECT cloudsync_db_version()")
.fetch_one(executor)
.await?)
}

/// https://docs.sqlitecloud.io/docs/sqlite-sync-api-cloudsync-uuid
pub async fn uuid<'e, E>(executor: E) -> Result<String, Error>
where
E: Executor<'e, Database = Sqlite>,
{
Ok(sqlx::query_scalar("SELECT cloudsync_uuid()")
.fetch_one(executor)
.await?)
}

/// https://docs.sqlitecloud.io/docs/sqlite-sync-api-cloudsync-terminate
pub async fn terminate<'e, E>(executor: E) -> Result<(), Error>
where
E: Executor<'e, Database = Sqlite>,
{
sqlx::query("SELECT cloudsync_terminate()")
.fetch_optional(executor)
.await?;

Ok(())
}
84 changes: 0 additions & 84 deletions crates/cloudsync/src/init.rs

This file was deleted.

7 changes: 5 additions & 2 deletions crates/cloudsync/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
#![forbid(unsafe_code)]

mod api;
mod bundle;
mod error;
mod init;
mod network;

use std::path::PathBuf;

use sqlx::sqlite::SqliteConnectOptions;

pub use api::{
begin_alter, cleanup, commit_alter, db_version, disable, enable, init, is_enabled, siteid,
terminate, uuid, version,
};
pub use bundle::bundled_extension_path;
pub use error::{Error, ErrorKind};
pub use init::{begin_alter, cleanup, commit_alter, init, terminate, version};
pub use network::{
network_check_changes, network_cleanup, network_has_unsent_changes, network_init,
network_logout, network_reset_sync_version, network_send_changes, network_set_apikey,
Expand Down
Loading
Loading