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
#![cfg_attr(docsrs, doc = include_str!("../docs/index.md"))]
#![cfg_attr(docsrs, feature(doc_cfg))]

pub use include_sql::include_sql;

/**
Generates Rust code to use included SQL.

This macro defines a trait with methods to access data and implements it for `postgres::Client` and `postgres::Transaction`.

This macro recognizes and generates 3 variants of database access methods using the following selectors:
* `?` - methods that process rows retrieved by `SELECT`,
* `!` - methods that execute all other non-`SELECT` methods, and
* `->` - methods that execute `RETURNING` statements and provide access to returned data.

For `SELECT` statements (`?`) like:

```sql
-- name: get_loaned_books?
-- param: user_id: &str
SELECT book_title FROM library WHERE loaned_to = :user_id;
```

The method with the following signature is generated:

```rust , ignore
fn get_loaned_books<F>(
    &self,
    user_id: &str,
    row_callback: F
) -> Result<(),postgres::Error>
where F: Fn(postgres::Row) -> Result<(),postgres::Error>;
```

For non-select statements (`!`) - INSERT, UPDATE, DELETE, etc. - like:

```sql
-- name: loan_books!
-- param: user_id: &str
-- param: book_ids: i32
UPDATE library
   SET loaned_to = :user_id
     , loaned_on = current_timestamp
 WHERE book_id IN (:book_ids);
```

The method with the following signature is generated:

```rust , ignore
fn loan_books(
    &self,
    user_id: &str,
    book_ids: &[i32]
) -> Result<u64,postgres::Error>;
```

For DELETE, INSERT, and UPDATE statements that return data via `RETURNING` clause (`->`) like:

```sql
-- name: add_new_book->
-- param: isbn: &str
-- param: book_title: &str
INSERT INTO library (isbn, book_title)
VALUES (:isbn, :book_title)
RETURNING book_id;
```

The method with the following signature is generated:

```rust , ignore
fn add_new_book(
    &self,
    isbn: &str,
    book_title: &str
) -> Result<postgres::Row,postgres::Error>;
```

### Tokio-Postgres

**Note** that when **include-postgres-sql** is used with `tokio` feature selected the generated methods will be `async`.
*/
#[cfg(not(feature = "tokio"))]
#[macro_export]
macro_rules! impl_sql {
    ( $sql_name:ident = $( { $kind:tt $name:ident ($($variant:tt $param:ident $ptype:tt)*) $doc:literal $s:tt $( $text:tt )+ } ),+ ) => {
        trait $sql_name {
            $( $crate::decl_method!{ $kind $name $doc () () $($param $variant $ptype)* } )+
        }
        impl $sql_name for postgres::Client {
            $( $crate::impl_method!{ $kind $name () () ($($param $variant $ptype)*) => ($($variant $param)*) $($text)+ } )+
        }
        impl $sql_name for postgres::Transaction<'_> {
            $( $crate::impl_method!{ $kind $name () () ($($param $variant $ptype)*) => ($($variant $param)*) $($text)+ } )+
        }
    };
}


#[cfg(feature = "tokio")]
#[macro_export]
macro_rules! impl_sql {
    ( $sql_name:ident = $( { $kind:tt $name:ident ($($variant:tt $param:ident $ptype:tt)*) $doc:literal $s:tt $( $text:tt )+ } ),+ ) => {
        trait $sql_name {
            $( $crate::decl_method!{ $kind $name $doc () () () $($param $variant $ptype)* } )+
        }
        impl $sql_name for tokio_postgres::Client {
            $( $crate::impl_method!{ $kind $name () () () ($($param $variant $ptype)*) => ($($variant $param)*) $($text)+ } )+
        }
        impl $sql_name for tokio_postgres::Transaction<'_> {
            $( $crate::impl_method!{ $kind $name () () () ($($param $variant $ptype)*) => ($($variant $param)*) $($text)+ } )+
        }
    };
}


#[cfg(not(feature = "tokio"))]
#[macro_export]
#[doc(hidden)]
macro_rules! decl_method {
    ( ? $name:ident $doc:literal ($($gen_type:ident)*) ($($fn_params:tt)*) ) => {
        #[doc=$doc]
        fn $name<$($gen_type : postgres::types::ToSql ,)* F>(&mut self $($fn_params)* , row_cb: F) -> std::result::Result<(),postgres::Error>
        where F: Fn(postgres::Row) -> std::result::Result<(),postgres::Error>;
    };
    ( ! $name:ident $doc:literal ($($gen_type:ident)*) ($($fn_params:tt)*) ) => {
        #[doc=$doc]
        fn $name<$($gen_type : postgres::types::ToSql),*>(&mut self $($fn_params)*) -> std::result::Result<u64,postgres::Error>;
    };
    ( -> $name:ident $doc:literal ($($gen_type:ident)*) ($($fn_params:tt)*) ) => {
        #[doc=$doc]
        fn $name<$($gen_type : postgres::types::ToSql),*>(&mut self $($fn_params)*) -> std::result::Result<postgres::Row,postgres::Error>;
    };
    ( $kind:tt $name:ident $doc:literal ($($gen_type:ident)*) ($($fn_params:tt)*) $param:ident : _ $($tail:tt)* ) => {
        $crate::decl_method!{
            $kind
            $name
            $doc
            ($($gen_type)*)
            ($($fn_params)* , $param : impl postgres::types::ToSql + Sync)
            $($tail)*
        }
    };
    ( $kind:tt $name:ident $doc:literal ($($gen_type:ident)*) ($($fn_params:tt)*) $param:ident : ($ptype:ty) $($tail:tt)* ) => {
        $crate::decl_method!{
            $kind
            $name
            $doc
            ($($gen_type)*)
            ($($fn_params)* , $param : $ptype)
            $($tail)*
        }
    };
    ( $kind:tt $name:ident $doc:literal ($($gen_type:ident)*) ($($fn_params:tt)*) $param:ident # [$gtype:ident] $($tail:tt)* ) => {
        $crate::decl_method!{
            $kind
            $name
            $doc
            ($($gen_type)* $gtype)
            ($($fn_params)* , $param : & [ $gtype ] )
            $($tail)*
        }
    };
    ( $kind:tt $name:ident $doc:literal ($($gen_type:ident)*) ($($fn_params:tt)*) $param:ident # ($ptype:ty) $($tail:tt)* ) => {
        $crate::decl_method!{
            $kind
            $name
            $doc
            ($($gen_type)*)
            ($($fn_params)* , $param : & [ $ptype ] )
            $($tail)*
        }
    };
}

#[cfg(feature = "tokio")]
#[macro_export]
#[doc(hidden)]
macro_rules! decl_method {
    ( ? $name:ident $doc:literal ($($lt:lifetime)*) ($($gen_type:tt)*) ($($fn_params:tt)*) ) => {
        #[doc=$doc]
        fn $name<'tr, 'st $(, $lt)* $($gen_type)*, F>(&'st self $($fn_params)* , row_cb: F)
        -> core::pin::Pin<Box<dyn core::future::Future<Output = core::result::Result<(),tokio_postgres::Error>> + Send + 'tr>>
        where
            F: Fn(tokio_postgres::Row) -> std::result::Result<(),tokio_postgres::Error>,
            F: Send, F: 'tr, Self: 'tr, 'st: 'tr $(, $lt : 'tr)*;
    };
    ( ! $name:ident $doc:literal ($($lt:lifetime)*) ($($gen_type:tt)*) ($($fn_params:tt)*) ) => {
        #[doc=$doc]
        fn $name<'tr, 'st $(, $lt)* $($gen_type)*>(&'st self $($fn_params)*)
        -> core::pin::Pin<Box<dyn core::future::Future<Output = core::result::Result<u64,tokio_postgres::Error>> + Send + 'tr>>
        where Self: 'tr, 'st: 'tr $(, $lt : 'tr)*;
    };
    ( -> $name:ident $doc:literal ($($lt:lifetime)*) ($($gen_type:tt)*) ($($fn_params:tt)*) ) => {
        #[doc=$doc]
        fn $name<'tr, 'st $(, $lt)* $($gen_type)*>(&'st self $($fn_params)*)
        -> core::pin::Pin<Box<dyn core::future::Future<Output = core::result::Result<tokio_postgres::Row,tokio_postgres::Error>> + Send + 'tr>>
        where Self: 'tr, 'st: 'tr $(, $lt : 'tr)*;
    };
    ( $kind:tt $name:ident $doc:literal ($($lt:lifetime)*) ($($gen_type:tt)*) ($($fn_params:tt)*) $param:ident : _ $($tail:tt)* ) => {
        $crate::decl_method!{
            $kind
            $name
            $doc
            ($($lt)*)
            ($($gen_type)*)
            ($($fn_params)* , $param : impl tokio_postgres::types::ToSql + Sync + Send + 'tr)
            $($tail)*
        }
    };
    ( $kind:tt $name:ident $doc:literal ($($lt:lifetime)*) ($($gen_type:tt)*) ($($fn_params:tt)*) $param:ident : ($plt:lifetime & $ptype:ty) $($tail:tt)* ) => {
        $crate::decl_method!{
            $kind
            $name
            $doc
            ($($lt)* $plt)
            ($($gen_type)*)
            ($($fn_params)* , $param : & $plt $ptype)
            $($tail)*
        }
    };
    ( $kind:tt $name:ident $doc:literal ($($lt:lifetime)*) ($($gen_type:tt)*) ($($fn_params:tt)*) $param:ident : ($ptype:ty) $($tail:tt)* ) => {
        $crate::decl_method!{
            $kind
            $name
            $doc
            ($($lt)*)
            ($($gen_type)*)
            ($($fn_params)* , $param : $ptype)
            $($tail)*
        }
    };
    ( $kind:tt $name:ident $doc:literal ($($lt:lifetime)*) ($($gen_type:tt)*) ($($fn_params:tt)*) $param:ident # [$alt:lifetime $gtype:ident] $($tail:tt)* ) => {
        $crate::decl_method!{
            $kind
            $name
            $doc
            ($($lt)* $alt)
            ($($gen_type)* , $gtype : tokio_postgres::types::ToSql + Sync + Send + 'tr)
            ($($fn_params)* , $param : & $alt [ $gtype ] )
            $($tail)*
        }
    };
    ( $kind:tt $name:ident $doc:literal ($($lt:lifetime)*) ($($gen_type:tt)*) ($($fn_params:tt)*) $param:ident # ($alt:lifetime $plt:lifetime & $ptype:ty) $($tail:tt)* ) => {
        $crate::decl_method!{
            $kind
            $name
            $doc
            ($($lt)* $alt $plt)
            ($($gen_type)*)
            ($($fn_params)* , $param : & $alt [ & $plt $ptype ] )
            $($tail)*
        }
    };
    ( $kind:tt $name:ident $doc:literal ($($lt:lifetime)*) ($($gen_type:tt)*) ($($fn_params:tt)*) $param:ident # ($alt:lifetime $ptype:ty) $($tail:tt)* ) => {
        $crate::decl_method!{
            $kind
            $name
            $doc
            ($($lt)* $alt)
            ($($gen_type)*)
            ($($fn_params)* , $param : & $alt [ $ptype ] )
            $($tail)*
        }
    };
}

#[cfg(not(feature = "tokio"))]
#[macro_export]
#[doc(hidden)]
macro_rules! impl_method {
    ( ? $name:ident () () () => () $text:literal ) => {
        fn $name<F>(&mut self, row_cb: F) -> std::result::Result<(),postgres::Error>
        where F: Fn(postgres::Row) -> std::result::Result<(),postgres::Error>
        {
            use postgres::fallible_iterator::FallibleIterator;

            let mut rows = self.query_raw( $text, [] as [&dyn postgres::types::ToSql; 0] )?;
            while let Some(row) = rows.next()? {
                row_cb(row)?;
            }
            Ok(())
        }
    };
    ( ? $name:ident () ($($fn_params:tt)+) () => (: $head:ident $(: $tail:ident)*) $($text:tt)+) => {
        fn $name<F>(&mut self $($fn_params)+ , row_cb: F) -> std::result::Result<(),postgres::Error>
        where F: Fn(postgres::Row) -> std::result::Result<(),postgres::Error>
        {
            use postgres::fallible_iterator::FallibleIterator;

            let mut rows = self.query_raw(
                $crate::sql_literal!( $head $($tail)* => $($text)+ ) ,
                [& $head as &(dyn postgres::types::ToSql + Sync) $(, & $tail)* ]
            )?;
            while let Some(row) = rows.next()? {
                row_cb(row)?;
            }
            Ok(())
        }
    };
    ( ? $name:ident ($($gen_type:ident)*) ($($fn_params:tt)+) () => ($($pv:tt $param:ident)+) $($text:tt)+) => {
        fn $name<$($gen_type : postgres::types::ToSql ,)* F>(&mut self $($fn_params)+, row_cb: F) -> std::result::Result<(),postgres::Error>
        where F: Fn(postgres::Row) -> std::result::Result<(),postgres::Error>
        {
            use postgres::fallible_iterator::FallibleIterator;

            let mut stmt = String::with_capacity($crate::sql_len!($($text)+));
            let mut args = Vec::<&dyn postgres::types::ToSql>::with_capacity($crate::num_args!($($pv $param)+));
            let mut i = 0;
            $crate::dynamic_sql!(stmt args i $($text)+);
            let mut rows = self.query_raw(&stmt, args)?;
            while let Some(row) = rows.next()? {
                row_cb(row)?;
            }
            Ok(())
        }
    };
    ( ! $name:ident () () () => () $text:literal ) => {
        fn $name(&mut self) -> std::result::Result<u64,postgres::Error> {
            self.execute( $text, &[] )
        }
    };
    ( ! $name:ident () ($($fn_params:tt)+) () => (: $head:ident $(: $tail:ident)*) $($text:tt)+) => {
        fn $name(&mut self $($fn_params)+ ) -> std::result::Result<u64,postgres::Error> {
            self.execute(
                $crate::sql_literal!( $head $($tail)* => $($text)+ ) ,
                &[& $head as &(dyn postgres::types::ToSql + Sync) $(, & $tail)* ]
            )
        }
    };
    ( ! $name:ident ($($gen_type:ident)*) ($($fn_params:tt)+) () => ($($pv:tt $param:ident)+) $($text:tt)+) => {
        fn $name<$($gen_type : postgres::types::ToSql),*>(&mut self $($fn_params)+ ) -> std::result::Result<u64,postgres::Error> {
            let mut stmt = String::with_capacity($crate::sql_len!($($text)+));
            let mut args = Vec::<&dyn postgres::types::ToSql>::with_capacity($crate::num_args!($($pv $param)+));
            let mut i = 0;
            $crate::dynamic_sql!(stmt args i $($text)+);
            self.execute(&stmt, &args)
        }
    };
    ( -> $name:ident () () () => () $text:literal ) => {
        fn $name(&mut self) -> std::result::Result<postgres::Row,postgres::Error> {
            self.query_one( $text, &[] )
        }
    };
    ( -> $name:ident () ($($fn_params:tt)+) () => (: $head:ident $(: $tail:ident)*) $($text:tt)+) => {
        fn $name(&mut self $($fn_params)+ ) -> std::result::Result<postgres::Row,postgres::Error> {
            self.query_one(
                $crate::sql_literal!( $head $($tail)* => $($text)+ ) ,
                &[& $head as &(dyn postgres::types::ToSql + Sync) $(, & $tail)* ]
            )
        }
    };
    ( -> $name:ident ($($gen_type:ident)*) ($($fn_params:tt)+) () => ($($pv:tt $param:ident)+) $($text:tt)+) => {
        fn $name<$($gen_type : postgres::types::ToSql),*>(&mut self $($fn_params)+ ) -> std::result::Result<postgres::Row,postgres::Error> {
            let mut stmt = String::with_capacity($crate::sql_len!($($text)+));
            let mut args = Vec::<&dyn postgres::types::ToSql>::with_capacity($crate::num_args!($($pv $param)+));
            let mut i = 0;
            $crate::dynamic_sql!(stmt args i $($text)+);
            self.query_one(&stmt, &args)
        }
    };
    ( $kind:tt $name:ident ($($gen_type:ident)*) ($($fn_params:tt)*) ($param:ident : _ $($tail:tt)*) => ($($pv:tt $param_name:ident)+) $($text:tt)+)  => {
        $crate::impl_method!{
            $kind
            $name
            ($($gen_type)*)
            ($($fn_params)* , $param : impl postgres::types::ToSql + Sync)
            ($($tail)*)
            =>
            ($($pv $param_name)+)
            $($text)+
        }
    };
    ( $kind:tt $name:ident ($($gen_type:ident)*) ($($fn_params:tt)*) ($param:ident : ($ptype:ty) $($tail:tt)*) => ($($pv:tt $param_name:ident)+) $($text:tt)+)  => {
        $crate::impl_method!{
            $kind
            $name
            ($($gen_type)*)
            ($($fn_params)* , $param : $ptype)
            ($($tail)*)
            =>
            ($($pv $param_name)+)
            $($text)+
        }
    };
    ( $kind:tt $name:ident ($($gen_type:ident)*) ($($fn_params:tt)*) ($param:ident # [$gtype:ident] $($tail:tt)*) => ($($pv:tt $param_name:ident)+) $($text:tt)+)  => {
        $crate::impl_method!{
            $kind
            $name
            ($($gen_type)* $gtype)
            ($($fn_params)* , $param : & [ $gtype ])
            ($($tail)*)
            =>
            ($($pv $param_name)+)
            $($text)+
        }
    };
    ( $kind:tt $name:ident ($($gen_type:ident)*) ($($fn_params:tt)*) ($param:ident # ($ptype:ty) $($tail:tt)*) => ($($pv:tt $param_name:ident)+) $($text:tt)+)  => {
        $crate::impl_method!{
            $kind
            $name
            ($($gen_type)*)
            ($($fn_params)* , $param : & [ $ptype ])
            ($($tail)*)
            =>
            ($($pv $param_name)+)
            $($text)+
        }
    };
}

#[cfg(feature = "tokio")]
#[macro_export]
#[doc(hidden)]
macro_rules! impl_method {
    ( ? $name:ident () () () () => () $text:literal ) => {
        fn $name<'tr, 'st, F>(&'st self, row_cb: F)
        -> core::pin::Pin<Box<dyn core::future::Future<Output = core::result::Result<(),tokio_postgres::Error>> + Send + 'tr>>
        where F: Fn(tokio_postgres::Row) -> std::result::Result<(),tokio_postgres::Error>, F: Send, F: 'tr, Self: 'tr, 'st: 'tr
        {
            use futures::stream::TryStreamExt;

            Box::pin(async move {
                let rows = self.query_raw( $text, [] as [&(dyn tokio_postgres::types::ToSql + Sync); 0] ).await?;
                let mut rows = Box::pin(rows);
                while let Some(row) = rows.try_next().await? {
                    row_cb(row)?;
                }
                Ok::<(),tokio_postgres::Error>(())
            })
        }
    };
    ( ? $name:ident ($($lt:lifetime)*) () ($($fn_params:tt)+) () => (: $head:ident $(: $tail:ident)*) $($text:tt)+) => {
        fn $name<'tr, 'st $(, $lt)*, F>(&'st self $($fn_params)+ , row_cb: F)
        -> core::pin::Pin<Box<dyn core::future::Future<Output = core::result::Result<(),tokio_postgres::Error>> + Send + 'tr>>
        where F: Fn(tokio_postgres::Row) -> std::result::Result<(),tokio_postgres::Error>, F: Send, F: 'tr, Self: 'tr, 'st: 'tr $(, $lt : 'tr)*
        {
            use futures::stream::TryStreamExt;

            Box::pin(async move {
                let rows = self.query_raw(
                    $crate::sql_literal!( $head $($tail)* => $($text)+ ) ,
                    [& $head as &(dyn tokio_postgres::types::ToSql + Sync) $(, & $tail)* ]
                ).await?;
                let mut rows = Box::pin(rows);
                while let Some(row) = rows.try_next().await? {
                    row_cb(row)?;
                }
                Ok::<(),tokio_postgres::Error>(())
            })
        }
    };
    ( ? $name:ident ($($lt:lifetime)*) ($($gen_type:tt)*) ($($fn_params:tt)+) () => ($($pv:tt $param:ident)+) $($text:tt)+) => {
        fn $name<'tr, 'st $(, $lt)* $($gen_type)*, F>(&'st self $($fn_params)+, row_cb: F)
        -> core::pin::Pin<Box<dyn core::future::Future<Output = core::result::Result<(),tokio_postgres::Error>> + Send + 'tr>>
        where F: Fn(tokio_postgres::Row) -> std::result::Result<(),tokio_postgres::Error>, F: Send, F: 'tr, Self: 'tr, 'st: 'tr $(, $lt : 'tr)*
        {
            use futures::stream::TryStreamExt;

            Box::pin(async move {
                let mut stmt = String::with_capacity($crate::sql_len!($($text)+));
                let mut args = Vec::<&(dyn tokio_postgres::types::ToSql + Sync)>::with_capacity($crate::num_args!($($pv $param)+));
                let mut i = 0;
                $crate::dynamic_sql!(stmt args i $($text)+);
                let rows = self.query_raw(&stmt, args).await?;
                let mut rows = Box::pin(rows);
                while let Some(row) = rows.try_next().await? {
                    row_cb(row)?;
                }
                Ok::<(),tokio_postgres::Error>(())
            })
        }
    };
    ( ! $name:ident () () () () => () $text:literal ) => {
        fn $name<'tr, 'st>(&'st self)
        -> core::pin::Pin<Box<dyn core::future::Future<Output = core::result::Result<u64,tokio_postgres::Error>> + Send + 'tr>>
        where Self: 'tr, 'st: 'tr
        {
            Box::pin(async move {
                self.execute( $text, &[] ).await
            })
        }
    };
    ( ! $name:ident ($($lt:lifetime)*) () ($($fn_params:tt)+) () => (: $head:ident $(: $tail:ident)*) $($text:tt)+) => {
        fn $name<'tr, 'st $(, $lt)*>(&'st self $($fn_params)+ )
        -> core::pin::Pin<Box<dyn core::future::Future<Output = core::result::Result<u64,tokio_postgres::Error>> + Send + 'tr>>
        where Self: 'tr, 'st: 'tr $(, $lt : 'tr)*
        {
            Box::pin(async move {
                self.execute(
                    $crate::sql_literal!( $head $($tail)* => $($text)+ ) ,
                    &[& $head as &(dyn tokio_postgres::types::ToSql + Sync) $(, & $tail)* ]
                ).await
            })
        }
    };
    ( ! $name:ident ($($lt:lifetime)*) ($($gen_type:tt)*) ($($fn_params:tt)+) () => ($($pv:tt $param:ident)+) $($text:tt)+) => {
        fn $name<'tr, 'st $(, $lt)* $($gen_type)*>(&'st self $($fn_params)+ )
        -> core::pin::Pin<Box<dyn core::future::Future<Output = core::result::Result<u64,tokio_postgres::Error>> + Send + 'tr>>
        where Self: 'tr, 'st: 'tr $(, $lt : 'tr)*
        {
            Box::pin(async move {
                let mut stmt = String::with_capacity($crate::sql_len!($($text)+));
                let mut args = Vec::<&(dyn tokio_postgres::types::ToSql + Sync)>::with_capacity($crate::num_args!($($pv $param)+));
                let mut i = 0;
                $crate::dynamic_sql!(stmt args i $($text)+);
                self.execute_raw(&stmt, args).await
            })
        }
    };
    ( -> $name:ident () () () () => () $text:literal ) => {
        fn $name<'tr, 'st>(&'st self)
        -> core::pin::Pin<Box<dyn core::future::Future<Output = core::result::Result<tokio_postgres::Row,tokio_postgres::Error>> + Send + 'tr>>
        where Self: 'tr, 'st: 'tr
        {
            Box::pin(async move {
                self.query_one( $text, &[] ).await
            })
        }
    };
    ( -> $name:ident ($($lt:lifetime)*) () ($($fn_params:tt)+) () => (: $head:ident $(: $tail:ident)*) $($text:tt)+) => {
        fn $name<'tr, 'st $(, $lt)*>(&'st self $($fn_params)+ )
        -> core::pin::Pin<Box<dyn core::future::Future<Output = core::result::Result<tokio_postgres::Row,tokio_postgres::Error>> + Send + 'tr>>
        where Self: 'tr, 'st: 'tr $(, $lt : 'tr)*
        {
            Box::pin(async move {
                self.query_one(
                    $crate::sql_literal!( $head $($tail)* => $($text)+ ) ,
                    &[& $head as &(dyn tokio_postgres::types::ToSql + Sync) $(, & $tail)* ]
                ).await
            })
        }
    };
    ( -> $name:ident ($($lt:lifetime)*) ($($gen_type:tt)*) ($($fn_params:tt)+) () => ($($pv:tt $param:ident)+) $($text:tt)+) => {
        fn $name<'tr, 'st $(, $lt)* $($gen_type)*>(&'st self $($fn_params)+ )
        -> core::pin::Pin<Box<dyn core::future::Future<Output = core::result::Result<tokio_postgres::Row,tokio_postgres::Error>> + Send + 'tr>>
        where Self: 'tr, 'st: 'tr $(, $lt : 'tr)*
        {
            Box::pin(async move {
                let mut stmt = String::with_capacity($crate::sql_len!($($text)+));
                let mut args = Vec::<&(dyn tokio_postgres::types::ToSql + Sync)>::with_capacity($crate::num_args!($($pv $param)+));
                let mut i = 0;
                $crate::dynamic_sql!(stmt args i $($text)+);
                self.query_one(&stmt, &args).await
            })
        }
    };
    ( $kind:tt $name:ident ($($lt:lifetime)*) ($($gen_type:tt)*) ($($fn_params:tt)*) ($param:ident : _ $($tail:tt)*) => ($($pv:tt $param_name:ident)+) $($text:tt)+)  => {
        $crate::impl_method!{
            $kind
            $name
            ($($lt)*)
            ($($gen_type)*)
            ($($fn_params)* , $param : impl tokio_postgres::types::ToSql + Sync + Send + 'tr)
            ($($tail)*)
            =>
            ($($pv $param_name)+)
            $($text)+
        }
    };
    ( $kind:tt $name:ident ($($lt:lifetime)*) ($($gen_type:tt)*) ($($fn_params:tt)*) ($param:ident : ($plt:lifetime & $ptype:ty) $($tail:tt)*) => ($($pv:tt $param_name:ident)+) $($text:tt)+)  => {
        $crate::impl_method!{
            $kind
            $name
            ($($lt)* $plt)
            ($($gen_type)*)
            ($($fn_params)* , $param : & $plt $ptype)
            ($($tail)*)
            =>
            ($($pv $param_name)+)
            $($text)+
        }
    };
    ( $kind:tt $name:ident ($($lt:lifetime)*) ($($gen_type:tt)*) ($($fn_params:tt)*) ($param:ident : ($ptype:ty) $($tail:tt)*) => ($($pv:tt $param_name:ident)+) $($text:tt)+)  => {
        $crate::impl_method!{
            $kind
            $name
            ($($lt)*)
            ($($gen_type)*)
            ($($fn_params)* , $param : $ptype)
            ($($tail)*)
            =>
            ($($pv $param_name)+)
            $($text)+
        }
    };
    ( $kind:tt $name:ident ($($lt:lifetime)*) ($($gen_type:tt)*) ($($fn_params:tt)*) ($param:ident # [$alt:lifetime $gtype:ident] $($tail:tt)*) => ($($pv:tt $param_name:ident)+) $($text:tt)+)  => {
        $crate::impl_method!{
            $kind
            $name
            ($($lt)* $alt)
            ($($gen_type)*  , $gtype : tokio_postgres::types::ToSql + Sync + Send + 'tr)
            ($($fn_params)* , $param : & $alt [ $gtype ])
            ($($tail)*)
            =>
            ($($pv $param_name)+)
            $($text)+
        }
    };
    ( $kind:tt $name:ident ($($lt:lifetime)*) ($($gen_type:tt)*) ($($fn_params:tt)*) ($param:ident # ($alt:lifetime $plt:lifetime & $ptype:ty) $($tail:tt)*) => ($($pv:tt $param_name:ident)+) $($text:tt)+)  => {
        $crate::impl_method!{
            $kind
            $name
            ($($lt)* $alt $plt)
            ($($gen_type)*)
            ($($fn_params)* , $param : & $alt [ & $plt $ptype ])
            ($($tail)*)
            =>
            ($($pv $param_name)+)
            $($text)+
        }
    };
    ( $kind:tt $name:ident ($($lt:lifetime)*) ($($gen_type:tt)*) ($($fn_params:tt)*) ($param:ident # ($alt:lifetime $ptype:ty) $($tail:tt)*) => ($($pv:tt $param_name:ident)+) $($text:tt)+)  => {
        $crate::impl_method!{
            $kind
            $name
            ($($lt)* $alt)
            ($($gen_type)*)
            ($($fn_params)* , $param : & $alt [ $ptype ])
            ($($tail)*)
            =>
            ($($pv $param_name)+)
            $($text)+
        }
    };
}

#[macro_export]
#[doc(hidden)]
macro_rules! sql_literal {
    ($($name:ident)+ => $text:literal) => {
        $text
    };
    ($($name:ident)+ => $text:literal : $param:ident) => {
        std::concat!( $text, '$', include_sql::index_of!($param in [ $( $name ),+ ] + 1) )
    };
    ($($name:ident)+ => $text:literal : $param:ident $($tail:tt)+) => {
        std::concat!(
            $text, '$', include_sql::index_of!($param in [ $( $name ),+ ] + 1),
            $crate::sql_literal!($($name)+ => $($tail)+)
        )
    };
}

#[macro_export]
#[doc(hidden)]
macro_rules! num_args {
    () => { 0 };
    (: $head:ident $($tail:tt)*) => { 1 + $crate::num_args!($($tail)*) };
    (# $head:ident $($tail:tt)*) => { $head.len() + $crate::num_args!($($tail)*) };
}

#[macro_export]
#[doc(hidden)]
macro_rules! sql_len {
    () => { 0 };
    ($text:literal $($tail:tt)*) => { $text.len() + $crate::sql_len!($($tail)*) };
    (: $head:ident $($tail:tt)*) => { 3 + $crate::sql_len!($($tail)*) };
    (# $head:ident $($tail:tt)*) => { $head.len() * 5 + $crate::sql_len!($($tail)*) };
}

#[macro_export]
#[doc(hidden)]
macro_rules! dynamic_sql {
    ($stmt:ident $args:ident $i:ident) => {};
    ($stmt:ident $args:ident $i:ident $text:literal $($tail:tt)*) => {
        $stmt.push_str($text);
        $crate::dynamic_sql!($stmt $args $i $($tail)*);
    };
    ($stmt:ident $args:ident $i:ident : $param:ident $($tail:tt)*) => {
        $i += 1;
        $stmt.push_str(&format!("${}", $i));
        $args.push(&$param);
        $crate::dynamic_sql!($stmt $args $i $($tail)*);
    };
    ($stmt:ident $args:ident $i:ident # $param:ident $($tail:tt)*) => {
        let mut iter = $param.into_iter();
        if let Some(arg) = iter.next() {
            $i += 1;
            $stmt.push_str(&format!("${}", $i));
            $args.push(arg);
            while let Some(arg) = iter.next() {
                $i += 1;
                $stmt.push_str(&format!(", ${}", $i));
                $args.push(arg);
            }
        } else {
            $stmt.push_str("NULL");
        }
        $crate::dynamic_sql!($stmt $args $i $($tail)*);
    };
}