59 releases (13 stable)

3.1.1 Oct 22, 2020
2.3.0 Oct 10, 2020
1.1.1 Jul 30, 2020
0.13.0 Jul 29, 2020
0.10.3 Feb 26, 2020

#89 in Database interfaces

Download history 2354/week @ 2024-06-09 1931/week @ 2024-06-16 2300/week @ 2024-06-23 1799/week @ 2024-06-30 1984/week @ 2024-07-07 1896/week @ 2024-07-14 2229/week @ 2024-07-21 2740/week @ 2024-07-28 1899/week @ 2024-08-04 2248/week @ 2024-08-11 1601/week @ 2024-08-18 2307/week @ 2024-08-25 2099/week @ 2024-09-01 2135/week @ 2024-09-08 1776/week @ 2024-09-15 2065/week @ 2024-09-22

8,213 downloads per month
Used in 20 crates (9 directly)

MIT license

180KB
2.5K SLoC

sql-builder

Build Status Latest Version Docs

Simple SQL code generator.

Usage

To use sql-builder, add this to your Cargo.toml:

[dependencies]
sql-builder = "3.1"

Examples:

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);

See more examples

SQL support

Statements

  • SELECT
  • INSERT
  • UPDATE
  • DELETE

Operations

  • join
  • distinct
  • group by
  • order by
  • where
  • union
  • limit, offset
  • subquery

Functions

  • escape
  • quote, double quote, back quote, brackets quote
  • bind, binds, bind_num, bind_nums, bind_name, bind_names

Macroes

  • name, qname, baname, brname, dname

License

This project is licensed under the MIT license.

Dependencies

~0.4–0.8MB
~19K SLoC