Struct rustorm::query::Query [] [src]

pub struct Query {
    pub sql_type: SqlType,
    pub distinct: bool,
    pub enumerate_all: bool,
    pub declared_query: BTreeMap<String, Query>,
    pub enumerated_fields: Vec<Field>,
    pub distinct_on_columns: Vec<String>,
    pub filters: Vec<Filter>,
    pub joins: Vec<Join>,
    pub order_by: Vec<(String, Direction)>,
    pub group_by: Vec<Operand>,
    pub having: Vec<Condition>,
    pub excluded_columns: Vec<ColumnName>,
    pub page: Option<usize>,
    pub page_size: Option<usize>,
    pub from: Option<Box<Field>>,
    pub values: Vec<Operand>,
    pub enumerated_returns: Vec<Field>,
}

Fields

sql_type

sql type determine which type of query to form, some fields are not applicable to other types of query

distinct

whether to select the records distinct

enumerate_all

whether to enumate all columns in involved models

declared_query
enumerated_fields

fields can be functions, column sql query, and even columns TODO; merge enumerated column to this, add a builder for fields

distinct_on_columns

specify to use distinct ON set of columns

filters

filter records, ~ where statement of the query

joins

joining multiple tables

order_by

ordering of the records via the columns specified TODO: ordering should be more flexible than this needs to support expressions

group_by

grouping columns to create an aggregate

having

having field

excluded_columns

exclude the mention of the columns in the SQL query, useful when ignoring changes in update/insert records

page

paging of records

page_size

size of a page

from

where the focus of values of column selection this is the table to insert to, update to delete, create, drop whe used in select, this is the pub from_table:Option, from field, where field can be a query, table, column, or function

values

The data values, used in bulk inserting, updating,

enumerated_returns

the returning clause of the query when supported,

Methods

impl Query

fn new() -> Self

the default query is select

fn select() -> Self

fn insert() -> Self

fn update() -> Self

fn delete() -> Self

fn distinct(&mut self) -> &mut Self

add DISTINCT ie: SELECT DISTINCT

fn select_all() -> Self

fn enumerate_all() -> Self

fn all(&mut self) -> &mut Self

fn column(&mut self, column: &str) -> &mut Self

all enumerated columns shall be called from this any conflict of columns from some other table will be automatically renamed columns that are not conflicts from some other table, but is the other conflicting column is not explicityly enumerated will not be renamed

fn columns(&mut self, columns: Vec<&str>) -> &mut Self

fn group_by(&mut self, columns: Vec<&str>) -> &mut Self

fn having(&mut self, column: &str, equality: Equality, value: &ToValue) -> &mut Self

fn exclude_column(&mut self, column: &str) -> &mut Self

exclude columns when inserting/updating data also ignores the column when selecting records useful for manipulating thin records by excluding huge binary blobs such as images

fn exclude_columns(&mut self, columns: Vec<&str>) -> &mut Self

fn distinct_on_columns(&mut self, columns: &Vec<String>) -> &mut Self

fn set_page(&mut self, page: usize) -> &mut Self

when paging multiple records

fn set_page_size(&mut self, items: usize) -> &mut Self

the number of items retrieve per page

fn limit(&mut self, limit: usize) -> &mut Self

the number of items retrieve per page

fn from(&mut self, table: &ToTableName) -> &mut Self

A more terse way to write the query

fn only_from(&mut self, table: &ToTableName) -> &mut Self

enumerate only the columns that is coming from this table this will invalidate enumerate_all

fn from_table(&mut self, table: &str) -> &mut Self

fn into_(&mut self, table: &ToTableName) -> &mut Self

into is used in rust, os settled with into_

fn into_table(&mut self, table: &str) -> &mut Self

can not use into since it's rust .into built-in (owned)

fn table(&mut self, table: &ToTableName) -> &mut Self

can be used in behalf of into_, from,

fn declare_query(&mut self, query: Query, alias: &str) -> &mut Self

if the database support CTE declareted query i.e WITH, then this query will be declared if database doesn't support WITH queries, then this query will be wrapped in the from_query build a builder for this

fn from_query(&mut self, query: Query, alias: &str) -> &mut Self

a query to query from use WITH (query) t1 SELECT from t1 declaration in postgresql, sqlite use SELECT FROM (query) in oracle, mysql, others alias of the table

fn from_field(&mut self, field: Field) -> &mut Self

fn get_from_table(&self) -> Option<&TableName>

fn join(&mut self, join: Join) -> &mut Self

join a table on this query

fn left_join_table(&mut self, table: &str, column1: &str, column2: &str) -> &mut Self

join a table on this query

fn left_join(&mut self, table: &ToTableName, column1: &str, column2: &str) -> &mut Self

fn right_join_table(&mut self, table: &str, column1: &str, column2: &str) -> &mut Self

fn right_join(&mut self, table: &ToTableName, column1: &str, column2: &str) -> &mut Self

fn full_join_table(&mut self, table: &str, column1: &str, column2: &str) -> &mut Self

fn full_join(&mut self, table: &ToTableName, column1: &str, column2: &str) -> &mut Self

fn inner_join_table(&mut self, table: &str, column1: &str, column2: &str) -> &mut Self

fn inner_join(&mut self, table: &ToTableName, column1: &str, column2: &str) -> &mut Self

fn asc(&mut self, column: &str) -> &mut Self

ascending orderby of this column

fn desc(&mut self, column: &str) -> &mut Self

ascending orderby of this column

fn get_involved_tables(&self) -> Vec<TableName>

fn finalize(&mut self) -> &Self

preprocess the missing fields of the query, such as mentioning the columns of the from_table enumerate the columns of the involved tables skipping those which are explicitly ignored the query will then be built and ready to be executed TODO: renamed conflicting enumerated columns if no enumerated fields and no excluded columns do a select all

fn get_renamed_columns(&self) -> Vec<(ColumnName, String)>

return the list of renamed columns, used in dao conversion to struc types

fn get_enumerated_columns(&self) -> Vec<&ColumnName>

return the list of enumerated columns will be used for updating records

fn add_filter(&mut self, filter: Filter) -> &mut Self

fn add_filters(&mut self, filters: Vec<Filter>) -> &mut Self

fn filter(&mut self, column: &str, equality: Equality, value: &ToValue) -> &mut Self

fn filter_eq(&mut self, column: &str, value: &ToValue) -> &mut Self

column = value

fn filter_lt(&mut self, column: &str, value: &ToValue) -> &mut Self

column < value

fn filter_lte(&mut self, column: &str, value: &ToValue) -> &mut Self

column <= value

fn filter_gt(&mut self, column: &str, value: &ToValue) -> &mut Self

column > value

fn filter_gte(&mut self, column: &str, value: &ToValue) -> &mut Self

column <= value

fn add_value(&mut self, value: Operand) -> &mut Self

fn value(&mut self, value: &ToValue) -> &mut Self

fn set(&mut self, column: &str, value: &ToValue) -> &mut Self

set a value of a column when inserting/updating records

fn return_all(&mut self) -> &mut Self

fn returns(&mut self, columns: Vec<&str>) -> &mut Self

fn enumerate_column_as_return(&mut self, column: &str) -> &mut Self

fn build(&mut self, db: &Database) -> SqlFrag

build the query only, not executed, useful when debugging

fn retrieve(&mut self, db: &Database) -> Result<DaoResult, DbError>

expects a return, such as select, insert/update with returning clause

fn retrieve_one(&mut self, db: &Database) -> Result<Dao, DbError>

expects a return, such as select, insert/update with returning clause no casting of data to structs is done This is used when retrieving multiple models in 1 query, then casting the records to its equivalent structs

fn execute(&mut self, db: &Database) -> Result<usize, DbError>

delete, update without caring for the return

fn collect<T: IsDao + IsTable>(&mut self, db: &Database) -> Result<Vec<T>, DbError>

execute the query, then convert the result

fn collect_one<T: IsDao + IsTable>(&mut self, db: &Database) -> Result<T, DbError>

execute the query then collect only 1 record TODO: use Result instead of Option

Trait Implementations

Derived Implementations

impl Debug for Query

fn fmt(&self, __arg_0: &mut Formatter) -> Result

impl Clone for Query

fn clone(&self) -> Query

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