11use std:: borrow:: Cow ;
22use std:: marker:: PhantomData ;
3+ use std:: rc:: Rc ;
34
45use byteorder:: NetworkEndian ;
56use byteorder:: WriteBytesExt ;
@@ -87,7 +88,7 @@ impl CopyFromOptions {
8788pub struct CopyFrom < S , F > {
8889 options : CopyFromOptions ,
8990 copy_callback : F ,
90- p : PhantomData < S > ,
91+ target : S ,
9192}
9293
9394pub ( crate ) struct InternalCopyFromQuery < S , T > {
@@ -163,7 +164,7 @@ where
163164 & ' b self ,
164165 pass : crate :: query_builder:: AstPass < ' _ , ' b , Pg > ,
165166 ) -> crate :: QueryResult < ( ) > {
166- S :: walk_target ( pass)
167+ self . target . walk_target ( pass)
167168 }
168169}
169170
@@ -269,12 +270,16 @@ macro_rules! impl_copy_from_insertable_helper_for_values_clause {
269270diesel_derives:: __diesel_for_each_tuple!( impl_copy_from_insertable_helper_for_values_clause) ;
270271
271272#[ derive( Debug ) ]
272- pub struct InsertableWrapper < I > ( Option < I > ) ;
273+ pub struct InsertableWrapper < I , T > {
274+ table : Rc < T > ,
275+ insertable : Option < I > ,
276+ }
273277
274- impl < I , T , V , QId , const STATIC_QUERY_ID : bool > CopyFromExpression < T > for InsertableWrapper < I >
278+ impl < I , T , V , QId , const STATIC_QUERY_ID : bool > CopyFromExpression < T > for InsertableWrapper < I , T >
275279where
276280 I : Insertable < T , Values = BatchInsert < Vec < V > , T , QId , STATIC_QUERY_ID > > ,
277281 V : CopyFromInsertableHelper ,
282+ T : CopyTarget ,
278283{
279284 type Error = crate :: result:: Error ;
280285
@@ -298,7 +303,7 @@ where
298303 // this skips reallocating
299304 let mut buffer = Vec :: < u8 > :: new ( ) ;
300305 let values = self
301- . 0
306+ . insertable
302307 . take ( )
303308 . expect ( "We only call this callback once" )
304309 . values ( ) ;
@@ -354,7 +359,7 @@ where
354359 & ' b self ,
355360 pass : crate :: query_builder:: AstPass < ' _ , ' b , Pg > ,
356361 ) -> crate :: QueryResult < ( ) > {
357- < V as CopyFromInsertableHelper > :: Target :: walk_target ( pass)
362+ self . table . walk_target ( pass)
358363 }
359364}
360365
@@ -372,7 +377,7 @@ where
372377#[ must_use = "`COPY FROM` statements are only executed when calling `.execute()`." ]
373378#[ cfg( feature = "postgres_backend" ) ]
374379pub struct CopyFromQuery < T , Action > {
375- table : T ,
380+ table : Rc < T > ,
376381 action : Action ,
377382}
378383
@@ -386,15 +391,15 @@ where
386391 /// `action` expects a callback which accepts a [`std::io::Write`] argument. The necessary format
387392 /// accepted by this writer sink depends on the options provided via the `with_*` methods
388393 #[ allow( clippy:: wrong_self_convention) ] // the sql struct is named that way
389- pub fn from_raw_data < F , C , E > ( self , _target : C , action : F ) -> CopyFromQuery < T , CopyFrom < C , F > >
394+ pub fn from_raw_data < F , C , E > ( self , target : C , action : F ) -> CopyFromQuery < T , CopyFrom < C , F > >
390395 where
391396 C : CopyTarget < Table = T > ,
392397 F : Fn ( & mut dyn std:: io:: Write ) -> Result < ( ) , E > ,
393398 {
394399 CopyFromQuery {
395400 table : self . table ,
396401 action : CopyFrom {
397- p : PhantomData ,
402+ target ,
398403 options : Default :: default ( ) ,
399404 copy_callback : action,
400405 } ,
@@ -411,13 +416,16 @@ where
411416 /// This uses the binary format. It internally configures the correct
412417 /// set of settings and does not allow to set other options
413418 #[ allow( clippy:: wrong_self_convention) ] // the sql struct is named that way
414- pub fn from_insertable < I > ( self , insertable : I ) -> CopyFromQuery < T , InsertableWrapper < I > >
419+ pub fn from_insertable < I > ( self , insertable : I ) -> CopyFromQuery < T , InsertableWrapper < I , T > >
415420 where
416- InsertableWrapper < I > : CopyFromExpression < T > ,
421+ InsertableWrapper < I , T > : CopyFromExpression < T > ,
417422 {
418423 CopyFromQuery {
419- table : self . table ,
420- action : InsertableWrapper ( Some ( insertable) ) ,
424+ table : self . table . clone ( ) ,
425+ action : InsertableWrapper {
426+ table : self . table ,
427+ insertable : Some ( insertable) ,
428+ } ,
421429 }
422430 }
423431}
@@ -650,7 +658,7 @@ where
650658 T : Table ,
651659{
652660 CopyFromQuery {
653- table,
661+ table : Rc :: new ( table ) ,
654662 action : NotSet ,
655663 }
656664}
0 commit comments