to_mysql : Efficient MySQL SQL Generation
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.
Table of Contents
Introduction
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.
Features
- Automatic Schema Generation: Creates
CREATE TABLEstatements based on struct fields and types. - Efficient Data Insertion: Generates
INSERTstatements with precomputed prefixes for maximum performance. - Type Safety: Ensures Rust types are correctly mapped to MySQL types.
- Caching: Caches the
INSERTstatement prefix to reduce overhead during repetitive insertions.
Usage
Add to_mysql to your Cargo.toml.
use Mysql;
use ;
Design
The core of to_mysql is the Mysql struct.
- Initialization (
new): When aMysqlinstance is created, it takes the table name and schema information (kinds and field names). It precomputes theINSERT INTO ... VALUES(prefix string. This optimization avoids rebuilding the static part of the SQL query for every insertion. - Schema Mapping: The
create_tablemethod iterates over the field names and kinds, mapping each Rust type (viaKind) to its corresponding MySQL type string (e.g.,u64->BIGINT UNSIGNED). - Data Serialization: The
insertmethod takes serialized byte data, converts it into SQL-compatible string values usingkind2sql, and appends them to the precomputed prefix.
Tech Stack
- Rust: The core programming language.
- tosql: Provides the
ToSqltrait andToSqlTraitfor struct serialization and metadata. - kind2sql: Handles the mapping between Rust types (
Kind) and SQL types, as well as value formatting.
Related Crates
- tosql: The core trait definition.
- tosql_derive: Macro to derive
ToSqlTrait. - tosql_meta: Metadata definition for SQL structs.
Directory Structure
to_mysql/
├── Cargo.toml # Project configuration and dependencies
├── src/
│ └── lib.rs # Core logic and Mysql struct implementation
└── tests/
└── main.rs # Integration tests demonstrating usage
API Documentation
Mysql Struct
The main entry point for the library.
pub fn new(table_name: impl Into<String>, (kind_li, field_li): (Vec<Kind>, Vec<String>)) -> Self
Creates a new Mysql instance.
table_name: The name of the MySQL table.kind_li: A vector ofKindenums representing the types of the fields.field_li: A vector of strings representing the names of the fields.
pub fn create_table(&self) -> String
Generates a CREATE TABLE SQL statement.
- Returns: A string containing the 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 (fromToSqlTrait::dump).- Returns: A
Resultcontaining the SQL string or an error.
History
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.
About
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 : 高效 MySQL SQL 生成
to_mysql 是一个 Rust 库,旨在高效地从 Rust 结构体生成 MySQL CREATE TABLE 和 INSERT 语句。它利用 tosql 和 kind2sql 将 Rust 类型映射到 MySQL 类型并序列化数据,为 SQL 生成提供了高性能解决方案。
目录
简介
to_mysql 通过自动化 SQL 语句生成,简化了与 MySQL 数据库交互的过程。它接收 Rust 结构体的模式定义和数据,并生成优化后的 SQL 字符串,可直接用于执行。
特性
- 自动模式生成:根据结构体字段和类型创建
CREATE TABLE语句。 - 高效数据插入:生成带有预计算前缀的
INSERT语句,以实现最高性能。 - 类型安全:确保 Rust 类型正确映射到 MySQL 类型。
- 缓存:缓存
INSERT语句前缀,减少重复插入时的开销。
使用方法
将 to_mysql 添加到 Cargo.toml。
use Mysql;
use ;
设计思路
to_mysql 的核心是 Mysql 结构体。
- 初始化 (
new):创建Mysql实例时,它接收表名和模式信息(类型和字段名)。它会预计算INSERT INTO ... VALUES(前缀字符串。这种优化避免了在每次插入时重建 SQL 查询的静态部分。 - 模式映射:
create_table方法遍历字段名和类型,将每个 Rust 类型(通过Kind)映射到其对应的 MySQL 类型字符串(例如u64->BIGINT UNSIGNED)。 - 数据序列化:
insert方法接收序列化的字节数据,使用kind2sql将其转换为兼容 SQL 的字符串值,并将它们追加到预计算的前缀后。
技术栈
- Rust:核心编程语言。
- tosql:提供
ToSqltrait 和ToSqlTrait用于结构体序列化和元数据。 - kind2sql:处理 Rust 类型 (
Kind) 和 SQL 类型之间的映射,以及值格式化。
相关库
- tosql:核心 trait 定义。
- tosql_derive:用于派生
ToSqlTrait的宏。 - tosql_meta:SQL 结构体的元数据定义。
目录结构
to_mysql/
├── Cargo.toml # 项目配置和依赖
├── src/
│ └── lib.rs # 核心逻辑和 Mysql 结构体实现
└── tests/
└── main.rs # 展示用法的集成测试
API 文档
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 语句。
- 返回: 包含 SQL 语句的字符串。
pub fn insert(&self, bytes: &[u8]) -> tosql::vb::Result<String>
为特定记录生成 INSERT SQL 语句。
bytes: 结构体的序列化字节表示(来自ToSqlTrait::dump)。- 返回: 包含 SQL 字符串或错误的
Result。
历史
"MySQL" 名称的由来
MySQL 由瑞典公司 MySQL AB 创建,该公司由 David Axmark、Allan Larsson 和 Michael "Monty" Widenius 创立。MySQL 中的 "My" 是以 Monty 的女儿 My 命名的。名为 "Sakila" 的海豚标志是从用户在 "为海豚命名" 比赛中建议的大量名字中选出的。该项目始于 1994 年,第一个版本于 1995 年 5 月 23 日发布。它的设计初衷是作为现有数据库系统(如 mSQL)的一种更快、更灵活的替代方案。
关于
本项目为 js0.site ⋅ 重构互联网计划 的开源组件。
我们正在以组件化的方式重新定义互联网的开发范式,欢迎关注: