Rename Database to Connection

This commit is contained in:
Ivan Ukhov 2015-07-04 08:53:26 -04:00
parent 626c022106
commit d94275417a
4 changed files with 34 additions and 34 deletions

View File

@ -7,14 +7,14 @@ The package provides an interface to [SQLite][1].
## Example ## Example
```rust ```rust
let database = sqlite::open(":memory:").unwrap(); let connection = sqlite::open(":memory:").unwrap();
database.execute(r#" connection.execute(r#"
CREATE TABLE `users` (id INTEGER, name VARCHAR(255)); CREATE TABLE `users` (id INTEGER, name VARCHAR(255));
INSERT INTO `users` (id, name) VALUES (1, 'Alice'); INSERT INTO `users` (id, name) VALUES (1, 'Alice');
"#).unwrap(); "#).unwrap();
database.process("SELECT * FROM `users`;", |pairs| { connection.process("SELECT * FROM `users`;", |pairs| {
for &(column, value) in pairs.iter() { for &(column, value) in pairs.iter() {
println!("{} = {}", column, value.unwrap()); println!("{} = {}", column, value.unwrap());
} }

View File

@ -6,22 +6,22 @@ use std::path::Path;
use {Result, Statement}; use {Result, Statement};
/// A connection to a database. /// A connection to a database.
pub struct Database<'l> { pub struct Connection<'l> {
raw: *mut ffi::sqlite3, raw: *mut ffi::sqlite3,
busy_callback: Option<Box<FnMut(usize) -> bool + 'l>>, busy_callback: Option<Box<FnMut(usize) -> bool + 'l>>,
phantom: PhantomData<ffi::sqlite3>, phantom: PhantomData<ffi::sqlite3>,
} }
impl<'l> Database<'l> { impl<'l> Connection<'l> {
/// Open a connection to a new or existing database. /// Open a connection to a new or existing database.
pub fn open<T: AsRef<Path>>(path: T) -> Result<Database<'l>> { pub fn open<T: AsRef<Path>>(path: T) -> Result<Connection<'l>> {
let mut raw = 0 as *mut _; let mut raw = 0 as *mut _;
unsafe { unsafe {
ok!(ffi::sqlite3_open_v2(path_to_c_str!(path.as_ref()), &mut raw, ok!(ffi::sqlite3_open_v2(path_to_c_str!(path.as_ref()), &mut raw,
ffi::SQLITE_OPEN_CREATE | ffi::SQLITE_OPEN_READWRITE, ffi::SQLITE_OPEN_CREATE | ffi::SQLITE_OPEN_READWRITE,
0 as *const _)); 0 as *const _));
} }
Ok(Database { Ok(Connection {
raw: raw, raw: raw,
busy_callback: None, busy_callback: None,
phantom: PhantomData, phantom: PhantomData,
@ -98,7 +98,7 @@ impl<'l> Database<'l> {
} }
} }
impl<'l> Drop for Database<'l> { impl<'l> Drop for Connection<'l> {
#[cfg(not(feature = "edge"))] #[cfg(not(feature = "edge"))]
#[inline] #[inline]
#[allow(unused_must_use)] #[allow(unused_must_use)]
@ -152,14 +152,14 @@ extern fn process_callback<F>(callback: *mut c_void, count: c_int, values: *mut
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::Database; use super::Connection;
use tests::setup; use tests::setup;
#[test] #[test]
fn execute() { fn execute() {
let (path, _directory) = setup(); let (path, _directory) = setup();
let database = Database::open(&path).unwrap(); let connection = Connection::open(&path).unwrap();
match database.execute(":)") { match connection.execute(":)") {
Err(error) => assert_eq!(error.message, Err(error) => assert_eq!(error.message,
Some(String::from(r#"unrecognized token: ":""#))), Some(String::from(r#"unrecognized token: ":""#))),
_ => unreachable!(), _ => unreachable!(),
@ -169,7 +169,7 @@ mod tests {
#[test] #[test]
fn set_busy_handler() { fn set_busy_handler() {
let (path, _directory) = setup(); let (path, _directory) = setup();
let mut database = Database::open(&path).unwrap(); let mut connection = Connection::open(&path).unwrap();
database.set_busy_handler(|_| true).unwrap(); connection.set_busy_handler(|_| true).unwrap();
} }
} }

View File

@ -3,14 +3,14 @@
//! ## Example //! ## Example
//! //!
//! ``` //! ```
//! let database = sqlite::open(":memory:").unwrap(); //! let connection = sqlite::open(":memory:").unwrap();
//! //!
//! database.execute(r#" //! connection.execute(r#"
//! CREATE TABLE `users` (id INTEGER, name VARCHAR(255)); //! CREATE TABLE `users` (id INTEGER, name VARCHAR(255));
//! INSERT INTO `users` (id, name) VALUES (1, 'Alice'); //! INSERT INTO `users` (id, name) VALUES (1, 'Alice');
//! "#).unwrap(); //! "#).unwrap();
//! //!
//! database.process("SELECT * FROM `users`;", |pairs| { //! connection.process("SELECT * FROM `users`;", |pairs| {
//! for &(column, value) in pairs.iter() { //! for &(column, value) in pairs.iter() {
//! println!("{} = {}", column, value.unwrap()); //! println!("{} = {}", column, value.unwrap());
//! } //! }
@ -31,8 +31,8 @@ macro_rules! raise(
); );
macro_rules! error( macro_rules! error(
($database:expr, $code:expr) => ( ($connection:expr, $code:expr) => (
match ::error::last($database) { match ::error::last($connection) {
Some(error) => return Err(error), Some(error) => return Err(error),
None => return Err(::Error::from(::ErrorKind::from($code as isize))), None => return Err(::Error::from(::ErrorKind::from($code as isize))),
} }
@ -40,10 +40,10 @@ macro_rules! error(
); );
macro_rules! ok( macro_rules! ok(
($database:expr, $result:expr) => ( ($connection:expr, $result:expr) => (
match $result { match $result {
::ffi::SQLITE_OK => {}, ::ffi::SQLITE_OK => {},
code => error!($database, code), code => error!($connection, code),
} }
); );
($result:expr) => ( ($result:expr) => (
@ -86,11 +86,11 @@ macro_rules! str_to_c_str(
); );
); );
mod database; mod connection;
mod error; mod error;
mod statement; mod statement;
pub use database::Database; pub use connection::Connection;
pub use error::{Error, ErrorKind}; pub use error::{Error, ErrorKind};
pub use statement::{Statement, State, Parameter, Value}; pub use statement::{Statement, State, Parameter, Value};
@ -99,8 +99,8 @@ pub type Result<T> = std::result::Result<T, Error>;
/// Open a connection to a new or existing database. /// Open a connection to a new or existing database.
#[inline] #[inline]
pub fn open<'l, T: AsRef<std::path::Path>>(path: T) -> Result<Database<'l>> { pub fn open<'l, T: AsRef<std::path::Path>>(path: T) -> Result<Connection<'l>> {
Database::open(path) Connection::open(path)
} }
#[cfg(test)] #[cfg(test)]

View File

@ -13,14 +13,14 @@ fn workflow() {
($one:expr, $two:expr) => (($one, Some($two))); ($one:expr, $two:expr) => (($one, Some($two)));
); );
let database = ok!(sqlite::open(":memory:")); let connection = ok!(sqlite::open(":memory:"));
let sql = r#"CREATE TABLE `users` (id INTEGER, name VARCHAR(255), age REAL);"#; let sql = r#"CREATE TABLE `users` (id INTEGER, name VARCHAR(255), age REAL);"#;
ok!(database.execute(sql)); ok!(connection.execute(sql));
{ {
let sql = r#"INSERT INTO `users` (id, name, age) VALUES (?, ?, ?);"#; let sql = r#"INSERT INTO `users` (id, name, age) VALUES (?, ?, ?);"#;
let mut statement = ok!(database.prepare(sql)); let mut statement = ok!(connection.prepare(sql));
ok!(statement.bind(1, 1i64)); ok!(statement.bind(1, 1i64));
ok!(statement.bind(2, "Alice")); ok!(statement.bind(2, "Alice"));
ok!(statement.bind(3, 20.99)); ok!(statement.bind(3, 20.99));
@ -30,7 +30,7 @@ fn workflow() {
{ {
let mut done = false; let mut done = false;
let sql = r#"SELECT * FROM `users`;"#; let sql = r#"SELECT * FROM `users`;"#;
ok!(database.process(sql, |pairs| { ok!(connection.process(sql, |pairs| {
assert!(pairs.len() == 3); assert!(pairs.len() == 3);
assert!(pairs[0] == pair!("id", "1")); assert!(pairs[0] == pair!("id", "1"));
assert!(pairs[1] == pair!("name", "Alice")); assert!(pairs[1] == pair!("name", "Alice"));
@ -43,7 +43,7 @@ fn workflow() {
{ {
let sql = r#"SELECT * FROM `users`;"#; let sql = r#"SELECT * FROM `users`;"#;
let mut statement = ok!(database.prepare(sql)); let mut statement = ok!(connection.prepare(sql));
assert!(ok!(statement.step()) == State::Row); assert!(ok!(statement.step()) == State::Row);
assert!(ok!(statement.read::<i64>(0)) == 1); assert!(ok!(statement.read::<i64>(0)) == 1);
assert!(ok!(statement.read::<String>(1)) == String::from("Alice")); assert!(ok!(statement.read::<String>(1)) == String::from("Alice"));
@ -62,17 +62,17 @@ fn stress() {
let directory = ok!(Directory::new("sqlite")); let directory = ok!(Directory::new("sqlite"));
let path = directory.path().join("database.sqlite3"); let path = directory.path().join("database.sqlite3");
let database = ok!(sqlite::open(&path)); let connection = ok!(sqlite::open(&path));
let sql = r#"CREATE TABLE `users` (id INTEGER, name VARCHAR(255), age REAL);"#; let sql = r#"CREATE TABLE `users` (id INTEGER, name VARCHAR(255), age REAL);"#;
ok!(database.execute(sql)); ok!(connection.execute(sql));
let guards = (0..100).map(|_| { let guards = (0..100).map(|_| {
let path = PathBuf::from(&path); let path = PathBuf::from(&path);
thread::spawn(move || { thread::spawn(move || {
let mut database = ok!(sqlite::open(&path)); let mut connection = ok!(sqlite::open(&path));
ok!(database.set_busy_handler(|_| true)); ok!(connection.set_busy_handler(|_| true));
let sql = r#"INSERT INTO `users` (id, name, age) VALUES (?, ?, ?);"#; let sql = r#"INSERT INTO `users` (id, name, age) VALUES (?, ?, ?);"#;
let mut statement = ok!(database.prepare(sql)); let mut statement = ok!(connection.prepare(sql));
ok!(statement.bind(1, 1i64)); ok!(statement.bind(1, 1i64));
ok!(statement.bind(2, "Alice")); ok!(statement.bind(2, "Alice"));
ok!(statement.bind(3, 20.99)); ok!(statement.bind(3, 20.99));