diff --git a/db/000146.sst b/db/000146.sst deleted file mode 100644 index bbb9a74..0000000 Binary files a/db/000146.sst and /dev/null differ diff --git a/db/000149.sst b/db/000149.sst deleted file mode 100644 index 71e8e14..0000000 Binary files a/db/000149.sst and /dev/null differ diff --git a/db/000154.sst b/db/000154.sst deleted file mode 100644 index 5ffab92..0000000 Binary files a/db/000154.sst and /dev/null differ diff --git a/db/000169.sst b/db/000169.sst new file mode 100644 index 0000000..e179ecc Binary files /dev/null and b/db/000169.sst differ diff --git a/db/000172.sst b/db/000172.sst new file mode 100644 index 0000000..de24ecb Binary files /dev/null and b/db/000172.sst differ diff --git a/db/000177.sst b/db/000177.sst new file mode 100644 index 0000000..8182709 Binary files /dev/null and b/db/000177.sst differ diff --git a/db/CURRENT b/db/CURRENT index c16f179..9289f7a 100644 --- a/db/CURRENT +++ b/db/CURRENT @@ -1 +1 @@ -MANIFEST-000156 +MANIFEST-000179 diff --git a/db/LOG.old.1710879487198594 b/db/LOG.old.1711007757055235 similarity index 100% rename from db/LOG.old.1710879487198594 rename to db/LOG.old.1711007757055235 diff --git a/db/LOG.old.1710947659977921 b/db/LOG.old.1711007799421652 similarity index 100% rename from db/LOG.old.1710947659977921 rename to db/LOG.old.1711007799421652 diff --git a/db/LOG.old.1710951404824640 b/db/LOG.old.1711008948349539 similarity index 100% rename from db/LOG.old.1710951404824640 rename to db/LOG.old.1711008948349539 diff --git a/db/LOG.old.1710951576558702 b/db/LOG.old.1711009663259814 similarity index 100% rename from db/LOG.old.1710951576558702 rename to db/LOG.old.1711009663259814 diff --git a/db/MANIFEST-000156 b/db/MANIFEST-000156 deleted file mode 100644 index c588da1..0000000 Binary files a/db/MANIFEST-000156 and /dev/null differ diff --git a/db/MANIFEST-000179 b/db/MANIFEST-000179 new file mode 100644 index 0000000..883a87b Binary files /dev/null and b/db/MANIFEST-000179 differ diff --git a/db/OPTIONS-000153 b/db/OPTIONS-000176 similarity index 100% rename from db/OPTIONS-000153 rename to db/OPTIONS-000176 diff --git a/db/OPTIONS-000158 b/db/OPTIONS-000181 similarity index 100% rename from db/OPTIONS-000158 rename to db/OPTIONS-000181 diff --git a/src/controller.rs b/src/controller.rs new file mode 100644 index 0000000..b5614dd --- /dev/null +++ b/src/controller.rs @@ -0,0 +1 @@ +pub mod utils; diff --git a/src/controller/utils.rs b/src/controller/utils.rs new file mode 100644 index 0000000..3953a04 --- /dev/null +++ b/src/controller/utils.rs @@ -0,0 +1,91 @@ +use surrealdb::{engine::local::Db, Surreal}; +use crate::Colorize; +use crate::{Magazine, Record}; + +pub async fn add_to(db: &Surreal, data: Vec) -> surrealdb::Result<()> { + for magazine in data { + let response = db + .query( + "CREATE product SET name=$name, + price=$price, day=$day, month=$month, year=$year", + ) + .bind(("name", magazine.name)) + .bind(("price", magazine.price)) + .bind(("day", magazine.day)) + .bind(("month", magazine.month)) + .bind(("year", magazine.year)) + .await?; + + match response.check() { + Ok(_) => {} + Err(err) => { + eprintln!("Could not add entry: '{}'", err); + return Err(err); + } + }; + } + Ok(()) +} + +pub async fn list_all(db: &Surreal) -> surrealdb::Result<()> { + let mut entries = db + .query( + "SELECT name, price, day, month, year, id + FROM type::table($table) ORDER BY name ASC", + ) + .bind(("table", "product")) + .await?; + let entries: Vec = entries.take(0)?; + println!("----------------------------------------------------------------"); + println!( + "{:<12} {:5} {:<2} {:<2} {:<2} {:}", + "Magazine", "price", "day", "month", "year", "table+id" + ); + println!("----------------------------------------------------------------"); + for entry in entries { + println!( + "{:<12} {:<5.2} {:<3} {:<5} {:<4} {:}", + entry.name.yellow(), + entry.price, + entry.day, + entry.month, + entry.year, + entry.id.to_raw().blue() + ); + } + + Ok(()) +} + +pub async fn list_year(db: &Surreal, year: u32) -> surrealdb::Result<()> { + let mut entries = db + .query("SELECT * FROM type::table($table) WHERE year=$year") + .bind(("table", "product")) + .bind(("year", year)) + .await?; + let entries: Vec = entries.take(0)?; + for entry in entries { + println!("{:?} ", entry); + } + Ok(()) +} + +pub async fn add_relate(db: &Surreal, topic: String) -> surrealdb::Result<()> { + let _relate = db + .query("RELATE product->featured->($topic)") + .bind(("topic", topic)) + .await?; + Ok(()) +} + +pub async fn list_related(db: &Surreal) -> surrealdb::Result<()> { + let mut entries = db + .query("SELECT * FROM type::table($table)") + .bind(("table", "featured")) + .await?; + let entries: Vec = entries.take(0)?; + for entry in entries { + println!("{:?} ", entry); + } + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index 9b674d4..abd52da 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,22 @@ -// run as ./target/debug/surrealtest 13 2 1998 +// run as ./target/debug/surrealtest add 13 2 1998 #![allow(unused)] -use std::fmt::format; - use clap::Parser; use colored::Colorize; +use controller::utils::*; use serde::Deserialize; +use std::fmt::format; use surrealdb::engine::local::Db; use surrealdb::engine::local::RocksDb; use surrealdb::sql::Thing; use surrealdb::Surreal; //use surrealkv::Store; +mod controller; + #[derive(Parser, Debug)] -struct Value { +pub struct Value { + /// add product + add: String, /// day number to add number_day: i32, /// month number to add @@ -22,7 +26,7 @@ struct Value { } #[derive(Debug, Deserialize)] -struct Magazine { +pub struct Magazine { name: String, price: f32, day: u8, @@ -31,7 +35,7 @@ struct Magazine { } #[derive(Debug, Deserialize)] -struct Record { +pub struct Record { id: Thing, name: String, price: f32, @@ -42,20 +46,24 @@ struct Record { #[tokio::main] async fn main() -> surrealdb::Result<()> { + // handle user input let value = Value::parse(); - - let day = value.number_day; - let month = value.number_month; - let year = value.number_year; - - let next = format!("Day:{} Month:{} Year:{}",day,month,year); - + let day: u8 = value.number_day.try_into().unwrap(); + let month: u8 = value.number_month.try_into().unwrap(); + let year: u32 = value.number_year.try_into().unwrap(); + let next = format!("Day:{} Month:{} Year:{}", day, month, year); println!("{}", next.green()); + let new_mag = Magazine { + name: "Autosport".to_string(), + price: 2.30, + day, + month, + year, + }; // Create database connection let mut db_path = std::env::current_dir().unwrap(); db_path.push("db"); - //println!("{db_path:?}"); // use embedded RocksDB for storage -try SurrealKV as soon as it compiles ok let db = Surreal::new::(db_path).await?; @@ -64,15 +72,15 @@ async fn main() -> surrealdb::Result<()> { // Select specific namespace & database db.use_ns("test").use_db("test").await?; + // clear old, test data let _cleanup = db.query("REMOVE TABLE product").await?; let _response = db //-- Create an index on the name, month and year fields of the product table - // DEFINE INDEX test ON user FIELDS account, email; .query("DEFINE INDEX magid ON TABLE product COLUMNS name,month,year UNIQUE") .await?; - let data = vec![ + let mut data = vec![ Magazine { name: "Autosport".to_string(), price: 1.80, @@ -102,7 +110,9 @@ async fn main() -> surrealdb::Result<()> { year: 1984, }, ]; + data.push(new_mag); + // Banner - read from seperate file for customizing println!( r" __ ____ @@ -125,90 +135,3 @@ async fn main() -> surrealdb::Result<()> { Ok(()) } -async fn add_to(db: &Surreal, data: Vec) -> surrealdb::Result<()> { - for magazine in data { - let response = db - .query( - "CREATE product SET name=$name, - price=$price, day=$day, month=$month, year=$year", - ) - .bind(("name", magazine.name)) - .bind(("price", magazine.price)) - .bind(("day", magazine.day)) - .bind(("month", magazine.month)) - .bind(("year", magazine.year)) - .await?; - - match response.check() { - Ok(_) => {} - Err(err) => { - eprintln!("Could not add entry: '{}'", err); - return Err(err); - } - }; - } - Ok(()) -} - -async fn list_all(db: &Surreal) -> surrealdb::Result<()> { - let mut entries = db - .query( - "SELECT name, price, day, month, year, id - FROM type::table($table) ORDER BY name ASC", - ) - .bind(("table", "product")) - .await?; - let entries: Vec = entries.take(0)?; - println!("----------------------------------------------------------------"); - println!( - "{:<12} {:5} {:<2} {:<2} {:<2} {:}", - "Magazine", "price", "day", "month", "year", "table+id" - ); - println!("----------------------------------------------------------------"); - for entry in entries { - println!( - "{:<12} {:<5.2} {:<3} {:<5} {:<4} {:}", - entry.name.yellow(), - entry.price, - entry.day, - entry.month, - entry.year, - entry.id.to_raw().blue() - ); - } - - Ok(()) -} - -async fn list_year(db: &Surreal, year: u32) -> surrealdb::Result<()> { - let mut entries = db - .query("SELECT * FROM type::table($table) WHERE year=$year") - .bind(("table", "product")) - .bind(("year", year)) - .await?; - let entries: Vec = entries.take(0)?; - for entry in entries { - println!("{:?} ", entry); - } - Ok(()) -} - -async fn add_relate(db: &Surreal, topic: String) -> surrealdb::Result<()> { - let _relate = db - .query("RELATE product->featured->($topic)") - .bind(("topic", topic)) - .await?; - Ok(()) -} - -async fn list_related(db: &Surreal) -> surrealdb::Result<()> { - let mut entries = db - .query("SELECT * FROM type::table($table)") - .bind(("table", "featured")) - .await?; - let entries: Vec = entries.take(0)?; - for entry in entries { - println!("{:?} ", entry); - } - Ok(()) -}