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
impl DB
Sourcepub async fn query<'a>(
&self,
query: &str,
params: &'a [&'a (dyn ToSql + Sync)],
assoc: bool,
) -> Option<Vec<Data>>
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.
Sourcepub async fn execute<'a>(
&self,
query: &str,
params: &'a [&'a (dyn ToSql + Sync)],
) -> Option<()>
pub async fn execute<'a>( &self, query: &str, params: &'a [&'a (dyn ToSql + Sync)], ) -> Option<()>
Sourcepub async fn query_group<'a>(
&self,
query: &str,
params: &'a [&'a (dyn ToSql + Sync)],
assoc: bool,
conds: &[&[impl StrOrI64OrUSize]],
) -> Option<Data>
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) ...