pub struct D1Connection { /* private fields */ }Expand description
A connection to a D1 database: a Workers D1 binding.
There is no socket behind it and nothing to pool. Build one per request
from the binding, with D1Connection::from_env or D1Connection::new.
Both &mut D1Connection and &D1Connection are sqlx executors. The
binding is stateless, so a shared reference is enough to run a query, and
one connection can serve concurrent queries within a request.
Implementations§
Source§impl D1Connection
impl D1Connection
Sourcepub fn execute_batch<'q, I>(
&self,
queries: I,
) -> impl Future<Output = Result<Vec<D1QueryResult>, Error>> + Send + '_
pub fn execute_batch<'q, I>( &self, queries: I, ) -> impl Future<Output = Result<Vec<D1QueryResult>, Error>> + Send + '_
Runs queries as one D1 batch: in order, in one round trip, and
atomically – if any statement fails, none of them take effect.
This is the D1 answer to a transaction, which it does not support
across calls (see D1TransactionManager).
Returns what each statement did, in the order given; for the rows each
statement returns as well, see fetch_batch.
let results = conn
.execute_batch([
sqlx::query("UPDATE accounts SET balance = balance - ?1 WHERE id = ?2")
.bind(amount)
.bind(from),
sqlx::query("UPDATE accounts SET balance = balance + ?1 WHERE id = ?2")
.bind(amount)
.bind(to),
])
.await?;§Errors
When binding an argument fails, or when D1 rejects any statement – in which case the whole batch was rolled back.
Sourcepub fn fetch_batch<'q, I>(
&self,
queries: I,
) -> impl Stream<Item = Result<D1BatchResult, Error>> + Send + '_
pub fn fetch_batch<'q, I>( &self, queries: I, ) -> impl Stream<Item = Result<D1BatchResult, Error>> + Send + '_
Runs queries as one D1 batch, as
execute_batch does, and streams each
statement’s rows along with what it did, in the order given.
The shape sqlc’s :batchexec, :batchone and :batchmany queries
take, each a stream with one item per statement – but atomic, and in
one round trip. So unlike a loop of queries it is not lazy: the whole
batch runs when the stream is first polled, and a failure is the one
item it yields, with nothing applied.
use futures_util::TryStreamExt;
use sqlx::Row;
let names: Vec<String> = conn
.fetch_batch([1_i64, 2, 3].map(|id| {
sqlx::query("SELECT name FROM users WHERE id = ?").bind(id)
}))
.map_ok(|result| result.rows()[0].get("name"))
.try_collect()
.await?;§Rows come back keyed by name
D1’s batch() returns rows as objects, not the arrays the executor
reads. Two columns with one name – SELECT a.id, b.id – collapse to
the last, and a column named like an integer is ordered first. Alias
such columns apart inside a batch.
§Errors
The stream’s one item is an error when binding an argument fails, or when D1 rejects any statement – in which case the whole batch was rolled back.
Source§impl D1Connection
impl D1Connection
Sourcepub fn new(db: D1Database) -> Self
pub fn new(db: D1Database) -> Self
Wraps a D1 binding.
Sourcepub fn from_env(env: &Env, binding: &str) -> Result<Self>
pub fn from_env(env: &Env, binding: &str) -> Result<Self>
Looks up the D1 binding called binding in the Worker’s environment.
§Errors
When there is no binding by that name, or it is not a D1 database.
Sourcepub fn database(&self) -> &D1Database
pub fn database(&self) -> &D1Database
The binding this connection wraps, for anything this crate does not
cover – dump, exec, sessions – while keeping the connection.
Sourcepub fn into_database(self) -> D1Database
pub fn into_database(self) -> D1Database
The binding this connection wraps, giving up the connection. See
database to borrow it instead.
Trait Implementations§
Source§impl<'c> Acquire<'c> for &'c mut D1Connection
impl<'c> Acquire<'c> for &'c mut D1Connection
Source§impl Connection for D1Connection
impl Connection for D1Connection
Source§fn begin(
&mut self,
) -> impl Future<Output = Result<Transaction<'_, D1>, Error>> + Send + '_
fn begin( &mut self, ) -> impl Future<Output = Result<Transaction<'_, D1>, Error>> + Send + '_
Always fails – see D1TransactionManager.
type Database = D1
type Options = D1ConnectOptions
Source§fn close(self) -> impl Future<Output = Result<(), Error>> + Send + 'static
fn close(self) -> impl Future<Output = Result<(), Error>> + Send + 'static
Source§fn ping(&mut self) -> impl Future<Output = Result<(), Error>> + Send + '_
fn ping(&mut self) -> impl Future<Output = Result<(), Error>> + Send + '_
Source§fn shrink_buffers(&mut self)
fn shrink_buffers(&mut self)
Source§fn begin_with(
&mut self,
statement: impl SqlSafeStr,
) -> impl Future<Output = Result<Transaction<'_, Self::Database>, Error>> + Sendwhere
Self: Sized,
fn begin_with(
&mut self,
statement: impl SqlSafeStr,
) -> impl Future<Output = Result<Transaction<'_, Self::Database>, Error>> + Sendwhere
Self: Sized,
Source§fn is_in_transaction(&self) -> bool
fn is_in_transaction(&self) -> bool
true if the connection is currently in a transaction. Read moreSource§fn transaction<'a, F, R, E>(
&'a mut self,
callback: F,
) -> impl Future<Output = Result<R, E>> + Send + 'a
fn transaction<'a, F, R, E>( &'a mut self, callback: F, ) -> impl Future<Output = Result<R, E>> + Send + 'a
Source§impl Debug for D1Connection
impl Debug for D1Connection
Source§impl<'c> Executor<'c> for &'c D1Connection
impl<'c> Executor<'c> for &'c D1Connection
Source§fn execute<'e, 'q: 'e, E>(
self,
query: E,
) -> BoxFuture<'e, Result<D1QueryResult, Error>>
fn execute<'e, 'q: 'e, E>( self, query: E, ) -> BoxFuture<'e, Result<D1QueryResult, Error>>
Runs the query with run(), which reports rows_affected and
last_insert_rowid but no rows.
Source§fn fetch_many<'e, 'q: 'e, E>(
self,
query: E,
) -> BoxStream<'e, Result<Either<D1QueryResult, D1Row>, Error>>
fn fetch_many<'e, 'q: 'e, E>( self, query: E, ) -> BoxStream<'e, Result<Either<D1QueryResult, D1Row>, Error>>
Yields the rows only, never a D1QueryResult: rows come from
raw({ columnNames: true }), which reports nothing else. Use
execute for what a statement changed.
type Database = D1
Source§fn execute_many<'e, 'q: 'e, E>(
self,
query: E,
) -> BoxStream<'e, Result<D1QueryResult, Error>>
fn execute_many<'e, 'q: 'e, E>( self, query: E, ) -> BoxStream<'e, Result<D1QueryResult, Error>>
Source§fn fetch_optional<'e, 'q: 'e, E>(
self,
query: E,
) -> BoxFuture<'e, Result<Option<D1Row>, Error>>
fn fetch_optional<'e, 'q: 'e, E>( self, query: E, ) -> BoxFuture<'e, Result<Option<D1Row>, Error>>
Source§fn prepare_with<'e>(
self,
sql: SqlStr,
_parameters: &'e [D1TypeInfo],
) -> BoxFuture<'e, Result<D1Statement, Error>>where
'c: 'e,
fn prepare_with<'e>(
self,
sql: SqlStr,
_parameters: &'e [D1TypeInfo],
) -> BoxFuture<'e, Result<D1Statement, Error>>where
'c: 'e,
Source§fn fetch<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::Row, Error>> + Send + 'e>>
fn fetch<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::Row, Error>> + Send + 'e>>
Source§fn fetch_all<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Future<Output = Result<Vec<<Self::Database as Database>::Row>, Error>> + Send + 'e>>
fn fetch_all<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Future<Output = Result<Vec<<Self::Database as Database>::Row>, Error>> + Send + 'e>>
Vec.Source§impl<'c> Executor<'c> for &'c mut D1Connection
impl<'c> Executor<'c> for &'c mut D1Connection
type Database = D1
Source§fn execute<'e, 'q: 'e, E>(
self,
query: E,
) -> BoxFuture<'e, Result<D1QueryResult, Error>>
fn execute<'e, 'q: 'e, E>( self, query: E, ) -> BoxFuture<'e, Result<D1QueryResult, Error>>
Source§fn execute_many<'e, 'q: 'e, E>(
self,
query: E,
) -> BoxStream<'e, Result<D1QueryResult, Error>>
fn execute_many<'e, 'q: 'e, E>( self, query: E, ) -> BoxStream<'e, Result<D1QueryResult, Error>>
Source§fn fetch_many<'e, 'q: 'e, E>(
self,
query: E,
) -> BoxStream<'e, Result<Either<D1QueryResult, D1Row>, Error>>
fn fetch_many<'e, 'q: 'e, E>( self, query: E, ) -> BoxStream<'e, Result<Either<D1QueryResult, D1Row>, Error>>
Source§fn fetch_optional<'e, 'q: 'e, E>(
self,
query: E,
) -> BoxFuture<'e, Result<Option<D1Row>, Error>>
fn fetch_optional<'e, 'q: 'e, E>( self, query: E, ) -> BoxFuture<'e, Result<Option<D1Row>, Error>>
Source§fn prepare_with<'e>(
self,
sql: SqlStr,
parameters: &'e [D1TypeInfo],
) -> BoxFuture<'e, Result<D1Statement, Error>>where
'c: 'e,
fn prepare_with<'e>(
self,
sql: SqlStr,
parameters: &'e [D1TypeInfo],
) -> BoxFuture<'e, Result<D1Statement, Error>>where
'c: 'e,
Source§fn fetch<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::Row, Error>> + Send + 'e>>
fn fetch<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::Row, Error>> + Send + 'e>>
Source§fn fetch_all<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Future<Output = Result<Vec<<Self::Database as Database>::Row>, Error>> + Send + 'e>>
fn fetch_all<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Future<Output = Result<Vec<<Self::Database as Database>::Row>, Error>> + Send + 'e>>
Vec.Auto Trait Implementations§
impl Freeze for D1Connection
impl RefUnwindSafe for D1Connection
impl Send for D1Connection
impl Sync for D1Connection
impl Unpin for D1Connection
impl UnsafeUnpin for D1Connection
impl UnwindSafe for D1Connection
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more