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
```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));
INSERT INTO `users` (id, name) VALUES (1, 'Alice');
"#).unwrap();
database.process("SELECT * FROM `users`;", |pairs| {
connection.process("SELECT * FROM `users`;", |pairs| {
for &(column, value) in pairs.iter() {
println!("{} = {}", column, value.unwrap());
}

View File

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

View File

@ -3,14 +3,14 @@
//! ## 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));
//! INSERT INTO `users` (id, name) VALUES (1, 'Alice');
//! "#).unwrap();
//!
//! database.process("SELECT * FROM `users`;", |pairs| {
//! connection.process("SELECT * FROM `users`;", |pairs| {
//! for &(column, value) in pairs.iter() {
//! println!("{} = {}", column, value.unwrap());
//! }
@ -31,8 +31,8 @@ macro_rules! raise(
);
macro_rules! error(
($database:expr, $code:expr) => (
match ::error::last($database) {
($connection:expr, $code:expr) => (
match ::error::last($connection) {
Some(error) => return Err(error),
None => return Err(::Error::from(::ErrorKind::from($code as isize))),
}
@ -40,10 +40,10 @@ macro_rules! error(
);
macro_rules! ok(
($database:expr, $result:expr) => (
($connection:expr, $result:expr) => (
match $result {
::ffi::SQLITE_OK => {},
code => error!($database, code),
code => error!($connection, code),
}
);
($result:expr) => (
@ -86,11 +86,11 @@ macro_rules! str_to_c_str(
);
);
mod database;
mod connection;
mod error;
mod statement;
pub use database::Database;
pub use connection::Connection;
pub use error::{Error, ErrorKind};
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.
#[inline]
pub fn open<'l, T: AsRef<std::path::Path>>(path: T) -> Result<Database<'l>> {
Database::open(path)
pub fn open<'l, T: AsRef<std::path::Path>>(path: T) -> Result<Connection<'l>> {
Connection::open(path)
}
#[cfg(test)]

View File

@ -13,14 +13,14 @@ fn workflow() {
($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);"#;
ok!(database.execute(sql));
ok!(connection.execute(sql));
{
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(2, "Alice"));
ok!(statement.bind(3, 20.99));
@ -30,7 +30,7 @@ fn workflow() {
{
let mut done = false;
let sql = r#"SELECT * FROM `users`;"#;
ok!(database.process(sql, |pairs| {
ok!(connection.process(sql, |pairs| {
assert!(pairs.len() == 3);
assert!(pairs[0] == pair!("id", "1"));
assert!(pairs[1] == pair!("name", "Alice"));
@ -43,7 +43,7 @@ fn workflow() {
{
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.read::<i64>(0)) == 1);
assert!(ok!(statement.read::<String>(1)) == String::from("Alice"));
@ -62,17 +62,17 @@ fn stress() {
let directory = ok!(Directory::new("sqlite"));
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);"#;
ok!(database.execute(sql));
ok!(connection.execute(sql));
let guards = (0..100).map(|_| {
let path = PathBuf::from(&path);
thread::spawn(move || {
let mut database = ok!(sqlite::open(&path));
ok!(database.set_busy_handler(|_| true));
let mut connection = ok!(sqlite::open(&path));
ok!(connection.set_busy_handler(|_| true));
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(2, "Alice"));
ok!(statement.bind(3, 20.99));