Skip to content

Commit

Permalink
feat: update query dsl condition
Browse files Browse the repository at this point in the history
  • Loading branch information
didy-kpn committed Mar 20, 2024
1 parent fb8655b commit 7fd51d1
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions src/model/query/dsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,68 @@ impl ConditionBuilder {
with(self.condition.add(col.is_not_null()))
}

/// where condition the given column is in
/// value
///
/// # Examples
/// ```
/// use loco_rs::tests_cfg::db::*;
/// use sea_orm::{EntityTrait, QueryFilter, QuerySelect, QueryTrait};
/// use loco_rs::prelude::*;
///
/// let query_str = test_db::Entity::find()
/// .select_only()
/// .column(test_db::Column::Id)
/// .filter(model::query::dsl::condition().is_in(test_db::Column::Id, [1]).build())
/// .build(sea_orm::DatabaseBackend::Postgres)
/// .to_string();
///
/// assert_eq!(
/// query_str,
/// "SELECT \"loco\".\"id\" FROM \"loco\" WHERE \"loco\".\"id\" IN (1)"
/// );
/// ````
#[must_use]
#[allow(clippy::wrong_self_convention)]
pub fn is_in<T: ColumnTrait, V: Into<Value>, I: IntoIterator<Item = V>>(
self,
col: T,
values: I,
) -> Self {
with(self.condition.add(col.is_in(values)))
}

/// where condition the given column is not in
/// value
///
/// # Examples
/// ```
/// use loco_rs::tests_cfg::db::*;
/// use sea_orm::{EntityTrait, QueryFilter, QuerySelect, QueryTrait};
/// use loco_rs::prelude::*;
///
/// let query_str = test_db::Entity::find()
/// .select_only()
/// .column(test_db::Column::Id)
/// .filter(model::query::dsl::condition().is_not_in(test_db::Column::Id, [1]).build())
/// .build(sea_orm::DatabaseBackend::Postgres)
/// .to_string();
///
/// assert_eq!(
/// query_str,
/// "SELECT \"loco\".\"id\" FROM \"loco\" WHERE \"loco\".\"id\" NOT IN (1)"
/// );
/// ````
#[must_use]
#[allow(clippy::wrong_self_convention)]
pub fn is_not_in<T: ColumnTrait, V: Into<Value>, I: IntoIterator<Item = V>>(
self,
col: T,
values: I,
) -> Self {
with(self.condition.add(col.is_not_in(values)))
}

/// where condition the given column is not null
/// value
///
Expand Down Expand Up @@ -747,4 +809,34 @@ mod tests {
"SELECT \"loco\".\"id\" FROM \"loco\" WHERE \"loco\".\"name\" IS NOT NULL"
);
}

#[test]
fn condition_is_in() {
let query_str = test_db::Entity::find()
.select_only()
.column(test_db::Column::Id)
.filter(condition().is_in(test_db::Column::Id, [1]).build())
.build(sea_orm::DatabaseBackend::Postgres)
.to_string();

assert_eq!(
query_str,
"SELECT \"loco\".\"id\" FROM \"loco\" WHERE \"loco\".\"id\" IN (1)"
);
}

#[test]
fn condition_is_not_in() {
let query_str = test_db::Entity::find()
.select_only()
.column(test_db::Column::Id)
.filter(condition().is_not_in(test_db::Column::Id, [1]).build())
.build(sea_orm::DatabaseBackend::Postgres)
.to_string();

assert_eq!(
query_str,
"SELECT \"loco\".\"id\" FROM \"loco\" WHERE \"loco\".\"id\" NOT IN (1)"
);
}
}

0 comments on commit 7fd51d1

Please sign in to comment.