Skip to content

Commit 8bd39bc

Browse files
vbeffamehcodetoshokanjplatteRhnSharma
authored
Merge master into ws-support (#6)
* release: v0.5.4 * Fix infinite compile loop regression from recursive Lazy reference * release: v0.5.5 * Improve root README.md and sqlx-cli/README.md (launchbadge#1262) * readme: Fix inconsistent list style * readme: Improve text alignment * readme: Fix missing links * readme: Consistently use code formatting for runtime & TLS crates and dedup links * readme: Add SQLx is not an ORM section * readme: Improve documentation about offline mode * Rename _expr to expr (launchbadge#1264) Co-authored-by: Ryan Leckey <[email protected]> Co-authored-by: toshokan <[email protected]> Co-authored-by: Jonas Platte <[email protected]> Co-authored-by: Rohan Sharma <[email protected]>
1 parent 38d57b5 commit 8bd39bc

File tree

3 files changed

+56
-21
lines changed

3 files changed

+56
-21
lines changed

README.md

+39-11
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<br />
5151

5252
<div align="center">
53-
<sub>Built with ❤️ by <a href="https://launchbadge.com">The LaunchBadge team</a></sub>
53+
<small>Built with ❤️ by <a href="https://launchbadge.com">The LaunchBadge team</a></small>
5454
</div>
5555

5656
<br />
@@ -59,20 +59,23 @@ SQLx is an async, pure Rust<sub>†</sub> SQL crate featuring compile-time check
5959

6060
- **Truly Asynchronous**. Built from the ground-up using async/await for maximum concurrency.
6161

62-
- **Type-safe SQL** (if you want it) without DSLs. Use the `query!()` macro to check your SQL and bind parameters at
63-
compile time. (You can still use dynamic SQL queries if you like.)
62+
- **Compile-time checked queries** (if you want). See [SQLx is not an ORM](#sqlx-is-not-an-orm).
6463

6564
- **Database Agnostic**. Support for [PostgreSQL], [MySQL], [SQLite], and [MSSQL].
6665

6766
- **Pure Rust**. The Postgres and MySQL/MariaDB drivers are written in pure Rust using **zero** unsafe<sub>††</sub> code.
6867

69-
* **Runtime Agnostic**. Works on different runtimes ([async-std](https://crates.io/crates/async-std) / [tokio](https://crates.io/crates/tokio) / [actix](https://crates.io/crates/actix-rt)) and TLS backends ([native-tls](https://crates.io/crates/native-tls), [rustls](https://crates.io/crates/rustls)).
68+
- **Runtime Agnostic**. Works on different runtimes ([`async-std`] / [`tokio`] / [`actix`]) and TLS backends ([`native-tls`], [`rustls`]).
7069

71-
<sub><sup>† The SQLite driver uses the libsqlite3 C library as SQLite is an embedded database (the only way
72-
we could be pure Rust for SQLite is by porting _all_ of SQLite to Rust).</sup></sub>
70+
<small><small>
7371

74-
<sub><sup>†† SQLx uses `#![forbid(unsafe_code)]` unless the `sqlite` feature is enabled. As the SQLite driver interacts
75-
with C, those interactions are `unsafe`.</sup></sub>
72+
† The SQLite driver uses the libsqlite3 C library as SQLite is an embedded database (the only way
73+
we could be pure Rust for SQLite is by porting _all_ of SQLite to Rust).
74+
75+
†† SQLx uses `#![forbid(unsafe_code)]` unless the `sqlite` feature is enabled. As the SQLite driver interacts
76+
with C, those interactions are `unsafe`.
77+
78+
</small></small>
7679

7780
[postgresql]: http://postgresql.org/
7881
[sqlite]: https://sqlite.org/
@@ -108,6 +111,8 @@ SQLx is compatible with the [`async-std`], [`tokio`] and [`actix`] runtimes; and
108111
[`async-std`]: https://github.com/async-rs/async-std
109112
[`tokio`]: https://github.com/tokio-rs/tokio
110113
[`actix`]: https://github.com/actix/actix-net
114+
[`native-tls`]: https://crates.io/crates/native-tls
115+
[`rustls`]: https://crates.io/crates/rustls
111116

112117
```toml
113118
# Cargo.toml
@@ -118,7 +123,7 @@ sqlx = { version = "0.5", features = [ "runtime-tokio-rustls" ] }
118123
sqlx = { version = "0.5", features = [ "runtime-async-std-native-tls" ] }
119124
```
120125

121-
<sub><sup>The runtime and TLS backend not being separate feature sets to select is a workaround for a [Cargo issue](https://github.com/rust-lang/cargo/issues/3494).</sup></sub>
126+
<small><small>The runtime and TLS backend not being separate feature sets to select is a workaround for a [Cargo issue](https://github.com/rust-lang/cargo/issues/3494).</small></small>
122127

123128
#### Cargo Feature Flags
124129

@@ -168,6 +173,24 @@ sqlx = { version = "0.5", features = [ "runtime-async-std-native-tls" ] }
168173

169174
- `tls`: Add support for TLS connections.
170175

176+
## SQLx is not an ORM!
177+
178+
SQLx supports **compile-time checked queries**. It does not, however, do this by providing a Rust
179+
API or DSL (domain-specific language) for building queries. Instead, it provides macros that take
180+
regular SQL as an input and ensure that it is valid for your database. The way this works is that
181+
SQLx connects to your development DB at compile time to have the database itself verify (and return
182+
some info on) your SQL queries. This has some potentially surprising implications:
183+
184+
- Since SQLx never has to parse the SQL string itself, any syntax that the development DB accepts
185+
can be used (including things added by database extensions)
186+
- Due to the different amount of information databases let you retrieve about queries, the extent of
187+
SQL verification you get from the query macros depends on the database
188+
189+
**If you are looking for an (asynchronous) ORM,** you can check out [`ormx`], which is built on top
190+
of SQLx.
191+
192+
[`ormx`]: https://crates.io/crates/ormx
193+
171194
## Usage
172195

173196
### Quickstart
@@ -336,8 +359,8 @@ Differences from `query()`:
336359
```
337360

338361
The biggest downside to `query!()` is that the output type cannot be named (due to Rust not
339-
officially supporting anonymous records). To address that, there is a `query_as!()` macro that is identical
340-
except that you can name the output type.
362+
officially supporting anonymous records). To address that, there is a `query_as!()` macro that is
363+
mostly identical except that you can name the output type.
341364

342365
```rust
343366
// no traits are needed
@@ -359,6 +382,11 @@ WHERE organization = ?
359382
// countries[0].count
360383
```
361384

385+
To avoid the need of having a development database around to compile the project even when no
386+
modifications (to the database-accessing parts of the code) are done, you can enable "offline mode"
387+
to cache the results of the SQL query analysis using the `sqlx` command-line tool. See
388+
[sqlx-cli/README.md](./sqlx-cli/README.md#enable-building-in-offline-mode-with-query).
389+
362390
## Safety
363391

364392
This crate uses `#![forbid(unsafe_code)]` to ensure everything is implemented in 100% Safe Rust.

sqlx-cli/README.md

+12-5
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,32 @@ $ sqlx migrate run
4949
Compares the migration history of the running database against the `migrations/` folder and runs
5050
any scripts that are still pending.
5151

52-
#### Enable building in "offline" mode with `query!()`
52+
#### Enable building in "offline mode" with `query!()`
5353

5454
Note: must be run as `cargo sqlx`.
5555

5656
```bash
5757
cargo sqlx prepare
5858
```
59-
Saves query data to `sqlx-data.json` in the current directory; check this file into version control
60-
and an active database connection will no longer be needed to build your project.
6159

62-
Has no effect unless the `offline` feature of `sqlx` is enabled in your project. Omitting that feature is the most likely cause if you get a `sqlx-data.json` file that looks like this:
60+
Saves query metadata to `sqlx-data.json` in the current directory; check this file into version
61+
control and an active database connection will no longer be needed to build your project.
62+
63+
Has no effect unless the `offline` feature of `sqlx` is enabled in your project. Omitting that
64+
feature is the most likely cause if you get a `sqlx-data.json` file that looks like this:
6365

6466
```json
6567
{
6668
"database": "PostgreSQL"
6769
}
6870
```
6971

70-
----
72+
---
73+
7174
```bash
7275
cargo sqlx prepare --check
7376
```
77+
7478
Exits with a nonzero exit status if the data in `sqlx-data.json` is out of date with the current
7579
database schema and queries in the project. Intended for use in Continuous Integration.
7680

@@ -79,3 +83,6 @@ database schema and queries in the project. Intended for use in Continuous Integ
7983
To make sure an accidentally-present `DATABASE_URL` environment variable or `.env` file does not
8084
result in `cargo build` (trying to) access the database, you can set the `SQLX_OFFLINE` environment
8185
variable to `true`.
86+
87+
If you want to make this the default, just add it to your `.env` file. `cargo sqlx prepare` will
88+
still do the right thing and connect to the database.

sqlx-macros/src/query/args.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ pub fn quote_args<DB: DatabaseExt>(
7979
use ::sqlx::ty_match::{WrapSameExt as _, MatchBorrowExt as _};
8080

8181
// evaluate the expression only once in case it contains moves
82-
let _expr = ::sqlx::ty_match::dupe_value(#name);
82+
let expr = ::sqlx::ty_match::dupe_value(#name);
8383

84-
// if `_expr` is `Option<T>`, get `Option<$ty>`, otherwise `$ty`
85-
let ty_check = ::sqlx::ty_match::WrapSame::<#param_ty, _>::new(&_expr).wrap_same();
84+
// if `expr` is `Option<T>`, get `Option<$ty>`, otherwise `$ty`
85+
let ty_check = ::sqlx::ty_match::WrapSame::<#param_ty, _>::new(&expr).wrap_same();
8686

87-
// if `_expr` is `&str`, convert `String` to `&str`
88-
let (mut _ty_check, match_borrow) = ::sqlx::ty_match::MatchBorrow::new(ty_check, &_expr);
87+
// if `expr` is `&str`, convert `String` to `&str`
88+
let (mut _ty_check, match_borrow) = ::sqlx::ty_match::MatchBorrow::new(ty_check, &expr);
8989

9090
_ty_check = match_borrow.match_borrow();
9191

0 commit comments

Comments
 (0)