From b37b027b60b2f2c905501cdc772ace8b9196de05 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Mon, 30 May 2022 16:21:38 +0100 Subject: [PATCH] Add ability to convert a record to another table --- lib/src/err/mod.rs | 18 ++++++++-------- lib/src/sql/value/def.rs | 4 ++-- lib/src/sql/value/mod.rs | 1 + lib/src/sql/value/retable.rs | 40 ++++++++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 lib/src/sql/value/retable.rs diff --git a/lib/src/err/mod.rs b/lib/src/err/mod.rs index 8464001e..3c0535df 100644 --- a/lib/src/err/mod.rs +++ b/lib/src/err/mod.rs @@ -1,6 +1,4 @@ use crate::sql::idiom::Idiom; -use crate::sql::thing::Thing; -use crate::sql::value::Value; use msgpack::encode::Error as SerdeError; use serde::Serialize; use storekey::decode::Error as DecodeError; @@ -205,6 +203,12 @@ pub enum Error { check: String, }, + /// Found a record id for the record but this is not a valid id + #[error("Found '{value}' for the record ID but this is not a valid id")] + IdInvalid { + value: String, + }, + /// There was an error processing a value in parallel #[error("There was an error processing a value in parallel")] Channel(String), @@ -255,14 +259,8 @@ impl From for Error { } } -impl From>> for Error { - fn from(e: channel::SendError>) -> Error { - Error::Channel(e.to_string()) - } -} - -impl From, Value)>> for Error { - fn from(e: channel::SendError<(Option, Value)>) -> Error { +impl From> for Error { + fn from(e: channel::SendError) -> Error { Error::Channel(e.to_string()) } } diff --git a/lib/src/sql/value/def.rs b/lib/src/sql/value/def.rs index 8e8c111e..5f195c8f 100644 --- a/lib/src/sql/value/def.rs +++ b/lib/src/sql/value/def.rs @@ -7,7 +7,7 @@ use crate::sql::thing::Thing; use crate::sql::value::Value; use once_cell::sync::Lazy; -static RID: Lazy<[Part; 1]> = Lazy::new(|| [Part::from("id")]); +static ID: Lazy<[Part; 1]> = Lazy::new(|| [Part::from("id")]); impl Value { pub async fn def( @@ -17,6 +17,6 @@ impl Value { txn: &Transaction, val: &Thing, ) -> Result<(), Error> { - self.set(ctx, opt, txn, RID.as_ref(), val.clone().into()).await + self.set(ctx, opt, txn, ID.as_ref(), val.clone().into()).await } } diff --git a/lib/src/sql/value/mod.rs b/lib/src/sql/value/mod.rs index 7e5b391b..02371ce1 100644 --- a/lib/src/sql/value/mod.rs +++ b/lib/src/sql/value/mod.rs @@ -22,5 +22,6 @@ mod object; mod patch; mod pick; mod replace; +mod retable; mod set; mod single; diff --git a/lib/src/sql/value/retable.rs b/lib/src/sql/value/retable.rs new file mode 100644 index 00000000..dd653cca --- /dev/null +++ b/lib/src/sql/value/retable.rs @@ -0,0 +1,40 @@ +use crate::err::Error; +use crate::sql::id::Id; +use crate::sql::part::Part; +use crate::sql::table::Table; +use crate::sql::thing::Thing; +use crate::sql::value::Value; +use once_cell::sync::Lazy; + +static ID: Lazy<[Part; 1]> = Lazy::new(|| [Part::from("id")]); + +impl Value { + pub fn retable(&self, val: &Table) -> Result { + // Fetch the id from the document + let id = match self.pick(&*ID) { + Value::Strand(id) => Thing { + tb: val.to_string(), + id: Id::String(id.0), + }, + Value::Number(id) => Thing { + tb: val.to_string(), + id: Id::Number(id), + }, + Value::Thing(id) => Thing { + tb: val.to_string(), + id: id.id, + }, + Value::None => Thing { + tb: val.to_string(), + id: Id::rand(), + }, + id => { + return Err(Error::IdInvalid { + value: id.to_string(), + }) + } + }; + // Return the record id + Ok(id) + } +}