surrealdb/sql/statements/
show.rs1use crate::ctx::Context;
2use crate::dbs::{Options, Transaction};
3use crate::doc::CursorDoc;
4use crate::err::Error;
5use crate::iam::{Action, ResourceKind};
6use crate::sql::{Base, Datetime, Table, Value};
7use derive::Store;
8use revision::revisioned;
9use serde::{Deserialize, Serialize};
10use std::fmt;
11
12#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
13#[revisioned(revision = 1)]
14pub enum ShowSince {
15 Timestamp(Datetime),
16 Versionstamp(u64),
17}
18
19#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
22#[revisioned(revision = 1)]
23pub struct ShowStatement {
24 pub table: Option<Table>,
25 pub since: ShowSince,
26 pub limit: Option<u32>,
27}
28
29impl ShowStatement {
30 pub(crate) async fn compute(
32 &self,
33 _ctx: &Context<'_>,
34 opt: &Options,
35 txn: &Transaction,
36 _doc: Option<&CursorDoc<'_>>,
37 ) -> Result<Value, Error> {
38 opt.is_allowed(Action::View, ResourceKind::Table, &Base::Db)?;
40 let txn = txn.clone();
42 let mut run = txn.lock().await;
44 let tb = self.table.as_deref();
46 let r = crate::cf::read(
47 &mut run,
48 opt.ns(),
49 opt.db(),
50 tb.map(|x| x.as_str()),
51 self.since.clone(),
52 self.limit,
53 )
54 .await?;
55 let mut a = Vec::<Value>::new();
57 for r in r.iter() {
58 let v: Value = r.clone().into_value();
59 a.push(v);
60 }
61 let v: Value = Value::Array(crate::sql::array::Array(a));
62 Ok(v)
63 }
64}
65
66impl fmt::Display for ShowStatement {
67 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
68 write!(f, "SHOW CHANGES FOR")?;
69 match self.table {
70 Some(ref v) => write!(f, " TABLE {}", v)?,
71 None => write!(f, " DATABASE")?,
72 }
73 match self.since {
74 ShowSince::Timestamp(ref v) => write!(f, " SINCE {}", v)?,
75 ShowSince::Versionstamp(ref v) => write!(f, " SINCE {}", v)?,
76 }
77 if let Some(ref v) = self.limit {
78 write!(f, " LIMIT {}", v)?
79 }
80 Ok(())
81 }
82}