Skip to content

Commit c498b47

Browse files
committed
Format code, rename JSON ops, simplify transaction
Apply broad formatting and minor refactors across the codebase: reflow long lines, normalize imports, and add small clippy annotations and PhantomData uses to silence warnings. Rename JSON string operator variants (KeyExists/KeyNotExists/PathExists/PathNotExists -> KeyPresent/KeyAbsent/PathPresent/PathAbsent) and adjust related condition building. Simplify the transaction helper by removing the unused model generic parameter. Update various query/sql builder and cache/DB call sites for clearer formatting and consistent locking/read access. Update tests to match formatting and a few expectation adjustments. These changes are behavioral-preserving and primarily target readability, lint fixes, and API simplification.
1 parent 5f38b42 commit c498b47

47 files changed

Lines changed: 547 additions & 340 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tideorm"
3-
version = "0.8.2"
3+
version = "0.8.3"
44
edition = "2024"
55
authors = ["Mohamad Al Zohbie <alzoubi528@gmail.com>"]
66
description = "A developer-friendly ORM for Rust with clean, expressive syntax"

benches/cache_benchmarks.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -473,19 +473,18 @@ fn benchmark_end_to_end_query_cache_paths(c: &mut Criterion) {
473473
rt.block_on(async {
474474
let conn = db.__internal_connection();
475475

476-
conn
477-
.execute_unprepared(
478-
r#"
476+
conn.execute_unprepared(
477+
r#"
479478
CREATE TABLE bench_cache_users (
480479
id INTEGER PRIMARY KEY AUTOINCREMENT,
481480
email TEXT NOT NULL,
482481
name TEXT NOT NULL,
483482
active INTEGER NOT NULL DEFAULT 1
484483
)
485484
"#,
486-
)
487-
.await
488-
.expect("failed to create benchmark table");
485+
)
486+
.await
487+
.expect("failed to create benchmark table");
489488

490489
for i in 0..100 {
491490
BenchCacheUser {
@@ -565,23 +564,21 @@ fn benchmark_uncached_query_concurrency(c: &mut Criterion) {
565564
rt.block_on(async {
566565
let conn = db.__internal_connection();
567566

568-
conn
569-
.execute_unprepared("DROP TABLE IF EXISTS bench_cache_users")
567+
conn.execute_unprepared("DROP TABLE IF EXISTS bench_cache_users")
570568
.await
571569
.expect("failed to drop benchmark table");
572-
conn
573-
.execute_unprepared(
574-
r#"
570+
conn.execute_unprepared(
571+
r#"
575572
CREATE TABLE bench_cache_users (
576573
id INTEGER PRIMARY KEY AUTOINCREMENT,
577574
email TEXT NOT NULL,
578575
name TEXT NOT NULL,
579576
active INTEGER NOT NULL DEFAULT 1
580577
)
581578
"#,
582-
)
583-
.await
584-
.expect("failed to create benchmark table");
579+
)
580+
.await
581+
.expect("failed to create benchmark table");
585582

586583
for i in 0..200 {
587584
BenchCacheUser {

src/cache.rs

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ use parking_lot::RwLock;
7676
use serde::{Deserialize, Serialize};
7777
use std::collections::HashMap;
7878
use std::hash::{Hash, Hasher};
79-
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering};
8079
use std::sync::OnceLock;
80+
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering};
8181
use std::time::{Duration, Instant};
8282

8383
use crate::error::{Error, Result};
@@ -383,12 +383,7 @@ impl QueryCache {
383383

384384
/// Generate a cache key from a query
385385
pub fn generate_key(&self, table: &str, query_hash: u64) -> String {
386-
let prefix = self
387-
.config
388-
.read()
389-
.key_prefix
390-
.clone()
391-
.unwrap_or_default();
386+
let prefix = self.config.read().key_prefix.clone().unwrap_or_default();
392387

393388
if prefix.is_empty() {
394389
format!("{}:{}", table, query_hash)
@@ -439,9 +434,7 @@ impl QueryCache {
439434
return Ok(());
440435
}
441436

442-
let config = self
443-
.config
444-
.read();
437+
let config = self.config.read();
445438

446439
let ttl = ttl.unwrap_or(config.default_ttl);
447440
let max_entries = config.max_entries;
@@ -453,10 +446,7 @@ impl QueryCache {
453446
// Check if we should cache empty results
454447
if let serde_json::Value::Array(arr) = &data {
455448
if arr.is_empty() {
456-
let should_cache = self
457-
.config
458-
.read()
459-
.cache_empty_results;
449+
let should_cache = self.config.read().cache_empty_results;
460450
if !should_cache {
461451
return Ok(());
462452
}
@@ -466,9 +456,7 @@ impl QueryCache {
466456
let entry_size = data.to_string().len();
467457
let entry = CacheEntry::new(data, entry_size, ttl, model_name);
468458

469-
let mut cache = self
470-
.cache
471-
.write();
459+
let mut cache = self.cache.write();
472460

473461
// Evict if necessary
474462
while cache.len() >= max_entries {
@@ -589,10 +577,7 @@ impl QueryCache {
589577

590578
/// Evict one entry based on the configured strategy
591579
fn evict_one(&self, cache: &mut HashMap<String, CacheEntry>) {
592-
let strategy = self
593-
.config
594-
.read()
595-
.strategy;
580+
let strategy = self.config.read().strategy;
596581

597582
let key_to_remove = match strategy {
598583
CacheStrategy::LRU => cache
@@ -849,10 +834,7 @@ impl PreparedStatementCache {
849834
}
850835

851836
let hash = Self::hash_sql(sql);
852-
let max_age = self
853-
.config
854-
.read()
855-
.max_age;
837+
let max_age = self.config.read().max_age;
856838

857839
// Fast path: read-only cache hit without taking the write lock.
858840
{

src/database.rs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@ impl Database {
189189

190190
fn from_internal_connection(inner: InternalConnection) -> Self {
191191
Self {
192-
inner: Arc::new(RwLock::new(Some(DatabaseHandle::Connection(Arc::new(inner))))),
192+
inner: Arc::new(RwLock::new(Some(DatabaseHandle::Connection(Arc::new(
193+
inner,
194+
))))),
193195
}
194196
}
195197

@@ -445,8 +447,7 @@ impl Database {
445447
use crate::internal::ConnectionTrait;
446448

447449
let conn = self.__internal_connection()?;
448-
conn
449-
.execute_unprepared("SELECT 1")
450+
conn.execute_unprepared("SELECT 1")
450451
.await
451452
.map(|_| true)
452453
.map_err(|e| Error::connection(e.to_string()))
@@ -509,10 +510,12 @@ impl Database {
509510
let stmt = Statement::from_string(backend, sql.to_string());
510511

511512
let results = match db.__get_connection()? {
512-
ConnectionRef::Database(conn) => crate::profiling::__profile_future(conn.query_all_raw(stmt))
513-
.await,
514-
ConnectionRef::Transaction(tx) => crate::profiling::__profile_future(tx.as_ref().query_all_raw(stmt))
515-
.await,
513+
ConnectionRef::Database(conn) => {
514+
crate::profiling::__profile_future(conn.query_all_raw(stmt)).await
515+
}
516+
ConnectionRef::Transaction(tx) => {
517+
crate::profiling::__profile_future(tx.as_ref().query_all_raw(stmt)).await
518+
}
516519
}
517520
.map_err(|e| Error::query(e.to_string()))?;
518521

@@ -564,7 +567,8 @@ impl Database {
564567
crate::profiling::__profile_future(conn.query_all_raw(stmt)).await
565568
}
566569
ConnectionRef::Transaction(tx) => {
567-
let stmt = Statement::from_sql_and_values(tx.as_ref().get_database_backend(), sql, params);
570+
let stmt =
571+
Statement::from_sql_and_values(tx.as_ref().get_database_backend(), sql, params);
568572
crate::profiling::__profile_future(tx.as_ref().query_all_raw(stmt)).await
569573
}
570574
}
@@ -640,7 +644,8 @@ impl Database {
640644
crate::profiling::__profile_future(conn.execute_raw(stmt)).await
641645
}
642646
ConnectionRef::Transaction(tx) => {
643-
let stmt = Statement::from_sql_and_values(tx.as_ref().get_database_backend(), sql, params);
647+
let stmt =
648+
Statement::from_sql_and_values(tx.as_ref().get_database_backend(), sql, params);
644649
crate::profiling::__profile_future(tx.as_ref().execute_raw(stmt)).await
645650
}
646651
}
@@ -679,10 +684,12 @@ impl Database {
679684
let stmt = Statement::from_string(backend, sql.to_string());
680685

681686
let results = match db.__get_connection()? {
682-
ConnectionRef::Database(conn) => crate::profiling::__profile_future(conn.query_all_raw(stmt))
683-
.await,
684-
ConnectionRef::Transaction(tx) => crate::profiling::__profile_future(tx.as_ref().query_all_raw(stmt))
685-
.await,
687+
ConnectionRef::Database(conn) => {
688+
crate::profiling::__profile_future(conn.query_all_raw(stmt)).await
689+
}
690+
ConnectionRef::Transaction(tx) => {
691+
crate::profiling::__profile_future(tx.as_ref().query_all_raw(stmt)).await
692+
}
686693
}
687694
.map_err(|e| Error::query(e.to_string()))?;
688695

@@ -713,7 +720,8 @@ impl Database {
713720
crate::profiling::__profile_future(conn.query_all_raw(stmt)).await
714721
}
715722
ConnectionRef::Transaction(tx) => {
716-
let stmt = Statement::from_sql_and_values(tx.as_ref().get_database_backend(), sql, params);
723+
let stmt =
724+
Statement::from_sql_and_values(tx.as_ref().get_database_backend(), sql, params);
717725
crate::profiling::__profile_future(tx.as_ref().query_all_raw(stmt)).await
718726
}
719727
}
@@ -978,7 +986,9 @@ impl DatabaseBuilder {
978986
.await
979987
.map_err(|e| Error::connection(e.to_string()))?;
980988

981-
Ok(Database::from_internal_connection(InternalConnection { conn }))
989+
Ok(Database::from_internal_connection(InternalConnection {
990+
conn,
991+
}))
982992
}
983993
}
984994

@@ -1008,7 +1018,9 @@ pub enum ConnectionRef {
10081018
impl Connection for Database {
10091019
fn __get_connection(&self) -> Result<ConnectionRef> {
10101020
Ok(match self.current_handle()? {
1011-
DatabaseHandle::Connection(inner) => ConnectionRef::Database(inner.connection().clone()),
1021+
DatabaseHandle::Connection(inner) => {
1022+
ConnectionRef::Database(inner.connection().clone())
1023+
}
10121024
DatabaseHandle::Transaction(tx) => ConnectionRef::Transaction(tx),
10131025
})
10141026
}

src/internal/mod.rs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,7 @@ impl QueryExecutor {
170170
M: InternalModel + crate::model::Model,
171171
C: ConnectionTrait,
172172
{
173-
let results = M::Entity::find()
174-
.all(conn);
173+
let results = M::Entity::find().all(conn);
175174
let results = crate::profiling::__profile_future(results)
176175
.await
177176
.map_err(translate_error)
@@ -186,8 +185,7 @@ impl QueryExecutor {
186185
M: InternalModel + crate::model::Model,
187186
C: ConnectionTrait,
188187
{
189-
let result = M::Entity::find()
190-
.one(conn);
188+
let result = M::Entity::find().one(conn);
191189
let result = crate::profiling::__profile_future(result)
192190
.await
193191
.map_err(translate_error)
@@ -212,8 +210,7 @@ impl QueryExecutor {
212210
query_label = format!("last(order_by={} desc)", M::primary_key_name());
213211
}
214212

215-
let result = select
216-
.one(conn);
213+
let result = select.one(conn);
217214
let result = crate::profiling::__profile_future(result)
218215
.await
219216
.map_err(translate_error)
@@ -275,10 +272,7 @@ impl QueryExecutor {
275272
M: InternalModel + crate::model::Model,
276273
C: ConnectionTrait,
277274
{
278-
let results = M::Entity::find()
279-
.offset(offset)
280-
.limit(limit)
281-
.all(conn);
275+
let results = M::Entity::find().offset(offset).limit(limit).all(conn);
282276
let results = crate::profiling::__profile_future(results)
283277
.await
284278
.map_err(translate_error)
@@ -299,8 +293,7 @@ impl QueryExecutor {
299293
C: ConnectionTrait,
300294
{
301295
let active = model.into_active_model();
302-
let result = active
303-
.delete(conn);
296+
let result = active.delete(conn);
304297
let result = crate::profiling::__profile_future(result)
305298
.await
306299
.map_err(translate_error)
@@ -333,10 +326,11 @@ impl QueryExecutor {
333326
// For single model, use regular insert for simplicity
334327
if models.len() == 1 {
335328
let active = models.into_iter().next().unwrap().into_active_model();
336-
let result = crate::profiling::__profile_future(async move { active.insert(conn).await })
337-
.await
338-
.map_err(translate_error)
339-
.map_err(|err| err.with_context(error_context.clone()))?;
329+
let result =
330+
crate::profiling::__profile_future(async move { active.insert(conn).await })
331+
.await
332+
.map_err(translate_error)
333+
.map_err(|err| err.with_context(error_context.clone()))?;
340334
return Ok(vec![M::from_sea_model(result)]);
341335
}
342336

@@ -353,8 +347,7 @@ impl QueryExecutor {
353347
// Build batch insert using SeaORM's insert_many with RETURNING
354348
let active_models: Vec<_> = models.into_iter().map(|m| m.into_active_model()).collect();
355349

356-
let results = M::Entity::insert_many(active_models)
357-
.exec_with_returning(conn);
350+
let results = M::Entity::insert_many(active_models).exec_with_returning(conn);
358351
let results = crate::profiling::__profile_future(results)
359352
.await
360353
.map_err(translate_error)
@@ -367,10 +360,11 @@ impl QueryExecutor {
367360
let mut results = Vec::with_capacity(models.len());
368361
for model in models {
369362
let active = model.into_active_model();
370-
let result = crate::profiling::__profile_future(async move { active.insert(conn).await })
371-
.await
372-
.map_err(translate_error)
373-
.map_err(|err| err.with_context(error_context.clone()))?;
363+
let result =
364+
crate::profiling::__profile_future(async move { active.insert(conn).await })
365+
.await
366+
.map_err(translate_error)
367+
.map_err(|err| err.with_context(error_context.clone()))?;
374368
results.push(M::from_sea_model(result));
375369
}
376370
Ok(results)

src/model.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ pub trait Model:
336336
> + Send,
337337
T: Send,
338338
{
339-
crud::transaction::<Self, F, T>(f).await
339+
crud::transaction::<F, T>(f).await
340340
}
341341

342342
/// Get the first record

src/model/batch.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,12 @@ impl<M: Model> BatchUpdateBuilder<M> {
622622
let mut set_parts = Vec::with_capacity(self.updates.len());
623623

624624
for (column, value) in &self.updates {
625-
set_parts.push(Self::build_assignment_sql(column, value, db_type, &mut params)?);
625+
set_parts.push(Self::build_assignment_sql(
626+
column,
627+
value,
628+
db_type,
629+
&mut params,
630+
)?);
626631
}
627632

628633
Ok((set_parts, params))
@@ -740,4 +745,4 @@ impl<M: Model> Default for BatchUpdateBuilder<M> {
740745

741746
#[cfg(test)]
742747
#[path = "../testing/model_batch_tests.rs"]
743-
mod tests;
748+
mod tests;

src/model/builders.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,4 @@ impl<M: Model> OnConflictBuilder<M> {
8989
{
9090
M::__insert_with_conflict(model, self).await
9191
}
92-
}
92+
}

0 commit comments

Comments
 (0)