Skip to content

Commit d5ed627

Browse files
committed
more concise examples
1 parent dd22476 commit d5ed627

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ actix-web = "4"
233233

234234
```rust
235235
use sqlx_oldapi::postgres::PgPoolOptions;
236+
use sqlx_oldapi::{query, query_as, query_as_unchecked, query_scalar, query_with};
236237
// use sqlx_oldapi::mysql::MySqlPoolOptions;
237238
// etc.
238239

@@ -249,7 +250,7 @@ async fn main() -> Result<(), sqlx_oldapi::Error> {
249250
.connect("postgres://postgres:password@localhost/test").await?;
250251

251252
// Make a simple query to return the given parameter (use a question mark `?` instead of `$1` for MySQL)
252-
let row: (i64,) = sqlx_oldapi::query_as("SELECT $1")
253+
let row: (i64,) = query_as("SELECT $1")
253254
.bind(150_i64)
254255
.fetch_one(&pool).await?;
255256

@@ -289,21 +290,21 @@ and a `Query` or `QueryAs` struct is treated as a prepared query.
289290
```rust
290291
// low-level, Executor trait
291292
conn.execute("BEGIN").await?; // unprepared, simple query
292-
conn.execute(sqlx_oldapi::query("DELETE FROM table")).await?; // prepared, cached query
293+
conn.execute(query("DELETE FROM table")).await?; // prepared, cached query
293294
```
294295

295296
We should prefer to use the high level, `query` interface whenever possible. To make this easier, there are finalizers
296297
on the type to avoid the need to wrap with an executor.
297298

298299
```rust
299-
sqlx_oldapi::query("DELETE FROM table").execute(&mut conn).await?;
300-
sqlx_oldapi::query("DELETE FROM table").execute(&pool).await?;
300+
query("DELETE FROM table").execute(&mut conn).await?;
301+
query("DELETE FROM table").execute(&pool).await?;
301302
```
302303

303304
The `execute` query finalizer returns the number of affected rows, if any, and drops all received results.
304305
In addition, there are `fetch`, `fetch_one`, `fetch_optional`, and `fetch_all` to receive results.
305306

306-
The `Query` type returned from `sqlx_oldapi::query` will return `Row<'conn>` from the database. Column values can be accessed
307+
The `Query` type returned from `query` will return `Row<'conn>` from the database. Column values can be accessed
307308
by ordinal or by name with `row.get()`. As the `Row` retains an immutable borrow on the connection, only one
308309
`Row` may exist at a time.
309310

@@ -313,7 +314,7 @@ The `fetch` query finalizer returns a stream-like type that iterates through the
313314
// provides `try_next`
314315
use futures::TryStreamExt;
315316

316-
let mut rows = sqlx_oldapi::query("SELECT * FROM users WHERE email = ?")
317+
let mut rows = query("SELECT * FROM users WHERE email = ?")
317318
.bind(email)
318319
.fetch(&mut conn);
319320

@@ -326,7 +327,7 @@ while let Some(row) = rows.try_next().await? {
326327
To assist with mapping the row into a domain type, there are two idioms that may be used:
327328

328329
```rust
329-
let mut stream = sqlx_oldapi::query("SELECT * FROM users")
330+
let mut stream = query("SELECT * FROM users")
330331
.map(|row: PgRow| {
331332
// map the row into a user-defined domain type
332333
})
@@ -337,7 +338,7 @@ let mut stream = sqlx_oldapi::query("SELECT * FROM users")
337338
#[derive(sqlx_oldapi::FromRow)]
338339
struct User { name: String, id: i64 }
339340

340-
let mut stream = sqlx_oldapi::query_as::<_, User>("SELECT * FROM users WHERE email = ? OR name = ?")
341+
let mut stream = query_as::<_, User>("SELECT * FROM users WHERE email = ? OR name = ?")
341342
.bind(user_email)
342343
.bind(user_name)
343344
.fetch(&mut conn);
@@ -352,7 +353,7 @@ We can use the macro, `sqlx_oldapi::query!` to achieve compile-time syntactic an
352353
an output to an anonymous record type where each SQL column is a Rust field (using raw identifiers where needed).
353354

354355
```rust
355-
let countries = sqlx_oldapi::query!(
356+
let countries = query!(
356357
"
357358
SELECT country, COUNT(*) as count
358359
FROM users
@@ -399,7 +400,7 @@ mostly identical except that you can name the output type.
399400
// no traits are needed
400401
struct Country { country: String, count: i64 }
401402

402-
let countries = sqlx_oldapi::query_as!(Country,
403+
let countries = query_as!(Country,
403404
"
404405
SELECT country, COUNT(*) as count
405406
FROM users

0 commit comments

Comments
 (0)