From 3233660037889414dd883a8477b446d9004d854b Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Wed, 4 May 2022 22:59:37 +0100 Subject: [PATCH] Implement SQL Table as a newtype tuple struct --- lib/src/dbs/channel.rs | 4 +-- lib/src/dbs/iterate.rs | 4 +-- lib/src/dbs/iterator.rs | 2 +- lib/src/fnc/type.rs | 4 +-- lib/src/sql/statements/define.rs | 2 +- lib/src/sql/table.rs | 53 +++++++++++++------------------- lib/src/sql/value/value.rs | 9 +++++- 7 files changed, 37 insertions(+), 41 deletions(-) diff --git a/lib/src/dbs/channel.rs b/lib/src/dbs/channel.rs index 2088246d..38c00673 100644 --- a/lib/src/dbs/channel.rs +++ b/lib/src/dbs/channel.rs @@ -127,8 +127,8 @@ impl Table { chn: &Sender<(Option, Value)>, ) -> Result<(), Error> { if ctx.is_ok() { - let beg = thing::prefix(opt.ns(), opt.db(), &self.name); - let end = thing::suffix(opt.ns(), opt.db(), &self.name); + let beg = thing::prefix(opt.ns(), opt.db(), &self); + let end = thing::suffix(opt.ns(), opt.db(), &self); let mut nxt: Option> = None; loop { if ctx.is_ok() { diff --git a/lib/src/dbs/iterate.rs b/lib/src/dbs/iterate.rs index 602ff0f4..58a2c9eb 100644 --- a/lib/src/dbs/iterate.rs +++ b/lib/src/dbs/iterate.rs @@ -124,8 +124,8 @@ impl Table { ite: &mut Iterator, ) -> Result<(), Error> { if ctx.is_ok() { - let beg = thing::prefix(opt.ns(), opt.db(), &self.name); - let end = thing::suffix(opt.ns(), opt.db(), &self.name); + let beg = thing::prefix(opt.ns(), opt.db(), &self); + let end = thing::suffix(opt.ns(), opt.db(), &self); let mut nxt: Option> = None; loop { if ctx.is_ok() { diff --git a/lib/src/dbs/iterator.rs b/lib/src/dbs/iterator.rs index be4b0117..b3d0cb40 100644 --- a/lib/src/dbs/iterator.rs +++ b/lib/src/dbs/iterator.rs @@ -110,7 +110,7 @@ impl Iterator { // Create a new record for processing pub fn produce(&mut self, val: Table) { self.prepare(Value::Thing(Thing { - tb: val.name, + tb: val.0, id: Id::rand(), })) } diff --git a/lib/src/fnc/type.rs b/lib/src/fnc/type.rs index 87f0283b..27caca87 100644 --- a/lib/src/fnc/type.rs +++ b/lib/src/fnc/type.rs @@ -93,9 +93,7 @@ pub fn string(_: &Runtime, mut args: Vec) -> Result { } pub fn table(_: &Runtime, mut args: Vec) -> Result { - Ok(Value::Table(Table { - name: args.remove(0).as_strand().value, - })) + Ok(Value::Table(Table(args.remove(0).as_string()))) } pub fn thing(_: &Runtime, mut args: Vec) -> Result { diff --git a/lib/src/sql/statements/define.rs b/lib/src/sql/statements/define.rs index 731c0a2f..3aada534 100644 --- a/lib/src/sql/statements/define.rs +++ b/lib/src/sql/statements/define.rs @@ -586,7 +586,7 @@ impl DefineTableStatement { // Process each foreign table for v in view.what.0.iter() { // Save the view config - let key = crate::key::ft::new(opt.ns(), opt.db(), &v.name, &self.name); + let key = crate::key::ft::new(opt.ns(), opt.db(), v, &self.name); run.set(key, self).await?; } // Release the transaction diff --git a/lib/src/sql/table.rs b/lib/src/sql/table.rs index 94facd1b..02dedd3c 100644 --- a/lib/src/sql/table.rs +++ b/lib/src/sql/table.rs @@ -3,9 +3,11 @@ use crate::sql::common::escape; use crate::sql::common::val_char; use crate::sql::error::IResult; use crate::sql::ident::ident_raw; +use crate::sql::strand::Strand; use nom::multi::separated_list1; use serde::{Deserialize, Serialize}; use std::fmt; +use std::ops::Deref; use std::str; #[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)] @@ -23,32 +25,36 @@ pub fn tables(i: &str) -> IResult<&str, Tables> { } #[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)] -pub struct Table { - pub name: String, -} +pub struct Table(pub String); impl From for Table { fn from(v: String) -> Self { - Table { - name: v, - } + Table(v) + } +} + +impl From for Table { + fn from(v: Strand) -> Self { + Table(v.value) + } +} + +impl Deref for Table { + type Target = String; + fn deref(&self) -> &Self::Target { + &self.0 } } impl fmt::Display for Table { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", escape(&self.name, &val_char, "`")) + write!(f, "{}", escape(&self.0, &val_char, "`")) } } pub fn table(i: &str) -> IResult<&str, Table> { let (i, v) = ident_raw(i)?; - Ok(( - i, - Table { - name: v, - }, - )) + Ok((i, Table(v))) } #[cfg(test)] @@ -63,12 +69,7 @@ mod tests { assert!(res.is_ok()); let out = res.unwrap().1; assert_eq!("test", format!("{}", out)); - assert_eq!( - out, - Table { - name: String::from("test"), - } - ); + assert_eq!(out, Table(String::from("test"))); } #[test] @@ -78,12 +79,7 @@ mod tests { assert!(res.is_ok()); let out = res.unwrap().1; assert_eq!("test", format!("{}", out)); - assert_eq!( - out, - Table { - name: String::from("test"), - } - ); + assert_eq!(out, Table(String::from("test"))); } #[test] @@ -93,11 +89,6 @@ mod tests { assert!(res.is_ok()); let out = res.unwrap().1; assert_eq!("test", format!("{}", out)); - assert_eq!( - out, - Table { - name: String::from("test"), - } - ); + assert_eq!(out, Table(String::from("test"))); } } diff --git a/lib/src/sql/value/value.rs b/lib/src/sql/value/value.rs index 14d1047e..f68896dc 100644 --- a/lib/src/sql/value/value.rs +++ b/lib/src/sql/value/value.rs @@ -510,7 +510,7 @@ impl Value { pub fn is_type_record(&self, types: &[Table]) -> bool { match self { - Value::Thing(v) => types.iter().any(|t| t.name == v.tb), + Value::Thing(v) => types.iter().any(|tb| tb.0 == v.tb), _ => false, } } @@ -590,6 +590,13 @@ impl Value { } } + pub fn as_string(self) -> String { + match self { + Value::Strand(v) => v.value, + _ => self.to_string(), + } + } + pub fn as_strand(self) -> Strand { match self { Value::Strand(v) => v,