| Crates.io | sqle |
| lib.rs | sqle |
| version | 0.1.4 |
| created_at | 2025-11-23 07:51:32.427338+00 |
| updated_at | 2025-11-23 10:37:00.485794+00 |
| description | Concise SQL string escaping and formatting / 简洁的 SQL 字符串转义与格式化 |
| homepage | https://github.com/js0-site/rust/tree/main/sqle |
| repository | https://github.com/js0-site/rust.git |
| max_upload_size | |
| id | 1946263 |
| size | 50,711 |
sqle is a lightweight Rust library designed for safe and efficient SQL string escaping and binary data formatting. It provides simple utilities to prevent SQL injection and handle database-specific binary formats (MySQL and PostgreSQL).
bool to SQL TRUE or FALSE.X'HEX' literals.E'\\xHEX' literals.&[u8] to avoid UTF-8 decoding overhead.String::from_utf8_unchecked for zero-overhead string construction.Add sqle to your Cargo.toml. Enable features as needed:
[dependencies]
sqle = { version = "0.1", features = ["mysql", "postgres"] }
use sqle;
fn main() {
// String Escaping - Single Quote
let s = "foo'bar";
assert_eq!(sqle::string(s), "'foo''bar'");
// String Escaping - Newlines, Tabs, Backslashes, etc.
let s = "hello\nworld\t!";
assert_eq!(sqle::string(s), "'hello\\nworld\\t!'");
// Boolean Formatting
assert_eq!(sqle::bool(true), "TRUE");
// MySQL Binary (requires "mysql" feature)
#[cfg(feature = "mysql")]
{
let bytes = b"hello";
assert_eq!(sqle::mysql::blob(bytes), "X'68656C6C6F'");
}
// PostgreSQL Binary (requires "postgres" feature)
#[cfg(feature = "postgres")]
{
let bytes = b"hello";
assert_eq!(sqle::postgres::blob(bytes), "E'\\\\x68656c6c6f'");
}
}
The library focuses on simplicity and performance.
string function pre-allocates memory for worst-case scenario (all characters need escaping) to prevent runtime reallocations.mysql and postgres features to keep the core lightweight.unsafe for string construction from known valid UTF-8 bytes (hex encoding) to squeeze out extra performance where safe.pub fn string(s: impl AsRef<[u8]>) -> StringEscapes a string for use in a SQL query. It wraps the string in single quotes and escapes the following special characters:
' → '' (single quote doubled)\ → \\ (backslash)\n → \n (newline)\r → \r (carriage return)\t → \t (tab)\0 → \0 (null character)This ensures safety and compatibility across various SQL dialects (MySQL, PostgreSQL, etc.).
pub fn bool(b: bool) -> &'static strReturns the SQL string representation of a boolean: "TRUE" or "FALSE".
pub mod mysqlAvailable with feature = "mysql".
pub fn blob(bytes: &[u8]) -> StringFormats a byte slice into a MySQL hex string literal: X'...'.
pub mod postgresAvailable with feature = "postgres".
pub fn blob(bytes: &[u8]) -> StringFormats a byte slice into a PostgreSQL hex string literal using the escape string syntax: E'\\x...'.
.
├── Cargo.toml # Project configuration and dependencies
├── src
│ └── lib.rs # Library source code
└── tests
└── main.rs # Integration tests
The Origin of SQL Injection
In December 1998, a cybersecurity researcher known as "Rain Forest Puppy" (Jeff Forristal) published an article in Phrack magazine (Issue 54). He detailed how he could "piggyback" SQL commands into legitimate queries through NT web servers running ODBC. This was the first formal documentation of SQL Injection.
Before this, the concept of "escaping" characters had existed since the 19th century (Baudot code), but the specific danger of mixing data and code in database queries wasn't widely recognized. Today, proper string escaping (like what sqle provides) and parameterized queries are the standard defense against this historic vulnerability.
This project is an open-source component of js0.site ⋅ Refactoring the Internet Plan.
We are redefining the development paradigm of the Internet in a componentized way. Welcome to follow us:
sqle 是一个轻量级的 Rust 库,专为安全高效的 SQL 字符串转义和二进制数据格式化而设计。它提供了简单的工具函数,用于防止 SQL 注入并处理特定数据库(MySQL 和 PostgreSQL)的二进制格式。
bool 转换为 SQL 的 TRUE 或 FALSE。X'HEX' 字面量。E'\\xHEX' 字面量。&[u8],避开 UTF-8 解码开销。String::from_utf8_unchecked 实现零开销字符串构建。在 Cargo.toml 中添加 sqle。根据需要启用特性:
[dependencies]
sqle = { version = "0.1", features = ["mysql", "postgres"] }
use sqle;
fn main() {
// 字符串转义 - 单引号
let s = "foo'bar";
assert_eq!(sqle::string(s), "'foo''bar'");
// 字符串转义 - 换行符、制表符、反斜杠等
let s = "hello\nworld\t!";
assert_eq!(sqle::string(s), "'hello\\nworld\\t!'");
// 布尔值格式化
assert_eq!(sqle::bool(true), "TRUE");
// MySQL 二进制(需要 "mysql" 特性)
#[cfg(feature = "mysql")]
{
let bytes = b"hello";
assert_eq!(sqle::mysql::blob(bytes), "X'68656C6C6F'");
}
// PostgreSQL 二进制(需要 "postgres" 特性)
#[cfg(feature = "postgres")]
{
let bytes = b"hello";
assert_eq!(sqle::postgres::blob(bytes), "E'\\\\x68656c6c6f'");
}
}
本库专注于简洁性与性能。
string 函数为最坏情况(所有字符都需转义)预分配内存,避免运行时重分配。mysql 和 postgres 特性进行门控,保持核心库的轻量。unsafe,以在保证安全的前提下榨取额外性能。pub fn string(s: impl AsRef<[u8]>) -> String转义字符串以用于 SQL 查询。它将字符串用单引号包裹,并转义以下特殊字符:
' → '' (单引号双写)\ → \\ (反斜杠)\n → \n (换行符)\r → \r (回车符)\t → \t (制表符)\0 → \0 (空字符)这确保了在各种 SQL 方言(MySQL、PostgreSQL 等)中的安全性和兼容性。
pub fn bool(b: bool) -> &'static str返回布尔值的 SQL 字符串表示形式:"TRUE" 或 "FALSE"。
pub mod mysql启用 feature = "mysql" 时可用。
pub fn blob(bytes: &[u8]) -> String将字节切片格式化为 MySQL 十六进制字符串字面量:X'...'。
pub mod postgres启用 feature = "postgres" 时可用。
pub fn blob(bytes: &[u8]) -> String使用转义字符串语法将字节切片格式化为 PostgreSQL 十六进制字符串字面量:E'\\x...'。
.
├── Cargo.toml # 项目配置与依赖
├── src
│ └── lib.rs # 库源代码
└── tests
└── main.rs # 集成测试
SQL 注入的起源
1998 年 12 月,一位网名为 "Rain Forest Puppy" (Jeff Forristal) 的网络安全研究员在 《Phrack》 杂志(第 54 期)上发表了一篇文章。他详细描述了如何通过运行 ODBC 的 NT Web 服务器,将 SQL 命令“搭载”到合法查询中。这是 SQL 注入 (SQL Injection) 的首次正式文档记录。
在此之前,“转义”字符的概念自 19 世纪(博多码)就已存在,但在数据库查询中混合数据与代码的危险性尚未被广泛认知。如今,正确的字符串转义(如 sqle 所提供的)和参数化查询已成为防御这一历史性漏洞的标准手段。
本项目为 js0.site ⋅ 重构互联网计划 的开源组件。
我们正在以组件化的方式重新定义互联网的开发范式,欢迎关注: