pub trait DatabaseInfo: Database {
// Required methods
fn placeholder(index: usize) -> String;
fn escape_identifier(name: &str) -> String;
fn get_driver() -> DbDriver;
}Expand description
数据库信息 trait
为不同的数据库类型提供统一的接口,用于获取数据库特定的信息, 如占位符格式、标识符转义方式等。
§实现要求
每个数据库类型(sqlx::MySql, sqlx::Postgres, sqlx::Sqlite)
都需要实现此 trait,以提供数据库特定的行为。
Required Methods§
Sourcefn placeholder(index: usize) -> String
fn placeholder(index: usize) -> String
获取占位符字符串
§参数
index- 占位符的索引(从 0 开始)
§返回值
- MySQL/SQLite:
"?" - PostgreSQL:
"$1","$2", … (index + 1)
§示例
ⓘ
assert_eq!(<sqlx::MySql as DatabaseInfo>::placeholder(0), "?");
assert_eq!(<sqlx::Postgres as DatabaseInfo>::placeholder(0), "$1");
assert_eq!(<sqlx::Postgres as DatabaseInfo>::placeholder(1), "$2");Sourcefn escape_identifier(name: &str) -> String
fn escape_identifier(name: &str) -> String
Sourcefn get_driver() -> DbDriver
fn get_driver() -> DbDriver
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl DatabaseInfo for MySql
Available on crate feature mysql only.
impl DatabaseInfo for MySql
Available on crate feature
mysql only.fn placeholder(_index: usize) -> String
fn escape_identifier(name: &str) -> String
fn get_driver() -> DbDriver
Source§impl DatabaseInfo for Postgres
Available on crate feature postgres only.
impl DatabaseInfo for Postgres
Available on crate feature
postgres only.fn placeholder(index: usize) -> String
fn escape_identifier(name: &str) -> String
fn get_driver() -> DbDriver
Source§impl DatabaseInfo for Sqlite
Available on crate feature sqlite only.
impl DatabaseInfo for Sqlite
Available on crate feature
sqlite only.