Compact SQL
A simple macros allows to write SQL queries for Postgres which will be compacted (and optionally checked) at compile-time.
Dependency
[]
= "0.0.5"
Pretty error
Use pretty-errors
feature (requires unstable Rust version) if you can get better errors with pointing to a concrete error span.
[]
= { = "0.0.5", = ["pretty-errors"] }
Using Compact SQL
Generate just an SQL string
use pg_sql;
Note! There are allowed commas between keywords (more convenient for VCS), named parameters in curly braces are replaced with "$x" Postgres numbered parameters (the same number for the same names).
Common keywords like "SELECT", "AS", etc. are required (as warnings) to be uppercase.
Generate SQL string and struct with SQL params
The macros replaces "named parameters" with numbered parameters for Postgres, and it is reasonably to create struct where attributes names are the same as names parameters. Moreover it is easy to constraint such parameters with Rust's types.
use pg_sql;
pg_sql!
Generate SQL, struct with SQL params and traits implementation
This library can generate trait implementation bodies for those who want to build a library
(or internal functions) that work with generated structs. Not all user structs are designed
to be public, which is why the generated trait implementations are not referenced in a public
crate. Instead, trait names are used from the local scope. However, you can still use traits
from the public compact_sql_traits
crate.
You can specify the expected number of rows, and different traits will be used accordingly.
Additionally, you must declare the return row type. You can use the
PgResultRow
derive macro to generate the necessary implementation.
use ;
use *;
use ;
use Client;
type PgResult<T> = ;
pg_sql!
async
async
You can see more examples in the test_trait_impl.rs
.
Test SQL queries
If you want to check queries (via "prepare") you can define ENVs for the DBMS:
TEST_PG_HOST
TEST_PG_PORT
(optional; default: 5432)TEST_PG_USER
(optional; default: "postgres")TEST_PG_PASS
(optional; default: "")TEST_PG_DATB
License
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.