@@ -494,6 +494,200 @@ impl<'a> Display for GraphqlInsertable<'a> {
494
494
}
495
495
}
496
496
writeln ! ( f, "}}" ) ?;
497
+ if cfg ! ( feature = "mysql" ) && self . table . primary_key . iter ( ) . len ( ) == 1 {
498
+ let mut out = PadAdapter :: new ( f) ;
499
+ writeln ! ( out) ?;
500
+ // FIXME ensure type of id is appropriate for i32
501
+ let id = self . table . primary_key . iter ( ) . next ( ) . unwrap ( ) ;
502
+ let table_name = & self . table . name . name ;
503
+
504
+ writeln ! (
505
+ out,
506
+ "impl<L, Ctx> HandleInsert<L, New{}, Mysql, Ctx> for {}::table" ,
507
+ fix_table_name( & self . table. name. name) ,
508
+ & table_name
509
+ ) ?;
510
+ writeln ! ( out, "where" ) ?;
511
+ writeln ! (
512
+ out,
513
+ " L: LoadingHandler<Mysql, Ctx, Table = {}::table> + 'static," ,
514
+ & table_name
515
+ ) ?;
516
+ writeln ! ( out, " L::FieldList: WundergraphFieldList<Mysql, L::PrimaryKeyIndex, {}::table, Ctx>," , & table_name) ?;
517
+ writeln ! (
518
+ out,
519
+ " <L::Filter as BuildFilter<Mysql>>::Ret: AppearsOnTable<{}::table>," ,
520
+ & table_name
521
+ ) ?;
522
+ writeln ! (
523
+ out,
524
+ " L::Columns: BuildOrder<{}::table, Mysql>" ,
525
+ & table_name
526
+ ) ?;
527
+ writeln ! ( out, " + BuildSelect<" ) ?;
528
+ writeln ! ( out, " {}::table," , & table_name) ?;
529
+ writeln ! ( out, " Mysql," ) ?;
530
+ writeln ! ( out, " SqlTypeOfPlaceholder<L::FieldList, Mysql, L::PrimaryKeyIndex, {}::table, Ctx>," , & table_name) ?;
531
+ writeln ! ( out, " >," ) ?;
532
+ writeln ! ( out, " &'static L: Identifiable," ) ?;
533
+ writeln ! (
534
+ out,
535
+ " Ctx: WundergraphContext + QueryModifier<L, Mysql>,"
536
+ ) ?;
537
+ writeln ! ( out, " Ctx::Connection: Connection<Backend = Mysql>," ) ?;
538
+ writeln ! (
539
+ out,
540
+ " <Ctx::Connection as Connection>::Backend: HasSqlType<SqlTypeOf<{}::id>>" ,
541
+ & table_name
542
+ ) ?;
543
+ writeln ! ( out, " + HasSqlType<SqlTypeOfPlaceholder<L::FieldList, Mysql, L::PrimaryKeyIndex, {}::table, Ctx>>," , table_name) ?;
544
+ writeln ! ( out, "{{" ) ?;
545
+ writeln ! ( out, " fn handle_insert(" ) ?;
546
+ writeln ! (
547
+ out,
548
+ " selection: Option<&'_ [Selection<'_, WundergraphScalarValue>]>,"
549
+ ) ?;
550
+ writeln ! (
551
+ out,
552
+ " executor: &Executor<'_, Ctx, WundergraphScalarValue>,"
553
+ ) ?;
554
+ writeln ! (
555
+ out,
556
+ " insertable: New{}," ,
557
+ fix_table_name( & self . table. name. name)
558
+ ) ?;
559
+ writeln ! ( out, " ) -> ExecutionResult<WundergraphScalarValue> {{" ) ?;
560
+ writeln ! ( out, " let ctx = executor.context();" ) ?;
561
+ writeln ! ( out, " let conn = ctx.get_connection();" ) ?;
562
+ writeln ! ( out, " let look_ahead = executor.look_ahead();" ) ?;
563
+ writeln ! (
564
+ out,
565
+ " insertable.insert_into({}::table).execute(conn).unwrap();" ,
566
+ & table_name
567
+ ) ?;
568
+ writeln ! (
569
+ out,
570
+ " let last_insert_id: i64 = diesel::select(LAST_INSERT_ID).first(conn)?;"
571
+ ) ?;
572
+ writeln ! (
573
+ out,
574
+ " let last_insert_id = i32::try_from(last_insert_id)?;"
575
+ ) ?;
576
+ writeln ! ( out, " let q = L::build_query(&[], &look_ahead)?;" ) ?;
577
+ writeln ! (
578
+ out,
579
+ " let q = FilterDsl::filter(q, {}::{}.eq_all(last_insert_id));" ,
580
+ & table_name, & id
581
+ ) ?;
582
+ writeln ! (
583
+ out,
584
+ " let items = L::load(&look_ahead, selection, executor, q)?;"
585
+ ) ?;
586
+ writeln ! (
587
+ out,
588
+ " Ok(items.into_iter().next().unwrap_or(Value::Null))"
589
+ ) ?;
590
+ writeln ! ( out, " }}" ) ?;
591
+ writeln ! ( out, "}}" ) ?;
592
+ writeln ! ( out) ?;
593
+
594
+ writeln ! (
595
+ out,
596
+ "impl<L, Ctx> HandleBatchInsert<L, New{}, Mysql, Ctx> for {}::table" ,
597
+ fix_table_name( & self . table. name. name) ,
598
+ & table_name
599
+ ) ?;
600
+ writeln ! ( out, "where" ) ?;
601
+ writeln ! (
602
+ out,
603
+ " L: LoadingHandler<Mysql, Ctx, Table = {}::table> + 'static," ,
604
+ & table_name
605
+ ) ?;
606
+ writeln ! ( out, " L::FieldList: WundergraphFieldList<Mysql, L::PrimaryKeyIndex, {}::table, Ctx>," , & table_name) ?;
607
+ writeln ! (
608
+ out,
609
+ " <L::Filter as BuildFilter<Mysql>>::Ret: AppearsOnTable<{}::table>," ,
610
+ & table_name
611
+ ) ?;
612
+ writeln ! (
613
+ out,
614
+ " L::Columns: BuildOrder<{}::table, Mysql>" ,
615
+ & table_name
616
+ ) ?;
617
+ writeln ! ( out, " + BuildSelect<" ) ?;
618
+ writeln ! ( out, " {}::table," , & table_name) ?;
619
+ writeln ! ( out, " Mysql," ) ?;
620
+ writeln ! ( out, " SqlTypeOfPlaceholder<L::FieldList, Mysql, L::PrimaryKeyIndex, {}::table, Ctx>," , & table_name) ?;
621
+ writeln ! ( out, " >," ) ?;
622
+ writeln ! ( out, " &'static L: Identifiable," ) ?;
623
+ writeln ! (
624
+ out,
625
+ " Ctx: WundergraphContext + QueryModifier<L, Mysql>,"
626
+ ) ?;
627
+ writeln ! ( out, " Ctx::Connection: Connection<Backend = Mysql>," ) ?;
628
+ writeln ! (
629
+ out,
630
+ " <Ctx::Connection as Connection>::Backend: HasSqlType<SqlTypeOf<{}::id>>" ,
631
+ & table_name
632
+ ) ?;
633
+ writeln ! ( out, " + HasSqlType<SqlTypeOfPlaceholder<L::FieldList, Mysql, L::PrimaryKeyIndex, {}::table, Ctx>>," , table_name) ?;
634
+ writeln ! ( out, "{{" ) ?;
635
+ writeln ! ( out, " fn handle_batch_insert(" ) ?;
636
+ writeln ! (
637
+ out,
638
+ " selection: Option<&'_ [Selection<'_, WundergraphScalarValue>]>,"
639
+ ) ?;
640
+ writeln ! (
641
+ out,
642
+ " executor: &Executor<'_, Ctx, WundergraphScalarValue>,"
643
+ ) ?;
644
+ writeln ! (
645
+ out,
646
+ " batch: Vec<New{}>," ,
647
+ fix_table_name( & self . table. name. name)
648
+ ) ?;
649
+ writeln ! ( out, " ) -> ExecutionResult<WundergraphScalarValue> {{" ) ?;
650
+ writeln ! ( out, " let ctx = executor.context();" ) ?;
651
+ writeln ! ( out, " let conn = ctx.get_connection();" ) ?;
652
+ writeln ! ( out, " let look_ahead = executor.look_ahead();" ) ?;
653
+ writeln ! ( out, " let single_insert = |insertable: New{}| -> ExecutionResult<WundergraphScalarValue> {{" , fix_table_name( & self . table. name. name) ) ?;
654
+ writeln ! (
655
+ out,
656
+ " insertable.insert_into({}::table).execute(conn).unwrap();" ,
657
+ & table_name
658
+ ) ?;
659
+ writeln ! ( out, " let last_insert_id: i64 = diesel::select(LAST_INSERT_ID).first(conn)?;" ) ?;
660
+ writeln ! (
661
+ out,
662
+ " let last_insert_id = i32::try_from(last_insert_id)?;"
663
+ ) ?;
664
+ writeln ! (
665
+ out,
666
+ " let q = L::build_query(&[], &look_ahead)?;"
667
+ ) ?;
668
+ writeln ! (
669
+ out,
670
+ " let q = FilterDsl::filter(q, {}::{}.eq_all(last_insert_id));" ,
671
+ & table_name, & id
672
+ ) ?;
673
+ writeln ! (
674
+ out,
675
+ " let items = L::load(&look_ahead, selection, executor, q)?;"
676
+ ) ?;
677
+ writeln ! (
678
+ out,
679
+ " Ok(items.into_iter().next().unwrap_or(Value::Null))"
680
+ ) ?;
681
+ writeln ! ( out, " }};" ) ?;
682
+ writeln ! ( out, " let r = batch" ) ?;
683
+ writeln ! ( out, " .into_iter()" ) ?;
684
+ writeln ! ( out, " .map(|i| single_insert(i))" ) ?;
685
+ writeln ! ( out, " .collect::<Result<Vec<_>, _>>()?;" ) ?;
686
+ writeln ! ( out, " Ok(Value::List(r))" ) ?;
687
+ writeln ! ( out, " }}" ) ?;
688
+ writeln ! ( out, "}}" ) ?;
689
+ writeln ! ( out) ?;
690
+ }
497
691
Ok ( ( ) )
498
692
}
499
693
}
0 commit comments