Skip to main content

SqlxClient

Struct SqlxClient 

Source
pub struct SqlxClient { /* private fields */ }

Implementations§

Source§

impl SqlxClient

Source

pub async fn connect(url: &str) -> Result<Self, Error>

Source

pub async fn connect_with_params( url: &str, params: &PoolParams, ) -> Result<Self, Error>

Source

pub async fn connect_with_auth( url: &str, username: &str, password: &str, ) -> Result<Self, Error>

Source

pub async fn from_config(cfg: SqlxConfig) -> Result<Self, Error>

Source

pub fn from_pool(pool: Pool) -> Self

用已有原生池构造客户端。

拿的是裸池,没有 SqlxConfig 可读,因此查询超时与 SqlxClient::warm_up 在这里都是关的(query_timeout: None、min_connections: 0)—— 这是 API 形状决定的,不是遗漏。需要两者请用 SqlxClient::connect_with_params 或 SqlxClient::from_config。

Source

pub fn dialect(&self) -> Dialect

Source

pub fn pool_size(&self) -> u32

池内已建立的连接总数(供 metrics 与测试)。

Source

pub async fn warm_up(&self) -> Result<(), RdbmsError>

同步建满 min_connections 条连接并归还,让服务启动后立刻处于就绪态。 在服务启动时调用一次。

为什么 sqlx 已有 min_connections 还需要它:sqlx 的保底由后台任务 异步维护(sqlx-core-0.8.6/src/pool/inner.rs:514-517),connect() 返回时不保证已建满 —— 启动后第一波请求会与后台任务抢跑。

Trait Implementations§

Source§

impl RdbmsClient for SqlxClient

Source§

fn transaction<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<Transaction, RdbmsError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

impl SqlExecutor for SqlxClient

Source§

fn execute_then_query<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, first: &'life1 str, first_params: &'life2 [Value], second: &'life3 str, ) -> Pin<Box<dyn Future<Output = Result<Vec<Row>, RdbmsError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

两步式原子执行:自己开一个事务包住两条语句并提交。 ORM 的 MySQL 主键回填走这里(LAST_INSERT_ID() 是连接作用域的, 池下直发两条可能落到不同连接);一步式后端用不到(默认实现报错)。

第一条失败时不提交,tx 在 drop 时由底层 sqlx 事务回滚。

Source§

fn execute<'life0, 'life1, 'async_trait>( &'life0 self, sql: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<u64, RdbmsError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

执行一条 SQL 语句,返回受影响行数。 用户提供的值请走 SqlExecutor::execute_with。
Source§

fn query<'life0, 'life1, 'async_trait>( &'life0 self, sql: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Vec<Row>, RdbmsError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

查询多行。用户提供的值请走 SqlExecutor::query_with。
Source§

fn execute_with<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, sql: &'life1 str, params: &'life2 [Value], ) -> Pin<Box<dyn Future<Output = Result<u64, RdbmsError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

参数化执行,防注入。无法绑定参数的后端返回错误。
Source§

fn query_with<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, sql: &'life1 str, params: &'life2 [Value], ) -> Pin<Box<dyn Future<Output = Result<Vec<Row>, RdbmsError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

参数化查询,防注入。无法绑定参数的后端返回错误。
Source§

fn dialect(&self) -> Dialect

本执行器背后的数据库方言。
Source§

fn query_write<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, sql: &'life1 str, params: &'life2 [Value], ) -> Pin<Box<dyn Future<Output = Result<Vec<Row>, RdbmsError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, Self: 'async_trait,

写路径且需要返回结果(INSERT ... RETURNING / OUTPUT INSERTED)。 默认委托给 SqlExecutor::query_with;只有读写分离路由需要覆写, 否则写语句会被路由到从库。

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more