Struct Conditions

Source
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

Source

pub fn new() -> Self

Creates a new empty Conditions instance.

Source

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”
  • 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”, “|”
  • 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")
Source

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");
Source

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

Source§

fn clone(&self) -> Conditions

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V