pub struct Table<'r, T> { /* private fields */ }Expand description
A table: its queries, and its rows’ inserts and upserts. Derefs to the
query of all its rows.
Implementations§
Source§impl<T: Serialize + DeserializeOwned> Table<'_, T>
impl<T: Serialize + DeserializeOwned> Table<'_, T>
Sourcepub fn insert(&self, row: &T) -> Result<T>
pub fn insert(&self, row: &T) -> Result<T>
Inserts a row, every field it has, and gives it back as the file keeps it: the defaults and the generated columns the database filled.
Sourcepub fn insert_all(&self, rows: &[T]) -> Result<Vec<T>>
pub fn insert_all(&self, rows: &[T]) -> Result<Vec<T>>
Inserts rows of one shape as one write, all of them or none.
Methods from Deref<Target = Query<'r, T>>§
Sourcepub fn filter(&self, condition: impl Into<Sql>) -> Self
pub fn filter(&self, condition: impl Into<Sql>) -> Self
Adds a condition with and: a piece from sql!, sql::eq, sql::or
and the rest. filter, since where is Rust’s own word.
Sourcepub fn select<S>(&self, columns: impl Into<Sql>) -> Query<'r, S>
pub fn select<S>(&self, columns: impl Into<Sql>) -> Query<'r, S>
The columns a row holds, as SQL text, and the shape of the row they make.
Sourcepub fn include<C>(&self, name: &str, query: &Query<'_, C>) -> Self
pub fn include<C>(&self, name: &str, query: &Query<'_, C>) -> Self
Gives each row the rows of another query as a list under name, a
field of the row’s type, from the same statement: the query names the
row it belongs to in its filter, by this table’s alias, names its
columns with select, and has a limit, which counts for each row.
Every value comes back as it is kept.
let latest = db
.table::<Post>("posts as p")
.select::<Post>("p.id, p.title")
.filter("p.author_id = u.id")
.order_by("p.id", Direction::Desc)
.limit(3);
let authors: Vec<Author> = db.table::<Author>("users as u").include("posts", &latest).all()?;Sourcepub fn join(&self, table: &str, on: impl Into<Sql>) -> Self
pub fn join(&self, table: &str, on: impl Into<Sql>) -> Self
Joins a table, "users as u", on a condition.
Sourcepub fn left_join(&self, table: &str, on: impl Into<Sql>) -> Self
pub fn left_join(&self, table: &str, on: impl Into<Sql>) -> Self
Joins a table whose columns may be null where no row of it meets the condition.
Sourcepub fn group_by<I: IntoIterator<Item = S>, S: AsRef<str>>(
&self,
columns: I,
) -> Self
pub fn group_by<I: IntoIterator<Item = S>, S: AsRef<str>>( &self, columns: I, ) -> Self
Groups the rows by columns, quoted as names.
Sourcepub fn having(&self, condition: impl Into<Sql>) -> Self
pub fn having(&self, condition: impl Into<Sql>) -> Self
A condition on the groups, as filter takes one.
Sourcepub fn order_by(&self, column: &str, direction: Direction) -> Self
pub fn order_by(&self, column: &str, direction: Direction) -> Self
Orders the rows by a column, quoted as a name, so that a sort a request chooses cannot be SQL.
Sourcepub fn order_by_sql(&self, piece: impl Into<Sql>, direction: Direction) -> Self
pub fn order_by_sql(&self, piece: impl Into<Sql>, direction: Direction) -> Self
Orders the rows by a piece of SQL: sql!("lower(title)").
pub fn limit(&self, rows: u64) -> Self
pub fn offset(&self, rows: u64) -> Self
Sourcepub fn to_sql(&self) -> Result<Sql>
pub fn to_sql(&self) -> Result<Sql>
The text and the values the query sends, to read or to log.
Sourcepub fn count(&self) -> Result<u64>
pub fn count(&self) -> Result<u64>
How many rows the conditions match, or how many groups with
group_by, without the order and the limit.
Sourcepub fn update<I, S>(&self, sets: I) -> Result<Done>
pub fn update<I, S>(&self, sets: I) -> Result<Done>
Sets columns of the rows the conditions match, each a value from
sql::value or a piece of SQL, and says how many changed.
Sourcepub fn delete(&self) -> Result<Done>
pub fn delete(&self) -> Result<Done>
Deletes the rows the conditions match, and says how many it deleted.