quantydb-sqlite 0.4.0

Reader for the SQLite 3 file format, used to import .sqlite files into QuantyDB
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
//! Reading a `create table` statement.
//!
//! The file says which pages a table lives on, and nothing else about it.
//! Column names, their order, their declared types, which columns form the
//! primary key: all of that exists only as the text of the statement that
//! created the table, kept verbatim in the schema table. So importing
//! anything means parsing SQLite's dialect of `create table`, and this is
//! that parser.
//!
//! It is not the SQL front end from quantydb-ql and should not become it.
//! That one parses our dialect, where we decide what is legal. This one
//! parses text another engine already accepted, including everything we do
//! not implement: check constraints, foreign key actions, collations,
//! generated columns, four ways of quoting an identifier, and type names
//! that are arbitrary words. Bending our own parser around a foreign
//! grammar would put that mess in the path of every query we run.
//!
//! Where it cannot understand something it stops rather than guesses.
//! A statement SQLite accepted and we cannot read means our parser has a
//! gap, and a gap that quietly produces a column list of the wrong shape
//! would be read as data of the wrong shape, which is the failure this
//! whole crate is built to prevent.

use crate::error::{Result, SqliteError};

/// Whether a column is computed, and if so whether the file holds it.
///
/// The distinction decides where every later column's value sits in the
/// record, so it is a variant rather than a flag: a stored generated column
/// occupies a slot like any other column, a virtual one occupies none. A
/// generated column with neither keyword written out is virtual, which was
/// checked against a file rather than assumed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Generated {
    No,
    Virtual,
    Stored,
}

impl Generated {
    pub fn is_generated(self) -> bool {
        !matches!(self, Generated::No)
    }

    /// Whether the file holds no value for this column at all.
    pub fn is_virtual(self) -> bool {
        matches!(self, Generated::Virtual)
    }
}

/// A column as the create statement declared it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ColumnDef {
    pub name: String,
    /// The declared type, exactly as written, with its arguments: `INTEGER`,
    /// `NVARCHAR(160)`, `DOUBLE PRECISION`. `None` when the column was
    /// declared without one, which SQLite allows.
    pub declared_type: Option<String>,
    pub not_null: bool,
    /// The default, as written. Kept as text because a default can be an
    /// expression, and deciding what a given expression means is a question
    /// for whoever imports it.
    pub default: Option<String>,
    pub generated: Generated,
}

/// One column of a primary key, in key order.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeyColumn {
    pub name: String,
    pub descending: bool,
}

/// A rule the source enforces that we have no equivalent for.
///
/// These are recorded, not applied. QuantyDB has keys, indexes and
/// nullability, and nothing else from this list, so a foreign key or a
/// check constraint cannot come across. That is a limitation rather than a
/// choice, and the reason to carry them here at all is that an importer
/// which does not mention them lets a developer believe their data is still
/// guarded when it is not.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Constraint {
    /// `unique (a, b)`, or a column declared `unique`.
    Unique { columns: Vec<String> },
    /// `check (...)`, kept as written.
    Check { expression: String },
    /// `foreign key (a) references other (b)`.
    ForeignKey {
        columns: Vec<String>,
        references: String,
    },
    /// `collate nocase` on a column, which changes how text compares.
    Collation { column: String, name: String },
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TableDef {
    pub name: String,
    pub columns: Vec<ColumnDef>,
    /// Empty when the table has no primary key at all.
    pub primary_key: Vec<KeyColumn>,
    pub without_rowid: bool,
    pub strict: bool,
    /// Rules the source enforces that we cannot: unique constraints beyond
    /// the key, check constraints, foreign keys and collations.
    pub unsupported_constraints: Vec<Constraint>,
    /// Whether the primary key was written as a column constraint that
    /// spelled out `desc`. See `rowid_alias` for why that is worth keeping.
    inline_pk_desc: bool,
}

impl TableDef {
    pub fn column(&self, name: &str) -> Option<&ColumnDef> {
        self.columns
            .iter()
            .find(|c| c.name.eq_ignore_ascii_case(name))
    }

    pub fn column_index(&self, name: &str) -> Option<usize> {
        self.columns
            .iter()
            .position(|c| c.name.eq_ignore_ascii_case(name))
    }

    /// The column that is an alias for the rowid, if the table has one.
    ///
    /// Such a column holds no value of its own in the record; SQLite stores
    /// NULL there and the value lives in the cell's rowid. Getting this
    /// wrong means reading the primary key of an entire table as NULL, or
    /// inventing one, and nothing about it fails loudly, so the rule is
    /// spelled out here and checked against a fixture built from these
    /// exact cases:
    ///
    /// | declaration                       | alias |
    /// |-----------------------------------|-------|
    /// | `x integer primary key`           | yes   |
    /// | `x integer primary key desc`      | no    |
    /// | `x integer, primary key (x)`      | yes   |
    /// | `x integer, primary key (x desc)` | yes   |
    /// | `x int primary key`               | no    |
    ///
    /// The two `desc` rows disagreeing is not a typo. Spelling out `desc`
    /// in a column constraint suppresses the alias, and spelling it out in
    /// a table constraint does not. SQLite documents that as a quirk kept
    /// for backwards compatibility, and every reader has to reproduce it.
    ///
    /// A without rowid table has no rowid to alias, so it never has one.
    pub fn rowid_alias(&self) -> Option<&ColumnDef> {
        if self.without_rowid || self.primary_key.len() != 1 || self.inline_pk_desc {
            return None;
        }
        let key = &self.primary_key[0];
        let column = self.column(&key.name)?;
        match &column.declared_type {
            // exactly "integer", in any mixture of case. "int" does not
            // count, and neither does "integer(8)".
            Some(declared) if declared.trim().eq_ignore_ascii_case("integer") => Some(column),
            _ => None,
        }
    }
}

// ---------------------------------------------------------------------------
// tokens
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, PartialEq, Eq)]
enum Token {
    /// A bare word: a keyword, a type name, an unquoted identifier. SQLite
    /// does not reserve most of these, so which one it is depends on where
    /// it appears.
    Word(String),
    /// An identifier that was quoted, and therefore is never a keyword.
    Quoted(String),
    /// A string, number or blob literal, kept as written.
    Literal(String),
    Punct(char),
}

/// A token together with where it sits in the original statement, so that
/// type names and defaults can be handed back exactly as they were written
/// rather than reassembled from their parts.
#[derive(Debug, Clone)]
struct Tok {
    token: Token,
    start: usize,
    end: usize,
}

impl Token {
    /// The text of a word or quoted identifier, for keyword comparison.
    fn word(&self) -> Option<&str> {
        match self {
            Token::Word(w) => Some(w),
            _ => None,
        }
    }

    fn is_word(&self, keyword: &str) -> bool {
        matches!(self, Token::Word(w) if w.eq_ignore_ascii_case(keyword))
    }

    fn is_punct(&self, c: char) -> bool {
        matches!(self, Token::Punct(p) if *p == c)
    }

    /// The identifier this token names, quoted or not.
    fn identifier(&self) -> Option<String> {
        match self {
            Token::Word(w) => Some(w.clone()),
            Token::Quoted(q) => Some(q.clone()),
            _ => None,
        }
    }

    /// How this token was written, for reassembling type names and
    /// defaults.
    fn text(&self) -> String {
        match self {
            Token::Word(w) => w.clone(),
            Token::Quoted(q) => format!("\"{}\"", q.replace('"', "\"\"")),
            Token::Literal(l) => l.clone(),
            Token::Punct(p) => p.to_string(),
        }
    }
}

fn tokenize(sql: &str) -> Result<Vec<Tok>> {
    let chars: Vec<char> = sql.chars().collect();
    // char index to byte offset, with a sentinel so that the end of the
    // last token is addressable
    let mut offsets: Vec<usize> = sql.char_indices().map(|(i, _)| i).collect();
    offsets.push(sql.len());

    let mut tokens: Vec<Tok> = Vec::new();
    let mut at = 0usize;

    while at < chars.len() {
        let c = chars[at];

        if c.is_whitespace() {
            at += 1;
            continue;
        }
        let began = at;

        // -- to end of line
        if c == '-' && chars.get(at + 1) == Some(&'-') {
            while at < chars.len() && chars[at] != '\n' {
                at += 1;
            }
            continue;
        }

        // /* ... */, which sqlite allows to run to end of input unclosed
        if c == '/' && chars.get(at + 1) == Some(&'*') {
            at += 2;
            while at < chars.len() && !(chars[at] == '*' && chars.get(at + 1) == Some(&'/')) {
                at += 1;
            }
            at = (at + 2).min(chars.len());
            continue;
        }

        // the three quoting styles for identifiers, plus strings
        if let Some((open, close)) = match c {
            '"' => Some(('"', '"')),
            '`' => Some(('`', '`')),
            '[' => Some(('[', ']')),
            _ => None,
        } {
            at += 1;
            let mut value = String::new();
            loop {
                let Some(&ch) = chars.get(at) else {
                    return Err(SqliteError::unsupported(format!(
                        "a create statement has an unterminated {open} quoted name"
                    )));
                };
                at += 1;
                if ch == close {
                    // brackets have no escape; the others double the quote
                    if close != ']' && chars.get(at) == Some(&close) {
                        value.push(close);
                        at += 1;
                        continue;
                    }
                    break;
                }
                value.push(ch);
            }
            tokens.push(Tok {
                token: Token::Quoted(value),
                start: offsets[began],
                end: offsets[at],
            });
            continue;
        }

        if c == '\'' {
            let mut text = String::from("'");
            at += 1;
            loop {
                let Some(&ch) = chars.get(at) else {
                    return Err(SqliteError::unsupported(
                        "a create statement has an unterminated string",
                    ));
                };
                at += 1;
                text.push(ch);
                if ch == '\'' {
                    if chars.get(at) == Some(&'\'') {
                        text.push('\'');
                        at += 1;
                        continue;
                    }
                    break;
                }
            }
            tokens.push(Tok {
                token: Token::Literal(text),
                start: offsets[began],
                end: offsets[at],
            });
            continue;
        }

        if c.is_ascii_digit() || (c == '.' && chars.get(at + 1).is_some_and(|d| d.is_ascii_digit()))
        {
            let start = at;
            while at < chars.len()
                && (chars[at].is_ascii_alphanumeric() || chars[at] == '.' || {
                    // an exponent's sign belongs to the number
                    (chars[at] == '+' || chars[at] == '-')
                        && matches!(chars[at - 1], 'e' | 'E')
                        && chars[start..at].iter().all(|c| *c != 'x' && *c != 'X')
                })
            {
                at += 1;
            }
            tokens.push(Tok {
                token: Token::Literal(chars[start..at].iter().collect()),
                start: offsets[began],
                end: offsets[at],
            });
            continue;
        }

        if c.is_alphabetic() || c == '_' || !c.is_ascii() {
            let start = at;
            while at < chars.len()
                && (chars[at].is_alphanumeric()
                    || chars[at] == '_'
                    || chars[at] == '$'
                    || !chars[at].is_ascii())
            {
                at += 1;
            }
            let word: String = chars[start..at].iter().collect();
            // x'00ff' is a blob literal, not the identifier x
            if (word.eq_ignore_ascii_case("x")) && chars.get(at) == Some(&'\'') {
                let mut text = word;
                text.push('\'');
                at += 1;
                while let Some(&ch) = chars.get(at) {
                    at += 1;
                    text.push(ch);
                    if ch == '\'' {
                        break;
                    }
                }
                tokens.push(Tok {
                    token: Token::Literal(text),
                    start: offsets[began],
                    end: offsets[at],
                });
            } else {
                tokens.push(Tok {
                    token: Token::Word(word),
                    start: offsets[began],
                    end: offsets[at],
                });
            }
            continue;
        }

        tokens.push(Tok {
            token: Token::Punct(c),
            start: offsets[began],
            end: offsets[at + 1],
        });
        at += 1;
    }

    Ok(tokens)
}

// ---------------------------------------------------------------------------
// parser
// ---------------------------------------------------------------------------

/// Words that end a column's type name and begin its constraints.
const CONSTRAINT_WORDS: &[&str] = &[
    "constraint",
    "primary",
    "not",
    "null",
    "unique",
    "check",
    "default",
    "collate",
    "references",
    "generated",
    "as",
];

struct Parser<'a> {
    sql: &'a str,
    tokens: Vec<Tok>,
    at: usize,
}

impl<'a> Parser<'a> {
    fn peek(&self) -> Option<&Token> {
        self.tokens.get(self.at).map(|t| &t.token)
    }

    fn at_word(&self, offset: usize, keyword: &str) -> bool {
        self.tokens
            .get(self.at + offset)
            .is_some_and(|t| t.token.is_word(keyword))
    }

    /// Where the token at `index` starts in the original statement.
    fn start_of(&self, index: usize) -> usize {
        match self.tokens.get(index) {
            Some(t) => t.start,
            None => self.sql.len(),
        }
    }

    /// Where the token before `index` ends.
    fn end_before(&self, index: usize) -> usize {
        match index.checked_sub(1).and_then(|i| self.tokens.get(i)) {
            Some(t) => t.end,
            None => 0,
        }
    }

    /// The original text spanned by the tokens from `from` up to the
    /// current position, exactly as it was written.
    fn text_since(&self, from: usize) -> String {
        let start = self.start_of(from);
        let end = self.end_before(self.at);
        if end <= start {
            String::new()
        } else {
            self.sql[start..end].trim().to_string()
        }
    }

    fn next(&mut self) -> Option<Token> {
        let token = self.tokens.get(self.at).map(|t| t.token.clone());
        if token.is_some() {
            self.at += 1;
        }
        token
    }

    fn eat_word(&mut self, keyword: &str) -> bool {
        if self.peek().is_some_and(|t| t.is_word(keyword)) {
            self.at += 1;
            true
        } else {
            false
        }
    }

    fn eat_punct(&mut self, c: char) -> bool {
        if self.peek().is_some_and(|t| t.is_punct(c)) {
            self.at += 1;
            true
        } else {
            false
        }
    }

    fn expect_word(&mut self, keyword: &str) -> Result<()> {
        if self.eat_word(keyword) {
            Ok(())
        } else {
            Err(self.gap(&format!("expected {keyword}")))
        }
    }

    fn expect_punct(&mut self, c: char) -> Result<()> {
        if self.eat_punct(c) {
            Ok(())
        } else {
            Err(self.gap(&format!("expected {c}")))
        }
    }

    fn identifier(&mut self) -> Result<String> {
        match self.next().and_then(|t| t.identifier()) {
            Some(name) => Ok(name),
            None => Err(self.gap("expected a name")),
        }
    }

    /// A statement this parser cannot read. Not `Malformed`: the file is
    /// fine and SQLite accepted the statement, so this is our gap and the
    /// message says which token we tripped on.
    fn gap(&self, what: &str) -> SqliteError {
        let found = match self.tokens.get(self.at) {
            Some(t) => format!("{:?}", t.token.text()),
            None => "the end of the statement".to_string(),
        };
        SqliteError::unsupported(format!(
            "cannot read this create statement: {what}, found {found} at token {}",
            self.at
        ))
    }

    fn at_constraint_word(&self) -> bool {
        self.peek()
            .and_then(|t| t.word())
            .is_some_and(|w| CONSTRAINT_WORDS.iter().any(|k| w.eq_ignore_ascii_case(k)))
    }

    /// Step over a parenthesised group, returning it exactly as written.
    /// Assumes the opening paren is next.
    fn balanced(&mut self) -> Result<String> {
        let from = self.at;
        self.expect_punct('(')?;
        let mut depth = 1usize;
        while depth > 0 {
            let Some(token) = self.next() else {
                return Err(self.gap("expected a closing paren"));
            };
            if token.is_punct('(') {
                depth += 1;
            } else if token.is_punct(')') {
                depth -= 1;
            }
        }
        Ok(self.text_since(from))
    }

    /// A parenthesised list of column names, with the per column extras
    /// (`collate x`, `asc`, `desc`) stepped over.
    fn column_list(&mut self) -> Result<Vec<String>> {
        self.expect_punct('(')?;
        let mut names = Vec::new();
        loop {
            names.push(self.identifier()?);
            if self.eat_word("collate") {
                self.identifier()?;
            }
            let _ = self.eat_word("asc") || self.eat_word("desc");
            if self.eat_punct(',') {
                continue;
            }
            self.expect_punct(')')?;
            return Ok(names);
        }
    }

    /// Skip to the next comma or closing paren at the top level, which is
    /// how clauses we do not interpret are stepped over.
    fn skip_to_end_of_item(&mut self) -> Result<()> {
        loop {
            match self.peek() {
                None => return Ok(()),
                Some(t) if t.is_punct(',') || t.is_punct(')') => return Ok(()),
                Some(t) if t.is_punct('(') => {
                    self.balanced()?;
                }
                _ => {
                    self.at += 1;
                }
            }
        }
    }

    /// The keyword after a generated column's expression. Absent means
    /// virtual, which is sqlite's default and not a guess.
    fn eat_generated_kind(&mut self) -> Generated {
        if self.eat_word("stored") {
            Generated::Stored
        } else {
            self.eat_word("virtual");
            Generated::Virtual
        }
    }

    /// `on conflict <action>`, which may follow several constraints.
    fn eat_conflict_clause(&mut self) {
        if self.at_word(0, "on") && self.at_word(1, "conflict") {
            self.at += 2;
            if self.peek().and_then(|t| t.word()).is_some() {
                self.at += 1;
            }
        }
    }
}

/// Parse a `create table` statement into the shape of the table it makes.
pub fn parse_create_table(sql: &str) -> Result<TableDef> {
    let tokens = tokenize(sql)?;
    let mut parser = Parser { sql, tokens, at: 0 };

    parser.expect_word("create")?;
    // temp tables never appear in a database file's schema, but the
    // statement is legal and costs one line to accept
    let _ = parser.eat_word("temp") || parser.eat_word("temporary");
    parser.expect_word("table")?;
    if parser.eat_word("if") {
        parser.expect_word("not")?;
        parser.expect_word("exists")?;
    }

    let mut name = parser.identifier()?;
    if parser.eat_punct('.') {
        // schema qualified: the part after the dot is the table
        name = parser.identifier()?;
    }

    if parser.peek().is_some_and(|t| t.is_word("as")) {
        return Err(SqliteError::unsupported(format!(
            "{name} was created by `create table ... as select`, so the file does not record \
             its column types; this reader cannot import it"
        )));
    }

    parser.expect_punct('(')?;

    let mut columns: Vec<ColumnDef> = Vec::new();
    let mut primary_key: Vec<KeyColumn> = Vec::new();
    let mut constraints: Vec<Constraint> = Vec::new();
    let mut inline_pk_desc = false;

    loop {
        if parser.peek().is_none() {
            return Err(parser.gap("the column list is not closed"));
        }
        if parser.eat_punct(')') {
            break;
        }

        // a table constraint, rather than another column
        let is_table_constraint = {
            // a table constraint may carry a name, in which case the
            // keyword that says which kind it is comes two tokens later
            let offset = if parser.at_word(0, "constraint") {
                2
            } else {
                0
            };
            parser.at_word(offset, "primary")
                || parser.at_word(offset, "unique")
                || parser.at_word(offset, "check")
                || parser.at_word(offset, "foreign")
        };

        if is_table_constraint {
            if parser.eat_word("constraint") {
                parser.identifier()?;
            }
            if parser.eat_word("primary") {
                parser.expect_word("key")?;
                if !primary_key.is_empty() {
                    return Err(SqliteError::unsupported(format!(
                        "{name} declares more than one primary key"
                    )));
                }
                parser.expect_punct('(')?;
                loop {
                    let column = parser.identifier()?;
                    if parser.eat_word("collate") {
                        parser.identifier()?;
                    }
                    let descending = if parser.eat_word("desc") {
                        true
                    } else {
                        parser.eat_word("asc");
                        false
                    };
                    primary_key.push(KeyColumn {
                        name: column,
                        descending,
                    });
                    if parser.eat_punct(',') {
                        continue;
                    }
                    parser.expect_punct(')')?;
                    break;
                }
                parser.eat_conflict_clause();
            } else if parser.eat_word("unique") {
                let columns = parser.column_list()?;
                constraints.push(Constraint::Unique { columns });
                parser.eat_conflict_clause();
                parser.skip_to_end_of_item()?;
            } else if parser.eat_word("check") {
                let expression = parser.balanced()?;
                constraints.push(Constraint::Check { expression });
                parser.skip_to_end_of_item()?;
            } else if parser.eat_word("foreign") {
                parser.expect_word("key")?;
                let columns = parser.column_list()?;
                let references = if parser.eat_word("references") {
                    parser.identifier()?
                } else {
                    String::new()
                };
                constraints.push(Constraint::ForeignKey {
                    columns,
                    references,
                });
                parser.skip_to_end_of_item()?;
            } else {
                parser.skip_to_end_of_item()?;
            }
        } else {
            let column_name = parser.identifier()?;
            let type_from = parser.at;
            while !parser.at_constraint_word() {
                match parser.peek() {
                    None => break,
                    Some(t) if t.is_punct(',') || t.is_punct(')') => break,
                    Some(t) if t.is_punct('(') => {
                        parser.balanced()?;
                    }
                    Some(_) => {
                        parser.at += 1;
                    }
                }
            }
            let declared_type = parser.text_since(type_from);

            let mut column = ColumnDef {
                name: column_name,
                declared_type: if declared_type.is_empty() {
                    None
                } else {
                    Some(declared_type)
                },
                not_null: false,
                default: None,
                generated: Generated::No,
            };

            // column constraints, in any order and any number
            loop {
                if parser.eat_word("constraint") {
                    parser.identifier()?;
                    continue;
                }
                if parser.eat_word("primary") {
                    parser.expect_word("key")?;
                    if !primary_key.is_empty() {
                        return Err(SqliteError::unsupported(format!(
                            "{name} declares more than one primary key"
                        )));
                    }
                    let descending = if parser.eat_word("desc") {
                        true
                    } else {
                        parser.eat_word("asc");
                        false
                    };
                    inline_pk_desc = descending;
                    primary_key.push(KeyColumn {
                        name: column.name.clone(),
                        descending,
                    });
                    parser.eat_conflict_clause();
                    parser.eat_word("autoincrement");
                    continue;
                }
                if parser.eat_word("not") {
                    parser.expect_word("null")?;
                    column.not_null = true;
                    parser.eat_conflict_clause();
                    continue;
                }
                if parser.eat_word("null") {
                    parser.eat_conflict_clause();
                    continue;
                }
                if parser.eat_word("unique") {
                    constraints.push(Constraint::Unique {
                        columns: vec![column.name.clone()],
                    });
                    parser.eat_conflict_clause();
                    continue;
                }
                if parser.eat_word("check") {
                    let expression = parser.balanced()?;
                    constraints.push(Constraint::Check { expression });
                    continue;
                }
                if parser.eat_word("default") {
                    let from = parser.at;
                    if parser.peek().is_some_and(|t| t.is_punct('(')) {
                        parser.balanced()?;
                    } else {
                        // a literal, a keyword like current_timestamp, or a
                        // signed number
                        let _ = parser.eat_punct('-') || parser.eat_punct('+');
                        if parser.next().is_none() {
                            return Err(parser.gap("expected a default value"));
                        }
                    }
                    column.default = Some(parser.text_since(from));
                    continue;
                }
                if parser.eat_word("collate") {
                    let name = parser.identifier()?;
                    // binary is sqlite's default and changes nothing
                    if !name.eq_ignore_ascii_case("binary") {
                        constraints.push(Constraint::Collation {
                            column: column.name.clone(),
                            name,
                        });
                    }
                    continue;
                }
                if parser.eat_word("references") {
                    let references = parser.identifier().unwrap_or_default();
                    constraints.push(Constraint::ForeignKey {
                        columns: vec![column.name.clone()],
                        references,
                    });
                    parser.skip_to_end_of_item()?;
                    continue;
                }
                // `generated always as (...)` and the shorthand `as (...)`
                // mean the same thing, and neither keyword after the
                // expression means virtual
                if parser.eat_word("generated") {
                    parser.expect_word("always")?;
                    parser.expect_word("as")?;
                    parser.balanced()?;
                    column.generated = parser.eat_generated_kind();
                    continue;
                }
                if parser.peek().is_some_and(|t| t.is_word("as")) {
                    parser.at += 1;
                    parser.balanced()?;
                    column.generated = parser.eat_generated_kind();
                    continue;
                }
                break;
            }

            columns.push(column);
        }

        if parser.eat_punct(',') {
            continue;
        }
        parser.expect_punct(')')?;
        break;
    }

    // table options: without rowid and strict, in either order
    let mut without_rowid = false;
    let mut strict = false;
    loop {
        if parser.eat_word("without") {
            parser.expect_word("rowid")?;
            without_rowid = true;
        } else if parser.eat_word("strict") {
            strict = true;
        } else {
            break;
        }
        if !parser.eat_punct(',') {
            break;
        }
    }
    let _ = parser.eat_punct(';');

    if columns.is_empty() {
        return Err(SqliteError::unsupported(format!(
            "{name} was parsed as having no columns"
        )));
    }
    for key in &primary_key {
        if !columns
            .iter()
            .any(|c| c.name.eq_ignore_ascii_case(&key.name))
        {
            return Err(SqliteError::unsupported(format!(
                "{name} has a primary key over {}, which is not one of its columns",
                key.name
            )));
        }
    }

    Ok(TableDef {
        name,
        columns,
        primary_key,
        without_rowid,
        strict,
        unsupported_constraints: constraints,
        inline_pk_desc,
    })
}