Support inspecting models with BQL

next
Sayan Nandan 2 years ago
parent 56041116de
commit 7a2c3e42cf
No known key found for this signature in database
GPG Key ID: 8BC07A0A4D41DD52

@ -54,7 +54,7 @@ pub enum Statement {
/// Inspect the given space
InspectSpace(Option<RawSlice>),
/// Inspect the given model
InspectModel(Entity),
InspectModel(Option<Entity>),
/// Inspect all the spaces in the database
InspectSpaces,
}
@ -281,7 +281,13 @@ impl<'a> Compiler<'a> {
#[inline(always)]
/// Parse `inspect model <model>`
fn parse_inspect_model(&mut self) -> LangResult<Statement> {
Ok(Statement::InspectModel(self.parse_entity_name()?))
match self.next() {
Some(Token::Identifier(ident)) => Ok(Statement::InspectModel(Some(
self.parse_entity_name_with_start(ident)?,
))),
Some(_) => Err(LangError::InvalidSyntax),
None => Ok(Statement::InspectModel(None)),
}
}
#[inline(always)]
/// Parse `inspect space <space>`

@ -82,7 +82,11 @@ where
.await?;
Ok(())
}
_ => todo!(),
Statement::InspectModel(model) => {
con.write_string(&handle.describe_table::<P>(model.as_ref().map(|v| v.into()))?)
.await?;
Ok(())
}
};
actions::translate_ddl_error::<P, ()>(result)?;
con._write_raw(P::RCODE_OKAY).await?;

@ -199,7 +199,7 @@ mod ast {
fn stmt_inspect_model() {
assert_eq!(
Compiler::compile(b"inspect model twitter.tweet").unwrap(),
Statement::InspectModel(Entity::Full("twitter".into(), "tweet".into()))
Statement::InspectModel(Some(Entity::Full("twitter".into(), "tweet".into())))
);
}
#[test]

@ -178,6 +178,13 @@ impl Corestore {
_ => Err(DdlError::DefaultNotFound),
}
}
/// Returns the current table, if set
pub fn get_ctable_result(&self) -> KeyspaceResult<&Table> {
match self.estate.table {
Some((_, ref tbl)) => Ok(tbl),
_ => Err(DdlError::DefaultNotFound),
}
}
pub fn get_keyspace<Q>(&self, ksid: &Q) -> Option<Arc<Keyspace>>
where
ObjectID: Borrow<Q>,
@ -207,12 +214,6 @@ impl Corestore {
pub fn get_ctable(&self) -> Option<Arc<Table>> {
self.estate.table.as_ref().map(|(_, tbl)| tbl.clone())
}
pub fn get_table_result(&self) -> KeyspaceResult<&Table> {
match self.estate.table {
Some((_, ref table)) => Ok(table),
_ => Err(DdlError::DefaultNotFound),
}
}
pub fn get_ctable_ref(&self) -> Option<&Table> {
self.estate.table.as_ref().map(|(_, tbl)| tbl.as_ref())
}
@ -355,4 +356,11 @@ impl Corestore {
}
})
}
pub fn describe_table<P: ProtocolSpec>(&self, table: Option<Entity>) -> ActionResult<String> {
let r = match table {
Some(tbl) => translate_ddl_error::<P, Arc<Table>>(self.get_table(tbl))?.describe_self(),
None => translate_ddl_error::<P, &Table>(self.get_ctable_result())?.describe_self(),
};
Ok(r.to_owned())
}
}

@ -25,8 +25,11 @@
*/
use {
super::ddl::{KEYSPACE, TABLE},
crate::{corestore::table::Table, dbnet::connection::prelude::*},
super::{
ddl::{KEYSPACE, TABLE},
parser::Entity,
},
crate::dbnet::connection::prelude::*,
};
const KEYSPACES: &[u8] = "KEYSPACES".as_bytes();
@ -70,17 +73,11 @@ action! {
/// INSPECT a table. This should only have the table ID
fn inspect_table(handle: &Corestore, con: &'a mut T, mut act: ActionIter<'a>) {
ensure_length::<P>(act.len(), |len| len < 2)?;
match act.next() {
Some(entity) => {
let entity = handle_entity!(con, entity);
con.write_string(get_tbl!(entity, handle, con).describe_self()).await?;
},
None => {
// inspect the current table
let tbl = translate_ddl_error::<P, &Table>(handle.get_table_result())?;
con.write_string(tbl.describe_self()).await?;
},
}
let maybe_entity = match act.next() {
Some(e) => Some(Entity::from_slice::<P>(e)?),
None => None,
};
con.write_string(&handle.describe_table::<P>(maybe_entity)?).await?;
Ok(())
}
}

Loading…
Cancel
Save