Skip to content

Commit 15d2c7c

Browse files
authored
fix: Add option for SQLite for perform decimal_cmp instead of BETWEEN (#333)
* fix: Add option for SQLite for perform instead of BETWEEN * docs: Add comment * docs: fix docs clippy
1 parent 1713e9a commit 15d2c7c

4 files changed

Lines changed: 618 additions & 29 deletions

File tree

src/sqlite.rs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ pub mod federation;
4949
#[cfg(feature = "sqlite-federation")]
5050
pub mod sqlite_interval;
5151

52+
#[cfg(feature = "sqlite-federation")]
53+
pub mod between;
54+
5255
pub mod sql_table;
5356
pub mod write;
5457

@@ -119,6 +122,7 @@ type Result<T, E = Error> = std::result::Result<T, E>;
119122
#[derive(Debug)]
120123
pub struct SqliteTableProviderFactory {
121124
instances: Arc<Mutex<HashMap<DbInstanceKey, SqliteConnectionPool>>>,
125+
decimal_between: bool,
122126
}
123127

124128
const SQLITE_DB_PATH_PARAM: &str = "file";
@@ -131,9 +135,16 @@ impl SqliteTableProviderFactory {
131135
pub fn new() -> Self {
132136
Self {
133137
instances: Arc::new(Mutex::new(HashMap::new())),
138+
decimal_between: false,
134139
}
135140
}
136141

142+
#[must_use]
143+
pub fn with_decimal_between(mut self, decimal_between: bool) -> Self {
144+
self.decimal_between = decimal_between;
145+
self
146+
}
147+
137148
#[must_use]
138149
pub fn attach_databases(&self, options: &HashMap<String, String>) -> Option<Vec<Arc<str>>> {
139150
options.get(SQLITE_ATTACH_DATABASES_PARAM).map(|databases| {
@@ -353,11 +364,10 @@ impl TableProviderFactory for SqliteTableProviderFactory {
353364

354365
let dyn_pool: Arc<DynSqliteConnectionPool> = read_pool;
355366

356-
let read_provider = Arc::new(SQLiteTable::new_with_schema(
357-
&dyn_pool,
358-
Arc::clone(&schema),
359-
name,
360-
));
367+
let read_provider = Arc::new(
368+
SQLiteTable::new_with_schema(&dyn_pool, Arc::clone(&schema), name)
369+
.with_decimal_between(self.decimal_between),
370+
);
361371

362372
let sqlite = Arc::into_inner(sqlite)
363373
.context(DanglingReferenceToSqliteSnafu)
@@ -377,12 +387,22 @@ impl TableProviderFactory for SqliteTableProviderFactory {
377387

378388
pub struct SqliteTableFactory {
379389
pool: Arc<SqliteConnectionPool>,
390+
decimal_between: bool,
380391
}
381392

382393
impl SqliteTableFactory {
383394
#[must_use]
384395
pub fn new(pool: Arc<SqliteConnectionPool>) -> Self {
385-
Self { pool }
396+
Self {
397+
pool,
398+
decimal_between: false,
399+
}
400+
}
401+
402+
#[must_use]
403+
pub fn with_decimal_between(mut self, decimal_between: bool) -> Self {
404+
self.decimal_between = decimal_between;
405+
self
386406
}
387407

388408
pub async fn table_provider(
@@ -398,11 +418,10 @@ impl SqliteTableFactory {
398418

399419
let dyn_pool: Arc<DynSqliteConnectionPool> = pool;
400420

401-
let read_provider = Arc::new(SQLiteTable::new_with_schema(
402-
&dyn_pool,
403-
Arc::clone(&schema),
404-
table_reference,
405-
));
421+
let read_provider = Arc::new(
422+
SQLiteTable::new_with_schema(&dyn_pool, Arc::clone(&schema), table_reference)
423+
.with_decimal_between(self.decimal_between),
424+
);
406425

407426
Ok(read_provider)
408427
}
@@ -473,12 +492,12 @@ impl Sqlite {
473492

474493
async fn table_exists(&self, sqlite_conn: &mut SqliteConnection) -> bool {
475494
let sql = format!(
476-
r#"SELECT EXISTS (
495+
"SELECT EXISTS (
477496
SELECT 1
478497
FROM sqlite_master
479498
WHERE type='table'
480499
AND name = '{name}'
481-
)"#,
500+
)",
482501
name = self.table
483502
);
484503
tracing::trace!("{sql}");
@@ -516,7 +535,7 @@ impl Sqlite {
516535

517536
fn delete_all_table_data(&self, transaction: &Transaction<'_>) -> rusqlite::Result<()> {
518537
transaction.execute(
519-
format!(r#"DELETE FROM {}"#, self.table.to_quoted_string()).as_str(),
538+
format!("DELETE FROM {}", self.table.to_quoted_string()).as_str(),
520539
[],
521540
)?;
522541

0 commit comments

Comments
 (0)