Implement SQL Ident as a newtype tuple struct

This commit is contained in:
Tobie Morgan Hitchcock 2022-05-05 01:27:57 +01:00
parent 3233660037
commit ed92fb4d85
9 changed files with 25 additions and 25 deletions

View file

@ -142,7 +142,7 @@ impl<'a> Executor<'a> {
// Allowed to run? // Allowed to run?
opt.check(Level::Db)?; opt.check(Level::Db)?;
// Process the option // Process the option
match &stm.name.name.to_uppercase()[..] { match &stm.name.to_uppercase()[..] {
"FIELDS" => opt = opt.fields(stm.what), "FIELDS" => opt = opt.fields(stm.what),
"EVENTS" => opt = opt.events(stm.what), "EVENTS" => opt = opt.events(stm.what),
"TABLES" => opt = opt.tables(stm.what), "TABLES" => opt = opt.tables(stm.what),

View file

@ -8,40 +8,40 @@ use nom::character::complete::char;
use nom::sequence::delimited; use nom::sequence::delimited;
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, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Ident { pub struct Ident(pub String);
pub name: String,
}
impl From<String> for Ident { impl From<String> for Ident {
fn from(s: String) -> Self { fn from(s: String) -> Self {
Ident { Ident(s)
name: s,
}
} }
} }
impl<'a> From<&'a str> for Ident { impl<'a> From<&'a str> for Ident {
fn from(i: &str) -> Ident { fn from(i: &str) -> Ident {
Ident { Ident(String::from(i))
name: String::from(i),
}
} }
} }
impl<'a> From<&'a String> for Ident { impl<'a> From<&'a String> for Ident {
fn from(i: &String) -> Ident { fn from(i: &String) -> Ident {
Ident { Ident(String::from(i))
name: String::from(i), }
} }
impl Deref for Ident {
type Target = String;
fn deref(&self) -> &Self::Target {
&self.0
} }
} }
impl fmt::Display for Ident { impl fmt::Display for Ident {
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, "`"))
} }
} }

View file

@ -41,7 +41,7 @@ impl Param {
// Find a base variable by name // Find a base variable by name
match self.parts.first() { match self.parts.first() {
// The first part will be a field // The first part will be a field
Some(Part::Field(v)) => match &v.name[..] { Some(Part::Field(v)) => match v.as_str() {
"this" => match doc { "this" => match doc {
// The base document exists // The base document exists
Some(v) => { Some(v) => {
@ -55,7 +55,7 @@ impl Param {
// The base document does not exist // The base document does not exist
None => Ok(Value::None), None => Ok(Value::None),
}, },
_ => match ctx.value::<Value>(&v.name) { _ => match ctx.value::<Value>(v) {
// The base variable exists // The base variable exists
Some(v) => { Some(v) => {
// Get the path parts // Get the path parts

View file

@ -16,7 +16,7 @@ impl Value {
Some(p) => match (self, other) { Some(p) => match (self, other) {
// Current path part is an object // Current path part is an object
(Value::Object(a), Value::Object(b)) => match p { (Value::Object(a), Value::Object(b)) => match p {
Part::Field(f) => match (a.get(&f.name), b.get(&f.name)) { Part::Field(f) => match (a.get(f as &str), b.get(f as &str)) {
(Some(a), Some(b)) => a.compare(b, path.next(), collate, numeric), (Some(a), Some(b)) => a.compare(b, path.next(), collate, numeric),
(Some(_), None) => Some(Ordering::Greater), (Some(_), None) => Some(Ordering::Greater),
(None, Some(_)) => Some(Ordering::Less), (None, Some(_)) => Some(Ordering::Less),

View file

@ -27,10 +27,10 @@ impl Value {
Value::Object(v) => match p { Value::Object(v) => match p {
Part::Field(f) => match path.len() { Part::Field(f) => match path.len() {
1 => { 1 => {
v.remove(&f.name); v.remove(f as &str);
Ok(()) Ok(())
} }
_ => match v.get_mut(&f.name) { _ => match v.get_mut(f as &str) {
Some(v) if v.is_some() => v.del(ctx, opt, txn, path.next()).await, Some(v) if v.is_some() => v.del(ctx, opt, txn, path.next()).await,
_ => Ok(()), _ => Ok(()),
}, },

View file

@ -13,7 +13,7 @@ impl Value {
Some(p) => match self { Some(p) => match self {
// Current path part is an object // Current path part is an object
Value::Object(v) => match p { Value::Object(v) => match p {
Part::Field(f) => match v.get(&f.name) { Part::Field(f) => match v.get(f as &str) {
Some(v) => v._each(path.next(), prev.push(p.clone())), Some(v) => v._each(path.next(), prev.push(p.clone())),
None => vec![], None => vec![],
}, },

View file

@ -26,7 +26,7 @@ impl Value {
Some(p) => match self { Some(p) => match self {
// Current path part is an object // Current path part is an object
Value::Object(v) => match p { Value::Object(v) => match p {
Part::Field(f) => match v.get(&f.name) { Part::Field(f) => match v.get(f as &str) {
Some(v) => v.get(ctx, opt, txn, path.next()).await, Some(v) => v.get(ctx, opt, txn, path.next()).await,
None => Ok(Value::None), None => Ok(Value::None),
}, },

View file

@ -9,7 +9,7 @@ impl Value {
Some(p) => match self { Some(p) => match self {
// Current path part is an object // Current path part is an object
Value::Object(v) => match p { Value::Object(v) => match p {
Part::Field(f) => match v.get(&f.name) { Part::Field(f) => match v.get(f as &str) {
Some(v) => v.pick(path.next()), Some(v) => v.pick(path.next()),
None => Value::None, None => Value::None,
}, },

View file

@ -24,12 +24,12 @@ impl Value {
Some(p) => match self { Some(p) => match self {
// Current path part is an object // Current path part is an object
Value::Object(v) => match p { Value::Object(v) => match p {
Part::Field(f) => match v.get_mut(&f.name) { Part::Field(f) => match v.get_mut(f as &str) {
Some(v) if v.is_some() => v.set(ctx, opt, txn, path.next(), val).await, Some(v) if v.is_some() => v.set(ctx, opt, txn, path.next(), val).await,
_ => { _ => {
let mut obj = Value::base(); let mut obj = Value::base();
obj.set(ctx, opt, txn, path.next(), val).await?; obj.set(ctx, opt, txn, path.next(), val).await?;
v.insert(f.name.to_owned(), obj); v.insert(f.to_string(), obj);
Ok(()) Ok(())
} }
}, },