mariadb-cli 0.1.1

One-shot agent-native command line client for MariaDB servers
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
//! The entries of the catalogue: what to read for each kind of object.
//!
//! Every structure command is the same operation: read a metadata table,
//! narrow it to a scope, convert the rows. Only the parameters differ, so the
//! parameters live here, as data, and the operation lives in
//! [`super::query`]. Adding a kind means adding a descriptor, never adding a
//! function.
//!
//! The vocabulary these entries are written in lives in [`super::kinds`].
//!
//! Every string in this module is written in this file and never comes from a
//! caller, so nothing here can be steered from outside. Caller-supplied names
//! travel as bound values, never as text pasted into a statement.

use super::kinds::{field, Descriptor, Field, Kind, PLAIN};

impl Kind {
    /// What this kind is, in full.
    #[must_use]
    pub const fn descriptor(self) -> &'static Descriptor {
        match self {
            Self::Database => &DATABASE,
            Self::Table => &TABLE,
            Self::Column => &COLUMN,
            Self::ForeignKey => &FOREIGN_KEY,
            Self::Index => &INDEX,
            Self::Check => &CHECK,
            Self::View => &VIEW,
            Self::Routine => &ROUTINE,
            Self::Trigger => &TRIGGER,
            Self::Event => &EVENT,
            Self::Sequence => &SEQUENCE,
            Self::Partition => &PARTITION,
            Self::Statistic => &STATISTIC,
            Self::DatabaseSpace => &DATABASE_SPACE,
            Self::ObjectSpace => &OBJECT_SPACE,
        }
    }
}

/// Only tables whose rows are data, including those keeping their own history.
const REAL_TABLES: &str = "TABLE_TYPE IN ('BASE TABLE', 'SYSTEM VERSIONED')";

/// The comment of an object, published so that absence is its own mark.
///
/// The catalogue of this server writes the empty string where an object
/// carries no comment, and empty text is indistinguishable from a comment
/// somebody deliberately wrote as empty. So a caller reading `""` cannot tell
/// "nobody said anything" from "somebody said nothing", which is the whole
/// distinction the mark exists to carry.
///
/// The mark is made inside the statement, with `NULLIF`, rather than by
/// inspecting the text after it arrives: the conversion then happens once, in
/// the server, and no second round trip is spent asking what an empty string
/// meant. The absence marker it produces is the same one `next_auto_value`
/// already carries on the same row.
///
/// The expression is a literal written in this file, like every other source
/// here, so nothing about it can be steered from outside.
const fn comment_or_absent(expression: &'static str) -> Field {
    field(expression, "comment")
}

/// A table's comment, with empty text turned into the absence marker.
const TABLE_COMMENT_OR_ABSENT: &str = "NULLIF(TABLE_COMMENT, '')";

/// An index's comment, with empty text turned into the absence marker.
const INDEX_COMMENT_OR_ABSENT: &str = "NULLIF(INDEX_COMMENT, '')";

/// A routine's comment, with empty text turned into the absence marker.
const ROUTINE_COMMENT_OR_ABSENT: &str = "NULLIF(ROUTINE_COMMENT, '')";

/// A view's comment, which this server has nowhere to keep.
///
/// `information_schema.VIEWS` carries no comment column at all, and the row a
/// view has in `information_schema.TABLES` carries the literal `VIEW` in the
/// place a comment would go: that is the server writing the kind of the
/// object, not a comment anybody wrote. Measured against this destination,
/// every view answers `VIEW` there and none answers anything else.
///
/// So both the empty string and that label are turned into the absence marker,
/// and the field is published rather than omitted. A caller that asks four
/// kinds of object for their comment then gets the same answer in the same
/// place from all four, and learns that a view here has none instead of having
/// to discover that this one listing is shaped differently.
const VIEW_COMMENT_OR_ABSENT: &str = "(SELECT NULLIF(NULLIF(t.TABLE_COMMENT, ''), 'VIEW') \
     FROM information_schema.TABLES t \
     WHERE t.TABLE_SCHEMA = information_schema.VIEWS.TABLE_SCHEMA \
     AND t.TABLE_NAME = information_schema.VIEWS.TABLE_NAME)";

/// Databases, with the character rules the data was written under.
static DATABASE: Descriptor = Descriptor {
    kind: Kind::Database,
    source: "information_schema.SCHEMATA",
    short: &[field("SCHEMA_NAME", "name")],
    extra: &[
        field("DEFAULT_CHARACTER_SET_NAME", "character_set"),
        field("DEFAULT_COLLATION_NAME", "collation"),
    ],
    database_column: Some("SCHEMA_NAME"),
    name_column: "SCHEMA_NAME",
    order_by: "SCHEMA_NAME",
    ..PLAIN
};

/// Base tables, with their storage properties on request.
///
/// A table that keeps history by time version is a base table the server
/// labels differently, so it is included and the label is published rather
/// than being silently dropped from the listing.
static TABLE: Descriptor = Descriptor {
    kind: Kind::Table,
    source: "information_schema.TABLES",
    short: &[
        field("TABLE_SCHEMA", "database"),
        field("TABLE_NAME", "name"),
        field("TABLE_TYPE", "type"),
    ],
    extra: &[
        field("TABLE_TYPE = 'SYSTEM VERSIONED'", "keeps_history"),
        field("ENGINE", "engine"),
        field("ROW_FORMAT", "row_format"),
        field("TABLE_COLLATION", "collation"),
        field("TABLE_ROWS", "estimated_rows"),
        field("DATA_LENGTH", "data_bytes"),
        field("INDEX_LENGTH", "index_bytes"),
        field("AUTO_INCREMENT", "next_auto_value"),
        comment_or_absent(TABLE_COMMENT_OR_ABSENT),
        field("CREATE_TIME", "created_at"),
        field("UPDATE_TIME", "updated_at"),
    ],
    database_column: Some("TABLE_SCHEMA"),
    table_column: Some("TABLE_NAME"),
    name_column: "TABLE_NAME",
    predicate: Some(REAL_TABLES),
    order_by: "TABLE_SCHEMA, TABLE_NAME",
    ..PLAIN
};

/// Columns, including the marks that decide what an insertion may say.
static COLUMN: Descriptor = Descriptor {
    kind: Kind::Column,
    source: "information_schema.COLUMNS",
    short: &[
        field("TABLE_SCHEMA", "database"),
        field("TABLE_NAME", "table"),
        field("COLUMN_NAME", "name"),
        field("ORDINAL_POSITION", "position"),
        field("COLUMN_TYPE", "type"),
        field("IS_NULLABLE = 'YES'", "nullable"),
        field("COLUMN_DEFAULT", "default_value"),
        field("COLUMN_KEY", "key"),
    ],
    extra: &[
        field("DATA_TYPE", "base_type"),
        field("CHARACTER_SET_NAME", "character_set"),
        field("COLLATION_NAME", "collation"),
        field("GENERATION_EXPRESSION", "generated_from"),
        field("EXTRA LIKE '%INVISIBLE%'", "invisible"),
        field(SERVER_FILLED, "server_filled"),
        field("IS_GENERATED = 'ALWAYS'", "derived"),
        field("COLUMN_COMMENT", "comment"),
    ],
    database_column: Some("TABLE_SCHEMA"),
    table_column: Some("TABLE_NAME"),
    name_column: "COLUMN_NAME",
    order_by: "TABLE_SCHEMA, TABLE_NAME, ORDINAL_POSITION",
    ..PLAIN
};

/// Whether the server puts a value in this column without being told to.
const SERVER_FILLED: &str = "EXTRA LIKE '%auto_increment%' \
     OR EXTRA LIKE '%DEFAULT_GENERATED%' OR EXTRA LIKE '%on update%'";

/// Links between tables, with the rules that fire on change.
static FOREIGN_KEY: Descriptor = Descriptor {
    kind: Kind::ForeignKey,
    source: "information_schema.KEY_COLUMN_USAGE k \
             JOIN information_schema.REFERENTIAL_CONSTRAINTS r \
             ON r.CONSTRAINT_SCHEMA = k.CONSTRAINT_SCHEMA \
             AND r.CONSTRAINT_NAME = k.CONSTRAINT_NAME \
             AND r.TABLE_NAME = k.TABLE_NAME",
    short: &[
        field("k.CONSTRAINT_SCHEMA", "database"),
        field("k.TABLE_NAME", "table"),
        field("k.CONSTRAINT_NAME", "name"),
        field("k.ORDINAL_POSITION", "position"),
        field("k.COLUMN_NAME", "column"),
        field("k.REFERENCED_TABLE_SCHEMA", "referenced_database"),
        field("k.REFERENCED_TABLE_NAME", "referenced_table"),
        field("k.REFERENCED_COLUMN_NAME", "referenced_column"),
        field("r.UPDATE_RULE", "on_update"),
        field("r.DELETE_RULE", "on_delete"),
    ],
    extra: &[field("r.MATCH_OPTION", "match_option")],
    database_column: Some("k.CONSTRAINT_SCHEMA"),
    table_column: Some("k.TABLE_NAME"),
    name_column: "k.CONSTRAINT_NAME",
    predicate: Some("k.REFERENCED_TABLE_NAME IS NOT NULL"),
    order_by: "k.CONSTRAINT_SCHEMA, k.TABLE_NAME, k.CONSTRAINT_NAME, k.ORDINAL_POSITION",
    ..PLAIN
};

/// Indexes, one row per indexed column so the order inside the index shows.
static INDEX: Descriptor = Descriptor {
    kind: Kind::Index,
    source: "information_schema.STATISTICS",
    short: &[
        field("TABLE_SCHEMA", "database"),
        field("TABLE_NAME", "table"),
        field("INDEX_NAME", "name"),
        field("SEQ_IN_INDEX", "position"),
        field("COLUMN_NAME", "column"),
        field("NON_UNIQUE = 0", "unique"),
        field("INDEX_NAME = 'PRIMARY'", "primary"),
    ],
    extra: &[
        field("INDEX_TYPE", "method"),
        field("CARDINALITY", "distinct_values"),
        field("SUB_PART", "prefix_length"),
        field("NULLABLE = 'YES'", "nullable"),
        comment_or_absent(INDEX_COMMENT_OR_ABSENT),
    ],
    database_column: Some("TABLE_SCHEMA"),
    table_column: Some("TABLE_NAME"),
    name_column: "INDEX_NAME",
    order_by: "TABLE_SCHEMA, TABLE_NAME, INDEX_NAME, SEQ_IN_INDEX",
    ..PLAIN
};

/// Value restrictions, with the condition each one imposes written out.
///
/// The condition is a short field rather than an extra: a restriction without
/// its condition says only that some values are refused, which is the half of
/// the answer nobody can act on.
///
/// `LEVEL` is quoted because the server keeps a keyword of the same spelling,
/// and it says whether the restriction was written beside one column or beside
/// the whole table. The two forms are enforced alike and read differently, so
/// a caller reconstructing the table needs to be told which one this is.
static CHECK: Descriptor = Descriptor {
    kind: Kind::Check,
    source: "information_schema.CHECK_CONSTRAINTS",
    short: &[
        field("CONSTRAINT_SCHEMA", "database"),
        field("TABLE_NAME", "table"),
        field("CONSTRAINT_NAME", "name"),
        field("CHECK_CLAUSE", "condition"),
    ],
    extra: &[field("`LEVEL`", "level")],
    database_column: Some("CONSTRAINT_SCHEMA"),
    table_column: Some("TABLE_NAME"),
    name_column: "CONSTRAINT_NAME",
    order_by: "CONSTRAINT_SCHEMA, TABLE_NAME, CONSTRAINT_NAME",
    ..PLAIN
};

/// Views, with who defines them and how they run on request.
static VIEW: Descriptor = Descriptor {
    kind: Kind::View,
    source: "information_schema.VIEWS",
    short: &[
        field("TABLE_SCHEMA", "database"),
        field("TABLE_NAME", "name"),
        field("IS_UPDATABLE = 'YES'", "updatable"),
    ],
    extra: &[
        field("DEFINER", "definer"),
        field("SECURITY_TYPE", "runs_as"),
        field("CHECK_OPTION", "check_option"),
        field("CHARACTER_SET_CLIENT", "character_set"),
        field("COLLATION_CONNECTION", "collation"),
        comment_or_absent(VIEW_COMMENT_OR_ABSENT),
        field("VIEW_DEFINITION", "definition"),
    ],
    database_column: Some("TABLE_SCHEMA"),
    table_column: Some("TABLE_NAME"),
    name_column: "TABLE_NAME",
    order_by: "TABLE_SCHEMA, TABLE_NAME",
    ..PLAIN
};

/// Routines, with their parameters gathered in declaration order.
///
/// The parameter list is read in the same statement rather than in a second
/// one, because a routine without its parameters cannot be called and a caller
/// that has to ask twice pays two round trips for one answer.
static ROUTINE: Descriptor = Descriptor {
    kind: Kind::Routine,
    source: "information_schema.ROUTINES",
    short: &[
        field("ROUTINE_SCHEMA", "database"),
        field("ROUTINE_NAME", "name"),
        field("ROUTINE_TYPE", "type"),
        field("DTD_IDENTIFIER", "returns"),
        field(PARAMETER_LIST, "parameters"),
    ],
    extra: &[
        field("DEFINER", "definer"),
        field("SECURITY_TYPE", "runs_as"),
        field("IS_DETERMINISTIC = 'YES'", "deterministic"),
        field("SQL_DATA_ACCESS", "data_access"),
        comment_or_absent(ROUTINE_COMMENT_OR_ABSENT),
        field("CREATED", "created_at"),
        field("LAST_ALTERED", "updated_at"),
        field("ROUTINE_DEFINITION", "body"),
    ],
    database_column: Some("ROUTINE_SCHEMA"),
    name_column: "ROUTINE_NAME",
    order_by: "ROUTINE_SCHEMA, ROUTINE_NAME, ROUTINE_TYPE",
    ..PLAIN
};

/// Gathers a routine's parameters without a second round trip.
const PARAMETER_LIST: &str = "(SELECT GROUP_CONCAT(\
     CONCAT_WS(' ', p.PARAMETER_MODE, p.PARAMETER_NAME, p.DTD_IDENTIFIER) \
     ORDER BY p.ORDINAL_POSITION SEPARATOR ', ') \
     FROM information_schema.PARAMETERS p \
     WHERE p.SPECIFIC_SCHEMA = information_schema.ROUTINES.ROUTINE_SCHEMA \
     AND p.SPECIFIC_NAME = information_schema.ROUTINES.SPECIFIC_NAME \
     AND p.ORDINAL_POSITION > 0)";

/// Triggers, with what fires them.
static TRIGGER: Descriptor = Descriptor {
    kind: Kind::Trigger,
    source: "information_schema.TRIGGERS",
    short: &[
        field("TRIGGER_SCHEMA", "database"),
        field("TRIGGER_NAME", "name"),
        field("ACTION_TIMING", "timing"),
        field("EVENT_MANIPULATION", "event"),
        field("EVENT_OBJECT_TABLE", "table"),
    ],
    extra: &[
        field("ACTION_ORIENTATION", "granularity"),
        field("ACTION_ORDER", "order"),
        field("DEFINER", "definer"),
        field("CREATED", "created_at"),
        field("ACTION_STATEMENT", "body"),
    ],
    database_column: Some("TRIGGER_SCHEMA"),
    table_column: Some("EVENT_OBJECT_TABLE"),
    name_column: "TRIGGER_NAME",
    order_by: "TRIGGER_SCHEMA, EVENT_OBJECT_TABLE, ACTION_TIMING, TRIGGER_NAME",
    ..PLAIN
};

/// Scheduled events, which change data with nobody asking.
static EVENT: Descriptor = Descriptor {
    kind: Kind::Event,
    source: "information_schema.EVENTS",
    short: &[
        field("EVENT_SCHEMA", "database"),
        field("EVENT_NAME", "name"),
        field("STATUS", "status"),
        field("EVENT_TYPE", "type"),
        field("EXECUTE_AT", "executes_at"),
        field("CONCAT_WS(' ', INTERVAL_VALUE, INTERVAL_FIELD)", "every"),
        field("LAST_EXECUTED", "last_executed"),
    ],
    extra: &[
        field("STARTS", "starts_at"),
        field("ENDS", "ends_at"),
        field("ON_COMPLETION", "on_completion"),
        field("DEFINER", "definer"),
        field("TIME_ZONE", "time_zone"),
        field("EVENT_COMMENT", "comment"),
        field("EVENT_DEFINITION", "body"),
    ],
    database_column: Some("EVENT_SCHEMA"),
    name_column: "EVENT_NAME",
    order_by: "EVENT_SCHEMA, EVENT_NAME",
    ..PLAIN
};

/// Number generators.
///
/// The generator's own current value is deliberately not read: reading it
/// advances it, which would make a listing change the server.
static SEQUENCE: Descriptor = Descriptor {
    kind: Kind::Sequence,
    source: "information_schema.TABLES",
    short: &[
        field("TABLE_SCHEMA", "database"),
        field("TABLE_NAME", "name"),
    ],
    extra: &[
        field("ENGINE", "engine"),
        field("TABLE_COMMENT", "comment"),
        field("CREATE_TIME", "created_at"),
    ],
    database_column: Some("TABLE_SCHEMA"),
    table_column: Some("TABLE_NAME"),
    name_column: "TABLE_NAME",
    predicate: Some("TABLE_TYPE = 'SEQUENCE'"),
    order_by: "TABLE_SCHEMA, TABLE_NAME",
    ..PLAIN
};

/// Partitions, with the estimated weight of each part.
static PARTITION: Descriptor = Descriptor {
    kind: Kind::Partition,
    source: "information_schema.PARTITIONS",
    short: &[
        field("TABLE_SCHEMA", "database"),
        field("TABLE_NAME", "table"),
        field("PARTITION_NAME", "name"),
        field("SUBPARTITION_NAME", "subpartition"),
        field("PARTITION_ORDINAL_POSITION", "position"),
        field("PARTITION_METHOD", "method"),
        field("PARTITION_EXPRESSION", "expression"),
        field("PARTITION_DESCRIPTION", "criterion"),
        field("TABLE_ROWS", "estimated_rows"),
        field("DATA_LENGTH", "data_bytes"),
        field("INDEX_LENGTH", "index_bytes"),
    ],
    extra: &[
        field("DATA_FREE", "free_bytes"),
        field("PARTITION_COMMENT", "comment"),
    ],
    database_column: Some("TABLE_SCHEMA"),
    table_column: Some("TABLE_NAME"),
    name_column: "PARTITION_NAME",
    predicate: Some("PARTITION_NAME IS NOT NULL"),
    order_by: "TABLE_SCHEMA, TABLE_NAME, PARTITION_ORDINAL_POSITION, SUBPARTITION_NAME",
    ..PLAIN
};

/// The cheap figures of a table, none of which reads the data itself.
static STATISTIC: Descriptor = Descriptor {
    kind: Kind::Statistic,
    source: "information_schema.TABLES",
    short: &[
        field("TABLE_SCHEMA", "database"),
        field("TABLE_NAME", "name"),
        field("TABLE_ROWS", "estimated_rows"),
        field("DATA_LENGTH", "data_bytes"),
        field("INDEX_LENGTH", "index_bytes"),
    ],
    extra: &[
        field("ENGINE", "engine"),
        field("AVG_ROW_LENGTH", "average_row_bytes"),
        field("DATA_FREE", "free_bytes"),
        field("AUTO_INCREMENT", "next_auto_value"),
        field("CHECK_TIME", "checked_at"),
        field("UPDATE_TIME", "updated_at"),
    ],
    database_column: Some("TABLE_SCHEMA"),
    table_column: Some("TABLE_NAME"),
    name_column: "TABLE_NAME",
    predicate: Some(REAL_TABLES),
    order_by: "TABLE_SCHEMA, TABLE_NAME",
    ..PLAIN
};

/// Space totalled per database.
static DATABASE_SPACE: Descriptor = Descriptor {
    kind: Kind::DatabaseSpace,
    source: "information_schema.TABLES",
    short: &[
        // The subject of this row is a database, so it is named under `name`,
        // the same key `schema databases` uses for the same subject. It used to
        // be `database`, which everywhere else is the scope a row sits in and
        // not the thing the row is about.
        field("TABLE_SCHEMA", "name"),
        field("COUNT(*)", "objects"),
        field("SUM(DATA_LENGTH)", "data_bytes"),
        field("SUM(INDEX_LENGTH)", "index_bytes"),
        field("SUM(DATA_LENGTH + INDEX_LENGTH)", "total_bytes"),
    ],
    extra: &[
        field("SUM(DATA_FREE)", "free_bytes"),
        field("SUM(TABLE_ROWS)", "estimated_rows"),
    ],
    database_column: Some("TABLE_SCHEMA"),
    name_column: "TABLE_SCHEMA",
    predicate: Some(REAL_TABLES),
    group_by: Some("TABLE_SCHEMA"),
    order_by: "SUM(DATA_LENGTH + INDEX_LENGTH) DESC, TABLE_SCHEMA",
    ..PLAIN
};

/// Objects ranked by the space they take.
static OBJECT_SPACE: Descriptor = Descriptor {
    kind: Kind::ObjectSpace,
    source: "information_schema.TABLES",
    short: &[
        field("TABLE_SCHEMA", "database"),
        field("TABLE_NAME", "name"),
        field("DATA_LENGTH", "data_bytes"),
        field("INDEX_LENGTH", "index_bytes"),
        field("DATA_LENGTH + INDEX_LENGTH", "total_bytes"),
    ],
    extra: &[
        field("ENGINE", "engine"),
        field("TABLE_ROWS", "estimated_rows"),
        field("DATA_FREE", "free_bytes"),
    ],
    database_column: Some("TABLE_SCHEMA"),
    table_column: Some("TABLE_NAME"),
    name_column: "TABLE_NAME",
    predicate: Some(REAL_TABLES),
    order_by: "DATA_LENGTH + INDEX_LENGTH DESC, TABLE_SCHEMA, TABLE_NAME",
    ..PLAIN
};

#[cfg(test)]
mod tests {
    use super::super::kinds::{fields, ALL};
    use super::*;

    /// Every kind resolves to its own descriptor and answers to its own name,
    /// so no two kinds can share either by accident.
    #[test]
    fn every_kind_carries_its_own_descriptor_and_name() {
        for kind in ALL.iter().copied() {
            assert_eq!(kind.descriptor().kind, kind, "{}", kind.as_str());
            assert_eq!(Kind::parse(kind.as_str()), Some(kind));
            let descriptor = kind.descriptor();
            assert!(!descriptor.short.is_empty(), "{}", kind.as_str());
            assert!(!descriptor.source.is_empty(), "{}", kind.as_str());
            assert!(!descriptor.name_column.is_empty(), "{}", kind.as_str());
            assert!(!descriptor.order_by.is_empty(), "{}", kind.as_str());
        }
        assert_eq!(Kind::parse("not_a_kind"), None);
    }

    /// Asking for more never returns less, because the detailed form is the
    /// short one plus its extras by construction.
    #[test]
    fn the_detailed_form_never_returns_less_than_the_short_one() {
        for kind in ALL.iter().copied() {
            let short = fields(kind.descriptor(), false);
            let detailed = fields(kind.descriptor(), true);
            assert!(detailed.len() >= short.len(), "{}", kind.as_str());
            for (position, field) in short.iter().enumerate() {
                assert_eq!(field.alias, detailed[position].alias, "{}", kind.as_str());
            }
        }
    }

    /// Two fields sharing an alias would silently overwrite each other in the
    /// answer, so one of them would never be seen.
    #[test]
    fn no_kind_publishes_the_same_alias_twice() {
        for kind in ALL.iter().copied() {
            let mut aliases: Vec<&str> = fields(kind.descriptor(), true)
                .iter()
                .map(|field| field.alias)
                .collect();
            aliases.sort_unstable();
            let before = aliases.len();
            aliases.dedup();
            assert_eq!(aliases.len(), before, "{} repeats an alias", kind.as_str());
        }
    }

    /// A sequence is not a base table, and a base table is not a sequence:
    /// both live in the same metadata table and only the condition tells them
    /// apart.
    #[test]
    fn a_sequence_and_a_table_are_told_apart_by_their_condition() {
        assert_eq!(SEQUENCE.source, TABLE.source);
        assert_ne!(SEQUENCE.predicate, TABLE.predicate);
    }

    /// FR-122. The four kinds the requirement names publish a comment, and
    /// each of them asks the server to turn empty text into the absence marker
    /// before it leaves. Reading the alias alone would pass while the field
    /// handed back the empty string the catalogue keeps, which is exactly the
    /// defect this requirement was reopened for, so the expression is read too.
    #[test]
    fn every_kind_that_publishes_a_comment_marks_its_absence() {
        for kind in [Kind::Table, Kind::View, Kind::Index, Kind::Routine] {
            let carried = fields(kind.descriptor(), true);
            let comment = carried
                .iter()
                .find(|field| field.alias == "comment")
                .unwrap_or_else(|| panic!("{} publishes no comment", kind.as_str()));
            assert!(
                comment.source.contains("NULLIF"),
                "{} hands back the empty text the catalogue keeps: {}",
                kind.as_str(),
                comment.source
            );
        }
    }

    /// The marks that decide what an insertion may say are all published.
    #[test]
    fn a_column_publishes_every_mark_an_insertion_depends_on() {
        let aliases: Vec<&str> = fields(&COLUMN, true)
            .iter()
            .map(|field| field.alias)
            .collect();
        for wanted in [
            "invisible",
            "server_filled",
            "derived",
            "collation",
            "comment",
        ] {
            assert!(
                aliases.contains(&wanted),
                "a column never publishes {wanted}"
            );
        }
    }
}