queries_for_sqlx/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
pub mod create_table_st;
pub(crate) mod create_table_st2;
#[cfg(test)]
pub mod debug_query;
pub mod debug_sql;
pub mod delete_st;
pub mod executable;
pub mod execute_no_cache;
pub mod expressions;
pub mod insert_many_st;
pub mod insert_one_st;
pub mod quick_query;
pub mod returning;
pub mod sanitize;
pub mod select_st;
pub mod string_query;
pub mod update_st;

pub mod macros {
    pub use query_macros::*;
}

pub mod prelude {
    pub use crate::create_table_st::constraints::exports::*;
    pub use crate::create_table_st::exports::*;
    pub use crate::execute_no_cache::ExecuteNoCache;
    pub use crate::expressions::exports::*;
    pub use crate::expressions::SelectHelpers2;
    use crate::sanitize::Sanitize;
    pub use crate::select_st::exports::*;
    pub use crate::select_st::joins::join_type;
    pub use crate::select_st::order_by;

    pub fn sanitize<T>(t: T) -> Sanitize<T> {
        Sanitize(t)
    }

    pub mod stmt {
        use crate::create_table_st::CreateTableSt;
        use crate::insert_one_st::InsertStOne;
        use crate::quick_query::QuickQuery;
        use crate::string_query::StringQuery;
        use crate::Query;
        use crate::SupportNamedBind;
        use sqlx::Database;
        use std::marker::PhantomData;

        pub use crate::insert_many_st::insert_many;

        pub fn insert_one<S>(
            from: &'static str,
        ) -> InsertStOne<S>
        where
            S: Database,
        {
            InsertStOne {
                input: Vec::new(),
                output: None,
                from,
                buffer: Default::default(),
                _pd: PhantomData,
                returning: (),
            }
        }

        pub fn create_table_if_not_exists<'q, S>(
            name: &'static str,
        ) -> CreateTableSt<S, QuickQuery<'q>>
        where
            QuickQuery<'q>: Query<S>,
        {
            CreateTableSt {
                header: "CREATE TABLE IF NOT EXISTS".to_string(),
                ident: (None, name.to_string()),
                columns: Vec::new(),
                foreign_keys: Vec::new(),
                ctx: Default::default(),
                _sqlx: Default::default(),
                verbatim: Default::default(),
            }
        }

        pub fn string_query<I>(
            query: String,
            input: I,
        ) -> StringQuery<I> {
            StringQuery { sql: query, input }
        }

        pub fn select<'q, S>(
            table: &'static str,
        ) -> crate::select_st::SelectSt<S, QuickQuery<'q>>
        where
            S: Database + SupportNamedBind,
        {
            crate::select_st::SelectSt {
                select_list: Default::default(),
                where_clause: Default::default(),
                joins: Default::default(),
                ctx: Default::default(),
                order_by: Default::default(),
                limit: Default::default(),
                shift: Default::default(),
                from: table,
                _sqlx: Default::default(),
            }
        }

        pub fn update<'q, S>(
            table: &'static str,
        ) -> crate::update_st::UpdateSt<S, QuickQuery<'q>, ()>
        where
            S: Database + SupportNamedBind,
        {
            crate::update_st::UpdateSt {
                sets: Default::default(),
                where_clause: Default::default(),
                ctx: Default::default(),
                table,
                returning: (),
                _sqlx: Default::default(),
            }
        }

        pub fn delete<'q, S>(
            table: &'static str,
        ) -> crate::delete_st::DeleteSt<S, QuickQuery<'q>>
        where
            S: Database + SupportNamedBind,
        {
            crate::delete_st::DeleteSt {
                where_clause: Default::default(),
                ctx: Default::default(),
                table,
                returning: (),
                _sqlx: Default::default(),
            }
        }
    }
}

pub mod from_row {
    use sqlx::{Database, FromRow};

    /// rely on Sqlx's `FromRow` trait to convert a row
    /// I put this on its own method because sometime
    /// I rely on more dynamic ways to convert a row
    /// and I want to keep sqlx's trait separate
    pub fn sqlx_from_row<T, S>(
    ) -> impl FnMut(S::Row) -> Result<T, sqlx::Error>
    where
        S: Database,
        for<'r> T: FromRow<'r, S::Row>,
    {
        |row| T::from_row(&row)
    }

    pub fn reflexive<S>(
    ) -> impl FnMut(S) -> Result<S, sqlx::Error> {
        |row| Ok(row)
    }
}

use sqlx::{database::HasArguments, Database, Postgres, Sqlite};

pub trait Query<S>: Sized {
    type SqlPart;
    type Context1: Default;
    type Context2: Default;
    #[deprecated = "in favor of ToSqlPart"]
    fn handle_where_item(
        _: impl WhereItem<S, Self> + 'static,
        _: &mut Self::Context1,
    ) -> Self::SqlPart {
        panic!("depricate in favor of ToSqlPart")
    }

    fn build_sql_part_back(
        ctx: &mut Self::Context2,
        from: Self::SqlPart,
    ) -> String;

    type Output;
    fn build_query(
        ctx1: Self::Context1,
        f: impl FnOnce(&mut Self::Context2) -> String,
    ) -> (String, Self::Output);
}

#[cfg(not(feature = "build_statments"))]
pub(crate) use sql_part_::inner as sql_part;
#[cfg(feature = "build_statements")]
pub use sql_part_::inner as sql_part;

mod sql_part_ {
    pub mod inner {
        use std::marker::PhantomData;

        use sqlx::Database;

        /// a given type can implement both WhereItem and Accept
        /// in order for rust to convert to SqlPart, it has to know
        /// which impl to target, here there are few new-type structs
        /// for each trait
        use crate::{
            create_table_st2::new_constraint_trait::Constraint,
            Accept, Query, WhereItem,
        };

        pub struct WhereItemToSqlPart<T>(pub T);
        pub struct AcceptToSqlPart<T>(pub T);
        pub struct ConstraintToSqlPart<T, Q>(
            pub T,
            pub PhantomData<Q>,
        );
        pub struct ColumnToSqlPart<T, Q>(
            pub T,
            pub PhantomData<Q>,
        );

        /// a given type can implement both WhereItem and Accept
        /// in order for rust to convert to SqlPart, it has to know
        /// which impl to target, here there are few new-type structs
        /// for each trait
        /// this has blanket implementation for trivial queries:
        /// SqlPart = String, Context2 = ()
        pub trait ToSqlPart<Q: Query<S>, S> {
            fn to_sql_part(
                self,
                ctx: &mut Q::Context1,
            ) -> Q::SqlPart;
        }

        impl<Q, S, T> ToSqlPart<Q, S> for WhereItemToSqlPart<T>
        where
            S: Database,
            T: WhereItem<S, Q>,
            Q: Query<S, SqlPart = String, Context2 = ()>,
        {
            fn to_sql_part(
                self,
                ctx: &mut <Q as Query<S>>::Context1,
            ) -> <Q as Query<S>>::SqlPart
            where
                Q: Query<S>,
            {
                let item = self.0.where_item(ctx);
                item(&mut ())
            }
        }

        impl<'q, Q, S, T> ToSqlPart<Q, S> for ConstraintToSqlPart<T, Q>
        where
            S: Database,
            T: Constraint<S>,
            Q: Query<S, SqlPart = String, Context2 = ()>,
        {
            fn to_sql_part(
                self,
                ctx: &mut <Q as Query<S>>::Context1,
            ) -> <Q as Query<S>>::SqlPart
            where
                Q: Query<S>,
            {
                self.0.constraint::<Q>(ctx)(&mut ())
            }
        }

        impl<'q, Q, S, T> ToSqlPart<Q, S>
            for crate::sql_part::ColumnToSqlPart<T, Q>
        where
            S: Database,
            T: Constraint<S>,
            Q: Query<S, SqlPart = String, Context2 = ()>,
        {
            fn to_sql_part(
                self,
                ctx: &mut <Q as Query<S>>::Context1,
            ) -> <Q as Query<S>>::SqlPart
            where
                Q: Query<S>,
            {
                self.0.constraint::<Q>(ctx)(&mut ())
            }
        }
        impl<Q, S, T> ToSqlPart<Q, S> for AcceptToSqlPart<T>
        where
            Q: Accept<T, S>,
            Q: Query<S, SqlPart = String, Context2 = ()>,
        {
            fn to_sql_part(
                self,
                ctx: &mut <Q as Query<S>>::Context1,
            ) -> <Q as Query<S>>::SqlPart
            where
                Q: Query<S>,
            {
                <Q as Accept<T, S>>::accept(self.0, ctx)(&mut ())
            }
        }
    }
}

pub trait Statement<S, Q: Query<S>> {
    type Init;
    fn init(init: Self::Init) -> Self;
    fn _build(self) -> (String, Q::Output);
}

pub trait SupportNamedBind {}
pub trait SupportReturning {}

pub trait Accept<This, S>: Query<S> {
    fn accept(
        this: This,
        ctx1: &mut Self::Context1,
    ) -> impl FnOnce(&mut Self::Context2) -> String + 'static + Send;
}

pub trait SelectItem<S> {
    fn select_item(self) -> String;
}

pub trait WhereItem<S, Q: Query<S>> {
    fn where_item(
        self,
        ctx: &mut Q::Context1,
    ) -> impl FnOnce(&mut Q::Context2) -> String;
}

pub trait IntoMutArguments<'q, DB>
where
    Self: Sized,
    DB: Database,
{
    fn into_arguments(
        self,
        argument: &mut <DB as HasArguments<'q>>::Arguments,
    );
}

#[cfg(todo)]
mod into_mut_generalization {
    use core::hash;
    use std::any::Any;
    use std::marker::PhantomData;
    use std::ops::Not;

    use futures_util::Future;
    use sqlx::database::HasArguments;
    use sqlx::{Arguments, Database, Pool, Sqlite};

    use crate::create_table_st::constraints::exports::default;
    use crate::executable::InnerExecutable;
    use crate::select_st::SelectSt;
    use crate::{Accept, IntoMutArguments, Query, Statement};

    pub trait IntoMutArgumentsMeta<'q, DB>:
        IntoMutArguments<'q, DB>
    where
        DB: Database,
    {
        type Meta: BindMeta;
    }

    pub trait BindMeta: Sized {
        fn shift(self, by: usize) -> Self;
        fn names(&self) -> &[&'static str];
    }

    struct DynamicQuery<S>(PhantomData<S>);

    impl<S> DynamicQuery<S> {
        pub fn build<St: Statement<S, Self>>(
            st: St,
        ) -> (CachedExecute<S>, DynamicDeserializer<S>) {
            let str = st._build();
            todo!()
        }
    }

    impl<S> Query<S> for DynamicQuery<S> {
        type SqlPart = String;

        type Context1 = Vec<Input>;

        type Context2 = ();

        fn build_sql_part_back(
            ctx: &mut Self::Context2,
            from: Self::SqlPart,
        ) -> String {
            todo!()
        }

        type Output = ();

        fn build_query(
            ctx1: Self::Context1,
            f: impl FnOnce(&mut Self::Context2) -> String,
        ) -> (String, Self::Output) {
            todo!()
        }
    }

    struct Input(&'static str, Box<dyn Ty>);

    trait Ty {}
    impl<T: 'static> Ty for PhantomData<T> {}

    fn input<T: 'static>(name: &'static str) -> Input {
        Input(name, Box::new(PhantomData::<T>))
    }

    impl<S> Accept<Input, S> for DynamicQuery<S> {
        fn accept(
            this: Input,
            ctx1: &mut Self::Context1,
        ) -> impl FnOnce(&mut Self::Context2) -> String + 'static + Send
        {
            ctx1.push(this);
            let len = ctx1.len();
            move |_| format!("${}", len)
        }
    }

    struct CachedExecute<S>(String, PhantomData<S>);

    struct H(std::collections::HashMap<(), ()>);

    impl<S: Database> CachedExecute<S> {
        fn execute<E, H: hash::Hash + 'static>(
            self,
            executor: E,
            // 'static restriction here can be lifted if I
            // restrict the output future to execute before
            // an lifetime 'q
            // the lifetime is just for Sqlite value that
            // can be some Cow<'q, str>
            input: (H, <S as HasArguments<'static>>::Arguments),
        ) -> impl Future<
            Output = Result<S::QueryResult, sqlx::Error>,
        > + Send
        where
            E: for<'e> sqlx::Executor<'e, Database = S>,
        {
            if <H as Any>::type_id(&input.0)
                .eq(&().type_id())
                .not()
            {
                todo!("chached statment")
            }
            async move {
                InnerExecutable {
                    stmt: self.0.as_str(),
                    buffer: input.1,
                    persistent: false,
                }
                .execute(executor)
                .await
            }
        }
    }

    struct DynamicDeserializer<S>(PhantomData<S>);

    impl<S: Database> DynamicDeserializer<S> {
        pub fn input(
            &self,
            input: serde_json::Value,
        ) -> Result<
            ((), <S as HasArguments<'static>>::Arguments),
            serde_json::Error,
        > {
            todo!()
        }
    }

    async fn concept() {
        use serde_json::json;

        use crate::{
            expressions::exports::{all_columns, col},
            from_row,
            prelude::stmt,
        };

        let mut st =
            SelectSt::<Sqlite, DynamicQuery<Sqlite>>::init(
                "Users",
            );

        st.select(all_columns());

        st.where_(col("id").eq(input::<i32>("by_id")));
        st.limit(input::<i32>("limit"));

        let (prepared_st, des) = DynamicQuery::build(st);

        let pool = Pool::<Sqlite>::connect("").await.unwrap();

        let dynamic_value = json!({
            "limit": 3,
            "by_id": 1,
        });

        prepared_st
            .execute(
                &pool,
                des.input(dynamic_value)
                    .expect("format of value is invalid"),
            )
            .await
            .unwrap();
    }
}

pub mod impl_into_mut_arguments_prelude {
    pub use super::IntoMutArguments;
    pub use sqlx::{
        database::HasArguments, Arguments, Database, Encode,
        Type,
    };
}

#[rustfmt::skip]
mod impl_consume_into_args_for_encode_types {
    use super::impl_into_mut_arguments_prelude::*;
    macro_rules! impls {
        ($([$ident:ident, $part:literal])*) => {
            #[allow(unused)]
            impl<'q, DB, $($ident,)*> IntoMutArguments<'q, DB> for ($($ident,)*)
            where
                DB: Database,
                $($ident: Encode<'q, DB> + Type<DB> + Send + 'q,)*
            {
                fn into_arguments(
                    self,
                    argument: &mut <DB as HasArguments<'q>>::Arguments,
                ) {
                    paste::paste! { $(
                        argument.add(self.$part);
                    )* }
                }
            }
        };
    }

    impls!();
    impls!([T0, 0]);
    impls!([T0, 0] [T1, 1]);
    impls!([T0, 0] [T1, 1] [T2, 2]);
    impls!([T0, 0] [T1, 1] [T2, 2] [T3, 3]);
    impls!([T0, 0] [T1, 1] [T2, 2] [T3, 3] [T4, 4]);
    impls!([T0, 0] [T1, 1] [T2, 2] [T3, 3] [T4, 4] [T5, 5]);
    impls!([T0, 0] [T1, 1] [T2, 2] [T3, 3] [T4, 4] [T5, 5] [T6, 6]);
    impls!([T0, 0] [T1, 1] [T2, 2] [T3, 3] [T4, 4] [T5, 5] [T6, 6] [T7, 7]);
    impls!([T0, 0] [T1, 1] [T2, 2] [T3, 3] [T4, 4] [T5, 5] [T6, 6] [T7, 7] [T8, 8]);
    impls!([T0, 0] [T1, 1] [T2, 2] [T3, 3] [T4, 4] [T5, 5] [T6, 6] [T7, 7] [T8, 8] [T9, 9]);
    impls!([T0, 0] [T1, 1] [T2, 2] [T3, 3] [T4, 4] [T5, 5] [T6, 6] [T7, 7] [T8, 8] [T9, 9] [T10, 10]);
    impls!([T0, 0] [T1, 1] [T2, 2] [T3, 3] [T4, 4] [T5, 5] [T6, 6] [T7, 7] [T8, 8] [T9, 9] [T10, 10] [T11, 11]);

}

mod impls {
    use super::*;
    impl SupportReturning for Sqlite {}
    impl SupportNamedBind for Sqlite {}

    impl SupportReturning for Postgres {}
    impl SupportNamedBind for Postgres {}
}

#[cfg(test)]
mod positional_buffer_concept {
    use crate::{
        sql_part::{ToSqlPart, WhereItemToSqlPart},
        Accept, Query, WhereItem,
    };
    use sqlx::MySql;
    use std::{marker::PhantomData, mem::take};

    pub trait DebugAny
    where
        Self: std::fmt::Debug,
    {
        fn ty(&self) -> &'static str;
        fn debug(&self) -> String {
            format!("{}: {:?}", self.ty(), self)
        }
    }

    #[derive(Debug, Default)]
    pub struct MockMySql;
    impl<T> ToSqlPart<MockMySql, MySql> for WhereItemToSqlPart<T>
    where
        T: WhereItem<MySql, MockMySql> + 'static,
    {
        fn to_sql_part(
            self,
            ctx: &mut <MockMySql as Query<MySql>>::Context1,
        ) -> <MockMySql as Query<MySql>>::SqlPart {
            let ctx2 = unsafe { &mut *(ctx as *mut _) };
            let item = self.0.where_item(ctx2);
            Box::new(move |ctx2| item(ctx2))
        }
    }
    impl Query<MySql> for MockMySql {
        type SqlPart =
            Box<dyn FnOnce(&mut Self::Context2) -> String>;

        type Context1 =
            Vec<Option<Box<dyn FnOnce() -> Box<dyn DebugAny>>>>;

        type Context2 = (
            Vec<Option<Box<dyn FnOnce() -> Box<dyn DebugAny>>>>,
            Vec<Box<dyn DebugAny>>,
        );

        fn build_sql_part_back(
            ctx: &mut Self::Context2,
            from: Self::SqlPart,
        ) -> String {
            from(ctx)
        }

        type Output = ();
        fn build_query(
            _: Self::Context1,
            _: impl FnOnce(&mut Self::Context2) -> String,
        ) -> (String, Self::Output) {
            panic!("just an example")
        }
    }

    macro_rules! debug_any {
    ($($ident:ident), *) => {
        $(impl DebugAny for $ident
        where $ident: std::fmt::Debug
        {
            fn ty(&self) -> &'static str {
                stringify!($ident)
            }
        })*
    };
}

    debug_any!(String, i8, i16, i32, u8, u16, u32, u64, bool);

    impl<A, T> Accept<A, MySql> for MockMySql
    where
        A: FnOnce() -> T + 'static,
        T: DebugAny + 'static,
    {
        fn accept(
            this: A,
            ctx1: &mut Self::Context1,
        ) -> impl FnOnce(&mut Self::Context2) -> String + 'static
        {
            ctx1.push(Some(Box::new(|| Box::new(this()))));
            let len = ctx1.len();

            move |ctx2| {
                let found = take(
                    ctx2.0.get_mut(len - 1).expect("overflow"),
                )
                .unwrap();
                let found = found();
                ctx2.1.push(found);
                "?".to_string()
            }
        }
    }

    struct Condition<const B: bool, A1, A2>(A1, A2);

    impl<const B: bool, A1, A2> WhereItem<MySql, MockMySql>
        for Condition<B, A1, A2>
    where
        MockMySql: Accept<A1, MySql>,
        MockMySql: Accept<A2, MySql>,
    {
        fn where_item(
            self,
            ctx: &mut <MockMySql as Query<MySql>>::Context1,
        ) -> impl FnOnce(
            &mut <MockMySql as Query<MySql>>::Context2,
        ) -> String {
            let ctx1 = unsafe { &mut *(ctx as *mut _) };
            let s1 = <MockMySql as Accept<A1, MySql>>::accept(
                self.0, ctx1,
            );
            let ctx2 = unsafe { &mut *(ctx as *mut _) };
            let s2 = <MockMySql as Accept<A2, MySql>>::accept(
                self.1, ctx2,
            );

            |ctx2| {
                if B {
                    format!("{} AND {}", s1(ctx2), s2(ctx2))
                } else {
                    format!("{} AND {}", s2(ctx2), s1(ctx2))
                }
            }
        }
    }

    #[test]
    fn test() {
        let mut ctx = Default::default();
        let ctx_mut = unsafe { &mut *((&mut ctx) as *mut _) };
        let part1 = Condition::<true, _, _>(
            || 3,
            || "hello".to_string(),
        )
        .where_item(ctx_mut);
        let ctx_mut2 = unsafe { &mut *((&mut ctx) as *mut _) };
        let part2 = Condition::<false, _, _>(
            || 3,
            || "hello".to_string(),
        )
        .where_item(ctx_mut2);

        let mut ctx2: <MockMySql as Query<MySql>>::Context2 =
            (ctx, Default::default());

        let res_str1 = part1(&mut ctx2);
        let _ = part2(&mut ctx2);

        let res_val = ctx2
            .1
            .into_iter()
            .map(|e| e.debug())
            .collect::<Vec<String>>();

        assert_eq!(res_str1, "? AND ?");

        assert_eq!(
            res_val,
            vec![
                "i32: 3".to_string(),
                "String: \"hello\"".to_string(),
                "String: \"hello\"".to_string(),
                "i32: 3".to_string(),
            ]
        );
    }

    struct WhereClause<S, Q: Query<S>> {
        columns: Vec<Q::SqlPart>,
        args: Q::Context1,
        _pd: PhantomData<S>,
    }

    impl<S, Q: Query<S>> Default for WhereClause<S, Q> {
        fn default() -> Self {
            Self {
                columns: Default::default(),
                args: Default::default(),
                _pd: PhantomData,
            }
        }
    }

    impl<S> WhereClause<S, MockMySql>
    where
        S: 'static,
        MockMySql: Query<
            S,
            Context2: 'static,
            Context1: 'static,
            SqlPart = Box<
                dyn FnOnce(
                    &mut <MockMySql as Query<S>>::Context2,
                ) -> String,
            >,
        >,
    {
        fn item<T>(&mut self, item: T)
        where
            T: WhereItem<S, MockMySql> + 'static,
            WhereItemToSqlPart<T>: ToSqlPart<MockMySql, S>,
        {
            let part = WhereItemToSqlPart(item)
                .to_sql_part(&mut self.args);
            self.columns.push(part);
        }
    }

    struct WhereEx<T>(T);

    impl<S, Q, T> WhereItem<S, Q> for WhereEx<T>
    where
        Q: Query<S>,
        Q: Accept<T, S>,
    {
        fn where_item(
            self,
            ctx: &mut Q::Context1,
        ) -> impl FnOnce(&mut Q::Context2) -> String {
            let ctx1 = unsafe { &mut *(ctx as *mut _) };
            let s1 = <Q as Accept<T, S>>::accept(self.0, ctx1);

            move |ctx2| s1(ctx2)
        }
    }

    #[test]
    fn test_where_clause() {
        let mut where_clause = WhereClause::default();

        where_clause.item(WhereEx(|| 3));
        where_clause.item(WhereEx(|| "hello".to_string()));

        let mut ctx2: <MockMySql as Query<MySql>>::Context2 =
            (where_clause.args, Default::default());

        let res = where_clause.columns.into_iter().map(|e| {
            MockMySql::build_sql_part_back(&mut ctx2, e)
        });

        let res = res.collect::<Vec<String>>();

        assert_eq!(res, vec!["?", "?"]);
    }
}