pub struct Conditions { /* private fields */ }
Expand description
Represents a set of conditions to be used in an execution.
§Example
use safety_postgres::access::conditions::ComparisonOperator::{Equal, Lower};
use safety_postgres::access::conditions::Conditions;
use safety_postgres::access::conditions::IsInJoinedTable::{No, Yes};
use safety_postgres::access::conditions::LogicalOperator::{And, FirstCondition};
let mut conditions = Conditions::new();
conditions.add_condition("column1", "condition1", Equal, FirstCondition, No).expect("add condition failed");
conditions.add_condition("column2", "condition2", Lower, And, Yes{
schema_name: "schema_name".to_string(), table_name: "table_name".to_string()
}).expect("add condition failed");
assert_eq!(
conditions.get_condition_text(),
"column1 = condition1 AND schema_name.table_name.column2 < condition2")
And you can specify the condition more intuitive using
Conditions.add_condition_from_str(column, value, condition_operator, condition_chain_operator, is_joined_table_condition)
method.
use safety_postgres::access::conditions::{Conditions, IsInJoinedTable};
let mut conditions = Conditions::new();
conditions.add_condition_from_str(
"column1",
"condition1",
"eq",
"", IsInJoinedTable::No).expect("add failed");
conditions.add_condition_from_str(
"column2",
"condition2",
">=",
"or",
IsInJoinedTable::Yes{
schema_name: "schema_name".to_string(), table_name: "table_name".to_string()
}).expect("add failed");
assert_eq!(
conditions.get_condition_text(),
"column1 = condition1 OR schema_name.table_name.column2 >= condition2")
Implementations§
Source§impl Conditions
impl Conditions
Sourcepub fn add_condition_from_str(
&mut self,
column: &str,
value: &str,
comparison_operator: &str,
condition_chain_operator: &str,
is_joined_table_condition: IsInJoinedTable,
) -> Result<&mut Self, ConditionError>
pub fn add_condition_from_str( &mut self, column: &str, value: &str, comparison_operator: &str, condition_chain_operator: &str, is_joined_table_condition: IsInJoinedTable, ) -> Result<&mut Self, ConditionError>
Adds a condition based on the input string parameters.
§Arguments
column
- The name of the column to compare.value
- The value to compare against.comparison_operator
- The operator to use for the comparison.- Available operator:
- Equal: “=”, “equal”, “eq”
- Greater: “>”, “greater”, “gt”
- GreaterEqual: “>=”, “greater_equal”, “ge”, “greater_eq”
- Lower: “<”, “lower”, “lt”
- LowerEqual: “<=”, “lower_equal”, “le”, “lower_eq”
- Available operator:
condition_chain_operator
- The operator to use for chaining multiple conditions.- Available operator:
- FirstCondition(there is no previous condition): “”, “first”, “none”
- And: “and”, “&”
- Or: “or”, “|”
- Available operator:
is_joined_table_condition
- Whether the condition is for a joined table.
§Errors
Returns a ConditionError
if there’s an error in the input parameters.
§Examples
use safety_postgres::access::conditions::Conditions;
use safety_postgres::access::conditions::IsInJoinedTable::No;
let mut conditions = Conditions::new();
conditions
.add_condition_from_str("name", "John", "=", "first", No)
.expect("adding condition failed");
conditions
.add_condition_from_str("age", "40", "le", "or", No)
.expect("adding condition failed");
assert_eq!(conditions.get_condition_text(), "name = John OR age <= 40")
Sourcepub fn add_condition(
&mut self,
column: &str,
value: &str,
comparison: ComparisonOperator,
condition_chain: LogicalOperator,
is_joined_table_condition: IsInJoinedTable,
) -> Result<&mut Self, ConditionError>
pub fn add_condition( &mut self, column: &str, value: &str, comparison: ComparisonOperator, condition_chain: LogicalOperator, is_joined_table_condition: IsInJoinedTable, ) -> Result<&mut Self, ConditionError>
Adds a condition to the query builder.
§Arguments
column
- The column name to which the condition is applied.value
- The value for comparison.comparison
- The operator used for comparison.condition_chain
- The logical operator used to chain the conditions.is_joined_table_condition
- Indicates whether the condition is for a joined table or not.
§Returns
A mutable reference to Self (Conditions)
if the condition is successfully added, otherwise a ConditionError
.
§Examples
use safety_postgres::access::conditions::Conditions;
use safety_postgres::access::conditions::{ComparisonOperator, LogicalOperator, IsInJoinedTable};
let mut conditions = Conditions::new();
let _ = conditions.add_condition(
"name",
"John",
ComparisonOperator::Equal,
LogicalOperator::FirstCondition,
IsInJoinedTable::No).expect("add condition failed")
.add_condition(
"age",
"40",
ComparisonOperator::LowerEq,
LogicalOperator::Or,
IsInJoinedTable::No).expect("add condition failed");
assert_eq!(conditions.get_condition_text(), "name = John OR age <= 40");
Sourcepub fn get_condition_text(&self) -> String
pub fn get_condition_text(&self) -> String
Returns the condition text generated by the conditions you set.
§Returns
The set condition as a String
.
Trait Implementations§
Source§impl Clone for Conditions
impl Clone for Conditions
Source§fn clone(&self) -> Conditions
fn clone(&self) -> Conditions
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreAuto Trait Implementations§
impl Freeze for Conditions
impl RefUnwindSafe for Conditions
impl Send for Conditions
impl Sync for Conditions
impl Unpin for Conditions
impl UnwindSafe for Conditions
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more