From 610aea022172fc81955656af11eed4470c6676ac Mon Sep 17 00:00:00 2001 From: Ivan Ukhov Date: Sun, 14 Jun 2015 11:33:55 -0400 Subject: [PATCH] Update the usage example --- README.md | 14 ++------------ examples/workflow.rs | 14 ++------------ src/lib.rs | 24 ++++++++++++++++++++++++ tests/lib.rs | 19 +++++++------------ 4 files changed, 35 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index bd1d093..d7d0000 100644 --- a/README.md +++ b/README.md @@ -15,12 +15,10 @@ cargo run --example workflow ```rust extern crate sqlite; -use std::fs; -use std::path::PathBuf; +use std::path::Path; fn main() { - let path = setup(); - let database = sqlite::open(&path).unwrap(); + let database = sqlite::open(&Path::new(":memory:")).unwrap(); database.execute(r#" CREATE TABLE `users` (id INTEGER, name VARCHAR(255)); @@ -34,14 +32,6 @@ fn main() { true }).unwrap(); } - -fn setup() -> PathBuf { - let path = PathBuf::from("database.sqlite3"); - if fs::metadata(&path).is_ok() { - fs::remove_file(&path).unwrap(); - } - path -} ``` ## Contributing diff --git a/examples/workflow.rs b/examples/workflow.rs index 5af9d0f..13fe3ff 100644 --- a/examples/workflow.rs +++ b/examples/workflow.rs @@ -1,11 +1,9 @@ extern crate sqlite; -use std::fs; -use std::path::PathBuf; +use std::path::Path; fn main() { - let path = setup(); - let database = sqlite::open(&path).unwrap(); + let database = sqlite::open(&Path::new(":memory:")).unwrap(); database.execute(r#" CREATE TABLE `users` (id INTEGER, name VARCHAR(255)); @@ -19,11 +17,3 @@ fn main() { true }).unwrap(); } - -fn setup() -> PathBuf { - let path = PathBuf::from("database.sqlite3"); - if fs::metadata(&path).is_ok() { - fs::remove_file(&path).unwrap(); - } - path -} diff --git a/src/lib.rs b/src/lib.rs index 4884c30..24b3263 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,27 @@ +//! Interface to [SQLite][1]. +//! +//! ## Example +//! +//! ``` +//! use std::path::Path; +//! +//! let database = sqlite::open(&Path::new(":memory:")).unwrap(); +//! +//! database.execute(r#" +//! CREATE TABLE `users` (id INTEGER, name VARCHAR(255)); +//! INSERT INTO `users` (id, name) VALUES (1, 'Alice'); +//! "#).unwrap(); +//! +//! database.process("SELECT * FROM `users`;", |pairs| { +//! for &(column, value) in pairs.iter() { +//! println!("{} = {}", column, value.unwrap()); +//! } +//! true +//! }).unwrap(); +//! ``` +//! +//! [1]: https://www.sqlite.org + #![allow(unused_unsafe)] extern crate libc; diff --git a/tests/lib.rs b/tests/lib.rs index fca9708..3485524 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -1,10 +1,6 @@ extern crate sqlite; extern crate temporary; -use std::path::PathBuf; -use std::thread; -use temporary::Directory; - macro_rules! ok( ($result:expr) => ($result.unwrap()); ); @@ -13,13 +9,13 @@ macro_rules! ok( fn workflow() { use sqlite::Binding::*; use sqlite::State; + use std::path::Path; macro_rules! pair( ($one:expr, $two:expr) => (($one, Some($two))); ); - let (path, _directory) = setup(); - let database = ok!(sqlite::open(&path)); + let database = ok!(sqlite::open(&Path::new(":memory:"))); let sql = r#"CREATE TABLE `users` (id INTEGER, name VARCHAR(255), age REAL);"#; ok!(database.execute(sql)); @@ -60,8 +56,12 @@ fn workflow() { fn stress() { use sqlite::Binding::*; use sqlite::State; + use std::path::PathBuf; + use std::thread; + use temporary::Directory; - let (path, _directory) = setup(); + let directory = ok!(Directory::new("sqlite")); + let path = directory.path().join("database.sqlite3"); let database = ok!(sqlite::open(&path)); let sql = r#"CREATE TABLE `users` (id INTEGER, name VARCHAR(255), age REAL);"#; @@ -84,8 +84,3 @@ fn stress() { assert!(guard.join().unwrap()); } } - -fn setup() -> (PathBuf, Directory) { - let directory = ok!(Directory::new("sqlite")); - (directory.path().join("database.sqlite3"), directory) -}