@@ -47,6 +47,7 @@ mod single_value_dsl;
47
47
pub use self :: belonging_to_dsl:: BelongingToDsl ;
48
48
pub use self :: combine_dsl:: CombineDsl ;
49
49
pub use self :: join_dsl:: { InternalJoinDsl , JoinOnDsl , JoinWithImplicitOnClause } ;
50
+ use self :: load_dsl:: LoadIntoDsl ;
50
51
#[ doc( hidden) ]
51
52
pub use self :: load_dsl:: LoadQuery ;
52
53
pub use self :: save_changes_dsl:: { SaveChangesDsl , UpdateAndFetchResults } ;
@@ -1303,8 +1304,63 @@ pub trait RunQueryDsl<Conn>: Sized {
1303
1304
self . internal_load ( conn)
1304
1305
}
1305
1306
1307
+ /// Executes the given query, returning a `Vec` with the returned rows.
1308
+ ///
1309
+ /// This method is similar to [load](self::RunQueryDsl::load), but sets
1310
+ /// a corresponding select/returning clause based on the
1311
+ /// [Selectable](crate::query_builder::Selectable) impl for `U`.
1312
+ ///
1313
+ ///
1314
+ /// # Examples
1315
+ ///
1316
+ /// ## Returning a struct
1317
+ ///
1318
+ /// ```rust
1319
+ /// # include!("../doctest_setup.rs");
1320
+ /// # use schema::users;
1321
+ /// #
1322
+ /// #[derive(Selectable, Queryable, PartialEq, Debug)]
1323
+ /// #[table_name = "users"]
1324
+ /// struct User {
1325
+ /// id: i32,
1326
+ /// name: String,
1327
+ /// }
1328
+ ///
1329
+ /// # fn main() {
1330
+ /// # run_test();
1331
+ /// # }
1332
+ /// #
1333
+ /// # fn run_test() -> QueryResult<()> {
1334
+ /// # use diesel::insert_into;
1335
+ /// # use schema::users::dsl::*;
1336
+ /// # let connection = establish_connection();
1337
+ /// let data = users
1338
+ /// .load_into::<User>(&connection)?;
1339
+ ///
1340
+ /// // This query is equivalent to
1341
+ /// // users.select((users::id, users::name)).load::<User>(&connection)?;
1342
+ ///
1343
+ /// let expected_data = vec![
1344
+ /// User { id: 1, name: String::from("Sean") },
1345
+ /// User { id: 2, name: String::from("Tess") },
1346
+ /// ];
1347
+ /// assert_eq!(expected_data, data);
1348
+ /// # Ok(())
1349
+ /// # }
1350
+ /// ```
1351
+ fn load_into < U > ( self , conn : & Conn ) -> QueryResult < Vec < U > >
1352
+ where
1353
+ Self : LoadIntoDsl < Conn , U > ,
1354
+ {
1355
+ LoadIntoDsl :: load_into ( self , conn)
1356
+ }
1357
+
1306
1358
/// Runs the command, and returns the affected row.
1307
1359
///
1360
+ /// This method behaves similar to [get_result](self::RunQueryDsl::get_result)
1361
+ /// than [load_into](self::RunQueryDsl::load_into) behaves to
1362
+ /// [load](self::RunQueryDsl::load)
1363
+ ///
1308
1364
/// `Err(NotFound)` will be returned if the query affected 0 rows. You can
1309
1365
/// call `.optional()` on the result of this if the command was optional to
1310
1366
/// get back a `Result<Option<U>>`
@@ -1355,6 +1411,67 @@ pub trait RunQueryDsl<Conn>: Sized {
1355
1411
first_or_not_found ( self . load ( conn) )
1356
1412
}
1357
1413
1414
+ /// Runs the command, and returns the affected row.
1415
+ ///
1416
+ /// `Err(NotFound)` will be returned if the query affected 0 rows. You can
1417
+ /// call `.optional()` on the result of this if the command was optional to
1418
+ /// get back a `Result<Option<U>>`
1419
+ ///
1420
+ /// When this method is called on an insert, update, or delete statement,
1421
+ /// it will implicitly add a `RETURNING *` to the query,
1422
+ /// unless a returning clause was already specified.
1423
+ ///
1424
+ /// This method only returns the first row that was affected, even if more
1425
+ /// rows are affected.
1426
+ ///
1427
+ /// # Example
1428
+ ///
1429
+ /// ```rust
1430
+ /// # include!("../doctest_setup.rs");
1431
+ /// #
1432
+ /// #
1433
+ /// # fn main() {
1434
+ /// # run_test();
1435
+ /// # }
1436
+ /// #
1437
+ /// # use schema::users;
1438
+ /// #
1439
+ /// #[derive(Selectable, Queryable, PartialEq, Debug)]
1440
+ /// #[table_name = "users"]
1441
+ /// pub struct UserName {
1442
+ /// name: String
1443
+ /// }
1444
+ /// #
1445
+ /// # #[cfg(feature = "postgres")]
1446
+ /// # fn run_test() -> QueryResult<()> {
1447
+ /// # use diesel::{insert_into, update};
1448
+ /// # use schema::users::dsl::*;
1449
+ /// # let connection = establish_connection();
1450
+ /// let inserted_row = insert_into(users)
1451
+ /// .values(name.eq("Ruby"))
1452
+ /// .load_into::<UserName>(&connection)?;
1453
+ /// assert_eq!(UserName { name: "Ruby".into_string() }, inserted_row);
1454
+ ///
1455
+ /// // This will return `NotFound`, as there is no user with ID 4
1456
+ /// let update_result = update(users.find(4))
1457
+ /// .set(name.eq("Jim"))
1458
+ /// .load_into::<UserName>(&connection);
1459
+ /// assert_eq!(Err(diesel::NotFound), update_result);
1460
+ /// # Ok(())
1461
+ /// # }
1462
+ /// #
1463
+ /// # #[cfg(not(feature = "postgres"))]
1464
+ /// # fn run_test() -> QueryResult<()> {
1465
+ /// # Ok(())
1466
+ /// # }
1467
+ /// ```
1468
+ fn load_into_single < U > ( self , conn : & Conn ) -> QueryResult < U >
1469
+ where
1470
+ Self : LoadIntoDsl < Conn , U > ,
1471
+ {
1472
+ first_or_not_found ( <_ as RunQueryDsl < _ > >:: load_into ( self , conn) )
1473
+ }
1474
+
1358
1475
/// Runs the command, returning an `Vec` with the affected rows.
1359
1476
///
1360
1477
/// This method is an alias for [`load`], but with a name that makes more
0 commit comments