pub enum Backend {
Sqlite,
Postgres,
MySql,
}Expand description
支持的后端
Variants§
Implementations§
Source§impl Backend
impl Backend
Sourcepub const ID_MAX: usize = 128
pub const ID_MAX: usize = 128
Self::id_text 列能装多少个字符
Sourcepub const ID_SHORT_MAX: usize = 45
pub const ID_SHORT_MAX: usize = 45
Self::id_short 列能装多少个字符
pub fn from_url(url: &str) -> Backend
Sourcepub fn id_text(&self) -> &'static str
pub fn id_text(&self) -> &'static str
主键/索引列的字符串类型。
MySQL 不能对 TEXT 建索引(1170:要 key length),必须用定长 VARCHAR。
128 跟 DTM 的 varchar(128) 对齐;复合主键三列合计仍在 InnoDB 限内。
Sourcepub fn text(&self, n: usize) -> String
pub fn text(&self, n: usize) -> String
自由文本列(payload / url / 原因之类)。
⚠ MySQL 上只能用 VARCHAR:经 sqlx::Any 读 MySQL 的 TEXT
(含 LONGTEXT、MEDIUMTEXT、显式 CHARACTER SET utf8mb4)一律报
mismatched types; Rust type String is not compatible with SQL type BLOB。
五种写法实测下来只有 VARCHAR 能解成 String。
代价是有长度上限:n 要够装最长的内容。MySQL 单行还有 65535 字节的
总限制(utf8mb4 每字符 4 字节),所以别把每列都开得很大。
Sourcepub fn q(&self, template: &str) -> String
pub fn q(&self, template: &str) -> String
把 SQL 模板渲染成这个后端能吃的语句。
做三件事:
?→$1..$n(MySQL 保持?){INS}→INSERT IGNORE INTO(MySQL)/INSERT INTO(其它){NOCONFLICT}→ 空(MySQL)/ON CONFLICT DO NOTHING(其它)
Sourcepub fn create_index(
&self,
name: &str,
table: &str,
cols: &str,
) -> Option<String>
pub fn create_index( &self, name: &str, table: &str, cols: &str, ) -> Option<String>
建索引。MySQL 不支持 CREATE INDEX IF NOT EXISTS,只能靠
建表时内联 KEY,所以这里对 MySQL 返回 None。
Sourcepub fn inline_index(&self, name: &str, cols: &str) -> String
pub fn inline_index(&self, name: &str, cols: &str) -> String
建表时内联的索引定义。只有 MySQL 用得上(见 Self::create_index)。
Sourcepub fn skip_locked(&self) -> &'static str
pub fn skip_locked(&self) -> &'static str
抢占待办事务时用的行锁后缀。
§为什么非要有这个
抢占是「先 SELECT 出最该跑的那笔,再 UPDATE 占坑」。不加锁的话, N 个推进 worker 的 SELECT 会全部选中同一行,然后挤在 UPDATE 上排队,最后只有一个成功、其余白跑一轮。实测 Postgres 上 1 个 worker 71 笔/秒、8 个也才 127 笔/秒 —— 并行度基本没了。
FOR UPDATE SKIP LOCKED 让每个 worker 自动跳过别人正在抢的行,
各拿各的,这才是真并行。
- Postgres 9.5+ / MySQL 8.0+ 都支持
- sqlite 返回空串:它没有行锁,写操作本来就是全库串行的, 加了也没用(语法上还不认)
Trait Implementations§
impl Copy for Backend
impl Eq for Backend
impl StructuralPartialEq for Backend
Auto Trait Implementations§
impl Freeze for Backend
impl RefUnwindSafe for Backend
impl Send for Backend
impl Sync for Backend
impl Unpin for Backend
impl UnsafeUnpin for Backend
impl UnwindSafe for Backend
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<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
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