1 个不稳定版本
0.1.0 | 2024年2月23日 |
---|
#2679 在 数据库接口
24KB
658 行
My Sq Bro
Rust SQL查询构建器。
示例
插入
let insert = Insert::new()
.table_name("users")
.data("name", "SQL Bro")
.data("email", "[email protected]")
.build()
.exec();
assert_eq!(
insert,
"INSERT INTO `users` (`name`, `email`) VALUES (\"SQL Bro\", \"[email protected]\")"
);
选择
简单选择
let select = Select::new()
.table_name("users")
.attr("name")
.attr("email")
.when("id", "1")
.when("email", "[email protected]")
.build()
.exec();
assert_eq!(
select,
"SELECT `users`.`name`, `users`.`email` FROM `users` WHERE (`users`.`id` = 1 AND `users`.`email` = \"[email protected]\")"
)
连接选择
let select = Select::new()
.table_name("students")
.attr("name")
.join(
Join::new()
.join_type(JoinType::Left)
.table_name("schools_students")
.attr("school_id")
.on("student_id", "`students`.`id`"),
)
.when("id", "1")
.build()
.exec();
assert_eq!(
select,
"SELECT `students`.`name`, `schools_students`.`school_id` FROM `students` LEFT JOIN `schools_students` ON (`schools_students`.`student_id` = `students`.`id`) WHERE (`students`.`id` = 1)"
);
更新
let update = Update::new()
.table_name("users")
.data("name", "Guest User")
.data("email", "[email protected]")
.when("id", "1")
.build()
.exec();
assert_eq!(
update,
"UPDATE `users` SET (`name` = \"Guest User\", `email` = \"[email protected]\") WHERE (`users`.`id` = 1)"
);
删除
let delete = Delete::new()
.table_name("users")
.when("id", "1")
.when("email", "[email protected]")
.build()
.exec();
assert_eq!(
delete,
"DELETE FROM `users` WHERE (`users`.`id` = 1 AND `users`.`email` = \"[email protected]\")"
);
贡献
欢迎贡献!我希望你能在这个项目中做出贡献。
路线图
该项目处于早期阶段,有许多需要实现的功能。查看问题部分,了解计划的功能、增强功能和错误修复。
许可证
该项目采用MIT许可证 - 请参阅LICENSE文件以获取详细信息。