Skip to content

Commit

Permalink
Reorder the functions
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanUkhov committed Oct 6, 2023
1 parent 7269972 commit d4fd98b
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 199 deletions.
48 changes: 24 additions & 24 deletions src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,6 @@ impl Connection {
crate::statement::new(self.raw.0, statement)
}

/// Return the number of rows inserted, updated, or deleted by the most
/// recent INSERT, UPDATE, or DELETE statement.
#[inline]
pub fn change_count(&self) -> usize {
unsafe { ffi::sqlite3_changes(self.raw.0) as usize }
}

/// Return the total number of rows inserted, updated, and deleted by all
/// INSERT, UPDATE, and DELETE statements since the connection was opened.
#[inline]
pub fn total_change_count(&self) -> usize {
unsafe { ffi::sqlite3_total_changes(self.raw.0) as usize }
}

/// Set a callback for handling busy events.
///
/// The callback is triggered when the database cannot perform an operation
Expand Down Expand Up @@ -200,9 +186,21 @@ impl Connection {
Ok(())
}

/// Disable loading extensions.
#[inline]
pub fn disable_extension(&self) -> Result<()> {
unsafe {
ok!(
self.raw.0,
ffi::sqlite3_enable_load_extension(self.raw.0, 0 as c_int)
);
}
Ok(())
}

/// Load an extension.
#[inline]
pub fn extend<T: AsRef<str>>(&self, name: T) -> Result<()> {
pub fn load_extension<T: AsRef<str>>(&self, name: T) -> Result<()> {
unsafe {
ok!(
self.raw.0,
Expand All @@ -217,16 +215,18 @@ impl Connection {
Ok(())
}

/// Disable loading extensions.
/// Return the number of rows inserted, updated, or deleted by the most
/// recent INSERT, UPDATE, or DELETE statement.
#[inline]
pub fn disable_extension(&self) -> Result<()> {
unsafe {
ok!(
self.raw.0,
ffi::sqlite3_enable_load_extension(self.raw.0, 0 as c_int)
);
}
Ok(())
pub fn change_count(&self) -> usize {
unsafe { ffi::sqlite3_changes(self.raw.0) as usize }
}

/// Return the total number of rows inserted, updated, and deleted by all
/// INSERT, UPDATE, and DELETE statements since the connection was opened.
#[inline]
pub fn total_change_count(&self) -> usize {
unsafe { ffi::sqlite3_total_changes(self.raw.0) as usize }
}

#[doc(hidden)]
Expand Down
62 changes: 31 additions & 31 deletions src/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,37 @@ impl<'l> Statement<'l> {
Ok(())
}

/// Create a cursor.
#[inline]
pub fn iter(&mut self) -> Cursor<'l, '_> {
self.into()
}

/// Advance to the next state.
///
/// The function should be called multiple times until `State::Done` is
/// reached in order to evaluate the statement entirely.
#[allow(clippy::should_implement_trait)]
pub fn next(&mut self) -> Result<State> {
Ok(match unsafe { ffi::sqlite3_step(self.raw.0) } {
ffi::SQLITE_ROW => State::Row,
ffi::SQLITE_DONE => State::Done,
code => error!(self.raw.1, code),
})
}

/// Read a value from a column.
///
/// In case of integer indices, the first column has index 0.
#[inline]
pub fn read<T, U>(&self, index: U) -> Result<T>
where
T: ReadableWithIndex,
U: ColumnIndex,
{
ReadableWithIndex::read(self, index)
}

/// Return the number of columns.
#[inline]
pub fn column_count(&self) -> usize {
Expand Down Expand Up @@ -199,25 +230,6 @@ impl<'l> Statement<'l> {
)
}

/// Create a cursor.
#[inline]
pub fn iter(&mut self) -> Cursor<'l, '_> {
self.into()
}

/// Advance to the next state.
///
/// The function should be called multiple times until `State::Done` is
/// reached in order to evaluate the statement entirely.
#[allow(clippy::should_implement_trait)]
pub fn next(&mut self) -> Result<State> {
Ok(match unsafe { ffi::sqlite3_step(self.raw.0) } {
ffi::SQLITE_ROW => State::Row,
ffi::SQLITE_DONE => State::Done,
code => error!(self.raw.1, code),
})
}

/// Return the index for a named parameter if exists.
///
/// # Examples
Expand All @@ -241,18 +253,6 @@ impl<'l> Statement<'l> {
}
}

/// Read a value from a column.
///
/// In case of integer indices, the first column has index 0.
#[inline]
pub fn read<T, U>(&self, index: U) -> Result<T>
where
T: ReadableWithIndex,
U: ColumnIndex,
{
ReadableWithIndex::read(self, index)
}

/// Reset the internal state.
#[inline]
pub fn reset(&mut self) -> Result<()> {
Expand Down
130 changes: 65 additions & 65 deletions tests/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,40 @@ use common::setup_users;
macro_rules! ok(($result:expr) => ($result.unwrap()));

#[test]
fn enable_extension() {
let connection = ok!(Connection::open(":memory:"));
ok!(connection.enable_extension());
}
fn open_with_flags() {
use temporary::Directory;

#[test]
fn extend() {
let connection = ok!(Connection::open(":memory:"));
ok!(connection.enable_extension());
assert!(connection.extend("libsqlitefunctions").is_err());
}
let directory = ok!(Directory::new("sqlite"));
let path = directory.path().join("database.sqlite3");
setup_users(&path);

#[test]
fn disable_extension() {
let connection = ok!(Connection::open(":memory:"));
ok!(connection.disable_extension());
let flags = OpenFlags::new().set_read_only();
let connection = ok!(Connection::open_with_flags(path, flags));
match connection.execute("INSERT INTO users VALUES (2, 'Bob', NULL, NULL)") {
Err(_) => {}
_ => unreachable!(),
}
}

#[test]
fn change_count() {
let connection = setup_users(":memory:");
assert_eq!(connection.change_count(), 1);
assert_eq!(connection.total_change_count(), 1);

ok!(connection.execute("INSERT INTO users VALUES (2, 'Bob', NULL, NULL, NULL)"));
assert_eq!(connection.change_count(), 1);
assert_eq!(connection.total_change_count(), 2);
fn open_with_full_mutex() {
use std::sync::Arc;
use std::thread;

ok!(connection.execute("UPDATE users SET name = 'Bob' WHERE id = 1"));
assert_eq!(connection.change_count(), 1);
assert_eq!(connection.total_change_count(), 3);
let connection = ok!(Connection::open_with_full_mutex(":memory:"));
let connection = Arc::new(connection);

ok!(connection.execute("DELETE FROM users"));
assert_eq!(connection.change_count(), 2);
assert_eq!(connection.total_change_count(), 5);
let mut threads = Vec::new();
for _ in 0..5 {
let connection_ = connection.clone();
let thread = thread::spawn(move || {
ok!(connection_.execute("SELECT 1"));
});
threads.push(thread);
}
for thread in threads {
ok!(thread.join());
}
}

#[test]
Expand Down Expand Up @@ -82,43 +81,6 @@ fn iterate() {
assert!(done);
}

#[test]
fn open_with_flags() {
use temporary::Directory;

let directory = ok!(Directory::new("sqlite"));
let path = directory.path().join("database.sqlite3");
setup_users(&path);

let flags = OpenFlags::new().set_read_only();
let connection = ok!(Connection::open_with_flags(path, flags));
match connection.execute("INSERT INTO users VALUES (2, 'Bob', NULL, NULL)") {
Err(_) => {}
_ => unreachable!(),
}
}

#[test]
fn open_with_full_mutex() {
use std::sync::Arc;
use std::thread;

let connection = ok!(Connection::open_with_full_mutex(":memory:"));
let connection = Arc::new(connection);

let mut threads = Vec::new();
for _ in 0..5 {
let connection_ = connection.clone();
let thread = thread::spawn(move || {
ok!(connection_.execute("SELECT 1"));
});
threads.push(thread);
}
for thread in threads {
ok!(thread.join());
}
}

#[test]
fn set_busy_handler() {
use std::thread;
Expand Down Expand Up @@ -151,3 +113,41 @@ fn set_busy_handler() {
assert!(ok!(guard.join()));
}
}

#[test]
fn enable_extension() {
let connection = ok!(Connection::open(":memory:"));
ok!(connection.enable_extension());
}

#[test]
fn disable_extension() {
let connection = ok!(Connection::open(":memory:"));
ok!(connection.disable_extension());
}

#[test]
fn load_extension() {
let connection = ok!(Connection::open(":memory:"));
ok!(connection.enable_extension());
assert!(connection.load_extension("libsqlitefunctions").is_err());
}

#[test]
fn change_count() {
let connection = setup_users(":memory:");
assert_eq!(connection.change_count(), 1);
assert_eq!(connection.total_change_count(), 1);

ok!(connection.execute("INSERT INTO users VALUES (2, 'Bob', NULL, NULL, NULL)"));
assert_eq!(connection.change_count(), 1);
assert_eq!(connection.total_change_count(), 2);

ok!(connection.execute("UPDATE users SET name = 'Bob' WHERE id = 1"));
assert_eq!(connection.change_count(), 1);
assert_eq!(connection.total_change_count(), 3);

ok!(connection.execute("DELETE FROM users"));
assert_eq!(connection.change_count(), 2);
assert_eq!(connection.total_change_count(), 5);
}
50 changes: 25 additions & 25 deletions tests/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,29 @@ fn bind_iter() {
}

#[test]
fn column_count() {
fn iter() {
let connection = setup_users(":memory:");
ok!(connection.execute("INSERT INTO users VALUES (2, 'Bob', NULL, NULL, NULL)"));
let query = "SELECT id, age FROM users ORDER BY 1 DESC";
let mut statement = ok!(connection.prepare(query));

let mut count = 0;
for row in statement.iter().map(|row| ok!(row)) {
let id = row.read::<i64, _>("id");
if id == 1 {
assert_eq!(row.read::<f64, _>("age"), 42.69);
} else if id == 2 {
assert_eq!(row.read::<Option<f64>, _>("age"), None);
} else {
assert!(false);
}
count += 1;
}
assert_eq!(count, 2);
}

#[test]
fn iter_column_count() {
let connection = setup_english(":memory:");
let query = "SELECT value FROM english WHERE value LIKE '%type'";
let mut statement = ok!(connection.prepare(query));
Expand All @@ -34,7 +56,7 @@ fn column_count() {
}

#[test]
fn column_type() {
fn iter_column_type() {
let connection = setup_english(":memory:");
let query = "SELECT value FROM english WHERE value LIKE '%type'";
let mut statement = ok!(connection.prepare(query));
Expand All @@ -61,36 +83,14 @@ fn column_type() {
}

#[test]
fn count() {
fn iter_count() {
let connection = setup_english(":memory:");
let query = "SELECT value FROM english WHERE value LIKE '%type'";
let mut statement = ok!(connection.prepare(query));

assert_eq!(statement.iter().filter(|row| row.is_ok()).count(), 6);
}

#[test]
fn iter() {
let connection = setup_users(":memory:");
ok!(connection.execute("INSERT INTO users VALUES (2, 'Bob', NULL, NULL, NULL)"));
let query = "SELECT id, age FROM users ORDER BY 1 DESC";
let mut statement = ok!(connection.prepare(query));

let mut count = 0;
for row in statement.iter().map(|row| ok!(row)) {
let id = row.read::<i64, _>("id");
if id == 1 {
assert_eq!(row.read::<f64, _>("age"), 42.69);
} else if id == 2 {
assert_eq!(row.read::<Option<f64>, _>("age"), None);
} else {
assert!(false);
}
count += 1;
}
assert_eq!(count, 2);
}

#[test]
fn next_index() {
let connection = setup_users(":memory:");
Expand Down
Loading

0 comments on commit d4fd98b

Please sign in to comment.