Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rustqlite example #49

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
/Cargo.lock
/toasty.db3
21 changes: 16 additions & 5 deletions examples/hello-toasty/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod db;
use std::path::PathBuf;
use std::{env, path::PathBuf};

use db::{Todo, User};

Expand All @@ -10,16 +10,27 @@ fn assert_sync_send<T: Send>(_: T) {}

#[tokio::main]
async fn main() {
let schema_file = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("schema.toasty");
let args: Vec<String> = env::args().collect();

// CLI flag required to persist DB to local file, instead of using the in-memory sqlite driver.
let flag = "--persist-to-file";
let driver = if args.len() > 1 && args[1] == flag {
// Persist to local file
let filename = "toasty.db3";
let file_path = format!("./{}", filename);
let file = PathBuf::from(file_path.as_str());
Sqlite::open(file).unwrap()
} else {
// Use the in-memory sqlite driver
Sqlite::in_memory()
};
Comment on lines +16 to +26

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let flag = "--persist-to-file";
let driver = if args.len() > 1 && args[1] == flag {
// Persist to local file
let filename = "toasty.db3";
let file_path = format!("./{}", filename);
let file = PathBuf::from(file_path.as_str());
Sqlite::open(file).unwrap()
} else {
// Use the in-memory sqlite driver
Sqlite::in_memory()
};
const PERSIST_FLAG: &str = "--persist-to-file";
const DB_FILENAME: &str = "toasty.db3";
let driver = if args.get(1).map_or(false, |arg| arg == PERSIST_FLAG) {
let file_path = PathBuf::from(DB_FILENAME);
Sqlite::open(file_path).expect("Failed to open SQLite file")
} else {
Sqlite::in_memory()
};


let schema_file = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("schema.toasty");
let schema = toasty::schema::from_file(schema_file).unwrap();

// NOTE enable this to see the enstire structure in STDOUT
// println!("{schema:#?}");

// Use the in-memory sqlite driver
let driver = Sqlite::in_memory();

let db = Db::new(schema, driver).await;
// For now, reset!s
db.reset_db().await.unwrap();
Expand Down