@@ -233,6 +233,7 @@ actix-web = "4"
233
233
234
234
``` rust
235
235
use sqlx_oldapi :: postgres :: PgPoolOptions ;
236
+ use sqlx_oldapi :: {query, query_as, query_as_unchecked, query_scalar, query_with};
236
237
// use sqlx_oldapi::mysql::MySqlPoolOptions;
237
238
// etc.
238
239
@@ -249,7 +250,7 @@ async fn main() -> Result<(), sqlx_oldapi::Error> {
249
250
. connect (" postgres://postgres:password@localhost/test" ). await ? ;
250
251
251
252
// 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" )
253
254
. bind (150_i64 )
254
255
. fetch_one (& pool ). await ? ;
255
256
@@ -289,21 +290,21 @@ and a `Query` or `QueryAs` struct is treated as a prepared query.
289
290
``` rust
290
291
// low-level, Executor trait
291
292
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
293
294
```
294
295
295
296
We should prefer to use the high level, ` query ` interface whenever possible. To make this easier, there are finalizers
296
297
on the type to avoid the need to wrap with an executor.
297
298
298
299
``` 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 ? ;
301
302
```
302
303
303
304
The ` execute ` query finalizer returns the number of affected rows, if any, and drops all received results.
304
305
In addition, there are ` fetch ` , ` fetch_one ` , ` fetch_optional ` , and ` fetch_all ` to receive results.
305
306
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
307
308
by ordinal or by name with ` row.get() ` . As the ` Row ` retains an immutable borrow on the connection, only one
308
309
` Row ` may exist at a time.
309
310
@@ -313,7 +314,7 @@ The `fetch` query finalizer returns a stream-like type that iterates through the
313
314
// provides `try_next`
314
315
use futures :: TryStreamExt ;
315
316
316
- let mut rows = sqlx_oldapi :: query (" SELECT * FROM users WHERE email = ?" )
317
+ let mut rows = query (" SELECT * FROM users WHERE email = ?" )
317
318
. bind (email )
318
319
. fetch (& mut conn );
319
320
@@ -326,7 +327,7 @@ while let Some(row) = rows.try_next().await? {
326
327
To assist with mapping the row into a domain type, there are two idioms that may be used:
327
328
328
329
``` rust
329
- let mut stream = sqlx_oldapi :: query (" SELECT * FROM users" )
330
+ let mut stream = query (" SELECT * FROM users" )
330
331
. map (| row : PgRow | {
331
332
// map the row into a user-defined domain type
332
333
})
@@ -337,7 +338,7 @@ let mut stream = sqlx_oldapi::query("SELECT * FROM users")
337
338
#[derive(sqlx_oldapi:: FromRow )]
338
339
struct User { name : String , id : i64 }
339
340
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 = ?" )
341
342
. bind (user_email )
342
343
. bind (user_name )
343
344
. fetch (& mut conn );
@@ -352,7 +353,7 @@ We can use the macro, `sqlx_oldapi::query!` to achieve compile-time syntactic an
352
353
an output to an anonymous record type where each SQL column is a Rust field (using raw identifiers where needed).
353
354
354
355
``` rust
355
- let countries = sqlx_oldapi :: query! (
356
+ let countries = query! (
356
357
"
357
358
SELECT country, COUNT(*) as count
358
359
FROM users
@@ -399,7 +400,7 @@ mostly identical except that you can name the output type.
399
400
// no traits are needed
400
401
struct Country { country: String , count: i64 }
401
402
402
- let countries = sqlx_oldapi :: query_as! (Country ,
403
+ let countries = query_as! (Country ,
403
404
"
404
405
SELECT country, COUNT (* ) as count
405
406
FROM users
0 commit comments