surrealdb/sql/statements/
show.rsuse crate::ctx::Context;
use crate::dbs::{Options, Transaction};
use crate::doc::CursorDoc;
use crate::err::Error;
use crate::iam::{Action, ResourceKind};
use crate::sql::{Base, Datetime, Table, Value};
use derive::Store;
use revision::revisioned;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
#[revisioned(revision = 1)]
pub enum ShowSince {
Timestamp(Datetime),
Versionstamp(u64),
}
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
#[revisioned(revision = 1)]
pub struct ShowStatement {
pub table: Option<Table>,
pub since: ShowSince,
pub limit: Option<u32>,
}
impl ShowStatement {
pub(crate) async fn compute(
&self,
_ctx: &Context<'_>,
opt: &Options,
txn: &Transaction,
_doc: Option<&CursorDoc<'_>>,
) -> Result<Value, Error> {
opt.is_allowed(Action::View, ResourceKind::Table, &Base::Db)?;
let txn = txn.clone();
let mut run = txn.lock().await;
let tb = self.table.as_deref();
let r = crate::cf::read(
&mut run,
opt.ns(),
opt.db(),
tb.map(|x| x.as_str()),
self.since.clone(),
self.limit,
)
.await?;
let mut a = Vec::<Value>::new();
for r in r.iter() {
let v: Value = r.clone().into_value();
a.push(v);
}
let v: Value = Value::Array(crate::sql::array::Array(a));
Ok(v)
}
}
impl fmt::Display for ShowStatement {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "SHOW CHANGES FOR")?;
match self.table {
Some(ref v) => write!(f, " TABLE {}", v)?,
None => write!(f, " DATABASE")?,
}
match self.since {
ShowSince::Timestamp(ref v) => write!(f, " SINCE {}", v)?,
ShowSince::Versionstamp(ref v) => write!(f, " SINCE {}", v)?,
}
if let Some(ref v) = self.limit {
write!(f, " LIMIT {}", v)?
}
Ok(())
}
}