use crate::{
Result,
engine::exec::{Action, Exec, Output, VarId},
};
use toasty_core::{
driver::{Rows, operation},
schema::db::TableId,
stmt::{self, ValueStream},
};
#[derive(Debug, Clone)]
pub(crate) struct UpdateByKey {
pub input: VarId,
pub output: Output,
pub table: TableId,
pub assignments: stmt::Assignments,
pub filter: Option<stmt::Expr>,
pub condition: Option<stmt::Expr>,
pub returning: bool,
}
impl Exec<'_> {
pub(super) async fn action_update_by_key(&mut self, action: &UpdateByKey) -> Result<()> {
let keys = self
.vars
.load(action.input)
.await?
.collect_as_value()
.await?
.into_list_unwrap();
let res = if keys.is_empty() {
if action.returning {
Rows::value_stream(ValueStream::default())
} else {
Rows::Count(0)
}
} else {
let op = operation::UpdateByKey {
table: action.table,
keys,
assignments: action.assignments.clone(),
filter: action.filter.clone(),
condition: action.condition.clone(),
returning: action.returning,
};
let res = self.connection.exec(&self.engine.schema, op.into()).await?;
debug_assert_eq!(!res.rows.is_count(), action.returning);
res.rows
};
self.vars
.store(action.output.var, action.output.num_uses, res);
Ok(())
}
}
impl From<UpdateByKey> for Action {
fn from(src: UpdateByKey) -> Self {
Self::UpdateByKey(src)
}
}