Struct DB

Source
pub struct DB { /* private fields */ }
Expand description

Pool of database connections for asynchronous work.

§Values

  • connections: Vec<Arc<Mutex<DB>>> - Vector of database connections;
  • semaphore: Arc<Semaphore> - Semaphore for finding free connection;
  • size: usize - Number of connected databases.

Implementations§

Source§

impl DB

Source

pub fn in_use(&self) -> bool

Is library uses database

Source

pub async fn query<'a>( &self, query: &str, params: &'a [&'a (dyn ToSql + Sync)], assoc: bool, ) -> Option<Vec<Data>>

Execute query to database synchronously

§Parmeters
  • query: &str - SQL query;
  • params: &[&(dyn ToSql + Sync)] - Array of params. (for PgSql)
  • params: &[&dyn ToSql] - Array of params. (for MsSql)
  • assoc: bool - Return columns as associate array if True or Vecor id False.
§Return
  • Option::None - When error query or diconnected;
  • Option::Some(Vec<Data::Map>) - Results, if assoc = true.
  • Option::Some(Vec<Data::Vec>) - Results, if assoc = false.
Source

pub async fn execute<'a>( &self, query: &str, params: &'a [&'a (dyn ToSql + Sync)], ) -> Option<()>

Execute query to database synchronously without results

§Parmeters
  • query: &str - SQL query;
  • params: &[&(dyn ToSql + Sync)] - Array of params. (for PgSql)
  • params: &[&dyn ToSql] - Array of params. (for MsSql)
§Return
  • Option::None - When error query or diconnected;
  • Option::Some(()) - Successed.
Source

pub async fn query_group<'a>( &self, query: &str, params: &'a [&'a (dyn ToSql + Sync)], assoc: bool, conds: &[&[impl StrOrI64OrUSize]], ) -> Option<Data>

Execute query to database and return a result,
and grouping tabular data according to specified conditions.

§Parmeters
  • query: &str - SQL query;
  • params: &[&(dyn ToSql + Sync)] - Array of params. (for PgSql)
  • params: &[&dyn ToSql] - Array of params. (for MsSql)
  • assoc: bool - Return columns as associate array if True or Vecor id False.
  • conds: Vec<Vec<&str>> - Grouping condition.

Grouping condition:

  • The number of elements in the first-level array corresponds to the hierarchy levels in the group.
  • The number of elements in the second-level array corresponds to the number of items in one hierarchy. The first element of the group (index=0) is considered unique.
  • &str - field names for Data::Vec<Data::Map<...>>.

The first value in the second-level array must be of type Data::I64.

For each group, a new field with the name sub (encoded using fnv1a_64) will be created, where child groups will be located.

If the data does not match the format Data::Vec<Data::Map<...>>, grouping will not occur, Option::None will be returned.
If the data does not match the tabular format, grouping will not occur, Option::None will be returned.

Fields that are not included in the group will be excluded.

§Return
  • Option::None - If the fields failed to group.
§if assoc = true
  • Some(Data::Map<cond[0][0], Data::Map<...>>) in hierarchical structure.

struct value=Data::Map ├── [value1 from column_name=cond[0][0]] => [value=Data::Map] : The unique value of the grouping field │ ├── [key=cond[0][0]] => [value1 from column_name=cond[0][0]] : The unique value of the grouping field │ ├── [key=cond[0][1]] => [value from column_name=cond[0][1]] │ │ ... │ ├── [key=cond[0][last]] => [value from column_name=cond[0][last]] │ └── [key="sub"] => [value=Data::Map] : (encoded using fnv1a_64) │ ├── [value1 from column_name=cond[1][0]] => [value=Data::Map] : The unique value of the grouping field │ │ ├── [cond[1][0]] => [value1 from column_name=cond[1][0]] : The unique value of the grouping field │ │ ├── [cond[1][1]] => [value from column_name=cond[1][1]] │ │ │ ... │ │ ├── [cond[0][last]] => [value from column_name=cond[1][last]] │ │ └── [key="sub"] => [value Data::Map] : (encoded using fnv1a_64) │ └── [value2 from column_name=cond[1][0]] => [value=Data::Map] : The unique value of the grouping field │ │ ... ├── [value2 from column_name=cond[0][0]] => [value=Data::Map] : The unique value of the grouping field │ ├── [key=cond[0][0]] => [value2 from column_name=cond[0][0]] : The unique value of the grouping field │ ├── [key=cond[0][1]] => [value from column_name=cond[0][1]] │ │ ... │ ├── [key=cond[0][last]] => [value from column_name=cond[0][last]] │ ├── [key="sub"] => [value Data::Map] : (encoded using fnv1a_64) ...

§if assoc = false
  • Some(Data::Map<cond[0][0], Data::Map<...>>) in hierarchical structure.

struct value=Data::Map ├── [value1 from column_name=cond[0][0]] => [value=Data::Vec] : The unique value of the grouping field │ ├── [0] => [value1 from column_name=cond[0][0]] : The unique value of the grouping field │ ├── [1] => [value from column_name=cond[0][1]] │ │ ... │ ├── [last] => [value from column_name=cond[0][last]] │ └── [last + 1] => [value=Data::Map] : (encoded using fnv1a_64) │ ├── [value1 from column_name=cond[1][0]] => [value=Data::Vec] : The unique value of the grouping field │ │ ├── [0] => [value1 from column_name=cond[1][0]] : The unique value of the grouping field │ │ ├── [1] => [value from column_name=cond[1][1]] │ │ │ ... │ │ ├── [last] => [value from column_name=cond[1][last]] │ │ └── [last+1] => [value Data::Map] : (encoded using fnv1a_64) │ └── [value2 from column_name=cond[1][0]] => [value=Data::Vec] : The unique value of the grouping field │ │ ... ├── [value2 from column_name=cond[0][0]] => [value=Data::Vec] : The unique value of the grouping field │ ├── [0] => [value2 from column_name=cond[0][0]] : The unique value of the grouping field │ ├── [1] => [value from column_name=cond[0][1]] │ │ ... │ ├── [last] => [value from column_name=cond[0][last]] │ ├── [last + 1] => [value Data::Map] : (encoded using fnv1a_64) ...

Trait Implementations§

Source§

impl Debug for DB

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for DB

§

impl !RefUnwindSafe for DB

§

impl Send for DB

§

impl Sync for DB

§

impl Unpin for DB

§

impl !UnwindSafe for DB

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> Chain<T> for T

Source§

fn len(&self) -> usize

The number of items that this chain link consists of.
Source§

fn append_to(self, v: &mut Vec<T>)

Append the elements in this link to the chain.
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, 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

Source§

impl<T> ErasedDestructor for T
where T: 'static,