tosql : Trait for SQL Struct Serialization
Defines the ToSqlTrait trait for serializing Rust structs into binary formats compatible with SQL bulk inserts.
Table of Contents
Introduction
tosql provides the fundamental ToSqlTrait trait used to map Rust structs to SQL table schemas. It works in conjunction with kind2sql to enable efficient, type-safe serialization of data for database operations.
Features
- Standardized Trait: Defines a common interface for SQL-compatible structs.
- Type Safety: Leverages
kind2sql::Kindto ensure data types match the database schema. - Zero-Copy Friendly: Designed to work with
bytes::Bytesfor efficient memory usage.
Usage
The most common way to use tosql is with the tosql_derive macro to automatically implement the ToSqlTrait trait.
First, add dependencies:
[]
= "0.1"
= "0.1"
Then, derive ToSql for your structs:
use ;
API Reference
trait ToSqlTrait
fn name() -> String: Returns the struct (or table) name.fn kind_li() -> Vec<Kind>: Returns the list of field types (Kind).fn field_li() -> Vec<String>: Returns the list of field names.fn dump(&self) -> Bytes: Serializes the struct instance into a binary buffer.
Tech Stack
- Rust
bytes: For efficient byte buffer management.kind2sql: For type definitions (Kind).
Related Crates
- tosql_derive: Macro to derive
ToSqlTrait. - to_mysql: MySQL SQL generation logic.
- tosql_meta: Metadata definition for SQL structs.
Directory Structure
.
├── Cargo.toml # Package manifest
├── readme/ # Documentation
│ ├── en.md # English README
│ └── zh.md # Chinese README
└── src/
└── lib.rs # Trait definition
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:
tosql : SQL结构体序列化Trait
定义了 ToSqlTrait trait,用于将 Rust 结构体序列化为兼容 SQL 批量插入的二进制格式。
目录
简介
tosql 提供了基础的 ToSqlTrait trait,用于将 Rust 结构体映射到 SQL 表模式。它与 kind2sql 配合使用,实现数据库操作的高效、类型安全的数据序列化。
特性
- 标准化接口:为兼容 SQL 的结构体定义通用接口。
- 类型安全:利用
kind2sql::Kind确保数据类型与数据库模式匹配。 - 零拷贝友好:设计用于配合
bytes::Bytes使用,实现高效内存管理。
使用演示
使用 tosql 最常见的方式是配合 tosql_derive 宏自动实现 ToSqlTrait trait。
首先,添加依赖:
[]
= "0.1"
= "0.1"
然后,为你的结构体派生 ToSql:
use ;
API 参考
trait ToSqlTrait
fn name() -> String:返回结构体(或表)名称。fn kind_li() -> Vec<Kind>:返回字段类型列表 (Kind)。fn field_li() -> Vec<String>:返回字段名称列表。fn dump(&self) -> Bytes:将结构体实例序列化为二进制缓冲区。
技术栈
- Rust
bytes:用于高效字节缓冲区管理。kind2sql:用于类型定义 (Kind)。
相关库
- tosql_derive:用于派生
ToSqlTrait的宏。 - to_mysql:MySQL SQL 生成逻辑。
- tosql_meta:SQL 结构体的元数据定义。
目录结构
.
├── Cargo.toml # 包配置
├── readme/ # 文档
│ ├── en.md # 英文 README
│ └── zh.md # 中文 README
└── src/
└── lib.rs # Trait 定义
关于
本项目为 js0.site ⋅ 重构互联网计划 的开源组件。
我们正在以组件化的方式重新定义互联网的开发范式,欢迎关注: