From 7f9ced7db34681488f0c940176557f29fa2ba1f5 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Thu, 2 Feb 2017 17:38:16 -0800 Subject: [PATCH] Add raw sqlite example; not in workspace due to sqlite3-sys conflict. --- Cargo.toml | 1 + examples/raw_sqlite/Cargo.toml | 12 ++++++++ examples/raw_sqlite/src/main.rs | 50 ++++++++++++++++++++++++++++++++ examples/raw_sqlite/src/tests.rs | 13 +++++++++ 4 files changed, 76 insertions(+) create mode 100644 examples/raw_sqlite/Cargo.toml create mode 100644 examples/raw_sqlite/src/main.rs create mode 100644 examples/raw_sqlite/src/tests.rs diff --git a/Cargo.toml b/Cargo.toml index cc099e2e..50fef2e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,4 +30,5 @@ members = [ "examples/pastebin", "examples/state", "examples/uuid", + # "examples/raw_sqlite", ] diff --git a/examples/raw_sqlite/Cargo.toml b/examples/raw_sqlite/Cargo.toml new file mode 100644 index 00000000..00192fd1 --- /dev/null +++ b/examples/raw_sqlite/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "raw_sqlite" +version = "0.0.1" +workspace = "../../" + +[dependencies] +rocket = { path = "../../lib" } +rocket_codegen = { path = "../../codegen" } +rusqlite = "0.9" + +[dev-dependencies] +rocket = { path = "../../lib", features = ["testing"] } diff --git a/examples/raw_sqlite/src/main.rs b/examples/raw_sqlite/src/main.rs new file mode 100644 index 00000000..9392da11 --- /dev/null +++ b/examples/raw_sqlite/src/main.rs @@ -0,0 +1,50 @@ +#![feature(plugin)] +#![plugin(rocket_codegen)] + +extern crate rocket; +extern crate rusqlite; + +#[cfg(test)] mod tests; + +use std::sync::Mutex; +use rocket::{Rocket, State}; +use rusqlite::{Connection, Error}; + +type DbConn = Mutex; + +fn init_database(conn: &Connection) { + conn.execute("CREATE TABLE entries ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL + )", &[]) + .expect("create entries table"); + + conn.execute("INSERT INTO entries (id, name) VALUES ($1, $2)", + &[&0, &"Rocketeer"]) + .expect("insert single entry into entries table"); +} + +#[get("/")] +fn hello(db_conn: State) -> Result { + db_conn.lock() + .expect("db connection lock") + .query_row("SELECT name FROM entries WHERE id = 0", + &[], |row| { row.get(0) }) +} + +fn rocket() -> Rocket { + // Open a new in-memory SQLite database. + let conn = Connection::open_in_memory().expect("in memory db"); + + // Initialize the `entries` table in the in-memory database. + init_database(&conn); + + // Have Rocket manage the database pool. + rocket::ignite() + .manage(Mutex::new(conn)) + .mount("/", routes![hello]) +} + +fn main() { + rocket().launch(); +} diff --git a/examples/raw_sqlite/src/tests.rs b/examples/raw_sqlite/src/tests.rs new file mode 100644 index 00000000..4f290935 --- /dev/null +++ b/examples/raw_sqlite/src/tests.rs @@ -0,0 +1,13 @@ +use super::rocket; +use rocket::testing::MockRequest; +use rocket::http::Method::*; + +#[test] +fn hello() { + let rocket = rocket(); + let mut req = MockRequest::new(Get, "/"); + let mut response = req.dispatch_with(&rocket); + + let body_str = response.body().and_then(|body| body.into_string()); + assert_eq!(body_str, Some("Rocketeer".to_string())); +}