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;
|
2022-01-13 07:29:41 +00:00
|
|
|
use crate::err::Error;
|
2022-02-06 21:06:52 +00:00
|
|
|
use std::sync::Arc;
|
2022-01-13 07:29:41 +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).
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
2022-02-06 21:06:52 +00:00
|
|
|
pub struct Options {
|
|
|
|
// Currently selected NS
|
|
|
|
pub ns: Option<Arc<String>>,
|
|
|
|
// Currently selected DB
|
|
|
|
pub db: Option<Arc<String>>,
|
|
|
|
// Connection authentication data
|
|
|
|
pub auth: Arc<Auth>,
|
|
|
|
// How many subqueries have we gone into?
|
|
|
|
pub dive: usize,
|
|
|
|
// Should we debug query response SQL?
|
|
|
|
pub debug: bool,
|
|
|
|
// Should we force tables/events to re-run?
|
|
|
|
pub force: bool,
|
|
|
|
// Should we process field queries?
|
|
|
|
pub fields: bool,
|
|
|
|
// Should we process event queries?
|
|
|
|
pub events: bool,
|
|
|
|
// Should we process table queries?
|
|
|
|
pub tables: bool,
|
|
|
|
// Should we process function futures?
|
|
|
|
pub futures: bool,
|
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 {
|
2022-02-06 21:06:52 +00:00
|
|
|
Options::new(Auth::No)
|
2022-01-13 07:29:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-06 21:06:52 +00:00
|
|
|
impl Options {
|
2022-01-13 07:29:41 +00:00
|
|
|
// Create a new Options object
|
2022-02-06 21:06:52 +00:00
|
|
|
pub fn new(auth: Auth) -> Options {
|
2022-01-13 07:29:41 +00:00
|
|
|
Options {
|
2022-02-06 21:06:52 +00:00
|
|
|
ns: None,
|
|
|
|
db: None,
|
2022-01-13 07:29:41 +00:00
|
|
|
dive: 0,
|
2022-01-27 08:21:04 +00:00
|
|
|
debug: false,
|
2022-01-13 07:29:41 +00:00
|
|
|
force: false,
|
|
|
|
fields: true,
|
|
|
|
events: true,
|
|
|
|
tables: true,
|
|
|
|
futures: false,
|
2022-02-06 21:06:52 +00:00
|
|
|
auth: Arc::new(auth),
|
2022-01-13 07:29:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-06 21:06:52 +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()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-01-13 07:29:41 +00:00
|
|
|
// Create a new Options object for a subquery
|
2022-02-06 21:06:52 +00:00
|
|
|
pub fn dive(&self) -> Result<Options, Error> {
|
2022-01-13 07:29:41 +00:00
|
|
|
if self.dive < cnf::MAX_RECURSIVE_QUERIES {
|
|
|
|
Ok(Options {
|
2022-02-06 21:06:52 +00:00
|
|
|
auth: self.auth.clone(),
|
|
|
|
ns: self.ns.clone(),
|
|
|
|
db: self.db.clone(),
|
2022-01-13 07:29:41 +00:00
|
|
|
dive: self.dive + 1,
|
|
|
|
..*self
|
|
|
|
})
|
|
|
|
} else {
|
2022-03-06 10:58:59 +00:00
|
|
|
Err(Error::TooManySubqueries {
|
2022-01-13 07:29:41 +00:00
|
|
|
limit: self.dive,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new Options object for a subquery
|
2022-02-06 21:06:52 +00:00
|
|
|
pub fn debug(&self, v: bool) -> Options {
|
2022-01-27 08:21:04 +00:00
|
|
|
Options {
|
2022-02-06 21:06:52 +00:00
|
|
|
auth: self.auth.clone(),
|
|
|
|
ns: self.ns.clone(),
|
|
|
|
db: self.db.clone(),
|
2022-01-27 08:21:04 +00:00
|
|
|
debug: v,
|
|
|
|
..*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2022-02-06 21:06:52 +00:00
|
|
|
auth: self.auth.clone(),
|
|
|
|
ns: self.ns.clone(),
|
|
|
|
db: self.db.clone(),
|
2022-01-13 07:29:41 +00:00
|
|
|
force: v,
|
|
|
|
..*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2022-02-06 21:06:52 +00:00
|
|
|
auth: self.auth.clone(),
|
|
|
|
ns: self.ns.clone(),
|
|
|
|
db: self.db.clone(),
|
2022-01-13 07:29:41 +00:00
|
|
|
fields: v,
|
|
|
|
..*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2022-02-06 21:06:52 +00:00
|
|
|
auth: self.auth.clone(),
|
|
|
|
ns: self.ns.clone(),
|
|
|
|
db: self.db.clone(),
|
2022-01-13 07:29:41 +00:00
|
|
|
events: v,
|
|
|
|
..*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2022-02-06 21:06:52 +00:00
|
|
|
auth: self.auth.clone(),
|
|
|
|
ns: self.ns.clone(),
|
|
|
|
db: self.db.clone(),
|
2022-01-13 07:29:41 +00:00
|
|
|
tables: v,
|
|
|
|
..*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2022-02-06 21:06:52 +00:00
|
|
|
auth: self.auth.clone(),
|
|
|
|
ns: self.ns.clone(),
|
|
|
|
db: self.db.clone(),
|
2022-01-13 07:29:41 +00:00
|
|
|
fields: v,
|
|
|
|
events: v,
|
|
|
|
tables: v,
|
|
|
|
..*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2022-02-06 21:06:52 +00:00
|
|
|
auth: self.auth.clone(),
|
|
|
|
ns: self.ns.clone(),
|
|
|
|
db: self.db.clone(),
|
2022-01-13 07:29:41 +00:00
|
|
|
futures: v,
|
|
|
|
..*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-06 21:06:52 +00:00
|
|
|
// Check whether the authentication permissions are ok
|
|
|
|
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
|
|
|
}
|
|
|
|
if self.ns.is_none() {
|
2022-03-06 10:58:59 +00:00
|
|
|
return Err(Error::NsEmpty);
|
2022-02-06 21:06:52 +00:00
|
|
|
}
|
|
|
|
if self.db.is_none() {
|
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
|
|
|
}
|
|
|
|
}
|