1
1
use std:: borrow:: Cow ;
2
2
use std:: marker:: PhantomData ;
3
+ use std:: rc:: Rc ;
3
4
4
5
use byteorder:: NetworkEndian ;
5
6
use byteorder:: WriteBytesExt ;
@@ -87,7 +88,7 @@ impl CopyFromOptions {
87
88
pub struct CopyFrom < S , F > {
88
89
options : CopyFromOptions ,
89
90
copy_callback : F ,
90
- p : PhantomData < S > ,
91
+ target : S ,
91
92
}
92
93
93
94
pub ( crate ) struct InternalCopyFromQuery < S , T > {
@@ -163,7 +164,7 @@ where
163
164
& ' b self ,
164
165
pass : crate :: query_builder:: AstPass < ' _ , ' b , Pg > ,
165
166
) -> crate :: QueryResult < ( ) > {
166
- S :: walk_target ( pass)
167
+ self . target . walk_target ( pass)
167
168
}
168
169
}
169
170
@@ -269,12 +270,16 @@ macro_rules! impl_copy_from_insertable_helper_for_values_clause {
269
270
diesel_derives:: __diesel_for_each_tuple!( impl_copy_from_insertable_helper_for_values_clause) ;
270
271
271
272
#[ derive( Debug ) ]
272
- pub struct InsertableWrapper < I > ( Option < I > ) ;
273
+ pub struct InsertableWrapper < I , T > {
274
+ table : Rc < T > ,
275
+ insertable : Option < I > ,
276
+ }
273
277
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 >
275
279
where
276
280
I : Insertable < T , Values = BatchInsert < Vec < V > , T , QId , STATIC_QUERY_ID > > ,
277
281
V : CopyFromInsertableHelper ,
282
+ T : CopyTarget ,
278
283
{
279
284
type Error = crate :: result:: Error ;
280
285
@@ -298,7 +303,7 @@ where
298
303
// this skips reallocating
299
304
let mut buffer = Vec :: < u8 > :: new ( ) ;
300
305
let values = self
301
- . 0
306
+ . insertable
302
307
. take ( )
303
308
. expect ( "We only call this callback once" )
304
309
. values ( ) ;
@@ -354,7 +359,7 @@ where
354
359
& ' b self ,
355
360
pass : crate :: query_builder:: AstPass < ' _ , ' b , Pg > ,
356
361
) -> crate :: QueryResult < ( ) > {
357
- < V as CopyFromInsertableHelper > :: Target :: walk_target ( pass)
362
+ self . table . walk_target ( pass)
358
363
}
359
364
}
360
365
@@ -372,7 +377,7 @@ where
372
377
#[ must_use = "`COPY FROM` statements are only executed when calling `.execute()`." ]
373
378
#[ cfg( feature = "postgres_backend" ) ]
374
379
pub struct CopyFromQuery < T , Action > {
375
- table : T ,
380
+ table : Rc < T > ,
376
381
action : Action ,
377
382
}
378
383
@@ -386,15 +391,15 @@ where
386
391
/// `action` expects a callback which accepts a [`std::io::Write`] argument. The necessary format
387
392
/// accepted by this writer sink depends on the options provided via the `with_*` methods
388
393
#[ 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 > >
390
395
where
391
396
C : CopyTarget < Table = T > ,
392
397
F : Fn ( & mut dyn std:: io:: Write ) -> Result < ( ) , E > ,
393
398
{
394
399
CopyFromQuery {
395
400
table : self . table ,
396
401
action : CopyFrom {
397
- p : PhantomData ,
402
+ target ,
398
403
options : Default :: default ( ) ,
399
404
copy_callback : action,
400
405
} ,
@@ -411,13 +416,16 @@ where
411
416
/// This uses the binary format. It internally configures the correct
412
417
/// set of settings and does not allow to set other options
413
418
#[ 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 > >
415
420
where
416
- InsertableWrapper < I > : CopyFromExpression < T > ,
421
+ InsertableWrapper < I , T > : CopyFromExpression < T > ,
417
422
{
418
423
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
+ } ,
421
429
}
422
430
}
423
431
}
@@ -650,7 +658,7 @@ where
650
658
T : Table ,
651
659
{
652
660
CopyFromQuery {
653
- table,
661
+ table : Rc :: new ( table ) ,
654
662
action : NotSet ,
655
663
}
656
664
}
0 commit comments