pub struct SqlxClient { /* private fields */ }Implementations§
Source§impl SqlxClient
impl SqlxClient
pub async fn connect(url: &str) -> Result<Self, Error>
pub async fn connect_with_params( url: &str, params: &PoolParams, ) -> Result<Self, Error>
Sourcepub async fn connect_with_mysql_compat(
url: &str,
params: &PoolParams,
compat: MysqlCompat,
) -> Result<Self, Error>
pub async fn connect_with_mysql_compat( url: &str, params: &PoolParams, compat: MysqlCompat, ) -> Result<Self, Error>
同 SqlxClient::connect_with_params,但可指定 MySQL 协议兼容模式。
Doris / StarRocks 走 MysqlCompat::Doris / MysqlCompat::StarRocks。
pub async fn connect_with_auth( url: &str, username: &str, password: &str, ) -> Result<Self, Error>
pub async fn from_config(cfg: SqlxConfig) -> Result<Self, Error>
Sourcepub fn from_pool(pool: Pool) -> Self
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。
Sourcepub fn pool(&self) -> &Pool
pub fn pool(&self) -> &Pool
池本身(metrics feature 的 [register_pool_metrics] 要它)。
sqlx 的池内部是 Arc,克隆很轻。
pub fn dialect(&self) -> Dialect
Sourcepub fn pool_size(&self) -> u32
pub fn pool_size(&self) -> u32
池内已建立的连接总数(供 metrics 与测试)。
与 trait 方法 RdbmsClient::pool_size(配置的最大连接数)同名:
具体类型上固有方法优先,预算校验请经由 &dyn RdbmsClient 调用。
Sourcepub async fn warm_up(&self) -> Result<(), RdbmsError>
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
impl RdbmsClient for SqlxClient
Source§fn replica_lag<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Option<Duration>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn replica_lag<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Option<Duration>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
只对 MySQL 探测(其余方言返回 None):版本决定用哪条 SHOW 语句。
Source§fn pool_size(&self) -> Option<u32>
fn pool_size(&self) -> Option<u32>
配置的最大连接数,不是当前已建立数:预算校验要在建池后立即拿到上限 (新建池的已建立连接数接近 0,用它校验等于永远通过)。
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
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,
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,
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,
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,
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,
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,
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 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,
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§
impl !Freeze for SqlxClient
impl !RefUnwindSafe for SqlxClient
impl !UnwindSafe for SqlxClient
impl Forget for SqlxClient
impl Send for SqlxClient
impl Sync for SqlxClient
impl Unpin for SqlxClient
impl UnsafeUnpin for SqlxClient
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> 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