Scooby
An SQL query builder with a pleasant fluent API closely imitating actual SQL. Meant to comfortably build dynamic queries with a little bit of safety checks sprinkled on top to ensure you don't forget important things like ON clauses. Does not do quoting, does not do validation.
Supports only PostgreSQL syntax.
Requires Rust 1.54.
Supported statements, clauses and features
-
SELECTWHEREGROUP BYHAVINGALL,DISTINCTandDISTINCT ONORDER BYLIMITandOFFSETFROMwith subselects and joins with a nice API:JOIN,INNER JOINandCROSS JOINLEFT JOINandLEFT OUTER JOINRIGHT JOINandRIGHT OUTER JOINFULL JOINandFULL OUTER JOIN
-
INSERT INTODEFAULT VALUESVALUESwith compile-time checking that lengths of all values are the same as columnsRETURNING
-
DELETE FROMWHERERETURNING
-
UPDATESETwith compile-time checking that you've actually set at least somethingWHERERETURNING
-
Convenient
x AS yaliasing -
Convenient
$1,$2... parameter placeholder builder
Examples
SELECT
use ;
// SELECT 1 + 1
select.to_string;
// SELECT id, name, age FROM Person
select.from.to_string;
// SELECT * FROM (SELECT id FROM City) AS x
select
.from
.to_string;
// SELECT col1, col2 FROM SomeTable LIMIT 10 OFFSET 5
select
.and_select
.from
.limit
.offset
.to_string;
// SELECT
// country.name AS name,
// COUNT(*) AS count
// FROM
// Country AS country
// INNER JOIN City AS city ON city.country_id = country.id
// WHERE
// city.population > 1000000
// GROUP BY country.id
// ORDER BY count DESC
// LIMIT 10
select
.from
.where_
.group_by
.order_by
.limit
.to_string;
INSERT INTO
use insert_into;
// INSERT INTO Dummy (col1, col2) VALUES (a, b), (c, d), (e, f) RETURNING id
insert_into
.columns
.values
.values
.returning
.to_string;
// INSERT INTO Dummy DEFAULT VALUES
insert_into.default_values.to_string;
DELETE FROM
use delete_from;
// DELETE FROM Dummy WHERE x > 0 AND y > 30
delete_from.where_.to_string;
Parameters
use ;
let mut params = new;
// SELECT id FROM Thing WHERE x > $1 AND y < $2
select
.from
.where_
.where_
.to_string;
Testing
Normally:
To check syntax:
- Run a local postgresql server on your machine at default port
cargo test --features validate-postgres-syntax