rok-sqlc 0.1.0

Generate SQLx Rust database modules from SQL schema files
Documentation
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
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
//! Typed filtering, allowlisted sorting, and pagination.
//!
//! `db/search.rs` holds the types every resource shares (`Page`, `Sort`, and
//! the per-type condition enums); each resource gets a `filter.rs` with its
//! `{Struct}Filter` and `{Struct}SortKey`, and `read.rs` gets `search`.
//! Conditions are rendered from schema column names only; every value is
//! bound.
//!
//! A resource with relationships (ADR 0014) also gets a filter field per
//! relationship it can follow, holding the related resource's filter. It is
//! rendered as an `EXISTS` subquery over the related table under an alias
//! (`_r1`, `_r2`, … by nesting depth), correlated with the enclosing row, so
//! rows never repeat. The related table's soft-delete condition always
//! applies inside it, and a related tenant table is correlated on the tenant
//! column with the enclosing row, which is itself scoped to the tenant.

use crate::schema::{Column, FilterKind, Relationship, Table, Through, mentions_generated_type};

use super::sql::{ident, live_condition, str_literal, table_ref};
use super::{HEADER, doc_name, push_doc, relationships};

/// A relationship `{Struct}Filter` can follow, resolved against its target.
pub struct FilterRelationship<'a> {
    pub relationship: &'a Relationship,
    pub target: &'a Table,
    /// A many-to-many relationship's join table.
    pub join: Option<&'a Table>,
}

/// The relationships `table`'s filter gets a field for: those whose target
/// has a filter that can be nested (it has relationships of its own, as
/// every target has the inverse). A tenant table is left out when `table`
/// has no tenant column: there is no tenant to scope its rows by.
pub fn filter_relationships<'a>(
    table: &'a Table,
    tables: &'a [Table],
) -> Vec<FilterRelationship<'a>> {
    if !SearchApi::for_table(table).any() {
        return Vec::new();
    }
    table
        .relationships
        .iter()
        .filter_map(|relationship| {
            let resolved = relationships::resolve(table, relationship, tables);
            let target = resolved.target;
            let join = resolved.join.map(|join| join.table);
            let nestable = SearchApi::for_table(target).any() && !target.relationships.is_empty();
            // A many-to-many also needs the tenant to scope its join rows.
            let scoped = std::iter::once(target)
                .chain(join)
                .all(|related| related.tenant().is_none() || table.tenant().is_some());
            (nestable && scoped).then_some(FilterRelationship {
                relationship,
                target,
                join,
            })
        })
        .collect()
}

/// Fails when a relationship filter field of `table` would be named like
/// one of its column fields or its `include_deleted` switch.
pub fn check_filter_fields(table: &Table, tables: &[Table]) -> Result<(), String> {
    let relationships = filter_relationships(table, tables);
    if relationships.is_empty() {
        return Ok(());
    }
    let mut fields: Vec<(String, String)> = Vec::new();
    if live_condition(table, None).is_some() {
        let name = super::conventions::INCLUDE_DELETED;
        fields.push((name.to_owned(), format!("`{name}` switch")));
    }
    for column in &table.columns {
        if column.filter_kind().is_some() {
            fields.push((
                column.rust_name.clone(),
                format!(
                    "`{}` field for column `{}`",
                    column.rust_name, column.sql_name
                ),
            ));
        }
        if column.nullable {
            let name = column.null_check_field();
            let described = format!("`{name}` field for column `{}`", column.sql_name);
            fields.push((name, described));
        }
    }
    for FilterRelationship { relationship, .. } in relationships {
        if let Some((_, field)) = fields.iter().find(|(name, _)| *name == relationship.name) {
            return Err(format!(
                "the `{}` relationship of `{}` collides with the search filter's {field}",
                relationship.name,
                table.display_name()
            ));
        }
    }
    Ok(())
}

/// Which search functions a resource gets.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SearchApi {
    /// `search`: needs at least one sort key, or `Sort<XSortKey>` could
    /// never be constructed.
    pub search: bool,
    /// `search_after`: needs a key (other than the tenant column) to page by.
    pub search_after: bool,
}

impl SearchApi {
    pub fn for_table(table: &Table) -> Self {
        Self {
            search: !sortable(table).is_empty(),
            search_after: !table.key_columns().is_empty(),
        }
    }

    /// Whether the resource gets a `filter.rs` at all.
    pub fn any(self) -> bool {
        self.search || self.search_after
    }
}

impl FilterKind {
    /// The condition enum in `db/search.rs`.
    fn type_name(self) -> &'static str {
        match self {
            FilterKind::Ordered => "Ordered",
            FilterKind::Text => "Text",
            FilterKind::Equality => "Equality",
        }
    }

    fn condition_type(self, column: &Column) -> String {
        match self {
            FilterKind::Text => self.type_name().to_owned(),
            _ => format!("{}<{}>", self.type_name(), column.value_type()),
        }
    }
}

/// Appends `callee(args);` at `indent`, one argument per line when the
/// arguments exceed rustfmt's `fn_call_width` (60) or the line its
/// `max_width` (100).
fn push_call(output: &mut String, indent: usize, callee: &str, args: &[String]) {
    push_call_ending(output, indent, callee, args, ";");
}

/// `push_call`, ending the call with `end` (`;`, `?;`, or nothing).
fn push_call_ending(output: &mut String, indent: usize, callee: &str, args: &[String], end: &str) {
    let pad = " ".repeat(indent);
    let joined = args.join(", ");
    let one_line = format!("{pad}{callee}({joined}){end}");
    if joined.len() <= 60 && one_line.len() <= super::MAX_WIDTH {
        output.push_str(&one_line);
        output.push('\n');
        return;
    }
    output.push_str(&format!("{pad}{callee}(\n"));
    for arg in args {
        output.push_str(&format!("{pad}    {arg},\n"));
    }
    output.push_str(&format!("{pad}){end}\n"));
}

/// `{ items }` for a struct, enum, or match body: `{}` when empty, as rustfmt
/// writes it.
fn push_block(output: &mut String, head: &str, items: &[String], indent: usize) {
    let pad = " ".repeat(indent);
    if items.is_empty() {
        output.push_str(&format!("{pad}{head} {{}}\n"));
        return;
    }
    output.push_str(&format!("{pad}{head} {{\n"));
    for item in items {
        output.push_str(&format!("{pad}    {item}\n"));
    }
    output.push_str(&format!("{pad}}}\n"));
}

/// Columns `search` may order by: every filterable column.
pub fn sortable(table: &Table) -> Vec<&Column> {
    table
        .columns
        .iter()
        .filter(|column| column.filter_kind().is_some())
        .collect()
}

/// The `query` parameter every condition-pushing function takes.
const QUERY_PARAM: &str = "query: &mut sqlx::QueryBuilder<sqlx::Postgres>";

/// Renders the resource's `filter.rs`, or `None` when the resource has no
/// search function to use it. `tables` holds every table generated with it.
pub fn render_filter(table: &Table, tables: &[Table]) -> Option<String> {
    let api = SearchApi::for_table(table);
    if !api.any() {
        return None;
    }
    let filter = format!("{}Filter", table.struct_name);
    let sort_key = format!("{}SortKey", table.struct_name);
    let filterable: Vec<(&Column, FilterKind)> = table
        .columns
        .iter()
        .filter_map(|column| column.filter_kind().map(|kind| (column, kind)))
        .collect();
    let relationships = filter_relationships(table, tables);
    // A table with relationships may be the target of another's filter, so
    // its conditions get their own function, `push_conditions`.
    let split = !table.relationships.is_empty();
    let fallible = !relationships.is_empty();

    let mut output = format!("{HEADER}\n");
    let has_nullable = table.columns.iter().any(|column| column.nullable);
    let mut imports: Vec<&str> = filterable
        .iter()
        .map(|(_, kind)| kind.type_name())
        .collect();
    if has_nullable {
        imports.push("push_null_check");
    }
    if fallible {
        imports.extend(["correlate", "open_exists"]);
    }
    imports.sort_unstable();
    imports.dedup();
    let mut paths = match imports.as_slice() {
        [] => Vec::new(),
        [single] => vec![format!("super::super::search::{single}")],
        many => vec![format!("super::super::search::{{{}}}", many.join(", "))],
    };
    // Typed ids appear in `Ordered<_>`/`Equality<_>` conditions only.
    paths.extend(super::keys::id_imports(
        table,
        filterable
            .iter()
            .filter(|(_, kind)| *kind != FilterKind::Text)
            .map(|(column, _)| *column),
        false,
    ));
    if filterable
        .iter()
        .any(|(column, _)| mentions_generated_type(column.value_type()))
    {
        paths.push("super::super::types".to_owned());
    }
    for FilterRelationship { target, .. } in &relationships {
        if target.module_name != table.module_name {
            paths.push(format!(
                "super::super::{}::filter::{}Filter",
                target.module_name, target.struct_name
            ));
        }
    }
    if !paths.is_empty() {
        output.push_str(&super::use_paths(&paths));
        output.push('\n');
    }
    let title = format!("Conditions for `search` over `{}`.", table.display_name());
    let mut doc = vec![
        title.as_str(),
        "",
        "Every condition in every field must hold (they are combined with `AND`);",
        "an empty filter matches every row. A nullable column's `*_is_null` field",
        "selects rows where it is (`Some(true)`) or is not (`Some(false)`) `NULL`.",
    ];
    let live = live_condition(table, None);
    if live.is_some() {
        doc.extend([
            "Soft-deleted rows match only when `include_deleted` is set; an empty filter",
            "matches every live row.",
        ]);
    }
    if fallible {
        doc.extend([
            "",
            "A relationship field matches rows with a related row matching its filter,",
            "through an `EXISTS` subquery, so no row is returned twice; for a to-many",
            "relationship, at least one related row. An empty related filter only",
            "requires a related row: a `NULL` foreign key has none. Soft-deleted related",
            "rows never match (`include_deleted` applies to the searched table only),",
            "and a related tenant table's rows match only within the searched row's",
            "tenant. Relationship filters nest at most `MAX_RELATIONSHIP_DEPTH` deep.",
        ]);
    }
    push_doc(&mut output, 0, &doc);
    output.push_str("#[derive(Debug, Clone, Default, PartialEq)]\n");
    let mut fields = Vec::new();
    if live.is_some() {
        fields.push("/// Whether soft-deleted rows match too.".to_owned());
        fields.push(format!(
            "pub {}: bool,",
            super::conventions::INCLUDE_DELETED
        ));
    }
    for column in &table.columns {
        let name = doc_name(&column.sql_name);
        if let Some(kind) = column.filter_kind() {
            fields.push(format!("/// Conditions on the {name} column."));
            fields.push(format!(
                "pub {}: Vec<{}>,",
                column.rust_name,
                kind.condition_type(column)
            ));
        }
        if column.nullable {
            fields.push(format!("/// Whether the {name} column is `NULL`."));
            fields.push(format!("pub {}: Option<bool>,", column.null_check_field()));
        }
    }
    for FilterRelationship {
        relationship,
        target,
        ..
    } in &relationships
    {
        let name = &relationship.name;
        let target_name = doc_name(&target.display_name());
        fields.push(if relationship.kind.is_to_many() {
            format!("/// Matches rows with at least one of their `{name}` ({target_name} rows) matching this filter.")
        } else {
            format!("/// Matches rows whose `{name}` ({target_name} row) exists and matches this filter.")
        });
        fields.push(format!(
            "pub {name}: Option<Box<{}Filter>>,",
            target.struct_name
        ));
    }
    push_block(&mut output, &format!("pub struct {filter}"), &fields, 0);
    output.push('\n');

    output.push_str(&format!("impl {filter} {{\n"));
    let mut where_doc = vec!["Appends the `WHERE` clause for these conditions."];
    if fallible {
        where_doc.extend(ERRORS_DOC);
        push_doc(&mut output, 4, &where_doc);
        output.push_str(&format!(
            "    pub(crate) fn push_where(\n        self,\n        {QUERY_PARAM},\n    ) -> Result<(), sqlx::Error> {{\n"
        ));
    } else {
        push_doc(&mut output, 4, &where_doc);
        output.push_str(&format!(
            "    pub(crate) fn push_where(self, {QUERY_PARAM}) {{\n"
        ));
    }
    output.push_str("        query.push(\" WHERE TRUE\");\n");
    if let Some(live) = &live {
        output.push_str(&format!(
            "        if !self.{} {{\n            query.push({});\n        }}\n",
            super::conventions::INCLUDE_DELETED,
            str_literal(&format!(" AND {live}"))
        ));
    }
    if !split {
        push_column_conditions(&mut output, table);
        output.push_str("    }\n}\n");
    } else {
        if fallible {
            push_call_ending(
                &mut output,
                8,
                "self.push_conditions",
                &[
                    str_literal(&ident(&table.sql_name)),
                    "0".to_owned(),
                    "query".to_owned(),
                ],
                "",
            );
        } else {
            output.push_str("        self.push_conditions(query);\n");
        }
        output.push_str("    }\n\n");
        push_conditions_fn(&mut output, table, tables, &relationships);
        output.push_str("}\n");
    }
    if !api.search {
        return Some(output);
    }

    output.push('\n');
    push_doc(&mut output, 0, &["Columns `search` may sort by."]);
    output.push_str("#[derive(Debug, Clone, Copy, PartialEq, Eq)]\n");
    let sortable = sortable(table);
    let variants: Vec<String> = sortable
        .iter()
        .flat_map(|column| {
            [
                format!("/// Sorts by the {} column.", doc_name(&column.sql_name)),
                format!("{},", column.sort_variant()),
            ]
        })
        .collect();
    push_block(&mut output, &format!("pub enum {sort_key}"), &variants, 0);
    output.push('\n');
    output.push_str(&format!("impl {sort_key} {{\n"));
    push_doc(&mut output, 4, &["The SQL column this key sorts by."]);
    output.push_str("    #[must_use]\n    pub fn column(self) -> &'static str {\n");
    let arms: Vec<String> = sortable
        .iter()
        .map(|column| {
            format!(
                "Self::{} => {},",
                column.sort_variant(),
                str_literal(&ident(&column.sql_name))
            )
        })
        .collect();
    push_block(&mut output, "match self", &arms, 8);
    output.push_str("    }\n}\n");
    Some(output)
}

/// The `# Errors` section of a function that follows relationship filters.
const ERRORS_DOC: [&str; 5] = [
    "",
    "# Errors",
    "",
    "`sqlx::Error::InvalidArgument` when relationship filters nest more than",
    "`MAX_RELATIONSHIP_DEPTH` deep.",
];

/// Appends ` AND <column> <condition>` for every column condition of the
/// filter, inside a function taking `self` and `query`.
fn push_column_conditions(output: &mut String, table: &Table) {
    for column in &table.columns {
        let name = str_literal(&ident(&column.sql_name));
        if column.filter_kind().is_some() {
            output.push_str(&format!(
                "        for condition in self.{} {{\n",
                column.rust_name
            ));
            push_call(
                output,
                12,
                "condition.push",
                &[name.clone(), "query".to_owned()],
            );
            output.push_str("        }\n");
        }
        if column.nullable {
            push_call(
                output,
                8,
                "push_null_check",
                &[
                    name,
                    format!("self.{}", column.null_check_field()),
                    "query".to_owned(),
                ],
            );
        }
    }
}

/// `push_conditions`: the column conditions, then an `EXISTS` subquery per
/// set relationship field. It takes the enclosing row's table or alias
/// (`outer`) and the nesting depth when the filter has relationship fields.
fn push_conditions_fn(
    output: &mut String,
    table: &Table,
    tables: &[Table],
    relationships: &[FilterRelationship<'_>],
) {
    let mut doc = vec![
        "Appends ` AND <condition>` for each condition, without the `WHERE` clause or",
        "the soft-delete condition, so another resource's filter can nest this one.",
    ];
    if relationships.is_empty() {
        push_doc(output, 4, &doc);
        output.push_str(&format!(
            "    pub(crate) fn push_conditions(self, {QUERY_PARAM}) {{\n"
        ));
        push_column_conditions(output, table);
        output.push_str("    }\n");
        return;
    }
    doc.extend([
        "`outer` names the row being filtered (its table, or a relationship alias)",
        "and `depth` counts the relationship filters this one is nested in.",
    ]);
    doc.extend(ERRORS_DOC);
    push_doc(output, 4, &doc);
    output.push_str(&format!(
        "    pub(crate) fn push_conditions(\n        self,\n        outer: &str,\n        depth: usize,\n        {QUERY_PARAM},\n    ) -> Result<(), sqlx::Error> {{\n"
    ));
    push_column_conditions(output, table);
    let quoted = |name: &str| str_literal(&ident(name));
    for FilterRelationship {
        relationship,
        target,
        join,
    } in relationships
    {
        output.push_str(&format!(
            "        if let Some(filter) = self.{} {{\n",
            relationship.name
        ));
        push_call_ending(
            output,
            12,
            "let alias = open_exists",
            &[
                str_literal(&table_ref(target)),
                "depth".to_owned(),
                "query".to_owned(),
            ],
            "?;",
        );
        // A many-to-many correlates through its join table instead.
        let mut pairs = Vec::new();
        if join.is_none() {
            pairs.push((&relationship.target_column, &relationship.column));
        }
        // `filter_relationships` keeps a tenant target only when this table
        // has a tenant column too; both use the configured column name.
        pairs.extend(
            target
                .tenant()
                .zip(table.tenant())
                .map(|(theirs, ours)| (&theirs.sql_name, &ours.sql_name)),
        );
        for (theirs, ours) in pairs {
            push_call(
                output,
                12,
                "correlate",
                &[
                    "&alias".to_owned(),
                    quoted(theirs),
                    "outer".to_owned(),
                    quoted(ours),
                    "query".to_owned(),
                ],
            );
        }
        if let Some(live) = live_condition(target, None) {
            output.push_str(&format!(
                "            query.push({});\n",
                str_literal(&format!(" AND {live}"))
            ));
        }
        if let (Some(join), Some(through)) = (join, &relationship.through) {
            push_join_exists(output, table, join, relationship, through);
        }
        if filter_relationships(target, tables).is_empty() {
            output.push_str("            filter.push_conditions(query);\n");
        } else {
            output.push_str("            filter.push_conditions(&alias, depth + 1, query)?;\n");
        }
        output.push_str("            query.push(\")\");\n        }\n");
    }
    output.push_str("        Ok(())\n    }\n");
}

/// Inside a many-to-many's `EXISTS` over the related table (`alias`), a
/// nested ` AND EXISTS (SELECT 1 FROM <join> AS <alias>_j …)` requiring a
/// join row pairing the related row with the outer row, in the outer row's
/// tenant, and live (its unqualified soft-delete column resolves to the join
/// table, the innermost one).
fn push_join_exists(
    output: &mut String,
    table: &Table,
    join: &Table,
    relationship: &Relationship,
    through: &Through,
) {
    let quoted = |name: &str| str_literal(&ident(name));
    output.push_str("            let through = format!(\"{alias}_j\");\n");
    output.push_str(&format!(
        "            query.push({});\n",
        str_literal(&format!(
            " AND EXISTS (SELECT 1 FROM {} AS ",
            table_ref(join)
        ))
    ));
    output.push_str("            query.push(&through);\n");
    output.push_str("            query.push(\" WHERE TRUE\");\n");
    let mut pairs = vec![
        (
            &through.target_column,
            "&alias",
            &relationship.target_column,
        ),
        (&through.column, "outer", &relationship.column),
    ];
    pairs.extend(
        join.tenant()
            .zip(table.tenant())
            .map(|(theirs, ours)| (&theirs.sql_name, "outer", &ours.sql_name)),
    );
    for (theirs, outer, ours) in pairs {
        push_call(
            output,
            12,
            "correlate",
            &[
                "&through".to_owned(),
                quoted(theirs),
                outer.to_owned(),
                quoted(ours),
                "query".to_owned(),
            ],
        );
    }
    let close = match live_condition(join, None) {
        Some(live) => format!(" AND {live})"),
        None => ")".to_owned(),
    };
    output.push_str(&format!(
        "            query.push({});\n",
        str_literal(&close)
    ));
}

/// Renders the shared `db/search.rs`.
pub fn render_shared() -> String {
    format!("{HEADER}\n{SHARED}")
}

const SHARED: &str = r#"//! Types shared by every resource's `search`: pages, sorting, and typed
//! filter conditions.

use sqlx::{Postgres, QueryBuilder};

/// The most rows a single page may hold; `Page::new` clamps larger limits.
pub const MAX_PAGE_SIZE: i64 = 500;

/// How many rows one request may return: always within `1..=MAX_PAGE_SIZE`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Limit(i64);

impl Limit {
    /// Clamps `rows` to `1..=MAX_PAGE_SIZE`.
    #[must_use]
    pub fn new(rows: i64) -> Self {
        Self(rows.clamp(1, MAX_PAGE_SIZE))
    }

    /// The number of rows.
    #[must_use]
    pub fn get(self) -> i64 {
        self.0
    }
}

/// A window of results: at most `limit` rows after skipping `offset`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Page {
    limit: Limit,
    offset: i64,
}

impl Page {
    /// Clamps `limit` to `1..=MAX_PAGE_SIZE` and `offset` to zero or more.
    #[must_use]
    pub fn new(limit: i64, offset: i64) -> Self {
        Self {
            limit: Limit::new(limit),
            offset: offset.max(0),
        }
    }

    /// The first page of `limit` rows.
    #[must_use]
    pub fn first(limit: i64) -> Self {
        Self::new(limit, 0)
    }

    /// The page that follows this one.
    #[must_use]
    pub fn next(self) -> Self {
        Self::new(self.limit(), self.offset.saturating_add(self.limit()))
    }

    /// The most rows the page holds.
    #[must_use]
    pub fn limit(self) -> i64 {
        self.limit.get()
    }

    /// The number of rows skipped before the page.
    #[must_use]
    pub fn offset(self) -> i64 {
        self.offset
    }
}

/// Sort direction.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum Direction {
    /// Ascending: smallest first.
    #[default]
    Asc,
    /// Descending: largest first.
    Desc,
}

impl Direction {
    /// The SQL keyword, with a leading space.
    #[must_use]
    pub fn sql(self) -> &'static str {
        match self {
            Direction::Asc => " ASC",
            Direction::Desc => " DESC",
        }
    }
}

/// Sorts by one allowlisted key; `search` breaks ties by primary key.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Sort<K> {
    /// The column to sort by.
    pub key: K,
    /// Ascending or descending.
    pub direction: Direction,
}

impl<K> Sort<K> {
    /// Sorts by `key`, smallest first.
    pub fn asc(key: K) -> Self {
        Self {
            key,
            direction: Direction::Asc,
        }
    }

    /// Sorts by `key`, largest first.
    pub fn desc(key: K) -> Self {
        Self {
            key,
            direction: Direction::Desc,
        }
    }
}

/// A condition on a number, decimal, date, or time column.
#[derive(Debug, Clone, PartialEq)]
pub enum Ordered<T> {
    /// Equal to the value.
    Eq(T),
    /// Not equal to the value.
    Ne(T),
    /// Less than the value.
    Lt(T),
    /// Less than or equal to the value.
    Le(T),
    /// Greater than the value.
    Gt(T),
    /// Greater than or equal to the value.
    Ge(T),
    /// Between the two values, inclusive on both ends.
    Between(T, T),
    /// Equal to one of the values.
    In(Vec<T>),
    /// Equal to none of the values.
    NotIn(Vec<T>),
}

impl<T> Ordered<T> {
    /// Appends ` AND <column> <condition>`, binding every value.
    #[doc(hidden)]
    pub fn push(self, column: &str, query: &mut QueryBuilder<Postgres>)
    where
        T: for<'q> sqlx::Encode<'q, Postgres> + sqlx::Type<Postgres>,
        Vec<T>: for<'q> sqlx::Encode<'q, Postgres> + sqlx::Type<Postgres>,
    {
        query.push(" AND ");
        query.push(column);
        let shared = match self {
            Ordered::Eq(value) => Equality::Eq(value),
            Ordered::Ne(value) => Equality::Ne(value),
            Ordered::In(values) => Equality::In(values),
            Ordered::NotIn(values) => Equality::NotIn(values),
            Ordered::Lt(value) => return push_operator(query, " < ", value),
            Ordered::Le(value) => return push_operator(query, " <= ", value),
            Ordered::Gt(value) => return push_operator(query, " > ", value),
            Ordered::Ge(value) => return push_operator(query, " >= ", value),
            Ordered::Between(low, high) => {
                push_operator(query, " BETWEEN ", low);
                return push_operator(query, " AND ", high);
            }
        };
        shared.push_condition(query);
    }
}

/// A condition on a boolean or UUID column.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Equality<T> {
    /// Equal to the value.
    Eq(T),
    /// Not equal to the value.
    Ne(T),
    /// Equal to one of the values.
    In(Vec<T>),
    /// Equal to none of the values.
    NotIn(Vec<T>),
}

impl<T> Equality<T> {
    /// Appends ` AND <column> <condition>`, binding every value.
    #[doc(hidden)]
    pub fn push(self, column: &str, query: &mut QueryBuilder<Postgres>)
    where
        T: for<'q> sqlx::Encode<'q, Postgres> + sqlx::Type<Postgres>,
        Vec<T>: for<'q> sqlx::Encode<'q, Postgres> + sqlx::Type<Postgres>,
    {
        query.push(" AND ");
        query.push(column);
        self.push_condition(query);
    }

    /// Appends the condition after its column. `Ordered` and `Text` share
    /// these four conditions and push them through here.
    fn push_condition(self, query: &mut QueryBuilder<Postgres>)
    where
        T: for<'q> sqlx::Encode<'q, Postgres> + sqlx::Type<Postgres>,
        Vec<T>: for<'q> sqlx::Encode<'q, Postgres> + sqlx::Type<Postgres>,
    {
        match self {
            Equality::Eq(value) => push_operator(query, " = ", value),
            Equality::Ne(value) => push_operator(query, " <> ", value),
            Equality::In(values) => push_set(query, " = ANY(", values),
            Equality::NotIn(values) => push_set(query, " <> ALL(", values),
        }
    }
}

/// Appends ` AND <column> IS [NOT] NULL` for `Some(true)` / `Some(false)`.
#[doc(hidden)]
pub fn push_null_check(column: &str, is_null: Option<bool>, query: &mut QueryBuilder<Postgres>) {
    match is_null {
        Some(true) => {
            query.push(" AND ");
            query.push(column);
            query.push(" IS NULL");
        }
        Some(false) => {
            query.push(" AND ");
            query.push(column);
            query.push(" IS NOT NULL");
        }
        None => {}
    }
}

/// How deeply relationship filters may nest: a filter's relationship field
/// counts one, a relationship field inside that filter two, and so on.
/// `search` fails with `sqlx::Error::InvalidArgument` past this depth.
pub const MAX_RELATIONSHIP_DEPTH: usize = 4;

/// Appends ` AND EXISTS (SELECT 1 FROM <table> AS <alias> WHERE TRUE` for a
/// relationship filter nested in `depth` others, returning the alias. The
/// caller appends the correlation and conditions, then `)`.
///
/// `table` is pushed as SQL text, not bound: only the generated filters
/// call this, with a table name rok-sqlc quoted. Never pass user input.
///
/// # Errors
///
/// `sqlx::Error::InvalidArgument` when `depth` reaches
/// `MAX_RELATIONSHIP_DEPTH`.
#[doc(hidden)]
pub fn open_exists(
    table: &str,
    depth: usize,
    query: &mut QueryBuilder<Postgres>,
) -> Result<String, sqlx::Error> {
    if depth >= MAX_RELATIONSHIP_DEPTH {
        return Err(sqlx::Error::InvalidArgument(format!(
            "relationship filters nest more than {MAX_RELATIONSHIP_DEPTH} deep"
        )));
    }
    let alias = format!("_r{}", depth + 1);
    query.push(" AND EXISTS (SELECT 1 FROM ");
    query.push(table);
    query.push(" AS ");
    query.push(&alias);
    query.push(" WHERE TRUE");
    Ok(alias)
}

/// Appends ` AND <alias>.<column> = <outer>.<outer_column>`.
///
/// Every argument is pushed as SQL text, not bound: only the generated
/// filters call this, with aliases and column names rok-sqlc generated and
/// quoted. Never pass user input.
#[doc(hidden)]
pub fn correlate(
    alias: &str,
    column: &str,
    outer: &str,
    outer_column: &str,
    query: &mut QueryBuilder<Postgres>,
) {
    query.push(" AND ");
    query.push(alias);
    query.push(".");
    query.push(column);
    query.push(" = ");
    query.push(outer);
    query.push(".");
    query.push(outer_column);
}

/// A condition on a text column. The substring variants match their
/// argument literally: `%`, `_`, and `\` in it are escaped.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Text {
    /// Equal to the text.
    Eq(String),
    /// Not equal to the text.
    Ne(String),
    /// Equal to one of the texts.
    In(Vec<String>),
    /// Equal to none of the texts.
    NotIn(Vec<String>),
    /// Contains the text.
    Contains(String),
    /// Starts with the text.
    StartsWith(String),
    /// Ends with the text.
    EndsWith(String),
}

impl Text {
    /// Appends ` AND <column> <condition>`, binding every value.
    #[doc(hidden)]
    pub fn push(self, column: &str, query: &mut QueryBuilder<Postgres>) {
        query.push(" AND ");
        query.push(column);
        let pattern = match self {
            Text::Eq(value) => return Equality::Eq(value).push_condition(query),
            Text::Ne(value) => return Equality::Ne(value).push_condition(query),
            Text::In(values) => return Equality::In(values).push_condition(query),
            Text::NotIn(values) => return Equality::NotIn(values).push_condition(query),
            Text::Contains(value) => format!("%{}%", escape_like(&value)),
            Text::StartsWith(value) => format!("{}%", escape_like(&value)),
            Text::EndsWith(value) => format!("%{}", escape_like(&value)),
        };
        push_operator(query, " LIKE ", pattern);
    }
}

/// Escapes `LIKE` wildcards with the default escape character, `\`.
fn escape_like(value: &str) -> String {
    let mut escaped = String::with_capacity(value.len());
    for character in value.chars() {
        if matches!(character, '%' | '_' | '\\') {
            escaped.push('\\');
        }
        escaped.push(character);
    }
    escaped
}

fn push_operator<T>(query: &mut QueryBuilder<Postgres>, operator: &str, value: T)
where
    T: for<'q> sqlx::Encode<'q, Postgres> + sqlx::Type<Postgres>,
{
    query.push(operator);
    query.push_bind(value);
}

fn push_set<T>(query: &mut QueryBuilder<Postgres>, operator: &str, values: Vec<T>)
where
    Vec<T>: for<'q> sqlx::Encode<'q, Postgres> + sqlx::Type<Postgres>,
{
    query.push(operator);
    query.push_bind(values);
    query.push(")");
}
"#;

#[cfg(test)]
mod tests {
    use super::super::test_support::{column, table};
    use super::*;

    /// SQLx 0.9's `QueryBuilder` takes no lifetime parameter.
    #[test]
    fn search_code_names_query_builder_without_a_lifetime() {
        let filter = render_filter(&table(vec![column("id", "i64")], &["id"]), &[])
            .expect("an i64 column is filterable");
        for rendered in [render_shared(), filter] {
            assert!(rendered.contains("QueryBuilder<"), "{rendered}");
            assert!(!rendered.contains("QueryBuilder<'"), "{rendered}");
        }
    }
}