pub struct DatabaseRef { /* private fields */ }Expand description
A handle to a Database with a fixed access level, created by
Database::admin, Database::write, or Database::read.
DatabaseRef is Copy and can be freely passed to other code. Calls run as
the database user for its access level. If the runtime has no user for that
level, it falls back to the next higher level and sets readonly = 2 on
read handles so writes are still rejected.
Implementations§
Source§impl DatabaseRef
impl DatabaseRef
Sourcepub fn query(&self, sql: impl Into<String>) -> Query<'static>
pub fn query(&self, sql: impl Into<String>) -> Query<'static>
Starts a query. Set parameters, settings, and an optional query ID on
the returned Query, then run it with a fetch method.
async fn kinds(id: u64) -> Result<Vec<String>, Error> {
EVENTS
.read()
.query("SELECT kind FROM events WHERE id = {id:UInt64}")
.param("id", id)
.fetch_all()
.await
}Sourcepub fn exec(&self, sql: impl Into<String>) -> Exec<'static>
pub fn exec(&self, sql: impl Into<String>) -> Exec<'static>
Starts a statement that returns no rows, such as ALTER TABLE … UPDATE
or INSERT … SELECT. Set parameters and settings on the returned
Exec, then await it for an ExecResult. Any rows returned by the
server are discarded.
async fn forget(id: u64) -> Result<(), Error> {
EVENTS
.write()
.exec("DELETE FROM events WHERE id = {id:UInt64}")
.param("id", id)
.await?;
Ok(())
}Sourcepub fn insert<'a, T: Serialize>(
&self,
table: &str,
rows: &'a [T],
) -> Insert<'a, T>
pub fn insert<'a, T: Serialize>( &self, table: &str, rows: &'a [T], ) -> Insert<'a, T>
Starts an insert of rows into table (table or db.table). Each
row must serialize as a struct or map whose fields match column names
(see the module docs). Configure optional settings
such as async_insert on the returned Insert, then await it for an
ExecResult.
#[derive(serde::Serialize)]
struct Event<'a> {
id: u64,
kind: &'a str,
}
async fn record(id: u64) -> Result<(), Error> {
EVENTS
.write()
.insert("events", &[Event { id, kind: "signup" }])
.await?;
Ok(())
}Sourcepub fn connection_string(&self) -> Result<String, Error>
pub fn connection_string(&self) -> Result<String, Error>
Returns a connection URL (http[s]://user:password@host:port/database)
for use with external ClickHouse clients, with URL-encoded credentials
for this handle’s access level.
If the runtime has no user for this access level, the URL uses the next
higher level’s credentials. In that case, readonly is not embedded in
the URL—configure it on your client if you want read-only enforcement.
Trait Implementations§
Source§impl Clone for DatabaseRef
impl Clone for DatabaseRef
impl Copy for DatabaseRef
Auto Trait Implementations§
impl !RefUnwindSafe for DatabaseRef
impl !UnwindSafe for DatabaseRef
impl Forget for DatabaseRef
impl Freeze for DatabaseRef
impl Move for DatabaseRef
impl Send for DatabaseRef
impl Sync for DatabaseRef
impl Unpin for DatabaseRef
impl UnsafeUnpin for DatabaseRefwhere
&'static Database: UnsafeUnpin,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request