Implement SQL Table as a newtype tuple struct

This commit is contained in:
Tobie Morgan Hitchcock 2022-05-04 22:59:37 +01:00
parent 1dd3095783
commit 3233660037
7 changed files with 37 additions and 41 deletions

View file

@ -127,8 +127,8 @@ impl Table {
chn: &Sender<(Option<Thing>, 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<Vec<u8>> = None;
loop {
if ctx.is_ok() {

View file

@ -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<Vec<u8>> = None;
loop {
if ctx.is_ok() {

View file

@ -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(),
}))
}

View file

@ -93,9 +93,7 @@ pub fn string(_: &Runtime, mut args: Vec<Value>) -> Result<Value, Error> {
}
pub fn table(_: &Runtime, mut args: Vec<Value>) -> Result<Value, Error> {
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<Value>) -> Result<Value, Error> {

View file

@ -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

View file

@ -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<String> for Table {
fn from(v: String) -> Self {
Table {
name: v,
}
Table(v)
}
}
impl From<Strand> 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")));
}
}

View file

@ -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,