pub struct OwnedConnection { /* private fields */ }Expand description
A duckdb_connection this crate opened and will disconnect on drop.
Open one during registration when the extension needs to run SQL later —
from a background thread, or from a callback, where the C API offers no way
back to a connection. A connection created from the load-time
duckdb_database holds a shared_ptr to the database instance, so it
outlives extension loading.
This is not the connection DuckDB passes to your entry point; that one is
borrowed and is disconnected when registration returns.
Implementations§
Source§impl OwnedConnection
impl OwnedConnection
Sourcepub unsafe fn open(db: duckdb_database) -> Result<Self, ExtensionError>
pub unsafe fn open(db: duckdb_database) -> Result<Self, ExtensionError>
Opens a new connection to db.
§Errors
Returns ExtensionError if DuckDB refuses to open the connection.
§Safety
db must be a valid duckdb_database, such as the one obtained from
Connection::as_raw_database.
Sourcepub fn query(&self, sql: &str) -> Result<QueryResult, ExtensionError>
pub fn query(&self, sql: &str) -> Result<QueryResult, ExtensionError>
Sourcepub fn prepare(&self, sql: &str) -> Result<PreparedStatement, ExtensionError>
pub fn prepare(&self, sql: &str) -> Result<PreparedStatement, ExtensionError>
Sourcepub const fn interrupt_handle(&self) -> InterruptHandle<'_>
pub const fn interrupt_handle(&self) -> InterruptHandle<'_>
Returns a Send + Sync handle for cancelling a query running on this
connection, or reading its progress, from another thread.
The handle borrows the connection, so it cannot outlive the
duckdb_disconnect in Drop.
Sourcepub fn interrupt(&self)
pub fn interrupt(&self)
Requests cancellation of whatever this connection is currently running.
See interrupt for what “cancellation” means here. To cancel from
another thread, use interrupt_handle.
Sourcepub fn progress(&self) -> QueryProgress
pub fn progress(&self) -> QueryProgress
Reads the progress of whatever this connection is currently running.
Sourcepub const fn as_raw(&self) -> duckdb_connection
pub const fn as_raw(&self) -> duckdb_connection
Returns the raw handle. Do not disconnect it — this value still owns it.
Sourcepub const unsafe fn from_raw(con: duckdb_connection) -> Self
pub const unsafe fn from_raw(con: duckdb_connection) -> Self
Takes ownership of a raw connection, which is then disconnected
(duckdb_disconnect) on drop.
§Safety
con must be a live connection from duckdb_connect that nothing
else disconnects. In particular not the connection DuckDB passes
to an extension’s entry point
(Connection::as_raw_connection):
that one is borrowed, and the entry point disconnects it.
§Example
use quack_rs::query::OwnedConnection;
let mut raw: libduckdb_sys::duckdb_connection = std::ptr::null_mut();
// SAFETY: `db` is open.
if unsafe { libduckdb_sys::duckdb_connect(db, &raw mut raw) } == libduckdb_sys::DuckDBSuccess {
// SAFETY: a fresh connection owned by nothing else.
let con = unsafe { OwnedConnection::from_raw(raw) };
let _ = con.execute("SELECT 1");
}Sourcepub const fn into_raw(self) -> duckdb_connection
pub const fn into_raw(self) -> duckdb_connection
Gives up ownership and returns the raw connection, which the caller
must disconnect (duckdb_disconnect) or hand back to
from_raw.