59 个版本 (13 个稳定版)

3.1.1 2020 年 10 月 22 日
2.3.0 2020 年 10 月 10 日
1.1.1 2020 年 7 月 30 日
0.13.0 2020 年 7 月 29 日
0.10.3 2020 年 2 月 26 日

数据库接口 中排名 116

Download history 2140/week @ 2024-03-14 2065/week @ 2024-03-21 2137/week @ 2024-03-28 2652/week @ 2024-04-04 3023/week @ 2024-04-11 2375/week @ 2024-04-18 2335/week @ 2024-04-25 1800/week @ 2024-05-02 2097/week @ 2024-05-09 1831/week @ 2024-05-16 1813/week @ 2024-05-23 2040/week @ 2024-05-30 2029/week @ 2024-06-06 2208/week @ 2024-06-13 2265/week @ 2024-06-20 1533/week @ 2024-06-27

每月下载量 8,445
19 Crates 中使用(9 个直接使用)

MIT 许可证

180KB
2.5K SLoC

sql-builder

Build Status Latest Version Docs

简单的 SQL 代码生成器。

用法

要使用 sql-builder,将以下内容添加到您的 Cargo.toml

[dependencies]
sql-builder = "3.1"

示例

SELECT

use sql_builder::SqlBuilder;

let sql = SqlBuilder::select_from("company")
    .field("id")
    .field("name")
    .and_where_gt("salary", 25_000)
    .sql()?;

assert_eq!("SELECT id, name FROM company WHERE salary > 25000;", &sql);
use sql_builder::prelude::*;

let sql = SqlBuilder::select_from("company")
    .fields(&["id", "name"])
    .and_where("salary BETWEEN ? AND ?".binds(&[&10_000, &25_000]))
    .and_where("staff BETWEEN ? AND ?".bind(&100).bind(&200))
    .sql()?;

assert_eq!("SELECT id, name FROM company WHERE (salary BETWEEN 10000 AND 25000) AND (staff BETWEEN 100 AND 200);", &sql);

INSERT

use sql_builder::{SqlBuilder, quote};

let sql = SqlBuilder::insert_into("company")
    .field("name")
    .field("salary")
    .field("staff")
    .values(&[&quote("D&G"), &10_000.to_string(), &100.to_string()])
    .values(&[&quote("G&D"), &25_000.to_string(), &200.to_string()])
    .sql()?;

assert_eq!("INSERT INTO company (name, salary, staff) VALUES ('D&G', 10000, 100), ('G&D', 25000, 200);", &sql);
use sql_builder::prelude::*;

let sql = SqlBuilder::insert_into("company")
    .field("name")
    .field("salary")
    .field("staff")
    .values(&["$1, ?, ?"])
    .values(&["$2, ?, ?"])
    .sql()?
    .bind_nums(&[&"D&G", &"G&D"])
    .binds(&[&10_000, &100]);

assert_eq!("INSERT INTO company (name, salary, staff) VALUES ('D&G', 10000, 100), ('G&D', 10000, 100);", &sql);

UPDATE

use sql_builder::SqlBuilder;

let sql = SqlBuilder::update_table("company")
    .set("salary", "salary + 100")
    .and_where_lt("salary", 1_000)
    .sql()?;

assert_eq!("UPDATE company SET salary = salary + 100 WHERE salary < 1000;", &sql);
use sql_builder::prelude::*;

let sql = SqlBuilder::update_table("company")
    .set("salary", "salary + $1")
    .set("comment", &quote("up $1$$"))
    .and_where("salary < ?".bind(&1_000))
    .sql()?
    .bind_nums(&[&100]);

assert_eq!("UPDATE company SET salary = salary + 100, comment = 'up 100$' WHERE salary < 1000;", &sql);

DELETE

use sql_builder::SqlBuilder;

let sql = SqlBuilder::delete_from("company")
    .or_where_lt("salary", 1_000)
    .or_where_gt("salary", 25_000)
    .sql()?;

assert_eq!("DELETE FROM company WHERE salary < 1000 OR salary > 25000;", &sql);
use sql_builder::prelude::*;
use std::collections::HashMap;

let mut names: HashMap<&str, &dyn SqlArg> = HashMap::new();
names.insert("min", &1_000);
names.insert("max", &25_000);

let sql = SqlBuilder::delete_from("company")
    .and_where("salary >= :min:")
    .and_where("salary <= :max:")
    .sql()?
    .bind_names(&names);

assert_eq!("DELETE FROM company WHERE (salary >= 1000) AND (salary <= 25000);", &sql);

更多示例请参阅 此处

SQL 支持

语句

  • SELECT
  • INSERT
  • UPDATE
  • DELETE

操作

  • 连接
  • 不重复
  • 按组
  • 排序
  • WHERE
  • 并集
  • 限制,偏移
  • 子查询

函数

  • 转义
  • 引号,双引号,反引号,括号引号
  • 绑定,绑定的,绑定的数字,绑定的数字,绑定名称,绑定名称

  • 名称,qname,baname,brname,dname

许可证

本项目采用 MIT 许可证

依赖项

~0.4–0.9MB
~19K SLoC