Implement SQL Table as a newtype tuple struct
This commit is contained in:
parent
1dd3095783
commit
3233660037
7 changed files with 37 additions and 41 deletions
|
@ -127,8 +127,8 @@ impl Table {
|
||||||
chn: &Sender<(Option<Thing>, Value)>,
|
chn: &Sender<(Option<Thing>, Value)>,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
if ctx.is_ok() {
|
if ctx.is_ok() {
|
||||||
let beg = thing::prefix(opt.ns(), opt.db(), &self.name);
|
let beg = thing::prefix(opt.ns(), opt.db(), &self);
|
||||||
let end = thing::suffix(opt.ns(), opt.db(), &self.name);
|
let end = thing::suffix(opt.ns(), opt.db(), &self);
|
||||||
let mut nxt: Option<Vec<u8>> = None;
|
let mut nxt: Option<Vec<u8>> = None;
|
||||||
loop {
|
loop {
|
||||||
if ctx.is_ok() {
|
if ctx.is_ok() {
|
||||||
|
|
|
@ -124,8 +124,8 @@ impl Table {
|
||||||
ite: &mut Iterator,
|
ite: &mut Iterator,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
if ctx.is_ok() {
|
if ctx.is_ok() {
|
||||||
let beg = thing::prefix(opt.ns(), opt.db(), &self.name);
|
let beg = thing::prefix(opt.ns(), opt.db(), &self);
|
||||||
let end = thing::suffix(opt.ns(), opt.db(), &self.name);
|
let end = thing::suffix(opt.ns(), opt.db(), &self);
|
||||||
let mut nxt: Option<Vec<u8>> = None;
|
let mut nxt: Option<Vec<u8>> = None;
|
||||||
loop {
|
loop {
|
||||||
if ctx.is_ok() {
|
if ctx.is_ok() {
|
||||||
|
|
|
@ -110,7 +110,7 @@ impl Iterator {
|
||||||
// Create a new record for processing
|
// Create a new record for processing
|
||||||
pub fn produce(&mut self, val: Table) {
|
pub fn produce(&mut self, val: Table) {
|
||||||
self.prepare(Value::Thing(Thing {
|
self.prepare(Value::Thing(Thing {
|
||||||
tb: val.name,
|
tb: val.0,
|
||||||
id: Id::rand(),
|
id: Id::rand(),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
|
@ -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> {
|
pub fn table(_: &Runtime, mut args: Vec<Value>) -> Result<Value, Error> {
|
||||||
Ok(Value::Table(Table {
|
Ok(Value::Table(Table(args.remove(0).as_string())))
|
||||||
name: args.remove(0).as_strand().value,
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn thing(_: &Runtime, mut args: Vec<Value>) -> Result<Value, Error> {
|
pub fn thing(_: &Runtime, mut args: Vec<Value>) -> Result<Value, Error> {
|
||||||
|
|
|
@ -586,7 +586,7 @@ impl DefineTableStatement {
|
||||||
// Process each foreign table
|
// Process each foreign table
|
||||||
for v in view.what.0.iter() {
|
for v in view.what.0.iter() {
|
||||||
// Save the view config
|
// 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?;
|
run.set(key, self).await?;
|
||||||
}
|
}
|
||||||
// Release the transaction
|
// Release the transaction
|
||||||
|
|
|
@ -3,9 +3,11 @@ use crate::sql::common::escape;
|
||||||
use crate::sql::common::val_char;
|
use crate::sql::common::val_char;
|
||||||
use crate::sql::error::IResult;
|
use crate::sql::error::IResult;
|
||||||
use crate::sql::ident::ident_raw;
|
use crate::sql::ident::ident_raw;
|
||||||
|
use crate::sql::strand::Strand;
|
||||||
use nom::multi::separated_list1;
|
use nom::multi::separated_list1;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use std::ops::Deref;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
|
#[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)]
|
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
|
||||||
pub struct Table {
|
pub struct Table(pub String);
|
||||||
pub name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<String> for Table {
|
impl From<String> for Table {
|
||||||
fn from(v: String) -> Self {
|
fn from(v: String) -> Self {
|
||||||
Table {
|
Table(v)
|
||||||
name: 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 {
|
impl fmt::Display for Table {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
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> {
|
pub fn table(i: &str) -> IResult<&str, Table> {
|
||||||
let (i, v) = ident_raw(i)?;
|
let (i, v) = ident_raw(i)?;
|
||||||
Ok((
|
Ok((i, Table(v)))
|
||||||
i,
|
|
||||||
Table {
|
|
||||||
name: v,
|
|
||||||
},
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -63,12 +69,7 @@ mod tests {
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
let out = res.unwrap().1;
|
let out = res.unwrap().1;
|
||||||
assert_eq!("test", format!("{}", out));
|
assert_eq!("test", format!("{}", out));
|
||||||
assert_eq!(
|
assert_eq!(out, Table(String::from("test")));
|
||||||
out,
|
|
||||||
Table {
|
|
||||||
name: String::from("test"),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -78,12 +79,7 @@ mod tests {
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
let out = res.unwrap().1;
|
let out = res.unwrap().1;
|
||||||
assert_eq!("test", format!("{}", out));
|
assert_eq!("test", format!("{}", out));
|
||||||
assert_eq!(
|
assert_eq!(out, Table(String::from("test")));
|
||||||
out,
|
|
||||||
Table {
|
|
||||||
name: String::from("test"),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -93,11 +89,6 @@ mod tests {
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
let out = res.unwrap().1;
|
let out = res.unwrap().1;
|
||||||
assert_eq!("test", format!("{}", out));
|
assert_eq!("test", format!("{}", out));
|
||||||
assert_eq!(
|
assert_eq!(out, Table(String::from("test")));
|
||||||
out,
|
|
||||||
Table {
|
|
||||||
name: String::from("test"),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -510,7 +510,7 @@ impl Value {
|
||||||
|
|
||||||
pub fn is_type_record(&self, types: &[Table]) -> bool {
|
pub fn is_type_record(&self, types: &[Table]) -> bool {
|
||||||
match self {
|
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,
|
_ => 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 {
|
pub fn as_strand(self) -> Strand {
|
||||||
match self {
|
match self {
|
||||||
Value::Strand(v) => v,
|
Value::Strand(v) => v,
|
||||||
|
|
Loading…
Reference in a new issue