Basic cozodb example
This commit is contained in:
commit
42a4468a23
5 changed files with 2045 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/target
|
1943
Cargo.lock
generated
Normal file
1943
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "cozodb-example"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
authors = [ "Philip (a-0) <@ph:a-0.me>" ]
|
||||
|
||||
[dependencies]
|
||||
cozo = "0.7.5"
|
7
LICENSE
Normal file
7
LICENSE
Normal file
|
@ -0,0 +1,7 @@
|
|||
Copyright © 2023 Philip (a-0)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
86
src/main.rs
Normal file
86
src/main.rs
Normal file
|
@ -0,0 +1,86 @@
|
|||
use std::collections::BTreeMap;
|
||||
|
||||
use cozo::{DbInstance, ScriptMutability, DataValue};
|
||||
|
||||
|
||||
|
||||
fn main() {
|
||||
// Initializing a database
|
||||
let db: DbInstance = DbInstance::new(
|
||||
"mem", // db engine (here: in-memory)
|
||||
"", // path: relevant for persisting the database
|
||||
Default::default() // options
|
||||
).expect("Creating in-memory database failed.");
|
||||
|
||||
// relations can be compared to "tables" in normal SQL
|
||||
add_relation(&db);
|
||||
|
||||
// Write some data
|
||||
insert_row(&db);
|
||||
|
||||
// Attempt to query for this data (and print result to stdout)
|
||||
get_query(&db);
|
||||
}
|
||||
|
||||
fn add_relation(db: &DbInstance) {
|
||||
db.run_script(
|
||||
":create people {
|
||||
name: String,
|
||||
=>
|
||||
age: Int,
|
||||
}", // CozoScript, introduction: https://docs.cozodb.org/en/latest/tutorial.html
|
||||
BTreeMap::new(), // Parameters. See queries below for examples
|
||||
ScriptMutability::Mutable // Mutable=The script will change something, Immutable=The script only needs read access
|
||||
).expect("Adding a relation failed");
|
||||
}
|
||||
|
||||
fn insert_row(db: &DbInstance) {
|
||||
let mut parameters1 = BTreeMap::new();
|
||||
parameters1.insert("name".to_string(), DataValue::Str("Alice".into())); // first one is the "variable name" that will be used in the script, second one the value
|
||||
parameters1.insert("age".to_string(), DataValue::Num(cozo::Num::Int(42))); // Values are strongly typed
|
||||
|
||||
db.run_script(
|
||||
"
|
||||
?[name, age] <- [[$name, $age]]
|
||||
:put people {name => age}
|
||||
",
|
||||
parameters1,
|
||||
ScriptMutability::Mutable
|
||||
).expect("Inserting a value failed");
|
||||
}
|
||||
|
||||
fn get_query(db: &DbInstance) {
|
||||
let mut parameters2 = BTreeMap::new();
|
||||
parameters2.insert("name".to_string(), DataValue::Str("Alice".into())); // We will query for the age of the person with this name
|
||||
let result = db.run_script(
|
||||
"?[age] := *people[$name, age]",
|
||||
parameters2,
|
||||
ScriptMutability::Immutable // "get" operations work read-only, so we should only grant read permissions
|
||||
);
|
||||
|
||||
match result {
|
||||
// Wrapper for all found rows
|
||||
Ok(named_rows) => {
|
||||
// Stripped rows, each row being a Vec<DataValue>
|
||||
let rows: Vec<Vec<DataValue>> = named_rows.rows;
|
||||
// We only care about the first found row (since it's the only one in this example)
|
||||
match rows.first() {
|
||||
// At least one row was returned from the query
|
||||
Some(row) => {
|
||||
// Attempt to extract the age from that row
|
||||
if let Some(DataValue::Num(cozo::Num::Int(age))) = row.first() {
|
||||
println!("Alice is allegedly {} years old.", age);
|
||||
}
|
||||
else {
|
||||
println!("Could not extract age from query result... Rows returned:\n{:?}", rows);
|
||||
}
|
||||
},
|
||||
// No row was returned from the query (should not happen in this example)
|
||||
None => {
|
||||
println!("No row returned: {:?}", rows)
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(e) => println!("Error: {}", e)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue