surrealdb_core/sql/statements/
info.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use crate::ctx::Context;
use crate::dbs::Options;
use crate::doc::CursorDoc;
use crate::err::Error;
use crate::iam::Action;
use crate::iam::ResourceKind;
use crate::sql::{Base, Ident, Object, Value};
use derive::Store;
use revision::revisioned;
use serde::{Deserialize, Serialize};
use std::fmt;

#[revisioned(revision = 2)]
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[non_exhaustive]
pub enum InfoStatement {
	#[revision(end = 2, convert_fn = "root_migrate")]
	Root,
	#[revision(start = 2)]
	Root(bool),
	#[revision(end = 2, convert_fn = "ns_migrate")]
	Ns,
	#[revision(start = 2)]
	Ns(bool),
	#[revision(end = 2, convert_fn = "db_migrate")]
	Db,
	#[revision(start = 2)]
	Db(bool),
	#[revision(end = 2, convert_fn = "tb_migrate")]
	Tb(Ident),
	#[revision(start = 2)]
	Tb(Ident, bool),
	#[revision(end = 2, convert_fn = "user_migrate")]
	User(Ident, Option<Base>),
	#[revision(start = 2)]
	User(Ident, Option<Base>, bool),
}

impl InfoStatement {
	fn root_migrate(_revision: u16, _: ()) -> Result<Self, revision::Error> {
		Ok(Self::Root(false))
	}

	fn ns_migrate(_revision: u16, _: ()) -> Result<Self, revision::Error> {
		Ok(Self::Ns(false))
	}

	fn db_migrate(_revision: u16, _: ()) -> Result<Self, revision::Error> {
		Ok(Self::Db(false))
	}

	fn tb_migrate(_revision: u16, n: (Ident,)) -> Result<Self, revision::Error> {
		Ok(Self::Tb(n.0, false))
	}

	fn user_migrate(
		_revision: u16,
		(i, b): (Ident, Option<Base>),
	) -> Result<Self, revision::Error> {
		Ok(Self::User(i, b, false))
	}
}

impl InfoStatement {
	/// Process this type returning a computed simple Value
	pub(crate) async fn compute(
		&self,
		ctx: &Context<'_>,
		opt: &Options,
		_doc: Option<&CursorDoc<'_>>,
	) -> Result<Value, Error> {
		// Allowed to run?
		match self {
			InfoStatement::Root(false) => {
				// Allowed to run?
				opt.is_allowed(Action::View, ResourceKind::Any, &Base::Root)?;
				// Claim transaction
				let mut run = ctx.tx_lock().await;
				// Create the result set
				let mut res = Object::default();
				// Process the namespaces
				let mut tmp = Object::default();
				for v in run.all_ns().await?.iter() {
					tmp.insert(v.name.to_string(), v.to_string().into());
				}
				res.insert("namespaces".to_owned(), tmp.into());
				// Process the users
				let mut tmp = Object::default();
				for v in run.all_root_users().await?.iter() {
					tmp.insert(v.name.to_string(), v.to_string().into());
				}
				res.insert("users".to_owned(), tmp.into());
				// Ok all good
				Value::from(res).ok()
			}
			InfoStatement::Ns(false) => {
				// Allowed to run?
				opt.is_allowed(Action::View, ResourceKind::Any, &Base::Ns)?;
				// Claim transaction
				let mut run = ctx.tx_lock().await;
				// Create the result set
				let mut res = Object::default();
				// Process the databases
				let mut tmp = Object::default();
				for v in run.all_db(opt.ns()?).await?.iter() {
					tmp.insert(v.name.to_string(), v.to_string().into());
				}
				res.insert("databases".to_owned(), tmp.into());
				// Process the users
				let mut tmp = Object::default();
				for v in run.all_ns_users(opt.ns()?).await?.iter() {
					tmp.insert(v.name.to_string(), v.to_string().into());
				}
				res.insert("users".to_owned(), tmp.into());
				// Process the accesses
				let mut tmp = Object::default();
				for v in run.all_ns_accesses_redacted(opt.ns()?).await?.iter() {
					tmp.insert(v.name.to_string(), v.to_string().into());
				}
				res.insert("accesses".to_owned(), tmp.into());
				// Ok all good
				Value::from(res).ok()
			}
			InfoStatement::Db(false) => {
				// Allowed to run?
				opt.is_allowed(Action::View, ResourceKind::Any, &Base::Db)?;
				// Claim transaction
				let mut run = ctx.tx_lock().await;
				// Create the result set
				let mut res = Object::default();
				// Process the users
				let mut tmp = Object::default();
				for v in run.all_db_users(opt.ns()?, opt.db()?).await?.iter() {
					tmp.insert(v.name.to_string(), v.to_string().into());
				}
				res.insert("users".to_owned(), tmp.into());
				// Process the functions
				let mut tmp = Object::default();
				for v in run.all_db_functions(opt.ns()?, opt.db()?).await?.iter() {
					tmp.insert(v.name.to_string(), v.to_string().into());
				}
				res.insert("functions".to_owned(), tmp.into());
				// Process the models
				let mut tmp = Object::default();
				for v in run.all_db_models(opt.ns()?, opt.db()?).await?.iter() {
					tmp.insert(format!("{}<{}>", v.name, v.version), v.to_string().into());
				}
				res.insert("models".to_owned(), tmp.into());
				// Process the params
				let mut tmp = Object::default();
				for v in run.all_db_params(opt.ns()?, opt.db()?).await?.iter() {
					tmp.insert(v.name.to_string(), v.to_string().into());
				}
				res.insert("params".to_owned(), tmp.into());
				// Process the accesses
				let mut tmp = Object::default();
				for v in run.all_db_accesses_redacted(opt.ns()?, opt.db()?).await?.iter() {
					tmp.insert(v.name.to_string(), v.to_string().into());
				}
				res.insert("accesses".to_owned(), tmp.into());
				// Process the tables
				let mut tmp = Object::default();
				for v in run.all_tb(opt.ns()?, opt.db()?).await?.iter() {
					tmp.insert(v.name.to_string(), v.to_string().into());
				}
				res.insert("tables".to_owned(), tmp.into());
				// Process the analyzers
				let mut tmp = Object::default();
				for v in run.all_db_analyzers(opt.ns()?, opt.db()?).await?.iter() {
					tmp.insert(v.name.to_string(), v.to_string().into());
				}
				res.insert("analyzers".to_owned(), tmp.into());
				// Ok all good
				Value::from(res).ok()
			}
			InfoStatement::Tb(tb, false) => {
				// Allowed to run?
				opt.is_allowed(Action::View, ResourceKind::Any, &Base::Db)?;
				// Claim transaction
				let mut run = ctx.tx_lock().await;
				// Create the result set
				let mut res = Object::default();
				// Process the events
				let mut tmp = Object::default();
				for v in run.all_tb_events(opt.ns()?, opt.db()?, tb).await?.iter() {
					tmp.insert(v.name.to_string(), v.to_string().into());
				}
				res.insert("events".to_owned(), tmp.into());
				// Process the fields
				let mut tmp = Object::default();
				for v in run.all_tb_fields(opt.ns()?, opt.db()?, tb).await?.iter() {
					tmp.insert(v.name.to_string(), v.to_string().into());
				}
				res.insert("fields".to_owned(), tmp.into());
				// Process the tables
				let mut tmp = Object::default();
				for v in run.all_tb_views(opt.ns()?, opt.db()?, tb).await?.iter() {
					tmp.insert(v.name.to_string(), v.to_string().into());
				}
				res.insert("tables".to_owned(), tmp.into());
				// Process the indexes
				let mut tmp = Object::default();
				for v in run.all_tb_indexes(opt.ns()?, opt.db()?, tb).await?.iter() {
					tmp.insert(v.name.to_string(), v.to_string().into());
				}
				res.insert("indexes".to_owned(), tmp.into());
				// Process the live queries
				let mut tmp = Object::default();
				for v in run.all_tb_lives(opt.ns()?, opt.db()?, tb).await?.iter() {
					tmp.insert(v.id.to_raw(), v.to_string().into());
				}
				res.insert("lives".to_owned(), tmp.into());
				// Ok all good
				Value::from(res).ok()
			}
			InfoStatement::User(user, base, false) => {
				let base = base.clone().unwrap_or(opt.selected_base()?);
				// Allowed to run?
				opt.is_allowed(Action::View, ResourceKind::Actor, &base)?;

				// Claim transaction
				let mut run = ctx.tx_lock().await;
				// Process the user
				let res = match base {
					Base::Root => run.get_root_user(user).await?,
					Base::Ns => run.get_ns_user(opt.ns()?, user).await?,
					Base::Db => run.get_db_user(opt.ns()?, opt.db()?, user).await?,
					_ => return Err(Error::InvalidLevel(base.to_string())),
				};
				// Ok all good
				Value::from(res.to_string()).ok()
			}
			InfoStatement::Root(true) => {
				// Allowed to run?
				opt.is_allowed(Action::View, ResourceKind::Any, &Base::Root)?;
				// Claim transaction
				let mut run = ctx.tx_lock().await;
				// Create the result set
				let mut res = Object::default();
				// Process the namespaces
				res.insert("namespaces".to_owned(), process_arr(run.all_ns().await?));
				// Process the users
				res.insert("users".to_owned(), process_arr(run.all_root_users().await?));
				// Ok all good
				Value::from(res).ok()
			}
			InfoStatement::Ns(true) => {
				// Allowed to run?
				opt.is_allowed(Action::View, ResourceKind::Any, &Base::Ns)?;
				// Claim transaction
				let mut run = ctx.tx_lock().await;
				// Create the result set
				let mut res = Object::default();
				// Process the databases
				res.insert("databases".to_owned(), process_arr(run.all_db(opt.ns()?).await?));
				// Process the users
				res.insert("users".to_owned(), process_arr(run.all_ns_users(opt.ns()?).await?));
				// Process the accesses
				res.insert(
					"accesses".to_owned(),
					process_arr(run.all_ns_accesses_redacted(opt.ns()?).await?),
				);
				// Ok all good
				Value::from(res).ok()
			}
			InfoStatement::Db(true) => {
				// Allowed to run?
				opt.is_allowed(Action::View, ResourceKind::Any, &Base::Db)?;
				// Claim transaction
				let mut run = ctx.tx_lock().await;
				// Create the result set
				let mut res = Object::default();
				// Process the users
				res.insert(
					"users".to_owned(),
					process_arr(run.all_db_users(opt.ns()?, opt.db()?).await?),
				);
				// Process the accesses
				res.insert(
					"accesses".to_owned(),
					process_arr(run.all_db_accesses(opt.ns()?, opt.db()?).await?),
				);
				// Process the functions
				res.insert(
					"functions".to_owned(),
					process_arr(run.all_db_functions(opt.ns()?, opt.db()?).await?),
				);
				// Process the models
				res.insert(
					"models".to_owned(),
					process_arr(run.all_db_models(opt.ns()?, opt.db()?).await?),
				);
				// Process the params
				res.insert(
					"params".to_owned(),
					process_arr(run.all_db_params(opt.ns()?, opt.db()?).await?),
				);
				// Process the accesses
				res.insert(
					"accesses".to_owned(),
					process_arr(run.all_db_accesses_redacted(opt.ns()?, opt.db()?).await?),
				);
				// Process the tables
				res.insert(
					"tables".to_owned(),
					process_arr(run.all_tb(opt.ns()?, opt.db()?).await?),
				);
				// Process the analyzers
				res.insert(
					"analyzers".to_owned(),
					process_arr(run.all_db_analyzers(opt.ns()?, opt.db()?).await?),
				);
				// Ok all good
				Value::from(res).ok()
			}
			InfoStatement::Tb(tb, true) => {
				// Allowed to run?
				opt.is_allowed(Action::View, ResourceKind::Any, &Base::Db)?;
				// Claim transaction
				let mut run = ctx.tx_lock().await;
				// Create the result set
				let mut res = Object::default();
				// Process the events
				res.insert(
					"events".to_owned(),
					process_arr(run.all_tb_events(opt.ns()?, opt.db()?, tb).await?),
				);
				// Process the fields
				res.insert(
					"fields".to_owned(),
					process_arr(run.all_tb_fields(opt.ns()?, opt.db()?, tb).await?),
				);
				// Process the tables
				res.insert(
					"tables".to_owned(),
					process_arr(run.all_tb_views(opt.ns()?, opt.db()?, tb).await?),
				);
				// Process the indexes
				res.insert(
					"indexes".to_owned(),
					process_arr(run.all_tb_indexes(opt.ns()?, opt.db()?, tb).await?),
				);
				// Process the live queries
				res.insert(
					"lives".to_owned(),
					process_arr(run.all_tb_lives(opt.ns()?, opt.db()?, tb).await?),
				);
				// Ok all good
				Value::from(res).ok()
			}
			InfoStatement::User(user, base, true) => {
				let base = base.clone().unwrap_or(opt.selected_base()?);
				// Allowed to run?
				opt.is_allowed(Action::View, ResourceKind::Actor, &base)?;

				// Claim transaction
				let mut run = ctx.tx_lock().await;
				// Process the user
				let res = match base {
					Base::Root => run.get_root_user(user).await?,
					Base::Ns => run.get_ns_user(opt.ns()?, user).await?,
					Base::Db => run.get_db_user(opt.ns()?, opt.db()?, user).await?,
					_ => return Err(Error::InvalidLevel(base.to_string())),
				};
				// Ok all good
				Ok(res.structure())
			}
		}
	}
}

impl fmt::Display for InfoStatement {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match self {
			Self::Root(false) => f.write_str("INFO FOR ROOT"),
			Self::Root(true) => f.write_str("INFO FOR ROOT STRUCTURE"),
			Self::Ns(false) => f.write_str("INFO FOR NAMESPACE"),
			Self::Ns(true) => f.write_str("INFO FOR NAMESPACE STRUCTURE"),
			Self::Db(false) => f.write_str("INFO FOR DATABASE"),
			Self::Db(true) => f.write_str("INFO FOR DATABASE STRUCTURE"),
			Self::Tb(ref t, false) => write!(f, "INFO FOR TABLE {t}"),
			Self::Tb(ref t, true) => write!(f, "INFO FOR TABLE {t} STRUCTURE"),
			Self::User(ref u, ref b, false) => match b {
				Some(ref b) => write!(f, "INFO FOR USER {u} ON {b}"),
				None => write!(f, "INFO FOR USER {u}"),
			},
			Self::User(ref u, ref b, true) => match b {
				Some(ref b) => write!(f, "INFO FOR USER {u} ON {b} STRUCTURE"),
				None => write!(f, "INFO FOR USER {u} STRUCTURE"),
			},
		}
	}
}

use std::sync::Arc;

pub(crate) trait InfoStructure {
	fn structure(self) -> Value;
}

impl InfoStatement {
	pub(crate) fn structurize(self) -> Self {
		match self {
			InfoStatement::Root(_) => InfoStatement::Root(true),
			InfoStatement::Ns(_) => InfoStatement::Ns(true),
			InfoStatement::Db(_) => InfoStatement::Db(true),
			InfoStatement::Tb(t, _) => InfoStatement::Tb(t, true),
			InfoStatement::User(u, b, _) => InfoStatement::User(u, b, true),
		}
	}
}

fn process_arr<T>(a: Arc<[T]>) -> Value
where
	T: InfoStructure + Clone,
{
	Value::Array(a.iter().cloned().map(InfoStructure::structure).collect())
}