| Crates.io | to_mysql |
| lib.rs | to_mysql |
| version | 0.1.6 |
| created_at | 2025-11-24 01:14:15.101854+00 |
| updated_at | 2025-11-24 13:28:26.669907+00 |
| description | Efficient MySQL SQL Generation / 高效 MySQL SQL 生成 |
| homepage | https://github.com/js0/rust/tree/main/to_mysql |
| repository | https://github.com/js0/rust.git |
| max_upload_size | |
| id | 1947244 |
| size | 54,474 |
to_mysql is a Rust library designed to efficiently generate MySQL CREATE TABLE and INSERT statements from Rust structs. It leverages tosql and kind2sql to map Rust types to MySQL types and serialize data, providing a high-performance solution for SQL generation.
to_mysql simplifies the process of interacting with MySQL databases by automating the generation of SQL statements. It takes the schema definition and data from Rust structs and produces optimized SQL strings, ready for execution.
CREATE TABLE statements based on struct fields and types.INSERT statements with precomputed prefixes for maximum performance.INSERT statement prefix to reduce overhead during repetitive insertions.Add to_mysql to your Cargo.toml.
use to_mysql::Mysql;
use tosql::{ToSqlTrait, ToSql};
#[derive(ToSql)]
struct User {
id: u64,
name: String,
}
fn main() {
// Generate CREATE TABLE statement
let mysql = Mysql::new("User", User::meta());
let create_table_sql = mysql.create_table();
println!("{}", create_table_sql);
// Output: CREATE TABLE User(id BIGINT UNSIGNED,name LONGTEXT);
// Generate INSERT statement
let user = User {
id: 123,
name: "Alice".to_string(),
};
let insert_sql = mysql.insert(&user.dump()).unwrap();
println!("{}", insert_sql);
// Output: INSERT INTO User(id,name)VALUES(123,'Alice');
}
The core of to_mysql is the Mysql struct.
new): When a Mysql instance is created, it takes the table name and schema information (kinds and field names). It precomputes the INSERT INTO ... VALUES( prefix string. This optimization avoids rebuilding the static part of the SQL query for every insertion.create_table method iterates over the field names and kinds, mapping each Rust type (via Kind) to its corresponding MySQL type string (e.g., u64 -> BIGINT UNSIGNED).insert method takes serialized byte data, converts it into SQL-compatible string values using kind2sql, and appends them to the precomputed prefix.ToSql trait and ToSqlTrait for struct serialization and metadata.Kind) and SQL types, as well as value formatting.ToSqlTrait.to_mysql/
├── Cargo.toml # Project configuration and dependencies
├── src/
│ └── lib.rs # Core logic and Mysql struct implementation
└── tests/
└── main.rs # Integration tests demonstrating usage
Mysql StructThe main entry point for the library.
pub fn new(table_name: impl Into<String>, (kind_li, field_li): (Vec<Kind>, Vec<String>)) -> SelfCreates a new Mysql instance.
table_name: The name of the MySQL table.kind_li: A vector of Kind enums representing the types of the fields.field_li: A vector of strings representing the names of the fields.pub fn create_table(&self) -> StringGenerates a CREATE TABLE SQL statement.
pub fn insert(&self, bytes: &[u8]) -> tosql::vb::Result<String>Generates an INSERT SQL statement for a specific record.
bytes: The serialized byte representation of the struct (from ToSqlTrait::dump).Result containing the SQL string or an error.The Name "MySQL"
MySQL was created by a Swedish company, MySQL AB, founded by David Axmark, Allan Larsson, and Michael "Monty" Widenius. The "My" in MySQL is named after Monty's daughter, My. The dolphin logo, named "Sakila," was chosen from a huge list of names suggested by users in a "Name the Dolphin" contest. The project started in 1994, and the first version was released on May 23, 1995. It was designed to be a faster, more flexible alternative to existing database systems like mSQL.
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:
to_mysql 是一个 Rust 库,旨在高效地从 Rust 结构体生成 MySQL CREATE TABLE 和 INSERT 语句。它利用 tosql 和 kind2sql 将 Rust 类型映射到 MySQL 类型并序列化数据,为 SQL 生成提供了高性能解决方案。
to_mysql 通过自动化 SQL 语句生成,简化了与 MySQL 数据库交互的过程。它接收 Rust 结构体的模式定义和数据,并生成优化后的 SQL 字符串,可直接用于执行。
CREATE TABLE 语句。INSERT 语句,以实现最高性能。INSERT 语句前缀,减少重复插入时的开销。将 to_mysql 添加到 Cargo.toml。
use to_mysql::Mysql;
use tosql::{ToSqlTrait, ToSql};
#[derive(ToSql)]
struct User {
id: u64,
name: String,
}
fn main() {
// 生成 CREATE TABLE 语句
let mysql = Mysql::new("User", User::meta());
let create_table_sql = mysql.create_table();
println!("{}", create_table_sql);
// 输出: CREATE TABLE User(id BIGINT UNSIGNED,name LONGTEXT);
// 生成 INSERT 语句
let user = User {
id: 123,
name: "Alice".to_string(),
};
let insert_sql = mysql.insert(&user.dump()).unwrap();
println!("{}", insert_sql);
// 输出: INSERT INTO User(id,name)VALUES(123,'Alice');
}
to_mysql 的核心是 Mysql 结构体。
new):创建 Mysql 实例时,它接收表名和模式信息(类型和字段名)。它会预计算 INSERT INTO ... VALUES( 前缀字符串。这种优化避免了在每次插入时重建 SQL 查询的静态部分。create_table 方法遍历字段名和类型,将每个 Rust 类型(通过 Kind)映射到其对应的 MySQL 类型字符串(例如 u64 -> BIGINT UNSIGNED)。insert 方法接收序列化的字节数据,使用 kind2sql 将其转换为兼容 SQL 的字符串值,并将它们追加到预计算的前缀后。ToSql trait 和 ToSqlTrait 用于结构体序列化和元数据。Kind) 和 SQL 类型之间的映射,以及值格式化。ToSqlTrait 的宏。to_mysql/
├── Cargo.toml # 项目配置和依赖
├── src/
│ └── lib.rs # 核心逻辑和 Mysql 结构体实现
└── tests/
└── main.rs # 展示用法的集成测试
Mysql 结构体库的主要入口点。
pub fn new(table_name: impl Into<String>, (kind_li, field_li): (Vec<Kind>, Vec<String>)) -> Self创建一个新的 Mysql 实例。
table_name: MySQL 表的名称。kind_li: 代表字段类型的 Kind 枚举向量。field_li: 代表字段名称的字符串向量。pub fn create_table(&self) -> String生成 CREATE TABLE SQL 语句。
pub fn insert(&self, bytes: &[u8]) -> tosql::vb::Result<String>为特定记录生成 INSERT SQL 语句。
bytes: 结构体的序列化字节表示(来自 ToSqlTrait::dump)。Result。"MySQL" 名称的由来
MySQL 由瑞典公司 MySQL AB 创建,该公司由 David Axmark、Allan Larsson 和 Michael "Monty" Widenius 创立。MySQL 中的 "My" 是以 Monty 的女儿 My 命名的。名为 "Sakila" 的海豚标志是从用户在 "为海豚命名" 比赛中建议的大量名字中选出的。该项目始于 1994 年,第一个版本于 1995 年 5 月 23 日发布。它的设计初衷是作为现有数据库系统(如 mSQL)的一种更快、更灵活的替代方案。
本项目为 js0.site ⋅ 重构互联网计划 的开源组件。
我们正在以组件化的方式重新定义互联网的开发范式,欢迎关注: