surrealdb_sql/statements/
live.rs1use crate::ctx::Context;
2use crate::dbs::{Options, Transaction};
3use crate::doc::CursorDoc;
4use crate::err::Error;
5use crate::iam::Auth;
6use crate::{Cond, Fetchs, Fields, Uuid, Value};
7use derive::Store;
8use revision::revisioned;
9use serde::{Deserialize, Serialize};
10use std::fmt;
11
12#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
13#[revisioned(revision = 2)]
14pub struct LiveStatement {
15 pub id: Uuid,
16 pub node: Uuid,
17 pub expr: Fields,
18 pub what: Value,
19 pub cond: Option<Cond>,
20 pub fetch: Option<Fetchs>,
21 #[doc(hidden)]
26 pub archived: Option<Uuid>,
27 #[revision(start = 2)]
33 #[doc(hidden)]
34 pub session: Option<Value>,
35 #[doc(hidden)]
41 pub auth: Option<Auth>,
42}
43
44impl LiveStatement {
45 pub(crate) fn from_source_parts(
47 expr: Fields,
48 what: Value,
49 cond: Option<Cond>,
50 fetch: Option<Fetchs>,
51 ) -> Self {
52 LiveStatement {
53 id: Uuid::new_v4(),
54 node: Uuid::new_v4(),
55 expr,
56 what,
57 cond,
58 fetch,
59 ..Default::default()
60 }
61 }
62
63 pub(crate) async fn compute(
65 &self,
66 ctx: &Context<'_>,
67 opt: &Options,
68 txn: &Transaction,
69 doc: Option<&CursorDoc<'_>>,
70 ) -> Result<Value, Error> {
71 opt.realtime()?;
73 opt.valid_for_db()?;
75 let nid = opt.id()?;
77 let mut stm = LiveStatement {
79 session: ctx.value("session").cloned(),
82 auth: Some(opt.auth.as_ref().clone()),
85 ..self.clone()
88 };
89 let id = stm.id.0;
90 let mut run = txn.lock().await;
92 match stm.what.compute(ctx, opt, txn, doc).await? {
94 Value::Table(tb) => {
95 stm.node = nid.into();
97 run.putc_ndlq(nid, id, opt.ns(), opt.db(), tb.as_str(), None).await?;
99 run.putc_tblq(opt.ns(), opt.db(), &tb, stm, None).await?;
101 }
102 v => {
103 return Err(Error::LiveStatement {
104 value: v.to_string(),
105 })
106 }
107 };
108 Ok(id.into())
110 }
111
112 pub(crate) fn archive(mut self, node_id: Uuid) -> LiveStatement {
113 self.archived = Some(node_id);
114 self
115 }
116}
117
118impl fmt::Display for LiveStatement {
119 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
120 write!(f, "LIVE SELECT {} FROM {}", self.expr, self.what)?;
121 if let Some(ref v) = self.cond {
122 write!(f, " {v}")?
123 }
124 if let Some(ref v) = self.fetch {
125 write!(f, " {v}")?
126 }
127 Ok(())
128 }
129}