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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// SPDX-License-Identifier: MIT OR Apache-2.0
//! PersistAnt: a thin persistence layer over Apache OpenDAL, for blobs and records by key.
//!
pub use ;
pub use ;
// The other kinds of persistence, behind features that are off by default. Additive: nothing here
// changes `Store`, `Config`, `Need` or any other item above. Names avoid `core` and `opendal`
// because those are already names in scope here.
/// The capability vocabulary, the matrix file and its gate lookup (`persistant-core`).
pub use persistant_core as capability;
/// The blob trait, its reference backend and its contract tests (`persistant-blob`).
pub use persistant_blob as blob;
/// The key-value trait, `KvOnBlob`, `KvOnRelational`, leases and their contract tests
/// (`persistant-kv`).
pub use persistant_kv as kv;
/// The small relational trait, values and typed errors (`persistant-relational`).
pub use persistant_relational as relational;
/// The OpenDAL blob driver and `ProcessLock` (`persistant-opendal`).
pub use persistant_opendal as opendal_driver;
/// The PostgreSQL driver over sqlx (`persistant-sqlx`). Needs Rust 1.94.
pub use persistant_sqlx as sqlx_driver;
/// The crate version, as declared in `Cargo.toml`.
pub const VERSION: &str = env!;
/// Compile-time SQL (feature `sql-macros`): `sql!` checks a literal statement and translates it for
/// every target named, at build time. The syntax, the placeholders and every target's translation are
/// build errors, not run-time ones; no database is consulted and no schema is read yet (the
/// catalog-based checks are a later stage, `docs/COMPILE-TIME-SQL.md`).
///
/// `schema` is required: `schema = none` for database-free checks only, or a path (relative to your
/// `Cargo.toml`) to a `.json` snapshot written by `persistant-migrate snapshot`, or a `.sql` DDL file.
/// With none stated the crate looks for `persistant-schema.json` and fails to build without it: a
/// missing schema is never a quiet downgrade. With one, UniSQL checks the names and the `INSERT`
/// arity, and PersistAnt checks that an `INSERT` does not leave out a `NOT NULL` column that has no
/// default and that an `ON CONFLICT (..)` names a unique key. It errors only when certain: what the
/// schema does not state is unknown and passes.
///
/// ```
/// use persistant::{Sql, sql};
/// const FIND: Sql = sql!(
/// source = postgres, targets = [postgres, sqlite], schema = none,
/// "SELECT id FROM users WHERE id = $1"
/// );
/// assert_eq!(FIND.params, 1);
/// ```
///
/// A statement that cannot be translated for a named target does not build. The twin above is the same
/// call with the fault (`md5` has no SQLite form) removed:
///
/// ```
/// use persistant::{Sql, sql};
/// const OK: Sql = sql!(source = postgres, targets = [postgres], schema = none, "SELECT md5($1)");
/// ```
///
/// ```compile_fail
/// use persistant::{Sql, sql};
/// const BAD: Sql = sql!(source = postgres, targets = [postgres, sqlite], schema = none, "SELECT md5($1)");
/// ```
///
/// Against a schema (`tests/doctest-schema.sql`: `users` has `id` and `email`, both `NOT NULL` with no
/// default), an `INSERT` that leaves `email` out does not build; the twin that supplies it does:
///
/// ```
/// use persistant::{Sql, sql};
/// const OK: Sql = sql!(
/// source = postgres, targets = [sqlite], schema = "tests/doctest-schema.sql",
/// "INSERT INTO users (id, email) VALUES ($1, $2)"
/// );
/// ```
///
/// ```compile_fail
/// use persistant::{Sql, sql};
/// const BAD: Sql = sql!(
/// source = postgres, targets = [sqlite], schema = "tests/doctest-schema.sql",
/// "INSERT INTO users (id) VALUES ($1)"
/// );
/// ```
pub use sql;
/// The sqlx bridge (feature `sqlx-query`): `sqlx_query!("SELECT ..", args..)`, `sqlx_query_as!(Type,
/// "SELECT ..", args..)` and `sqlx_query_scalar!("SELECT ..", args..)` take sqlx's arguments and return a
/// [`Bound`] statement; `.fetch_one(&db)`, `.fetch_optional(&db)`, `.fetch_all(&db)` and `.execute(&db)`
/// run it on a PersistAnt backend (or `InTx::new(&mut tx, db.dialect())` in a transaction). The result
/// types are read from sqlx's offline data (`.sqlx/query-<sha256>.json`, from `cargo sqlx prepare`),
/// never from a second schema, and the statement is translated by UniSQL for `targets = [..]`
/// (default `[postgres]`). With feature `sqlx-query-checked` each literal is also checked by sqlx's own
/// macro, in a closure that is never called. Options come first and are all optional: `targets = [..]`,
/// `allow = [..]` (as for `sql!`), `crate = ..` and `sqlx = ..`.
/// See `docs/DESIGN-SQLX-BRIDGE.md`.
pub use ;
/// What `sql!` expands to, and the extension traits that run it (feature `sql-macros`).
pub use ;
/// Typed rows (feature `sql-macros`): `#[derive(FromRow)]` reads a struct from a result row by
/// position, and `sql!(as T, ..)` checks a `SELECT` against it **at build time**, using the schema: the
/// number of columns, each column's name (the alias, if any), its class (a numeric column into an
/// integer or float field, text into `String`, a boolean into `bool`) and its nullability (a column
/// that can be NULL needs an `Option` field). A mismatch is a build error naming the column. Run the
/// result with `query_as`.
///
/// ```
/// use persistant::{FromRow, TypedSql, sql};
/// #[derive(FromRow)]
/// struct User { id: i64, email: String, note: Option<String> }
/// const FIND: TypedSql<User> = sql!(
/// as User, source = postgres, targets = [sqlite], schema = "tests/doctest-schema.sql",
/// "SELECT id, email, note FROM users WHERE id = $1"
/// );
/// ```
///
/// An expression column is named and typed: `count(*) AS "n: i64"` (or `Option<i64>`). The twins of
/// the failing cases below are the example above:
///
/// ```compile_fail
/// use persistant::{FromRow, TypedSql, sql};
/// #[derive(FromRow)]
/// struct User { id: i64, email: i64, note: Option<String> } // `email` is text
/// const FIND: TypedSql<User> = sql!(
/// as User, source = postgres, targets = [sqlite], schema = "tests/doctest-schema.sql",
/// "SELECT id, email, note FROM users WHERE id = $1"
/// );
/// ```
///
/// ```compile_fail
/// use persistant::{FromRow, TypedSql, sql};
/// #[derive(FromRow)]
/// struct User { id: i64, email: String, note: String } // `note` can be NULL
/// const FIND: TypedSql<User> = sql!(
/// as User, source = postgres, targets = [sqlite], schema = "tests/doctest-schema.sql",
/// "SELECT id, email, note FROM users WHERE id = $1"
/// );
/// ```
///
/// ```compile_fail
/// use persistant::{FromRow, TypedSql, sql};
/// #[derive(FromRow)]
/// struct User { id: i64, mail: String, note: Option<String> } // the column is `email`
/// const FIND: TypedSql<User> = sql!(
/// as User, source = postgres, targets = [sqlite], schema = "tests/doctest-schema.sql",
/// "SELECT id, email, note FROM users WHERE id = $1"
/// );
/// ```
pub use FromRow;
/// The trait `#[derive(FromRow)]` implements (feature `sql-macros`).
pub use FromRow;