diff --git a/Changelog.md b/Changelog.md index d3c78b4..e1d2597 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,6 @@ # Version UPCOMING (TBD) +* Renamed `SqliteConnection` to `Connection`. The old name remains as a typealias. * Adds a variety of `..._named` methods for executing queries using named placeholder parameters. * Adds `backup` feature that exposes SQLite's online backup API. * Adds `functions` feature that allows user-defined scalar functions to be added to diff --git a/README.md b/README.md index 1bc6742..96f0fd3 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ extern crate rusqlite; extern crate time; use time::Timespec; -use rusqlite::SqliteConnection; +use rusqlite::Connection; #[derive(Debug)] struct Person { @@ -22,7 +22,7 @@ struct Person { } fn main() { - let conn = SqliteConnection::open_in_memory().unwrap(); + let conn = Connection::open_in_memory().unwrap(); conn.execute("CREATE TABLE person ( id INTEGER PRIMARY KEY, @@ -72,7 +72,7 @@ performing checks at runtime to ensure you do not try to retrieve the values of will panic if you do so. A specific example that will panic: ```rust -fn bad_function_will_panic(conn: &SqliteConnection) -> SqliteResult { +fn bad_function_will_panic(conn: &Connection) -> SqliteResult { let mut stmt = try!(conn.prepare("SELECT id FROM my_table")); let mut rows = try!(stmt.query(&[])); diff --git a/src/backup.rs b/src/backup.rs index e50ad20..5d9c2c2 100644 --- a/src/backup.rs +++ b/src/backup.rs @@ -1,6 +1,6 @@ //! Online SQLite backup API. //! -//! To create a `Backup`, you must have two distinct `SqliteConnection`s - one +//! To create a `Backup`, you must have two distinct `Connection`s - one //! for the source (which can be used while the backup is running) and one for //! the destination (which cannot). A `Backup` handle exposes three methods: //! `step` will attempt to back up a specified number of pages, `progress` gets @@ -14,13 +14,13 @@ //! documentation](https://www.sqlite.org/backup.html). //! //! ```rust,no_run -//! # use rusqlite::{backup, SqliteConnection, SqliteResult}; +//! # use rusqlite::{backup, Connection, SqliteResult}; //! # use std::path::Path; //! # use std::time; //! -//! fn backupDb>(src: &SqliteConnection, dst: P, progress: fn(backup::Progress)) +//! fn backupDb>(src: &Connection, dst: P, progress: fn(backup::Progress)) //! -> SqliteResult<()> { -//! let mut dst = try!(SqliteConnection::open(dst)); +//! let mut dst = try!(Connection::open(dst)); //! let backup = try!(backup::Backup::new(src, &mut dst)); //! backup.run_to_completion(5, time::Duration::from_millis(250), Some(progress)) //! } @@ -36,9 +36,9 @@ use std::time::Duration; use ffi; -use {DatabaseName, SqliteConnection, SqliteError, SqliteResult}; +use {DatabaseName, Connection, SqliteError, SqliteResult}; -impl SqliteConnection { +impl Connection { /// Back up the `name` database to the given destination path. /// If `progress` is not `None`, it will be called periodically /// until the backup completes. @@ -57,7 +57,7 @@ impl SqliteConnection { progress: Option) -> SqliteResult<()> { use self::StepResult::{More, Done, Busy, Locked}; - let mut dst = try!(SqliteConnection::open(dst_path)); + let mut dst = try!(Connection::open(dst_path)); let backup = try!(Backup::new_with_names(self, name, &mut dst, DatabaseName::Main)); let mut r = More; @@ -94,7 +94,7 @@ impl SqliteConnection { progress: Option) -> SqliteResult<()> { use self::StepResult::{More, Done, Busy, Locked}; - let src = try!(SqliteConnection::open(src_path)); + let src = try!(Connection::open(src_path)); let restore = try!(Backup::new_with_names(&src, DatabaseName::Main, self, name)); let mut r = More; @@ -170,8 +170,8 @@ impl<'a, 'b> Backup<'a, 'b> { /// /// Will return `Err` if the underlying `sqlite3_backup_init` call returns /// `NULL`. - pub fn new(from: &'a SqliteConnection, - to: &'b mut SqliteConnection) + pub fn new(from: &'a Connection, + to: &'b mut Connection) -> SqliteResult> { Backup::new_with_names(from, DatabaseName::Main, to, DatabaseName::Main) } @@ -185,9 +185,9 @@ impl<'a, 'b> Backup<'a, 'b> { /// /// Will return `Err` if the underlying `sqlite3_backup_init` call returns /// `NULL`. - pub fn new_with_names(from: &'a SqliteConnection, + pub fn new_with_names(from: &'a Connection, from_name: DatabaseName, - to: &'b mut SqliteConnection, + to: &'b mut Connection, to_name: DatabaseName) -> SqliteResult> { let to_name = try!(to_name.to_cstring()); @@ -298,21 +298,21 @@ impl<'a, 'b> Drop for Backup<'a, 'b> { #[cfg(test)] mod test { - use {SqliteConnection, DatabaseName}; + use {Connection, DatabaseName}; use std::time::Duration; use super::Backup; #[test] #[cfg_attr(rustfmt, rustfmt_skip)] fn test_backup() { - let src = SqliteConnection::open_in_memory().unwrap(); + let src = Connection::open_in_memory().unwrap(); let sql = "BEGIN; CREATE TABLE foo(x INTEGER); INSERT INTO foo VALUES(42); END;"; src.execute_batch(sql).unwrap(); - let mut dst = SqliteConnection::open_in_memory().unwrap(); + let mut dst = Connection::open_in_memory().unwrap(); { let backup = Backup::new(&src, &mut dst).unwrap(); @@ -336,14 +336,14 @@ mod test { #[test] #[cfg_attr(rustfmt, rustfmt_skip)] fn test_backup_temp() { - let src = SqliteConnection::open_in_memory().unwrap(); + let src = Connection::open_in_memory().unwrap(); let sql = "BEGIN; CREATE TEMPORARY TABLE foo(x INTEGER); INSERT INTO foo VALUES(42); END;"; src.execute_batch(sql).unwrap(); - let mut dst = SqliteConnection::open_in_memory().unwrap(); + let mut dst = Connection::open_in_memory().unwrap(); { let backup = Backup::new_with_names(&src, @@ -375,7 +375,7 @@ mod test { #[test] #[cfg_attr(rustfmt, rustfmt_skip)] fn test_backup_attached() { - let src = SqliteConnection::open_in_memory().unwrap(); + let src = Connection::open_in_memory().unwrap(); let sql = "ATTACH DATABASE ':memory:' AS my_attached; BEGIN; CREATE TABLE my_attached.foo(x INTEGER); @@ -383,7 +383,7 @@ mod test { END;"; src.execute_batch(sql).unwrap(); - let mut dst = SqliteConnection::open_in_memory().unwrap(); + let mut dst = Connection::open_in_memory().unwrap(); { let backup = Backup::new_with_names(&src, diff --git a/src/functions.rs b/src/functions.rs index d8df619..3ebe3d0 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -12,11 +12,11 @@ //! extern crate rusqlite; //! extern crate regex; //! -//! use rusqlite::{SqliteConnection, SqliteError, SqliteResult}; +//! use rusqlite::{Connection, SqliteError, SqliteResult}; //! use std::collections::HashMap; //! use regex::Regex; //! -//! fn add_regexp_function(db: &SqliteConnection) -> SqliteResult<()> { +//! fn add_regexp_function(db: &Connection) -> SqliteResult<()> { //! let mut cached_regexes = HashMap::new(); //! db.create_scalar_function("regexp", 2, true, move |ctx| { //! let regex_s = try!(ctx.get::(0)); @@ -41,7 +41,7 @@ //! } //! //! fn main() { -//! let db = SqliteConnection::open_in_memory().unwrap(); +//! let db = Connection::open_in_memory().unwrap(); //! add_regexp_function(&db).unwrap(); //! //! let is_match = db.query_row("SELECT regexp('[aeiou]*', 'aaaaeeeiii')", &[], @@ -65,7 +65,7 @@ pub use ffi::sqlite3_value_numeric_type; use types::Null; -use {SqliteResult, SqliteError, SqliteConnection, str_to_cstring, InnerSqliteConnection}; +use {SqliteResult, SqliteError, Connection, str_to_cstring, InnerConnection}; /// A trait for types that can be converted into the result of an SQL function. pub trait ToResult { @@ -341,7 +341,7 @@ impl<'a> Context<'a> { } } -impl SqliteConnection { +impl Connection { /// Attach a user-defined scalar function to this database connection. /// /// `fn_name` is the name the function will be accessible from SQL. @@ -355,9 +355,9 @@ impl SqliteConnection { /// # Example /// /// ```rust - /// # use rusqlite::{SqliteConnection, SqliteResult}; + /// # use rusqlite::{Connection, SqliteResult}; /// # type c_double = f64; - /// fn scalar_function_example(db: SqliteConnection) -> SqliteResult<()> { + /// fn scalar_function_example(db: Connection) -> SqliteResult<()> { /// try!(db.create_scalar_function("halve", 1, true, |ctx| { /// let value = try!(ctx.get::(0)); /// Ok(value / 2f64) @@ -397,7 +397,7 @@ impl SqliteConnection { } } -impl InnerSqliteConnection { +impl InnerConnection { fn create_scalar_function(&mut self, fn_name: &str, n_arg: c_int, @@ -477,7 +477,7 @@ mod test { use libc::c_double; use self::regex::Regex; - use {SqliteConnection, SqliteError, SqliteResult}; + use {Connection, SqliteError, SqliteResult}; use ffi; use functions::Context; @@ -489,7 +489,7 @@ mod test { #[test] fn test_function_half() { - let db = SqliteConnection::open_in_memory().unwrap(); + let db = Connection::open_in_memory().unwrap(); db.create_scalar_function("half", 1, true, half).unwrap(); let result = db.query_row("SELECT half(6)", &[], |r| r.get::(0)); @@ -498,7 +498,7 @@ mod test { #[test] fn test_remove_function() { - let db = SqliteConnection::open_in_memory().unwrap(); + let db = Connection::open_in_memory().unwrap(); db.create_scalar_function("half", 1, true, half).unwrap(); let result = db.query_row("SELECT half(6)", &[], |r| r.get::(0)); assert_eq!(3f64, result.unwrap()); @@ -546,7 +546,7 @@ mod test { #[test] #[cfg_attr(rustfmt, rustfmt_skip)] fn test_function_regexp_with_auxilliary() { - let db = SqliteConnection::open_in_memory().unwrap(); + let db = Connection::open_in_memory().unwrap(); db.execute_batch("BEGIN; CREATE TABLE foo (x string); INSERT INTO foo VALUES ('lisa'); @@ -571,7 +571,7 @@ mod test { #[test] #[cfg_attr(rustfmt, rustfmt_skip)] fn test_function_regexp_with_hashmap_cache() { - let db = SqliteConnection::open_in_memory().unwrap(); + let db = Connection::open_in_memory().unwrap(); db.execute_batch("BEGIN; CREATE TABLE foo (x string); INSERT INTO foo VALUES ('lisa'); @@ -621,7 +621,7 @@ mod test { #[test] fn test_varargs_function() { - let db = SqliteConnection::open_in_memory().unwrap(); + let db = Connection::open_in_memory().unwrap(); db.create_scalar_function("my_concat", -1, true, |ctx| { let mut ret = String::new(); diff --git a/src/lib.rs b/src/lib.rs index 0af8ff5..773a333 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,7 @@ //! extern crate time; //! //! use time::Timespec; -//! use rusqlite::SqliteConnection; +//! use rusqlite::Connection; //! //! #[derive(Debug)] //! struct Person { @@ -17,7 +17,7 @@ //! } //! //! fn main() { -//! let conn = SqliteConnection::open_in_memory().unwrap(); +//! let conn = Connection::open_in_memory().unwrap(); //! //! conn.execute("CREATE TABLE person ( //! id INTEGER PRIMARY KEY, @@ -179,27 +179,30 @@ impl<'a> DatabaseName<'a> { } } +/// Old name for `Connection`. `SqliteConnection` is deprecated. +pub type SqliteConnection = Connection; + /// A connection to a SQLite database. -pub struct SqliteConnection { - db: RefCell, +pub struct Connection { + db: RefCell, path: Option, } -unsafe impl Send for SqliteConnection {} +unsafe impl Send for Connection {} -impl SqliteConnection { +impl Connection { /// Open a new connection to a SQLite database. /// - /// `SqliteConnection::open(path)` is equivalent to `SqliteConnection::open_with_flags(path, + /// `Connection::open(path)` is equivalent to `Connection::open_with_flags(path, /// SQLITE_OPEN_READ_WRITE | SQLITE_OPEN_CREATE)`. /// /// # Failure /// /// Will return `Err` if `path` cannot be converted to a C-compatible string or if the /// underlying SQLite open call fails. - pub fn open>(path: P) -> SqliteResult { + pub fn open>(path: P) -> SqliteResult { let flags = Default::default(); - SqliteConnection::open_with_flags(path, flags) + Connection::open_with_flags(path, flags) } /// Open a new connection to an in-memory SQLite database. @@ -207,9 +210,9 @@ impl SqliteConnection { /// # Failure /// /// Will return `Err` if the underlying SQLite open call fails. - pub fn open_in_memory() -> SqliteResult { + pub fn open_in_memory() -> SqliteResult { let flags = Default::default(); - SqliteConnection::open_in_memory_with_flags(flags) + Connection::open_in_memory_with_flags(flags) } /// Open a new connection to a SQLite database. @@ -223,10 +226,10 @@ impl SqliteConnection { /// underlying SQLite open call fails. pub fn open_with_flags>(path: P, flags: SqliteOpenFlags) - -> SqliteResult { + -> SqliteResult { let c_path = try!(path_to_cstring(path.as_ref())); - InnerSqliteConnection::open_with_flags(&c_path, flags).map(|db| { - SqliteConnection { + InnerConnection::open_with_flags(&c_path, flags).map(|db| { + Connection { db: RefCell::new(db), path: Some(path.as_ref().to_path_buf()), } @@ -241,10 +244,10 @@ impl SqliteConnection { /// # Failure /// /// Will return `Err` if the underlying SQLite open call fails. - pub fn open_in_memory_with_flags(flags: SqliteOpenFlags) -> SqliteResult { + pub fn open_in_memory_with_flags(flags: SqliteOpenFlags) -> SqliteResult { let c_memory = try!(str_to_cstring(":memory:")); - InnerSqliteConnection::open_with_flags(&c_memory, flags).map(|db| { - SqliteConnection { + InnerConnection::open_with_flags(&c_memory, flags).map(|db| { + Connection { db: RefCell::new(db), path: None, } @@ -259,10 +262,10 @@ impl SqliteConnection { /// ## Example /// /// ```rust,no_run - /// # use rusqlite::{SqliteConnection, SqliteResult}; - /// # fn do_queries_part_1(conn: &SqliteConnection) -> SqliteResult<()> { Ok(()) } - /// # fn do_queries_part_2(conn: &SqliteConnection) -> SqliteResult<()> { Ok(()) } - /// fn perform_queries(conn: &SqliteConnection) -> SqliteResult<()> { + /// # use rusqlite::{Connection, SqliteResult}; + /// # fn do_queries_part_1(conn: &Connection) -> SqliteResult<()> { Ok(()) } + /// # fn do_queries_part_2(conn: &Connection) -> SqliteResult<()> { Ok(()) } + /// fn perform_queries(conn: &Connection) -> SqliteResult<()> { /// let tx = try!(conn.transaction()); /// /// try!(do_queries_part_1(conn)); // tx causes rollback if this fails @@ -299,8 +302,8 @@ impl SqliteConnection { /// ## Example /// /// ```rust,no_run - /// # use rusqlite::{SqliteConnection, SqliteResult}; - /// fn create_tables(conn: &SqliteConnection) -> SqliteResult<()> { + /// # use rusqlite::{Connection, SqliteResult}; + /// fn create_tables(conn: &Connection) -> SqliteResult<()> { /// conn.execute_batch("BEGIN; /// CREATE TABLE foo(x INTEGER); /// CREATE TABLE bar(y TEXT); @@ -324,8 +327,8 @@ impl SqliteConnection { /// ## Example /// /// ```rust,no_run - /// # use rusqlite::{SqliteConnection}; - /// fn update_rows(conn: &SqliteConnection) { + /// # use rusqlite::{Connection}; + /// fn update_rows(conn: &Connection) { /// match conn.execute("UPDATE foo SET bar = 'baz' WHERE qux = ?", &[&1i32]) { /// Ok(updated) => println!("{} rows were updated", updated), /// Err(err) => println!("update failed: {}", err), @@ -354,8 +357,8 @@ impl SqliteConnection { /// ## Example /// /// ```rust,no_run - /// # use rusqlite::{SqliteResult,SqliteConnection}; - /// fn preferred_locale(conn: &SqliteConnection) -> SqliteResult { + /// # use rusqlite::{SqliteResult,Connection}; + /// fn preferred_locale(conn: &Connection) -> SqliteResult { /// conn.query_row("SELECT value FROM preferences WHERE name='locale'", &[], |row| { /// row.get(0) /// }) @@ -384,8 +387,8 @@ impl SqliteConnection { /// ## Example /// /// ```rust,no_run - /// # use rusqlite::{SqliteResult,SqliteConnection}; - /// fn preferred_locale(conn: &SqliteConnection) -> SqliteResult { + /// # use rusqlite::{SqliteResult,Connection}; + /// fn preferred_locale(conn: &Connection) -> SqliteResult { /// conn.query_row_and_then("SELECT value FROM preferences WHERE name='locale'", &[], |row| { /// row.get_checked(0) /// }) @@ -413,8 +416,8 @@ impl SqliteConnection { /// ## Example /// /// ```rust,no_run - /// # use rusqlite::{SqliteResult,SqliteConnection}; - /// fn preferred_locale(conn: &SqliteConnection) -> SqliteResult { + /// # use rusqlite::{SqliteResult,Connection}; + /// fn preferred_locale(conn: &Connection) -> SqliteResult { /// conn.query_row_safe("SELECT value FROM preferences WHERE name='locale'", &[], |row| { /// row.get(0) /// }) @@ -438,8 +441,8 @@ impl SqliteConnection { /// ## Example /// /// ```rust,no_run - /// # use rusqlite::{SqliteConnection, SqliteResult}; - /// fn insert_new_people(conn: &SqliteConnection) -> SqliteResult<()> { + /// # use rusqlite::{Connection, SqliteResult}; + /// fn insert_new_people(conn: &Connection) -> SqliteResult<()> { /// let mut stmt = try!(conn.prepare("INSERT INTO People (name) VALUES (?)")); /// try!(stmt.execute(&[&"Joe Smith"])); /// try!(stmt.execute(&[&"Bob Jones"])); @@ -457,7 +460,7 @@ impl SqliteConnection { /// Close the SQLite connection. /// - /// This is functionally equivalent to the `Drop` implementation for `SqliteConnection` except + /// This is functionally equivalent to the `Drop` implementation for `Connection` except /// that it returns any error encountered to the caller. /// /// # Failure @@ -474,9 +477,9 @@ impl SqliteConnection { /// ## Example /// /// ```rust,no_run - /// # use rusqlite::{SqliteConnection, SqliteResult}; + /// # use rusqlite::{Connection, SqliteResult}; /// # use std::path::{Path}; - /// fn load_my_extension(conn: &SqliteConnection) -> SqliteResult<()> { + /// fn load_my_extension(conn: &Connection) -> SqliteResult<()> { /// try!(conn.load_extension_enable()); /// try!(conn.load_extension(Path::new("my_sqlite_extension"), None)); /// conn.load_extension_disable() @@ -513,9 +516,9 @@ impl SqliteConnection { /// ## Example /// /// ```rust,no_run - /// # use rusqlite::{SqliteConnection, SqliteResult, SqliteLoadExtensionGuard}; + /// # use rusqlite::{Connection, SqliteResult, SqliteLoadExtensionGuard}; /// # use std::path::{Path}; - /// fn load_my_extension(conn: &SqliteConnection) -> SqliteResult<()> { + /// fn load_my_extension(conn: &Connection) -> SqliteResult<()> { /// let _guard = try!(SqliteLoadExtensionGuard::new(conn)); /// /// conn.load_extension("my_sqlite_extension", None) @@ -542,15 +545,15 @@ impl SqliteConnection { } } -impl fmt::Debug for SqliteConnection { +impl fmt::Debug for Connection { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("SqliteConnection") + f.debug_struct("Connection") .field("path", &self.path) .finish() } } -struct InnerSqliteConnection { +struct InnerConnection { db: *mut ffi::Struct_sqlite3, } @@ -577,10 +580,10 @@ impl Default for SqliteOpenFlags { } } -impl InnerSqliteConnection { +impl InnerConnection { fn open_with_flags(c_path: &CString, flags: SqliteOpenFlags) - -> SqliteResult { + -> SqliteResult { unsafe { let mut db: *mut ffi::sqlite3 = mem::uninitialized(); let r = ffi::sqlite3_open_v2(c_path.as_ptr(), &mut db, flags.bits(), ptr::null()); @@ -604,7 +607,7 @@ impl InnerSqliteConnection { ffi::sqlite3_close(db); return Err(e); } - Ok(InnerSqliteConnection { db: db }) + Ok(InnerConnection { db: db }) } } @@ -686,7 +689,7 @@ impl InnerSqliteConnection { } fn prepare<'a>(&mut self, - conn: &'a SqliteConnection, + conn: &'a Connection, sql: &str) -> SqliteResult> { if sql.len() >= ::std::i32::MAX as usize { @@ -713,7 +716,7 @@ impl InnerSqliteConnection { } } -impl Drop for InnerSqliteConnection { +impl Drop for InnerConnection { #[allow(unused_must_use)] fn drop(&mut self) { self.close(); @@ -722,14 +725,14 @@ impl Drop for InnerSqliteConnection { /// A prepared statement. pub struct SqliteStatement<'conn> { - conn: &'conn SqliteConnection, + conn: &'conn Connection, stmt: *mut ffi::sqlite3_stmt, needs_reset: bool, column_count: c_int, } impl<'conn> SqliteStatement<'conn> { - fn new(conn: &SqliteConnection, stmt: *mut ffi::sqlite3_stmt) -> SqliteStatement { + fn new(conn: &Connection, stmt: *mut ffi::sqlite3_stmt) -> SqliteStatement { SqliteStatement { conn: conn, stmt: stmt, @@ -758,8 +761,8 @@ impl<'conn> SqliteStatement<'conn> { /// ## Example /// /// ```rust,no_run - /// # use rusqlite::{SqliteConnection, SqliteResult}; - /// fn update_rows(conn: &SqliteConnection) -> SqliteResult<()> { + /// # use rusqlite::{Connection, SqliteResult}; + /// fn update_rows(conn: &Connection) -> SqliteResult<()> { /// let mut stmt = try!(conn.prepare("UPDATE foo SET bar = 'baz' WHERE qux = ?")); /// /// try!(stmt.execute(&[&1i32])); @@ -810,8 +813,8 @@ impl<'conn> SqliteStatement<'conn> { /// ## Example /// /// ```rust,no_run - /// # use rusqlite::{SqliteConnection, SqliteResult}; - /// fn get_names(conn: &SqliteConnection) -> SqliteResult> { + /// # use rusqlite::{Connection, SqliteResult}; + /// fn get_names(conn: &Connection) -> SqliteResult> { /// let mut stmt = try!(conn.prepare("SELECT name FROM people")); /// let mut rows = try!(stmt.query(&[])); /// @@ -994,8 +997,8 @@ where E: convert::From, /// iterator). For example: /// /// ```rust,no_run -/// # use rusqlite::{SqliteConnection, SqliteResult}; -/// fn bad_function_will_panic(conn: &SqliteConnection) -> SqliteResult { +/// # use rusqlite::{Connection, SqliteResult}; +/// fn bad_function_will_panic(conn: &Connection) -> SqliteResult { /// let mut stmt = try!(conn.prepare("SELECT id FROM my_table")); /// let mut rows = try!(stmt.query(&[])); /// @@ -1090,8 +1093,8 @@ impl<'stmt> SqliteRow<'stmt> { /// for example) this isn't an issue, but it means you cannot do something like this: /// /// ```rust,no_run - /// # use rusqlite::{SqliteConnection, SqliteResult}; - /// fn bad_function_will_panic(conn: &SqliteConnection) -> SqliteResult { + /// # use rusqlite::{Connection, SqliteResult}; + /// fn bad_function_will_panic(conn: &Connection) -> SqliteResult { /// let mut stmt = try!(conn.prepare("SELECT id FROM my_table")); /// let mut rows = try!(stmt.query(&[])); /// @@ -1164,11 +1167,11 @@ mod test { // that those types are `Send`. #[allow(dead_code, unconditional_recursion)] fn ensure_send() { - ensure_send::(); + ensure_send::(); } - pub fn checked_memory_handle() -> SqliteConnection { - SqliteConnection::open_in_memory().unwrap() + pub fn checked_memory_handle() -> Connection { + Connection::open_in_memory().unwrap() } #[test] @@ -1178,7 +1181,7 @@ mod test { let path = temp_dir.path().join("test.db3"); { - let db = SqliteConnection::open(&path).unwrap(); + let db = Connection::open(&path).unwrap(); let sql = "BEGIN; CREATE TABLE foo(x INTEGER); INSERT INTO foo VALUES(42); @@ -1187,7 +1190,7 @@ mod test { } let path_string = path.to_str().unwrap(); - let db = SqliteConnection::open(&path_string).unwrap(); + let db = Connection::open(&path_string).unwrap(); let the_answer = db.query_row("SELECT x FROM foo", &[], |r| r.get::(0)); assert_eq!(42i64, the_answer.unwrap()); @@ -1195,7 +1198,7 @@ mod test { #[test] fn test_open() { - assert!(SqliteConnection::open_in_memory().is_ok()); + assert!(Connection::open_in_memory().is_ok()); let db = checked_memory_handle(); assert!(db.close().is_ok()); @@ -1207,7 +1210,7 @@ mod test { SQLITE_OPEN_READ_ONLY | SQLITE_OPEN_READ_WRITE, SQLITE_OPEN_READ_ONLY | SQLITE_OPEN_CREATE] .iter() { - assert!(SqliteConnection::open_in_memory_with_flags(*bad_flags).is_err()); + assert!(Connection::open_in_memory_with_flags(*bad_flags).is_err()); } } diff --git a/src/load_extension_guard.rs b/src/load_extension_guard.rs index ecc93e5..2c02798 100644 --- a/src/load_extension_guard.rs +++ b/src/load_extension_guard.rs @@ -1,26 +1,26 @@ -use {SqliteResult, SqliteConnection}; +use {SqliteResult, Connection}; /// RAII guard temporarily enabling SQLite extensions to be loaded. /// /// ## Example /// /// ```rust,no_run -/// # use rusqlite::{SqliteConnection, SqliteResult, SqliteLoadExtensionGuard}; +/// # use rusqlite::{Connection, SqliteResult, SqliteLoadExtensionGuard}; /// # use std::path::{Path}; -/// fn load_my_extension(conn: &SqliteConnection) -> SqliteResult<()> { +/// fn load_my_extension(conn: &Connection) -> SqliteResult<()> { /// let _guard = try!(SqliteLoadExtensionGuard::new(conn)); /// /// conn.load_extension(Path::new("my_sqlite_extension"), None) /// } /// ``` pub struct SqliteLoadExtensionGuard<'conn> { - conn: &'conn SqliteConnection, + conn: &'conn Connection, } impl<'conn> SqliteLoadExtensionGuard<'conn> { /// Attempt to enable loading extensions. Loading extensions will be disabled when this /// guard goes out of scope. Cannot be meaningfully nested. - pub fn new(conn: &SqliteConnection) -> SqliteResult { + pub fn new(conn: &Connection) -> SqliteResult { conn.load_extension_enable().map(|_| SqliteLoadExtensionGuard { conn: conn }) } } diff --git a/src/named_params.rs b/src/named_params.rs index 49305e7..60e99d8 100644 --- a/src/named_params.rs +++ b/src/named_params.rs @@ -2,11 +2,11 @@ use libc::c_int; use super::ffi; -use {SqliteResult, SqliteError, SqliteConnection, SqliteStatement, SqliteRows, SqliteRow, +use {SqliteResult, SqliteError, Connection, SqliteStatement, SqliteRows, SqliteRow, str_to_cstring}; use types::ToSql; -impl SqliteConnection { +impl Connection { /// Convenience method to prepare and execute a single SQL statement with named parameter(s). /// /// On success, returns the number of rows that were changed or inserted or deleted (via @@ -15,8 +15,8 @@ impl SqliteConnection { /// ## Example /// /// ```rust,no_run - /// # use rusqlite::{SqliteConnection, SqliteResult}; - /// fn insert(conn: &SqliteConnection) -> SqliteResult { + /// # use rusqlite::{Connection, SqliteResult}; + /// fn insert(conn: &Connection) -> SqliteResult { /// conn.execute_named("INSERT INTO test (name) VALUES (:name)", &[(":name", &"one")]) /// } /// ``` @@ -79,8 +79,8 @@ impl<'conn> SqliteStatement<'conn> { /// ## Example /// /// ```rust,no_run - /// # use rusqlite::{SqliteConnection, SqliteResult}; - /// fn insert(conn: &SqliteConnection) -> SqliteResult { + /// # use rusqlite::{Connection, SqliteResult}; + /// fn insert(conn: &Connection) -> SqliteResult { /// let mut stmt = try!(conn.prepare("INSERT INTO test (name) VALUES (:name)")); /// stmt.execute_named(&[(":name", &"one")]) /// } @@ -105,8 +105,8 @@ impl<'conn> SqliteStatement<'conn> { /// ## Example /// /// ```rust,no_run - /// # use rusqlite::{SqliteConnection, SqliteResult, SqliteRows}; - /// fn query(conn: &SqliteConnection) -> SqliteResult<()> { + /// # use rusqlite::{Connection, SqliteResult, SqliteRows}; + /// fn query(conn: &Connection) -> SqliteResult<()> { /// let mut stmt = try!(conn.prepare("SELECT * FROM test where name = :name")); /// let mut rows = try!(stmt.query_named(&[(":name", &"one")])); /// for row in rows { @@ -146,11 +146,11 @@ impl<'conn> SqliteStatement<'conn> { #[cfg(test)] mod test { - use SqliteConnection; + use Connection; #[test] fn test_execute_named() { - let db = SqliteConnection::open_in_memory().unwrap(); + let db = Connection::open_in_memory().unwrap(); db.execute_batch("CREATE TABLE foo(x INTEGER)").unwrap(); assert_eq!(db.execute_named("INSERT INTO foo(x) VALUES (:x)", &[(":x", &1i32)]).unwrap(), @@ -167,7 +167,7 @@ mod test { #[test] fn test_stmt_execute_named() { - let db = SqliteConnection::open_in_memory().unwrap(); + let db = Connection::open_in_memory().unwrap(); let sql = "CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, flag \ INTEGER)"; db.execute_batch(sql).unwrap(); @@ -184,7 +184,7 @@ mod test { #[test] fn test_query_named() { - let db = SqliteConnection::open_in_memory().unwrap(); + let db = Connection::open_in_memory().unwrap(); let sql = "CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, flag \ INTEGER)"; db.execute_batch(sql).unwrap(); @@ -195,7 +195,7 @@ mod test { #[test] fn test_unbound_parameters_are_null() { - let db = SqliteConnection::open_in_memory().unwrap(); + let db = Connection::open_in_memory().unwrap(); let sql = "CREATE TABLE test (x TEXT, y TEXT)"; db.execute_batch(sql).unwrap(); @@ -209,7 +209,7 @@ mod test { #[test] fn test_unbound_parameters_are_reused() { - let db = SqliteConnection::open_in_memory().unwrap(); + let db = Connection::open_in_memory().unwrap(); let sql = "CREATE TABLE test (x TEXT, y TEXT)"; db.execute_batch(sql).unwrap(); diff --git a/src/trace.rs b/src/trace.rs index 682662c..f79f619 100644 --- a/src/trace.rs +++ b/src/trace.rs @@ -8,7 +8,7 @@ use std::str; use std::time::Duration; use super::ffi; -use {SqliteError, SqliteResult, SqliteConnection}; +use {SqliteError, SqliteResult, Connection}; /// Set up the process-wide SQLite error logging callback. /// This function is marked unsafe for two reasons: @@ -60,7 +60,7 @@ pub fn log(err_code: c_int, msg: &str) { } } -impl SqliteConnection { +impl Connection { /// Register or clear a callback function that can be used for tracing the execution of SQL statements. /// /// Prepared statement placeholders are replaced/logged with their assigned values. @@ -120,7 +120,7 @@ mod test { use std::sync::Mutex; use std::time::Duration; - use SqliteConnection; + use Connection; #[test] fn test_trace() { @@ -132,7 +132,7 @@ mod test { traced_stmts.push(s.to_owned()); } - let mut db = SqliteConnection::open_in_memory().unwrap(); + let mut db = Connection::open_in_memory().unwrap(); db.trace(Some(tracer)); { let _ = db.query_row("SELECT ?", &[&1i32], |_| {}); @@ -160,7 +160,7 @@ mod test { profiled.push((s.to_owned(), d)); } - let mut db = SqliteConnection::open_in_memory().unwrap(); + let mut db = Connection::open_in_memory().unwrap(); db.profile(Some(profiler)); db.execute_batch("PRAGMA application_id = 1").unwrap(); db.profile(None); diff --git a/src/transaction.rs b/src/transaction.rs index 1981f24..a26cc9d 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -1,4 +1,4 @@ -use {SqliteResult, SqliteConnection}; +use {SqliteResult, Connection}; pub use SqliteTransactionBehavior::{SqliteTransactionDeferred, SqliteTransactionImmediate, SqliteTransactionExclusive}; @@ -22,10 +22,10 @@ pub enum SqliteTransactionBehavior { /// ## Example /// /// ```rust,no_run -/// # use rusqlite::{SqliteConnection, SqliteResult}; -/// # fn do_queries_part_1(conn: &SqliteConnection) -> SqliteResult<()> { Ok(()) } -/// # fn do_queries_part_2(conn: &SqliteConnection) -> SqliteResult<()> { Ok(()) } -/// fn perform_queries(conn: &SqliteConnection) -> SqliteResult<()> { +/// # use rusqlite::{Connection, SqliteResult}; +/// # fn do_queries_part_1(conn: &Connection) -> SqliteResult<()> { Ok(()) } +/// # fn do_queries_part_2(conn: &Connection) -> SqliteResult<()> { Ok(()) } +/// fn perform_queries(conn: &Connection) -> SqliteResult<()> { /// let tx = try!(conn.transaction()); /// /// try!(do_queries_part_1(conn)); // tx causes rollback if this fails @@ -35,7 +35,7 @@ pub enum SqliteTransactionBehavior { /// } /// ``` pub struct SqliteTransaction<'conn> { - conn: &'conn SqliteConnection, + conn: &'conn Connection, depth: u32, commit: bool, finished: bool, @@ -43,7 +43,7 @@ pub struct SqliteTransaction<'conn> { impl<'conn> SqliteTransaction<'conn> { /// Begin a new transaction. Cannot be nested; see `savepoint` for nested transactions. - pub fn new(conn: &SqliteConnection, + pub fn new(conn: &Connection, behavior: SqliteTransactionBehavior) -> SqliteResult { let query = match behavior { @@ -71,9 +71,9 @@ impl<'conn> SqliteTransaction<'conn> { /// ## Example /// /// ```rust,no_run - /// # use rusqlite::{SqliteConnection, SqliteResult}; - /// # fn perform_queries_part_1_succeeds(conn: &SqliteConnection) -> bool { true } - /// fn perform_queries(conn: &SqliteConnection) -> SqliteResult<()> { + /// # use rusqlite::{Connection, SqliteResult}; + /// # fn perform_queries_part_1_succeeds(conn: &Connection) -> bool { true } + /// fn perform_queries(conn: &Connection) -> SqliteResult<()> { /// let tx = try!(conn.transaction()); /// /// { @@ -173,10 +173,10 @@ impl<'conn> Drop for SqliteTransaction<'conn> { #[cfg(test)] mod test { - use SqliteConnection; + use Connection; - fn checked_memory_handle() -> SqliteConnection { - let db = SqliteConnection::open_in_memory().unwrap(); + fn checked_memory_handle() -> Connection { + let db = Connection::open_in_memory().unwrap(); db.execute_batch("CREATE TABLE foo (x INTEGER)").unwrap(); db } diff --git a/src/types.rs b/src/types.rs index ddc0234..bf5f86c 100644 --- a/src/types.rs +++ b/src/types.rs @@ -176,12 +176,12 @@ impl ToSql for Option { /// ```rust,no_run /// # extern crate libc; /// # extern crate rusqlite; -/// # use rusqlite::{SqliteConnection, SqliteResult}; +/// # use rusqlite::{Connection, SqliteResult}; /// # use rusqlite::types::{Null}; /// # use libc::{c_int}; /// fn main() { /// } -/// fn insert_null(conn: &SqliteConnection) -> SqliteResult { +/// fn insert_null(conn: &Connection) -> SqliteResult { /// conn.execute("INSERT INTO people (name) VALUES (?)", &[&Null]) /// } /// ``` @@ -305,13 +305,13 @@ impl FromSql for Option { #[cfg(test)] mod test { - use SqliteConnection; + use Connection; use ffi; use super::time; use libc::{c_int, c_double}; - fn checked_memory_handle() -> SqliteConnection { - let db = SqliteConnection::open_in_memory().unwrap(); + fn checked_memory_handle() -> Connection { + let db = Connection::open_in_memory().unwrap(); db.execute_batch("CREATE TABLE foo (b BLOB, t TEXT, i INTEGER, f FLOAT, n)").unwrap(); db }