1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//! Compile-time syntax checking for PostgreSQL queries.
//!
//! `postgres-syntax` exposes the [`sql!`] macro, which parses a PostgreSQL
//! query string at compile time and returns the string unchanged when parsing
//! succeeds. If the query is not valid PostgreSQL syntax, compilation fails
//! with the parser error reported by [`pg_query`].
//!
//! This crate validates syntax only. It does not connect to a database, inspect
//! your schema, validate table or column names, or type-check query parameters.
//!
//! # Example
//!
//! ```
//! use postgres_syntax::sql;
//!
//! let query = sql!("SELECT name, email_address FROM users WHERE id = $1");
//! assert_eq!(
//! query,
//! "SELECT name, email_address FROM users WHERE id = $1",
//! );
//! ```
//!
//! Invalid SQL produces a compile error:
//!
//! ```compile_fail
//! use postgres_syntax::sql;
//!
//! let _ = sql!("SELECT * FROM users WHERE id = $1 BLARG");
//! ```
//!
//! [`pg_query`]: https://docs.rs/pg_query/
use StringLit;
use TokenStream;
use quote;
/// Checks a PostgreSQL query for syntax errors at compile time.
///
/// The macro accepts exactly one string literal. When the string parses as
/// PostgreSQL SQL, the macro expands back to that same literal, so it can be
/// used anywhere a string literal would be accepted.
///
/// ```
/// use postgres_syntax::sql;
///
/// let query: &str = sql!("SELECT now()");
/// assert_eq!(query, "SELECT now()");
/// ```
///
/// Syntax errors are reported during compilation:
///
/// ```compile_fail
/// use postgres_syntax::sql;
///
/// let _ = sql!("SELECT FROM");
/// ```
///
/// The macro only checks PostgreSQL syntax. It does not verify object names,
/// parameter types, permissions, or any other database-specific semantics.