use std::fmt;
use std::marker::PhantomData;
use std::time::Duration;
use futures_core::stream::BoxStream;
use sqlx::postgres::PgRow;
use sqlx::{Decode, FromRow, Postgres, Row, Type};
use crate::cursor::{Cursor, CursorPage};
use crate::exec;
use crate::expr::Direction;
use crate::expr::{Cond, IntoProjections, Projection};
use crate::model::{push_columns, push_returning};
use crate::sql::Sql;
use crate::{Column, Error, Executor, Expr, Model, Order, Page, Result, Value};
fn push_where<'a>(sql: &mut Sql, keyword: &str, filters: impl IntoIterator<Item = &'a Cond>) {
let mut filters = filters.into_iter().peekable();
if filters.peek().is_none() {
return;
}
sql.push(keyword);
sql.push_list(filters, " AND ", |sql, cond| cond.write(sql));
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Trashed {
Exclude,
Include,
Only,
}
impl Trashed {
fn cond<M: Model>(self) -> Option<Cond> {
let column = M::DELETED_AT_COLUMN?;
match self {
Trashed::Exclude => Some(Column::<M>::new(column).is_null().cond),
Trashed::Only => Some(Column::<M>::new(column).is_not_null().cond),
Trashed::Include => None,
}
}
}
fn implicit<M: Model>(trashed: Trashed, scoped: bool, all_tenants: bool) -> Vec<Cond> {
let default = scoped.then(M::default_scope).flatten().map(|e| e.cond);
tenant_cond::<M>(all_tenants)
.into_iter()
.chain(trashed.cond::<M>())
.chain(default)
.collect()
}
fn tenant_cond<M: Model>(all_tenants: bool) -> Option<Cond> {
let column = M::TENANT_COLUMN.filter(|_| !all_tenants)?;
Some(match crate::tenant::current() {
Some(tenant) => Column::<M>::new(column).eq(tenant).cond,
None => {
tracing::warn!(
target: "rok_db::tenant",
table = M::TABLE,
"query on a tenant model outside `with_tenant`; matching no rows (use `all_tenants()` to opt out)"
);
Expr::<M>::none().cond
}
})
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Lock {
Update,
Share,
}
pub struct Select<M> {
filters: Vec<Cond>,
order: Vec<Order<M>>,
group_by: Vec<&'static str>,
having: Vec<Cond>,
limit: Option<u64>,
offset: Option<u64>,
lock: Option<Lock>,
trashed: Trashed,
scoped: bool,
primary: bool,
all_tenants: bool,
_model: PhantomData<fn() -> M>,
}
impl<M: Model> Default for Select<M> {
fn default() -> Self {
Self::new()
}
}
impl<M: Model> Select<M> {
pub fn new() -> Self {
Self {
filters: Vec::new(),
order: Vec::new(),
group_by: Vec::new(),
having: Vec::new(),
limit: None,
offset: None,
lock: None,
trashed: Trashed::Exclude,
scoped: true,
primary: false,
all_tenants: false,
_model: PhantomData,
}
}
pub fn filter(mut self, expr: Expr<M>) -> Self {
self.filters.push(expr.cond);
self
}
pub fn all_tenants(mut self) -> Self {
self.all_tenants = true;
self
}
pub fn on_primary(mut self) -> Self {
self.primary = true;
self
}
pub(crate) fn use_replica(&self) -> bool {
!self.primary && self.lock.is_none()
}
pub fn scope(self, scope: impl FnOnce(Self) -> Self) -> Self {
scope(self)
}
pub fn unscoped(mut self) -> Self {
self.scoped = false;
self
}
pub fn with_trashed(mut self) -> Self {
self.trashed = Trashed::Include;
self
}
pub fn only_trashed(mut self) -> Self {
self.trashed = Trashed::Only;
self
}
pub fn filter_if(self, condition: bool, expr: impl FnOnce() -> Expr<M>) -> Self {
if condition { self.filter(expr()) } else { self }
}
pub fn filter_opt<T>(self, value: Option<T>, expr: impl FnOnce(T) -> Expr<M>) -> Self {
match value {
Some(v) => self.filter(expr(v)),
None => self,
}
}
pub fn order_by(mut self, order: impl Into<Order<M>>) -> Self {
self.order.push(order.into());
self
}
pub fn group_by(mut self, column: Column<M>) -> Self {
self.group_by.push(column.name());
self
}
pub fn having(mut self, expr: Expr<M>) -> Self {
self.having.push(expr.cond);
self
}
pub fn limit(mut self, limit: u64) -> Self {
self.limit = Some(limit);
self
}
pub fn offset(mut self, offset: u64) -> Self {
self.offset = Some(offset);
self
}
pub fn for_update(mut self) -> Self {
self.lock = Some(Lock::Update);
self
}
pub fn for_share(mut self) -> Self {
self.lock = Some(Lock::Share);
self
}
pub fn select(self, projections: impl IntoProjections<M>) -> Projected<M> {
Projected {
select: self,
items: projections.into_projections(),
}
}
pub fn memoize(self, ttl: Duration) -> Memoized<M> {
Memoized { select: self, ttl }
}
pub fn update(self) -> Update<M> {
Update {
filters: self.filters,
sets: Vec::new(),
trashed: self.trashed,
scoped: self.scoped,
all_tenants: self.all_tenants,
_model: PhantomData,
}
}
pub fn to_sql(&self) -> Sql {
let mut sql = Sql::new();
sql.push("SELECT ");
push_columns::<M>(&mut sql);
self.write_tail(&mut sql);
sql
}
fn write_tail(&self, sql: &mut Sql) {
self.write_from_where(sql);
if !self.group_by.is_empty() {
sql.push(" GROUP BY ")
.push_list(&self.group_by, ", ", |sql, c| {
sql.push_ident(c);
});
}
push_where(sql, " HAVING ", &self.having);
self.write_order(sql, "");
if let Some(limit) = self.limit {
sql.push(&format!(" LIMIT {limit}"));
}
if let Some(offset) = self.offset {
sql.push(&format!(" OFFSET {offset}"));
}
match self.lock {
Some(Lock::Update) => sql.push(" FOR UPDATE"),
Some(Lock::Share) => sql.push(" FOR SHARE"),
None => sql,
};
}
fn write_from_where(&self, sql: &mut Sql) {
sql.push(" FROM ").push_ident(M::TABLE);
let implicit = implicit::<M>(self.trashed, self.scoped, self.all_tenants);
push_where(sql, " WHERE ", self.filters.iter().chain(&implicit));
}
pub(crate) fn write_exists_body(&self, sql: &mut Sql) {
sql.push("SELECT 1");
self.write_tail(sql);
}
fn write_order(&self, sql: &mut Sql, prefix: &str) {
if !self.order.is_empty() {
sql.push(" ORDER BY ")
.push_list(self.order.iter().enumerate(), ", ", |sql, (i, o)| {
let alias = (!prefix.is_empty()).then(|| format!("__rok_o{i}"));
o.write_key(sql, prefix, alias.as_deref());
o.write_direction(sql);
});
}
}
fn count_sql(&self) -> Sql {
let mut sql = Sql::new();
sql.push("SELECT COUNT(*)");
self.write_from_where(&mut sql);
sql
}
fn exists_sql(&self) -> Sql {
let mut sql = Sql::new();
sql.push("SELECT EXISTS(SELECT 1");
self.write_from_where(&mut sql);
sql.push(")");
sql
}
fn aggregate_sql(&self, projection: Projection<M>) -> Sql {
let mut sql = Sql::new();
sql.push("SELECT ");
projection.write(&mut sql);
self.write_from_where(&mut sql);
sql
}
pub async fn all<'e, E: Executor<'e>>(self, executor: E) -> Result<Vec<M>> {
exec::fetch_all(executor, &self.to_sql(), &[], self.use_replica()).await
}
pub async fn first<'e, E: Executor<'e>>(self, executor: E) -> Result<Option<M>> {
let replica = self.use_replica();
exec::fetch_optional(executor, &self.limit(1).to_sql(), &[], replica).await
}
pub async fn one<'e, E: Executor<'e>>(self, executor: E) -> Result<M> {
self.first(executor)
.await?
.ok_or_else(|| Error::not_found::<M>(None))
}
pub fn stream<'e, E: Executor<'e> + 'e>(self, executor: E) -> BoxStream<'e, Result<M>> {
let replica = self.use_replica();
exec::stream(executor, self.to_sql(), replica)
}
pub async fn count<'e, E: Executor<'e>>(self, executor: E) -> Result<i64> {
exec::fetch_scalar(executor, &self.count_sql(), self.use_replica()).await
}
pub async fn exists<'e, E: Executor<'e>>(self, executor: E) -> Result<bool> {
exec::fetch_scalar(executor, &self.exists_sql(), self.use_replica()).await
}
pub async fn sum<'e, E: Executor<'e>>(
self,
executor: E,
column: Column<M>,
) -> Result<Option<i64>> {
let sql = self.aggregate_sql(column.sum().cast("BIGINT"));
exec::fetch_scalar(executor, &sql, self.use_replica()).await
}
pub async fn avg<'e, E: Executor<'e>>(
self,
executor: E,
column: Column<M>,
) -> Result<Option<f64>> {
let sql = self.aggregate_sql(column.avg().cast("DOUBLE PRECISION"));
exec::fetch_scalar(executor, &sql, self.use_replica()).await
}
pub async fn min<'e, T, E>(self, executor: E, column: Column<M>) -> Result<Option<T>>
where
T: Type<Postgres> + for<'r> Decode<'r, Postgres> + Send + Unpin,
E: Executor<'e>,
{
exec::fetch_scalar(
executor,
&self.aggregate_sql(column.min()),
self.use_replica(),
)
.await
}
pub async fn max<'e, T, E>(self, executor: E, column: Column<M>) -> Result<Option<T>>
where
T: Type<Postgres> + for<'r> Decode<'r, Postgres> + Send + Unpin,
E: Executor<'e>,
{
exec::fetch_scalar(
executor,
&self.aggregate_sql(column.max()),
self.use_replica(),
)
.await
}
pub async fn paginate<'e, E: Executor<'e>>(
self,
executor: E,
page: u64,
per_page: u64,
) -> Result<Page<M>> {
if per_page == 0 {
return Err(Error::InvalidQuery(
"`per_page` must be greater than 0".into(),
));
}
let page = page.max(1);
let rows = exec::fetch_rows(
executor,
&self.paginate_sql(page, per_page),
&[],
self.use_replica(),
)
.await?;
let mut total = 0;
let mut items = Vec::with_capacity(rows.len());
for row in &rows {
total = row.try_get::<i64, _>("__rok_total")?;
if row.try_get::<Option<bool>, _>("__rok_present")?.is_some() {
items.push(M::from_row(row)?);
}
}
Ok(Page::new(items, total.max(0) as u64, page, per_page))
}
fn paginate_sql(&self, page: u64, per_page: u64) -> Sql {
let mut sql = Sql::new();
sql.push(r#"SELECT c."__rok_total", p.* FROM (SELECT COUNT(*) AS "__rok_total""#);
self.write_from_where(&mut sql);
sql.push(r#") c LEFT JOIN LATERAL (SELECT TRUE AS "__rok_present", "#);
push_columns::<M>(&mut sql);
for (i, o) in self.order.iter().enumerate() {
if o.column().is_none() {
sql.push(", ");
o.write_key(&mut sql, "", None);
sql.push(" AS ").push_ident(&format!("__rok_o{i}"));
}
}
self.write_from_where(&mut sql);
self.write_order(&mut sql, "");
let offset = (page - 1).saturating_mul(per_page);
sql.push(&format!(" LIMIT {per_page} OFFSET {offset}) p ON TRUE"));
self.write_order(&mut sql, "p.");
sql
}
pub async fn delete<'e, E: Executor<'e>>(self, executor: E) -> Result<u64> {
exec::execute(executor, &self.delete_sql(), &[M::TABLE]).await
}
pub fn delete_sql(&self) -> Sql {
match M::DELETED_AT_COLUMN {
Some(deleted_at) => self
.clone()
.update()
.set_raw(Column::new(deleted_at), "now()", [] as [Value; 0])
.to_sql(),
None => self.force_delete_sql(),
}
}
pub async fn force_delete<'e, E: Executor<'e>>(self, executor: E) -> Result<u64> {
exec::execute(executor, &self.force_delete_sql(), &[M::TABLE]).await
}
pub(crate) fn force_delete_sql(&self) -> Sql {
let mut sql = Sql::new();
sql.push("DELETE");
self.write_from_where(&mut sql);
sql
}
pub async fn restore<'e, E: Executor<'e>>(self, executor: E) -> Result<u64> {
let Some(deleted_at) = M::DELETED_AT_COLUMN else {
return Err(Error::InvalidQuery(format!(
"`{}` has no soft-delete column",
M::TABLE
)));
};
self.only_trashed()
.update()
.set_raw(Column::new(deleted_at), "NULL", [] as [Value; 0])
.exec(executor)
.await
}
fn keyset_order(&self) -> Vec<(&'static str, Direction)> {
let mut order: Vec<_> = self
.order
.iter()
.filter_map(|o| o.column().map(|c| (c, o.direction)))
.collect();
if !order.iter().any(|(c, _)| *c == M::PRIMARY_KEY) {
order.push((M::PRIMARY_KEY, Direction::Asc));
}
order
}
pub async fn cursor_paginate<'e, E: Executor<'e>>(
self,
executor: E,
after: Option<&Cursor>,
limit: u64,
) -> Result<CursorPage<M>> {
if limit == 0 {
return Err(Error::InvalidQuery("`limit` must be greater than 0".into()));
}
let select = self.cursor_select(after, limit)?;
let order = select.keyset_order();
let mut items = select.all(executor).await?;
let next = if items.len() as u64 > limit {
items.truncate(limit as usize);
let last = items.last().expect("limit > 0");
let values = order
.iter()
.map(|(c, _)| {
last.value_of(c).ok_or_else(|| {
Error::InvalidQuery(format!("`{c}` is not a column of `{}`", M::TABLE))
})
})
.collect::<Result<Vec<_>>>()?;
Some(Cursor::new(values))
} else {
None
};
Ok(CursorPage { items, next })
}
fn cursor_select(self, after: Option<&Cursor>, limit: u64) -> Result<Self> {
if self.order.iter().any(|o| o.column().is_none()) {
return Err(Error::InvalidQuery(
"keyset pagination can only order by columns, not expressions".into(),
));
}
let order = self.keyset_order();
let mut select = self;
select.order = order
.iter()
.map(|(c, d)| match d {
Direction::Asc => Column::new(c).asc(),
Direction::Desc => Column::new(c).desc(),
})
.collect();
select.offset = None;
select.limit = Some(limit + 1);
if let Some(cursor) = after {
if cursor.values.len() != order.len() {
return Err(Error::InvalidCursor(format!(
"expected {} values, got {}",
order.len(),
cursor.values.len()
)));
}
let branches = (0..order.len()).map(|i| {
let eqs = (0..i).map(|j| Column::<M>::new(order[j].0).eq(cursor.values[j].clone()));
let (column, direction) = order[i];
let value = cursor.values[i].clone();
let step = match direction {
Direction::Asc => Column::<M>::new(column).gt(value),
Direction::Desc => Column::<M>::new(column).lt(value),
};
Expr::all_of(eqs.chain([step]))
});
select = select.filter(Expr::any_of(branches));
}
Ok(select)
}
}
impl<M> Clone for Select<M> {
fn clone(&self) -> Self {
Self {
filters: self.filters.clone(),
order: self.order.clone(),
group_by: self.group_by.clone(),
having: self.having.clone(),
limit: self.limit,
offset: self.offset,
lock: self.lock,
trashed: self.trashed,
scoped: self.scoped,
primary: self.primary,
all_tenants: self.all_tenants,
_model: PhantomData,
}
}
}
impl<M: Model> fmt::Debug for Select<M> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Select").field(&self.to_sql()).finish()
}
}
pub struct Projected<M> {
select: Select<M>,
items: Vec<Projection<M>>,
}
impl<M: Model> Projected<M> {
pub fn to_sql(&self) -> Sql {
let mut sql = Sql::new();
self.write_into(&mut sql);
sql
}
pub(crate) fn write_into(&self, sql: &mut Sql) {
sql.push("SELECT ");
sql.push_list(&self.items, ", ", |sql, p| p.write(sql));
self.select.write_tail(sql);
}
pub async fn fetch_all<'e, T, E>(self, executor: E) -> Result<Vec<T>>
where
T: for<'r> FromRow<'r, PgRow> + Send + Unpin,
E: Executor<'e>,
{
exec::fetch_all(executor, &self.to_sql(), &[], self.select.use_replica()).await
}
pub async fn fetch_optional<'e, T, E>(self, executor: E) -> Result<Option<T>>
where
T: for<'r> FromRow<'r, PgRow> + Send + Unpin,
E: Executor<'e>,
{
let projected = Projected {
select: self.select.limit(1),
items: self.items,
};
let replica = projected.select.use_replica();
exec::fetch_optional(executor, &projected.to_sql(), &[], replica).await
}
pub async fn fetch_one<'e, T, E>(self, executor: E) -> Result<T>
where
T: for<'r> FromRow<'r, PgRow> + Send + Unpin,
E: Executor<'e>,
{
self.fetch_optional(executor)
.await?
.ok_or_else(|| Error::not_found::<M>(None))
}
pub async fn scalar<'e, T, E>(self, executor: E) -> Result<T>
where
T: Type<Postgres> + for<'r> Decode<'r, Postgres> + Send + Unpin,
E: Executor<'e>,
{
exec::fetch_scalar(executor, &self.to_sql(), self.select.use_replica()).await
}
pub fn stream<'e, T, E>(self, executor: E) -> BoxStream<'e, Result<T>>
where
T: for<'r> FromRow<'r, PgRow> + Send + Unpin + 'e,
E: Executor<'e> + 'e,
{
let replica = self.select.use_replica();
exec::stream(executor, self.to_sql(), replica)
}
}
impl<M> Clone for Projected<M> {
fn clone(&self) -> Self {
Self {
select: self.select.clone(),
items: self.items.clone(),
}
}
}
impl<M: Model> fmt::Debug for Projected<M> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Projected").field(&self.to_sql()).finish()
}
}
pub struct Memoized<M> {
select: Select<M>,
ttl: Duration,
}
impl<M: Model + Clone> Memoized<M> {
async fn cached<'e, T, E, F, Fut>(&self, executor: E, op: &str, sql: &Sql, run: F) -> Result<T>
where
T: Clone + Send + Sync + 'static,
E: Executor<'e>,
F: FnOnce(E) -> Fut,
Fut: Future<Output = Result<T>>,
{
let cache = executor.__context().and_then(|c| c.cache.clone());
let Some(cache) = cache else {
return run(executor).await;
};
let key = format!(
"{op}\u{1f}{}\u{1f}{}\u{1f}{:?}",
std::any::type_name::<M>(),
sql.as_str(),
sql.params()
);
if let Some(hit) = cache.get::<T>(&key) {
tracing::debug!(target: "rok_db::cache", table = M::TABLE, op, "cache hit");
return Ok(hit);
}
let generation = cache.generation(M::TABLE);
let value = run(executor).await?;
cache.put(key, M::TABLE, generation, self.ttl, value.clone());
Ok(value)
}
pub async fn all<'e, E: Executor<'e>>(self, executor: E) -> Result<Vec<M>> {
let sql = self.select.to_sql();
let replica = self.select.use_replica();
self.cached(executor, "all", &sql, |e| {
exec::fetch_all(e, &sql, &[], replica)
})
.await
}
pub async fn first<'e, E: Executor<'e>>(self, executor: E) -> Result<Option<M>> {
let sql = self.select.clone().limit(1).to_sql();
self.cached(executor, "first", &sql, |e| {
exec::fetch_optional(e, &sql, &[], self.select.use_replica())
})
.await
}
pub async fn one<'e, E: Executor<'e>>(self, executor: E) -> Result<M> {
self.first(executor)
.await?
.ok_or_else(|| Error::not_found::<M>(None))
}
pub async fn count<'e, E: Executor<'e>>(self, executor: E) -> Result<i64> {
let sql = self.select.count_sql();
let replica = self.select.use_replica();
self.cached(executor, "count", &sql, |e| {
exec::fetch_scalar(e, &sql, replica)
})
.await
}
pub async fn exists<'e, E: Executor<'e>>(self, executor: E) -> Result<bool> {
let sql = self.select.exists_sql();
let replica = self.select.use_replica();
self.cached(executor, "exists", &sql, |e| {
exec::fetch_scalar(e, &sql, replica)
})
.await
}
pub async fn paginate<'e, E: Executor<'e>>(
self,
executor: E,
page: u64,
per_page: u64,
) -> Result<Page<M>> {
let sql = self.select.paginate_sql(page.max(1), per_page);
let select = self.select.clone();
self.cached(executor, "paginate", &sql, |e| {
select.paginate(e, page, per_page)
})
.await
}
}
impl<M> Clone for Memoized<M> {
fn clone(&self) -> Self {
Self {
select: self.select.clone(),
ttl: self.ttl,
}
}
}
impl<M: Model> fmt::Debug for Memoized<M> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Memoized")
.field("sql", &self.select.to_sql())
.field("ttl", &self.ttl)
.finish()
}
}
pub struct Update<M> {
filters: Vec<Cond>,
sets: Vec<(&'static str, Assign)>,
trashed: Trashed,
scoped: bool,
all_tenants: bool,
_model: PhantomData<fn() -> M>,
}
#[derive(Debug, Clone)]
enum Assign {
Value(Value),
Raw(String, Vec<Value>),
}
impl<M: Model> Update<M> {
pub(crate) fn new() -> Self {
Self {
filters: Vec::new(),
sets: Vec::new(),
trashed: Trashed::Exclude,
scoped: true,
all_tenants: false,
_model: PhantomData,
}
}
pub fn all_tenants(mut self) -> Self {
self.all_tenants = true;
self
}
pub fn unscoped(mut self) -> Self {
self.scoped = false;
self
}
pub fn filter(mut self, expr: Expr<M>) -> Self {
self.filters.push(expr.cond);
self
}
pub fn with_trashed(mut self) -> Self {
self.trashed = Trashed::Include;
self
}
pub fn set(mut self, column: Column<M>, value: impl Into<Value>) -> Self {
self.sets.push((column.name(), Assign::Value(value.into())));
self
}
pub fn set_raw<V: Into<Value>>(
mut self,
column: Column<M>,
sql: impl Into<String>,
params: impl IntoIterator<Item = V>,
) -> Self {
let params = params.into_iter().map(Into::into).collect();
self.sets
.push((column.name(), Assign::Raw(sql.into(), params)));
self
}
pub fn increment(self, column: Column<M>, by: impl Into<Value>) -> Self {
let raw = format!("{} + ?", quoted(column.name()));
self.set_raw(column, raw, [by.into()])
}
pub fn decrement(self, column: Column<M>, by: impl Into<Value>) -> Self {
let raw = format!("{} - ?", quoted(column.name()));
self.set_raw(column, raw, [by.into()])
}
pub(crate) fn has_sets(&self) -> bool {
!self.sets.is_empty()
}
pub(crate) fn into_select(self) -> Select<M> {
Select {
filters: self.filters,
trashed: self.trashed,
scoped: self.scoped,
all_tenants: self.all_tenants,
..Select::new()
}
}
fn is_set(&self, column: &str) -> bool {
self.sets.iter().any(|(c, _)| *c == column)
}
pub fn to_sql(&self) -> Sql {
let mut sql = Sql::new();
self.write_into(&mut sql);
sql
}
pub(crate) fn write_into(&self, sql: &mut Sql) {
sql.push("UPDATE ").push_ident(M::TABLE).push(" SET ");
sql.push_list(&self.sets, ", ", |sql, (column, assign)| {
sql.push_ident(column).push(" = ");
match assign {
Assign::Value(v) => sql.bind(v.clone()),
Assign::Raw(raw, params) => sql.push_raw(raw, params),
};
});
if let Some(updated_at) = M::UPDATED_AT_COLUMN {
if !self.is_set(updated_at) {
sql.push(", ").push_ident(updated_at).push(" = now()");
}
}
if let Some(version) = M::VERSION_COLUMN {
if !self.is_set(version) {
sql.push(", ")
.push_ident(version)
.push(" = ")
.push_ident(version)
.push(" + 1");
}
}
let implicit = implicit::<M>(self.trashed, self.scoped, self.all_tenants);
push_where(sql, " WHERE ", self.filters.iter().chain(&implicit));
}
fn check(&self) -> Result<()> {
if let Some(tenant) = M::TENANT_COLUMN {
if self.is_set(tenant) && crate::tenant::current().is_some() {
return Err(Error::InvalidQuery(format!(
"can't change the tenant column `{tenant}` inside a tenant scope"
)));
}
}
if self.sets.is_empty() {
return Err(Error::InvalidQuery(format!(
"UPDATE on `{}` has no columns to set",
M::TABLE
)));
}
Ok(())
}
pub async fn exec<'e, E: Executor<'e>>(self, executor: E) -> Result<u64> {
self.check()?;
exec::execute(executor, &self.to_sql(), &[M::TABLE]).await
}
pub async fn returning<'e, E: Executor<'e>>(self, executor: E) -> Result<Vec<M>> {
self.check()?;
let mut sql = self.to_sql();
push_returning::<M>(&mut sql);
exec::fetch_all(executor, &sql, &[M::TABLE], false).await
}
pub(crate) async fn returning_one<'e, E: Executor<'e>>(self, executor: E) -> Result<Option<M>> {
let mut sql = self.to_sql();
push_returning::<M>(&mut sql);
exec::fetch_optional(executor, &sql, &[M::TABLE], false).await
}
}
impl<M> Clone for Update<M> {
fn clone(&self) -> Self {
Self {
filters: self.filters.clone(),
sets: self.sets.clone(),
trashed: self.trashed,
scoped: self.scoped,
all_tenants: self.all_tenants,
_model: PhantomData,
}
}
}
impl<M: Model> fmt::Debug for Update<M> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Update").field(&self.to_sql()).finish()
}
}
fn quoted(ident: &str) -> String {
let mut s = String::new();
crate::sql::push_ident(&mut s, ident);
s
}
#[derive(Debug, Clone)]
pub(crate) enum ConflictTarget {
None,
Columns(Vec<&'static str>),
Constraint(&'static str),
}
#[derive(Debug, Clone)]
pub(crate) enum ConflictAction {
Nothing,
Update(Vec<&'static str>),
UpdateAll,
}
#[derive(Debug, Clone)]
pub(crate) struct Conflict {
pub(crate) target: ConflictTarget,
pub(crate) action: ConflictAction,
}
impl Conflict {
pub(crate) fn write<M: Model>(&self, sql: &mut Sql, inserted: &[&'static str]) -> Result<()> {
sql.push(" ON CONFLICT");
let target_columns: &[&str] = match &self.target {
ConflictTarget::None => &[],
ConflictTarget::Columns(columns) => {
sql.push(" (")
.push_list(columns, ", ", |sql, c| {
sql.push_ident(c);
})
.push(")");
columns
}
ConflictTarget::Constraint(name) => {
sql.push(" ON CONSTRAINT ").push_ident(name);
&[]
}
};
let mut updates: Vec<&'static str> = match &self.action {
ConflictAction::Nothing => {
sql.push(" DO NOTHING");
return Ok(());
}
ConflictAction::Update(columns) => columns.clone(),
ConflictAction::UpdateAll => inserted
.iter()
.copied()
.filter(|c| {
!target_columns.contains(c)
&& *c != M::PRIMARY_KEY
&& M::CREATED_AT_COLUMN != Some(*c)
})
.collect(),
};
if matches!(self.target, ConflictTarget::None) {
return Err(Error::InvalidQuery(
"ON CONFLICT DO UPDATE needs conflict columns or a constraint".into(),
));
}
updates.retain(|c| {
M::VERSION_COLUMN != Some(*c)
&& M::UPDATED_AT_COLUMN != Some(*c)
&& M::TENANT_COLUMN != Some(*c)
});
sql.push(" DO UPDATE SET ");
let mut first = true;
let mut sep = |sql: &mut Sql| {
if !std::mem::take(&mut first) {
sql.push(", ");
}
};
for column in &updates {
sep(sql);
sql.push_ident(column)
.push(" = EXCLUDED.")
.push_ident(column);
}
if let Some(updated_at) = M::UPDATED_AT_COLUMN {
sep(sql);
sql.push_ident(updated_at).push(" = now()");
}
if let Some(version) = M::VERSION_COLUMN {
sep(sql);
sql.push_ident(version)
.push(" = ")
.push_ident(M::TABLE)
.push(".")
.push_ident(version)
.push(" + 1");
}
if first {
sql.push_ident(M::PRIMARY_KEY)
.push(" = ")
.push_ident(M::TABLE)
.push(".")
.push_ident(M::PRIMARY_KEY);
}
if let Some(tenant) = M::TENANT_COLUMN {
if inserted.contains(&tenant) {
sql.push(" WHERE ")
.push_ident(M::TABLE)
.push(".")
.push_ident(tenant)
.push(" = EXCLUDED.")
.push_ident(tenant);
}
}
Ok(())
}
}
macro_rules! on_conflict_methods {
() => {
pub fn on_conflict(mut self, columns: impl IntoIterator<Item = Column<M>>) -> Self {
self.conflict_target = Some(ConflictTarget::Columns(
columns.into_iter().map(|c| c.name()).collect(),
));
self
}
pub fn on_constraint(mut self, name: &'static str) -> Self {
self.conflict_target = Some(ConflictTarget::Constraint(name));
self
}
pub fn do_nothing(mut self) -> Self {
self.conflict_action = Some(ConflictAction::Nothing);
self
}
pub fn do_update(mut self, columns: impl IntoIterator<Item = Column<M>>) -> Self {
self.conflict_action = Some(ConflictAction::Update(
columns.into_iter().map(|c| c.name()).collect(),
));
self
}
pub fn do_update_all(mut self) -> Self {
self.conflict_action = Some(ConflictAction::UpdateAll);
self
}
fn conflict(&self) -> Result<Option<Conflict>> {
match (&self.conflict_target, &self.conflict_action) {
(None, None) => Ok(None),
(target, Some(action)) => Ok(Some(Conflict {
target: target.clone().unwrap_or(ConflictTarget::None),
action: action.clone(),
})),
(Some(_), None) => Err(Error::InvalidQuery(
"`on_conflict` needs `do_nothing`, `do_update` or `do_update_all`".into(),
)),
}
}
};
}
pub struct Insert<M> {
values: Vec<(&'static str, String, Vec<Value>)>,
conflict_target: Option<ConflictTarget>,
conflict_action: Option<ConflictAction>,
_model: PhantomData<fn() -> M>,
}
impl<M: Model> Insert<M> {
pub(crate) fn new() -> Self {
Self {
values: Vec::new(),
conflict_target: None,
conflict_action: None,
_model: PhantomData,
}
}
pub fn set(mut self, column: Column<M>, value: impl Into<Value>) -> Self {
self.values
.push((column.name(), "?".into(), vec![value.into()]));
self
}
pub fn set_raw<V: Into<Value>>(
mut self,
column: Column<M>,
sql: impl Into<String>,
params: impl IntoIterator<Item = V>,
) -> Self {
let params = params.into_iter().map(Into::into).collect();
self.values.push((column.name(), sql.into(), params));
self
}
on_conflict_methods!();
pub fn to_sql(&self) -> Sql {
self.build(false).expect("lenient rendering never fails")
}
fn build(&self, strict: bool) -> Result<Sql> {
let mut values = self.values.clone();
if let Some(column) = M::TENANT_COLUMN {
if let Some(tenant) = crate::model::tenant_override::<M>(column) {
values.retain(|(c, _, _)| *c != column);
values.push((column, "?".into(), vec![tenant]));
}
}
for ts in [M::CREATED_AT_COLUMN, M::UPDATED_AT_COLUMN]
.into_iter()
.flatten()
{
if !values.iter().any(|(c, _, _)| *c == ts) {
values.push((ts, "now()".into(), Vec::new()));
}
}
let mut sql = Sql::new();
sql.push("INSERT INTO ").push_ident(M::TABLE);
if values.is_empty() {
sql.push(" DEFAULT VALUES");
} else {
sql.push(" (")
.push_list(&values, ", ", |sql, (c, _, _)| {
sql.push_ident(c);
})
.push(") VALUES (")
.push_list(&values, ", ", |sql, (_, raw, params)| {
sql.push_raw(raw, params);
})
.push(")");
}
let conflict = match self.conflict() {
Ok(conflict) => conflict,
Err(e) if strict => return Err(e),
Err(_) => None,
};
if let Some(conflict) = conflict {
let inserted: Vec<_> = values.iter().map(|(c, _, _)| *c).collect();
let mut with_conflict = sql.clone();
match conflict.write::<M>(&mut with_conflict, &inserted) {
Ok(()) => sql = with_conflict,
Err(e) if strict => return Err(e),
Err(_) => {}
}
}
push_returning::<M>(&mut sql);
Ok(sql)
}
pub async fn exec<'e, E: Executor<'e>>(self, executor: E) -> Result<M> {
self.exec_optional(executor)
.await?
.ok_or_else(|| Error::not_found::<M>(None))
}
pub async fn exec_optional<'e, E: Executor<'e>>(self, executor: E) -> Result<Option<M>> {
exec::fetch_optional(executor, &self.build(true)?, &[M::TABLE], false).await
}
}
impl<M> Clone for Insert<M> {
fn clone(&self) -> Self {
Self {
values: self.values.clone(),
conflict_target: self.conflict_target.clone(),
conflict_action: self.conflict_action.clone(),
_model: PhantomData,
}
}
}
impl<M: Model> fmt::Debug for Insert<M> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Insert").field(&self.to_sql()).finish()
}
}
pub struct InsertMany<'a, M> {
records: &'a [M],
conflict_target: Option<ConflictTarget>,
conflict_action: Option<ConflictAction>,
}
impl<'a, M: Model> InsertMany<'a, M> {
pub(crate) fn new(records: &'a [M]) -> Self {
Self {
records,
conflict_target: None,
conflict_action: None,
}
}
on_conflict_methods!();
pub fn to_sql(&self) -> Sql {
let conflict = self.conflict().ok().flatten();
crate::model::insert_sql(self.records, false, conflict.as_ref())
.or_else(|_| crate::model::insert_sql(self.records, false, None))
.expect("rendering without ON CONFLICT never fails")
}
fn build(&self) -> Result<Sql> {
crate::model::insert_sql(self.records, false, self.conflict()?.as_ref())
}
pub async fn exec<'e, E: Executor<'e>>(self, executor: E) -> Result<Vec<M>> {
if self.records.is_empty() {
return Ok(Vec::new());
}
for record in self.records {
crate::model::pre_insert(record)?;
}
let rows: Vec<M> = exec::fetch_all(executor, &self.build()?, &[M::TABLE], false).await?;
for row in &rows {
row.after_insert()?;
}
Ok(rows)
}
}
impl<M> Clone for InsertMany<'_, M> {
fn clone(&self) -> Self {
Self {
records: self.records,
conflict_target: self.conflict_target.clone(),
conflict_action: self.conflict_action.clone(),
}
}
}
impl<M: Model> fmt::Debug for InsertMany<'_, M> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("InsertMany").field(&self.to_sql()).finish()
}
}
#[doc(hidden)]
pub fn __paginate_sql<M: Model>(select: &Select<M>, page: u64, per_page: u64) -> Sql {
select.paginate_sql(page, per_page)
}
#[doc(hidden)]
pub fn __cursor_sql<M: Model>(
select: Select<M>,
after: Option<&Cursor>,
limit: u64,
) -> Result<Sql> {
Ok(select.cursor_select(after, limit)?.to_sql())
}