Merge pull request #251 from liangxianzhe/feat_into_payload

NamedRow into Payload.
main
Ziyang Hu 6 months ago committed by GitHub
commit 1eb588fccf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -75,6 +75,7 @@ pub use crate::runtime::db::evaluate_expressions;
pub use crate::runtime::db::get_variables; pub use crate::runtime::db::get_variables;
pub use crate::runtime::db::Poison; pub use crate::runtime::db::Poison;
pub use crate::runtime::db::ScriptMutability; pub use crate::runtime::db::ScriptMutability;
pub use crate::runtime::db::Payload;
pub use crate::runtime::db::TransactionPayload; pub use crate::runtime::db::TransactionPayload;
pub(crate) mod data; pub(crate) mod data;

@ -231,11 +231,23 @@ impl NamedRows {
next: None, next: None,
}) })
} }
/// Create a query and parameters to apply an operation (insert, put, delete, rm) to a stored
/// relation with the named rows.
pub fn into_payload(self, relation: &str, op: &str) -> Payload {
let cols_str = self.headers.join(", ");
let query = format!("?[{cols_str}] <- $data :{op} {relation} {{ {cols_str} }}");
let data = DataValue::List(self.rows.into_iter().map(|r| DataValue::List(r)).collect());
(query, [("data".to_string(), data)].into())
}
} }
const STATUS_STR: &str = "status"; const STATUS_STR: &str = "status";
const OK_STR: &str = "OK"; const OK_STR: &str = "OK";
/// The query and parameters.
pub type Payload = (String, BTreeMap<String, DataValue>);
/// Commands to be sent to a multi-transaction /// Commands to be sent to a multi-transaction
#[derive(Eq, PartialEq, Debug)] #[derive(Eq, PartialEq, Debug)]
pub enum TransactionPayload { pub enum TransactionPayload {
@ -244,7 +256,7 @@ pub enum TransactionPayload {
/// Abort the current transaction /// Abort the current transaction
Abort, Abort,
/// Run a query inside the transaction /// Run a query inside the transaction
Query((String, BTreeMap<String, DataValue>)), Query(Payload),
} }
impl<'s, S: Storage<'s>> Db<S> { impl<'s, S: Storage<'s>> Db<S> {
@ -397,6 +409,7 @@ impl<'s, S: Storage<'s>> Db<S> {
mutability == ScriptMutability::Immutable, mutability == ScriptMutability::Immutable,
) )
} }
/// Run the CozoScript passed in. The `params` argument is a map of parameters. /// Run the CozoScript passed in. The `params` argument is a map of parameters.
pub fn run_script_read_only( pub fn run_script_read_only(
&'s self, &'s self,

@ -1183,6 +1183,26 @@ fn deletion() {
db.run_default(r"?[x] <- [[1]] :delete a {x}").unwrap(); db.run_default(r"?[x] <- [[1]] :delete a {x}").unwrap();
} }
#[test]
fn into_payload() {
let db = DbInstance::new("mem", "", "").unwrap();
db.run_default(r":create a {x => y}").unwrap();
db.run_default(r"?[x, y] <- [[1, 2], [3, 4]] :insert a {x => y}",).unwrap();
let mut res = db.run_default(r"?[x, y] := *a[x, y]").unwrap();
assert_eq!(res.rows.len(), 2);
let delete = res.clone().into_payload("a", "rm");
db.run_script(delete.0.as_str(), delete.1, ScriptMutability::Mutable).unwrap();
assert_eq!(db.run_default(r"?[x, y] := *a[x, y]").unwrap().rows.len(), 0);
db.run_default(r":create b {m => n}").unwrap();
res.headers = vec!["m".into(), "n".into()];
let put = res.into_payload("b", "put");
db.run_script(put.0.as_str(), put.1, ScriptMutability::Mutable).unwrap();
assert_eq!(db.run_default(r"?[m, n] := *b[m, n]").unwrap().rows.len(), 2);
}
#[test] #[test]
fn returning() { fn returning() {
let db = DbInstance::new("mem", "", "").unwrap(); let db = DbInstance::new("mem", "", "").unwrap();
@ -1553,4 +1573,4 @@ fn fts_drop() {
db.run_default(r#" db.run_default(r#"
::fts drop entity:fts_index ::fts drop entity:fts_index
"#).unwrap(); "#).unwrap();
} }

Loading…
Cancel
Save