Skip to content

Commit 75b0807

Browse files
icefoxenweiznich
authored andcommitted
Impl UndecoratedInsertRecord for OwnedBatchInsert.
Same as diesel-rs#1932 but rebased onto the current `master`.
1 parent 5441a22 commit 75b0807

3 files changed

Lines changed: 29 additions & 6 deletions

File tree

diesel/src/insertable.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ where
113113
}
114114
}
115115

116-
impl<T, DB> CanInsertInSingleQuery<DB> for OwnedBatchInsert<T>
116+
impl<T, Table, DB> CanInsertInSingleQuery<DB> for OwnedBatchInsert<T, Table>
117117
where
118118
DB: Backend + SupportsDefaultKeyword,
119119
{
@@ -233,11 +233,12 @@ impl<T, Tab> Insertable<Tab> for Vec<T>
233233
where
234234
T: Insertable<Tab> + UndecoratedInsertRecord<Tab>,
235235
{
236-
type Values = OwnedBatchInsert<T::Values>;
236+
type Values = OwnedBatchInsert<T::Values, Tab>;
237237

238238
fn values(self) -> Self::Values {
239239
OwnedBatchInsert {
240240
values: self.into_iter().map(Insertable::values).collect(),
241+
_marker: PhantomData,
241242
}
242243
}
243244
}
@@ -293,11 +294,12 @@ where
293294
}
294295

295296
#[derive(Debug)]
296-
pub struct OwnedBatchInsert<V> {
297+
pub struct OwnedBatchInsert<V, Tab> {
297298
pub(crate) values: Vec<V>,
299+
_marker: PhantomData<Tab>,
298300
}
299301

300-
impl<Tab, DB, Inner> QueryFragment<DB> for OwnedBatchInsert<ValuesClause<Inner, Tab>>
302+
impl<Tab, DB, Inner> QueryFragment<DB> for OwnedBatchInsert<ValuesClause<Inner, Tab>, Tab>
301303
where
302304
DB: Backend + SupportsDefaultKeyword,
303305
ValuesClause<Inner, Tab>: QueryFragment<DB>,

diesel/src/query_builder/insert_statement/mod.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ where
249249

250250
#[cfg(feature = "sqlite")]
251251
impl<T, U, Op> ExecuteDsl<SqliteConnection>
252-
for InsertStatement<T, OwnedBatchInsert<ValuesClause<U, T>>, Op>
252+
for InsertStatement<T, OwnedBatchInsert<ValuesClause<U, T>, T>, Op>
253253
where
254254
InsertStatement<T, ValuesClause<U, T>, Op>: QueryFragment<Sqlite>,
255255
T: Copy,
@@ -406,7 +406,17 @@ impl<'a, T, Table> UndecoratedInsertRecord<Table> for BatchInsert<'a, T, Table>
406406
{
407407
}
408408

409-
impl<T, Table> UndecoratedInsertRecord<Table> for Vec<T> where [T]: UndecoratedInsertRecord<Table> {}
409+
impl<T, Table> UndecoratedInsertRecord<Table> for OwnedBatchInsert<T, Table>
410+
where
411+
T: UndecoratedInsertRecord<Table>,
412+
{
413+
}
414+
415+
impl<T, Table> UndecoratedInsertRecord<Table> for Vec<T>
416+
where
417+
[T]: UndecoratedInsertRecord<Table>,
418+
{
419+
}
410420

411421
impl<Lhs, Rhs> UndecoratedInsertRecord<Lhs::Table> for Eq<Lhs, Rhs> where Lhs: Column {}
412422

diesel_tests/tests/update.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,17 @@ fn upsert_with_no_changes_executes_do_nothing() {
243243
.execute(&connection);
244244

245245
assert_eq!(Ok(0), result);
246+
247+
// Try the same thing with an owned type.
248+
let connection = connection_with_sean_and_tess_in_users_table();
249+
let result = insert_into(users::table)
250+
.values(User::new(1, "Sean"))
251+
.on_conflict(users::id)
252+
.do_update()
253+
.set(&Changes { hair_color: None })
254+
.execute(&connection);
255+
256+
assert_eq!(Ok(0), result);
246257
}
247258

248259
#[test]

0 commit comments

Comments
 (0)