Connection

Struct Connection 

Source
pub struct Connection { /* private fields */ }
Expand description

A sqlite database connection.

Connections are not thread-safe objects.

§Examples

Opening a connection to a filesystem path:

use sqlite_ll::Connection;

let c = Connection::open("database.db")?;
c.execute("CREATE TABLE test (id INTEGER);")?;

Opening an in-memory database:

use sqlite_ll::Connection;

let c = Connection::open_memory()?;
c.execute("CREATE TABLE test (id INTEGER);")?;

Implementations§

Source§

impl Connection

Source

pub fn open(path: impl AsRef<Path>) -> Result<Connection>

Available on crate feature std only.

Open a database to the given path.

Note that it is possible to open an in-memory database by passing ":memory:" here, this call might require allocating depending on the platform, so it should be avoided in favor of using memory. To avoid allocating for regular paths, you can use open_c_str, however you are responsible for ensuring the c-string is a valid path.

This is the same as calling OpenOptions::new().read_write().create().open(path).

Source

pub fn open_c_str(name: &CStr) -> Result<Connection>

Open a database connection with a raw c-string.

This can be used to open in-memory databases by passing c":memory:" or a regular open call with a filesystem path like c"/path/to/database.sql".

This is the same as calling OpenOptions::new().read_write().create().open_c_str(name).

Source

pub fn open_memory() -> Result<Connection>

Open an in-memory database.

This is the same as calling OpenOptions::new().read_write().create().open_memory().

Source

pub fn execute(&self, stmt: impl AsRef<str>) -> Result<()>

Execute a statement without processing the resulting rows if any.

Source

pub fn prepare(&self, stmt: impl AsRef<str>) -> Result<Statement>

Build a prepared statement.

This is the same as calling prepare_with with Prepare::EMPTY.

The database connection will be kept open for the lifetime of this statement.

§Errors

If the prepare call contains multiple statements, it will error. To execute multiple statements, use execute instead.

use sqlite_ll::{Connection, Code};

let c = Connection::open_memory()?;

let e = c.prepare(
    "
    CREATE TABLE test (id INTEGER) /* test */;
    INSERT INTO test (id) VALUES (1);
    "
).unwrap_err();

assert_eq!(e.code(), Code::ERROR);
§Examples
use sqlite_ll::{Connection, State, Prepare};

let c = Connection::open_memory()?;
c.execute("CREATE TABLE test (id INTEGER);")?;

let mut insert_stmt = c.prepare("INSERT INTO test (id) VALUES (?);")?;
let mut query_stmt = c.prepare("SELECT id FROM test;")?;

drop(c);

insert_stmt.reset()?;
insert_stmt.bind(1, 42)?;
assert_eq!(insert_stmt.step()?, State::Done);

query_stmt.reset()?;

while let State::Row = query_stmt.step()? {
    let id: i64 = query_stmt.read(0)?;
    assert_eq!(id, 42);
}
Source

pub fn prepare_with( &self, stmt: impl AsRef<str>, flags: Prepare, ) -> Result<Statement>

Build a prepared statement with custom flags.

For long-running statements it is recommended that they have the Prepare::PERSISTENT flag set.

The database connection will be kept open for the lifetime of this statement.

§Errors

If the prepare call contains multiple statements, it will error. To execute multiple statements, use execute instead.

use sqlite_ll::{Connection, Code, Prepare};

let c = Connection::open_memory()?;

let e = c.prepare_with(
    "
    CREATE TABLE test (id INTEGER) /* test */;
    INSERT INTO test (id) VALUES (1);
    ",
    Prepare::PERSISTENT
).unwrap_err();
assert_eq!(e.code(), Code::ERROR);
§Examples
use sqlite_ll::{Connection, State, Prepare};

let c = Connection::open_memory()?;
c.execute("CREATE TABLE test (id INTEGER);")?;

let mut insert_stmt = c.prepare_with("INSERT INTO test (id) VALUES (?);", Prepare::PERSISTENT)?;
let mut query_stmt = c.prepare_with("SELECT id FROM test;", Prepare::PERSISTENT)?;

drop(c);

/* .. */

insert_stmt.reset()?;
insert_stmt.bind(1, 42)?;
assert_eq!(insert_stmt.step()?, State::Done);

query_stmt.reset()?;

while let State::Row = query_stmt.step()? {
    let id: i64 = query_stmt.read(0)?;
    assert_eq!(id, 42);
}
Source

pub fn change_count(&self) -> usize

Return the number of rows inserted, updated, or deleted by the most recent INSERT, UPDATE, or DELETE statement.

Source

pub fn total_change_count(&self) -> usize

Return the total number of rows inserted, updated, and deleted by all INSERT, UPDATE, and DELETE statements since the connection was opened.

Source

pub fn set_busy_handler<F>(&mut self, callback: F) -> Result<()>
where F: FnMut(usize) -> bool + Send + 'static,

Set a callback for handling busy events.

The callback is triggered when the database cannot perform an operation due to processing of some other request. If the callback returns true, the operation will be repeated.

Source

pub fn set_busy_timeout(&mut self, ms: c_int) -> Result<()>

Set an implicit callback for handling busy events that tries to repeat rejected operations until a timeout expires.

Source

pub fn remove_busy_handler(&mut self) -> Result<()>

Remove the callback handling busy events.

Trait Implementations§

Source§

impl Drop for Connection

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for Connection

Connection is Send.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.