2022-01-13 07:29:41 +00:00
|
|
|
use crate::cnf;
|
|
|
|
use crate::dbs::Auth;
|
2022-02-06 21:06:52 +00:00
|
|
|
use crate::dbs::Level;
|
2023-06-20 22:50:26 +00:00
|
|
|
use crate::dbs::Notification;
|
2022-01-13 07:29:41 +00:00
|
|
|
use crate::err::Error;
|
2023-06-20 22:50:26 +00:00
|
|
|
use channel::Sender;
|
2022-02-06 21:06:52 +00:00
|
|
|
use std::sync::Arc;
|
2023-06-20 22:50:26 +00:00
|
|
|
use uuid::Uuid;
|
2022-01-13 07:29:41 +00:00
|
|
|
|
2022-11-23 09:42:59 +00:00
|
|
|
/// An Options is passed around when processing a set of query
|
|
|
|
/// statements. An Options contains specific information for how
|
|
|
|
/// to process each particular statement, including the record
|
|
|
|
/// version to retrieve, whether futures should be processed, and
|
|
|
|
/// whether field/event/table queries should be processed (useful
|
|
|
|
/// when importing data, where these queries might fail).
|
2022-01-13 07:29:41 +00:00
|
|
|
|
2023-06-20 22:50:26 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2022-02-06 21:06:52 +00:00
|
|
|
pub struct Options {
|
2023-06-20 22:50:26 +00:00
|
|
|
/// Current Node ID
|
|
|
|
pub id: Arc<Uuid>,
|
2023-04-29 23:23:19 +00:00
|
|
|
/// Currently selected NS
|
2022-09-22 23:54:53 +00:00
|
|
|
pub ns: Option<Arc<str>>,
|
2023-04-29 23:23:19 +00:00
|
|
|
/// Currently selected DB
|
2022-09-22 23:54:53 +00:00
|
|
|
pub db: Option<Arc<str>>,
|
2023-04-29 23:23:19 +00:00
|
|
|
/// Connection authentication data
|
2022-02-06 21:06:52 +00:00
|
|
|
pub auth: Arc<Auth>,
|
2023-04-29 23:23:19 +00:00
|
|
|
/// Approximately how large is the current call stack?
|
2022-10-06 16:35:03 +00:00
|
|
|
dive: u8,
|
2023-04-29 23:23:19 +00:00
|
|
|
/// Whether live queries are allowed?
|
2022-06-27 16:01:39 +00:00
|
|
|
pub live: bool,
|
2023-04-29 23:23:19 +00:00
|
|
|
/// Should we force tables/events to re-run?
|
2022-02-06 21:06:52 +00:00
|
|
|
pub force: bool,
|
2023-04-29 23:23:19 +00:00
|
|
|
/// Should we run permissions checks?
|
2022-04-01 09:39:19 +00:00
|
|
|
pub perms: bool,
|
2023-04-29 23:23:19 +00:00
|
|
|
/// Should we error if tables don't exist?
|
2022-08-25 13:49:33 +00:00
|
|
|
pub strict: bool,
|
2023-04-29 23:23:19 +00:00
|
|
|
/// Should we process field queries?
|
2022-02-06 21:06:52 +00:00
|
|
|
pub fields: bool,
|
2023-04-29 23:23:19 +00:00
|
|
|
/// Should we process event queries?
|
2022-02-06 21:06:52 +00:00
|
|
|
pub events: bool,
|
2023-04-29 23:23:19 +00:00
|
|
|
/// Should we process table queries?
|
2022-02-06 21:06:52 +00:00
|
|
|
pub tables: bool,
|
2023-04-29 23:23:19 +00:00
|
|
|
/// Should we process index queries?
|
2022-08-25 13:49:33 +00:00
|
|
|
pub indexes: bool,
|
2023-04-29 23:23:19 +00:00
|
|
|
/// Should we process function futures?
|
2022-02-06 21:06:52 +00:00
|
|
|
pub futures: bool,
|
2023-06-20 22:50:26 +00:00
|
|
|
///
|
|
|
|
pub sender: Sender<Notification>,
|
2022-01-13 07:29:41 +00:00
|
|
|
}
|
|
|
|
|
2022-02-06 21:06:52 +00:00
|
|
|
impl Default for Options {
|
2022-01-13 07:29:41 +00:00
|
|
|
fn default() -> Self {
|
2023-06-20 22:50:26 +00:00
|
|
|
Options::new(Arc::new(Uuid::new_v4()), channel::unbounded().0, Arc::new(Auth::No))
|
2022-01-13 07:29:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-06 21:06:52 +00:00
|
|
|
impl Options {
|
2022-09-16 08:55:18 +00:00
|
|
|
/// Create a new Options object
|
2023-06-20 22:50:26 +00:00
|
|
|
pub fn new(id: Arc<Uuid>, send: Sender<Notification>, auth: Arc<Auth>) -> Options {
|
2022-01-13 07:29:41 +00:00
|
|
|
Options {
|
2023-06-20 22:50:26 +00:00
|
|
|
id,
|
2022-02-06 21:06:52 +00:00
|
|
|
ns: None,
|
|
|
|
db: None,
|
2022-01-13 07:29:41 +00:00
|
|
|
dive: 0,
|
2022-06-27 16:01:39 +00:00
|
|
|
live: false,
|
2022-04-01 09:39:19 +00:00
|
|
|
perms: true,
|
2022-01-13 07:29:41 +00:00
|
|
|
force: false,
|
2022-08-25 13:49:33 +00:00
|
|
|
strict: false,
|
2022-01-13 07:29:41 +00:00
|
|
|
fields: true,
|
|
|
|
events: true,
|
|
|
|
tables: true,
|
2022-08-25 13:49:33 +00:00
|
|
|
indexes: true,
|
2022-01-13 07:29:41 +00:00
|
|
|
futures: false,
|
2023-06-20 22:50:26 +00:00
|
|
|
sender: send,
|
|
|
|
auth,
|
2022-01-13 07:29:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-20 22:50:26 +00:00
|
|
|
/// Get current Node ID
|
|
|
|
pub fn id(&self) -> &Uuid {
|
|
|
|
self.id.as_ref()
|
|
|
|
}
|
|
|
|
|
2022-09-16 08:55:18 +00:00
|
|
|
/// Get currently selected NS
|
2022-02-26 00:35:11 +00:00
|
|
|
pub fn ns(&self) -> &str {
|
2022-02-06 21:06:52 +00:00
|
|
|
self.ns.as_ref().unwrap()
|
|
|
|
}
|
|
|
|
|
2022-09-16 08:55:18 +00:00
|
|
|
/// Get currently selected DB
|
2022-02-26 00:35:11 +00:00
|
|
|
pub fn db(&self) -> &str {
|
2022-02-06 21:06:52 +00:00
|
|
|
self.db.as_ref().unwrap()
|
|
|
|
}
|
|
|
|
|
2022-10-06 16:35:03 +00:00
|
|
|
/// Create a new Options object for a function/subquery/future/etc.
|
|
|
|
///
|
|
|
|
/// The parameter is the approximate cost of the operation (more concretely, the size of the
|
|
|
|
/// stack frame it uses relative to a simple function call). When in doubt, use a value of 1.
|
|
|
|
pub fn dive(&self, cost: u8) -> Result<Options, Error> {
|
|
|
|
let dive = self.dive.saturating_add(cost);
|
2023-05-09 06:37:07 +00:00
|
|
|
if dive <= *cnf::MAX_COMPUTATION_DEPTH {
|
2022-01-13 07:29:41 +00:00
|
|
|
Ok(Options {
|
2023-06-20 22:50:26 +00:00
|
|
|
sender: self.sender.clone(),
|
2022-02-06 21:06:52 +00:00
|
|
|
auth: self.auth.clone(),
|
2023-06-20 22:50:26 +00:00
|
|
|
id: self.id.clone(),
|
2022-02-06 21:06:52 +00:00
|
|
|
ns: self.ns.clone(),
|
|
|
|
db: self.db.clone(),
|
2022-10-06 16:35:03 +00:00
|
|
|
dive,
|
2022-01-13 07:29:41 +00:00
|
|
|
..*self
|
|
|
|
})
|
|
|
|
} else {
|
2022-10-06 16:35:03 +00:00
|
|
|
Err(Error::ComputationDepthExceeded)
|
2022-01-13 07:29:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-16 08:55:18 +00:00
|
|
|
/// Create a new Options object for a subquery
|
2022-02-06 21:06:52 +00:00
|
|
|
pub fn force(&self, v: bool) -> Options {
|
2022-01-13 07:29:41 +00:00
|
|
|
Options {
|
2023-06-20 22:50:26 +00:00
|
|
|
sender: self.sender.clone(),
|
2022-02-06 21:06:52 +00:00
|
|
|
auth: self.auth.clone(),
|
2023-06-20 22:50:26 +00:00
|
|
|
id: self.id.clone(),
|
2022-02-06 21:06:52 +00:00
|
|
|
ns: self.ns.clone(),
|
|
|
|
db: self.db.clone(),
|
2022-01-13 07:29:41 +00:00
|
|
|
force: v,
|
|
|
|
..*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-16 08:55:18 +00:00
|
|
|
/// Create a new Options object for a subquery
|
2022-04-01 09:39:19 +00:00
|
|
|
pub fn perms(&self, v: bool) -> Options {
|
|
|
|
Options {
|
2023-06-20 22:50:26 +00:00
|
|
|
sender: self.sender.clone(),
|
2022-04-01 09:39:19 +00:00
|
|
|
auth: self.auth.clone(),
|
2023-06-20 22:50:26 +00:00
|
|
|
id: self.id.clone(),
|
2022-04-01 09:39:19 +00:00
|
|
|
ns: self.ns.clone(),
|
|
|
|
db: self.db.clone(),
|
|
|
|
perms: v,
|
|
|
|
..*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-16 08:55:18 +00:00
|
|
|
/// Create a new Options object for a subquery
|
2022-02-06 21:06:52 +00:00
|
|
|
pub fn fields(&self, v: bool) -> Options {
|
2022-01-13 07:29:41 +00:00
|
|
|
Options {
|
2023-06-20 22:50:26 +00:00
|
|
|
sender: self.sender.clone(),
|
2022-02-06 21:06:52 +00:00
|
|
|
auth: self.auth.clone(),
|
2023-06-20 22:50:26 +00:00
|
|
|
id: self.id.clone(),
|
2022-02-06 21:06:52 +00:00
|
|
|
ns: self.ns.clone(),
|
|
|
|
db: self.db.clone(),
|
2022-01-13 07:29:41 +00:00
|
|
|
fields: v,
|
|
|
|
..*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-16 08:55:18 +00:00
|
|
|
/// Create a new Options object for a subquery
|
2022-02-06 21:06:52 +00:00
|
|
|
pub fn events(&self, v: bool) -> Options {
|
2022-01-13 07:29:41 +00:00
|
|
|
Options {
|
2023-06-20 22:50:26 +00:00
|
|
|
sender: self.sender.clone(),
|
2022-02-06 21:06:52 +00:00
|
|
|
auth: self.auth.clone(),
|
2023-06-20 22:50:26 +00:00
|
|
|
id: self.id.clone(),
|
2022-02-06 21:06:52 +00:00
|
|
|
ns: self.ns.clone(),
|
|
|
|
db: self.db.clone(),
|
2022-01-13 07:29:41 +00:00
|
|
|
events: v,
|
|
|
|
..*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-16 08:55:18 +00:00
|
|
|
/// Create a new Options object for a subquery
|
2022-02-06 21:06:52 +00:00
|
|
|
pub fn tables(&self, v: bool) -> Options {
|
2022-01-13 07:29:41 +00:00
|
|
|
Options {
|
2023-06-20 22:50:26 +00:00
|
|
|
sender: self.sender.clone(),
|
2022-02-06 21:06:52 +00:00
|
|
|
auth: self.auth.clone(),
|
2023-06-20 22:50:26 +00:00
|
|
|
id: self.id.clone(),
|
2022-02-06 21:06:52 +00:00
|
|
|
ns: self.ns.clone(),
|
|
|
|
db: self.db.clone(),
|
2022-01-13 07:29:41 +00:00
|
|
|
tables: v,
|
|
|
|
..*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-16 08:55:18 +00:00
|
|
|
/// Create a new Options object for a subquery
|
2022-08-25 13:49:33 +00:00
|
|
|
pub fn indexes(&self, v: bool) -> Options {
|
|
|
|
Options {
|
2023-06-20 22:50:26 +00:00
|
|
|
sender: self.sender.clone(),
|
2022-08-25 13:49:33 +00:00
|
|
|
auth: self.auth.clone(),
|
2023-06-20 22:50:26 +00:00
|
|
|
id: self.id.clone(),
|
2022-08-25 13:49:33 +00:00
|
|
|
ns: self.ns.clone(),
|
|
|
|
db: self.db.clone(),
|
|
|
|
indexes: v,
|
|
|
|
..*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-16 08:55:18 +00:00
|
|
|
/// Create a new Options object for a subquery
|
2022-02-06 21:06:52 +00:00
|
|
|
pub fn import(&self, v: bool) -> Options {
|
2022-01-13 07:29:41 +00:00
|
|
|
Options {
|
2023-06-20 22:50:26 +00:00
|
|
|
sender: self.sender.clone(),
|
2022-02-06 21:06:52 +00:00
|
|
|
auth: self.auth.clone(),
|
2023-06-20 22:50:26 +00:00
|
|
|
id: self.id.clone(),
|
2022-02-06 21:06:52 +00:00
|
|
|
ns: self.ns.clone(),
|
|
|
|
db: self.db.clone(),
|
2022-04-01 10:33:16 +00:00
|
|
|
fields: !v,
|
|
|
|
events: !v,
|
|
|
|
tables: !v,
|
2022-01-13 07:29:41 +00:00
|
|
|
..*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-16 08:55:18 +00:00
|
|
|
/// Create a new Options object for a subquery
|
2022-07-27 13:05:28 +00:00
|
|
|
pub fn strict(&self, v: bool) -> Options {
|
|
|
|
Options {
|
2023-06-20 22:50:26 +00:00
|
|
|
sender: self.sender.clone(),
|
2022-07-27 13:05:28 +00:00
|
|
|
auth: self.auth.clone(),
|
2023-06-20 22:50:26 +00:00
|
|
|
id: self.id.clone(),
|
2022-07-27 13:05:28 +00:00
|
|
|
ns: self.ns.clone(),
|
|
|
|
db: self.db.clone(),
|
|
|
|
strict: v,
|
|
|
|
..*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-16 08:55:18 +00:00
|
|
|
/// Create a new Options object for a subquery
|
2022-02-06 21:06:52 +00:00
|
|
|
pub fn futures(&self, v: bool) -> Options {
|
2022-01-13 07:29:41 +00:00
|
|
|
Options {
|
2023-06-20 22:50:26 +00:00
|
|
|
sender: self.sender.clone(),
|
2022-02-06 21:06:52 +00:00
|
|
|
auth: self.auth.clone(),
|
2023-06-20 22:50:26 +00:00
|
|
|
id: self.id.clone(),
|
2022-02-06 21:06:52 +00:00
|
|
|
ns: self.ns.clone(),
|
|
|
|
db: self.db.clone(),
|
2022-01-13 07:29:41 +00:00
|
|
|
futures: v,
|
|
|
|
..*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-20 22:50:26 +00:00
|
|
|
/// Create a new Options object for a subquery
|
|
|
|
pub fn sender(&self, v: Sender<Notification>) -> Options {
|
|
|
|
Options {
|
|
|
|
auth: self.auth.clone(),
|
|
|
|
id: self.id.clone(),
|
|
|
|
ns: self.ns.clone(),
|
|
|
|
db: self.db.clone(),
|
|
|
|
sender: v,
|
|
|
|
..*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-16 08:55:18 +00:00
|
|
|
/// Check whether realtime queries are supported
|
2022-06-27 16:01:39 +00:00
|
|
|
pub fn realtime(&self) -> Result<(), Error> {
|
|
|
|
if !self.live {
|
|
|
|
return Err(Error::RealtimeDisabled);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-09-16 08:55:18 +00:00
|
|
|
/// Check whether the authentication permissions are ok
|
2022-02-06 21:06:52 +00:00
|
|
|
pub fn check(&self, level: Level) -> Result<(), Error> {
|
2022-03-04 16:01:32 +00:00
|
|
|
if !self.auth.check(level) {
|
2022-03-06 10:58:59 +00:00
|
|
|
return Err(Error::QueryPermissions);
|
2022-02-06 21:06:52 +00:00
|
|
|
}
|
2022-07-26 09:06:33 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-09-16 08:55:18 +00:00
|
|
|
/// Check whether the necessary NS / DB options have been set
|
2022-07-26 09:06:33 +00:00
|
|
|
pub fn needs(&self, level: Level) -> Result<(), Error> {
|
|
|
|
if self.ns.is_none() && matches!(level, Level::Ns | Level::Db) {
|
2022-03-06 10:58:59 +00:00
|
|
|
return Err(Error::NsEmpty);
|
2022-02-06 21:06:52 +00:00
|
|
|
}
|
2022-07-26 09:06:33 +00:00
|
|
|
if self.db.is_none() && matches!(level, Level::Db) {
|
2022-03-06 10:58:59 +00:00
|
|
|
return Err(Error::DbEmpty);
|
2022-01-13 07:29:41 +00:00
|
|
|
}
|
2022-02-06 21:06:52 +00:00
|
|
|
Ok(())
|
2022-01-13 07:29:41 +00:00
|
|
|
}
|
|
|
|
}
|