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.
Sourcepub fn upsert(&self, row: &T, upsert: Upsert) -> Result<Option<T>>
pub fn upsert(&self, row: &T, upsert: Upsert) -> Result<Option<T>>
Inserts a row, or, when its key is taken, sets the columns update
names on the row that holds it, if that row meets the upsert’s
condition; None when the condition kept it.
Sourcepub fn upsert_all(&self, rows: &[T], upsert: Upsert) -> Result<Vec<T>>
pub fn upsert_all(&self, rows: &[T], upsert: Upsert) -> Result<Vec<T>>
Upserts rows of one shape as one statement, all of them or none, each
as Table::upsert does one. It gives back the rows it wrote as the
file keeps them, without a row whose key is held by a row the
condition kept; SQLite refuses a list that holds one key twice.
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.
pub fn all(&self) -> Result<Vec<T>>
pub fn one(&self) -> Result<Option<T>>
pub fn scalar<V: DeserializeOwned>(&self) -> Result<V>
Sourcepub fn each(&self) -> Result<Each<T>>
pub fn each(&self) -> Result<Each<T>>
The rows one at a time, read a part at a time from one snapshot, so
that memory holds a part and not the rows: for more than fits in
memory. A snapshot lasts five seconds at most. Ordered by columns by
their names, a unique one last, as list is, the walk reads on past
that in a snapshot of its own, after the last row it gave, which sees
the rows as they are then; any other walk fails limit there. Inside
a transaction it gives the rows all reads.
for order in db.table::<Order>("orders").order_by("id", Direction::Asc).each()? {
let order = order?;
}