boxdb 0.3.2

An extended SQLite client with typed columns and readable storage
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
//! Normalize table declarations while leaving SQLite constraints and expressions
//! to SQLite. The SQL parser's AST does not cover every SQLite table option.
use super::{Alteration, Declaration, translate_column};
use crate::{Error, Result, Type};
use rusqlite::Connection;
use sqlparser::{
    ast::{ColumnDef, DataType, Ident, ObjectName},
    dialect::SQLiteDialect,
    tokenizer::{Token, Tokenizer},
};

fn word(token: &Token, text: &str) -> bool {
    matches!(token, Token::Word(w) if w.quote_style.is_none() && w.value.eq_ignore_ascii_case(text))
}
fn identifier(token: &Token) -> Result<String> {
    match token {
        Token::Word(w) => Ok(w.value.clone()),
        Token::SingleQuotedString(s) => Ok(s.clone()),
        _ => Err(Error::new("expected table or column name".into())),
    }
}
fn render(tokens: &[Token]) -> String {
    tokens
        .iter()
        .map(|token| match token {
            Token::Word(w) if w.quote_style == Some('"') => super::quote(&w.value),
            Token::Word(w) if w.quote_style == Some('`') => {
                format!("`{}`", w.value.replace('`', "``"))
            }
            Token::SingleQuotedString(s) => format!("'{}'", s.replace('\'', "''")),
            Token::HexStringLiteral(s) => format!("X'{}'", s.replace('\'', "''")),
            _ => token.to_string(),
        })
        .collect::<Vec<_>>()
        .join(" ")
}
fn constraint(token: &Token) -> bool {
    [
        "CONSTRAINT",
        "PRIMARY",
        "NOT",
        "NULL",
        "UNIQUE",
        "CHECK",
        "DEFAULT",
        "COLLATE",
        "REFERENCES",
        "GENERATED",
        "AS",
    ]
    .iter()
    .any(|s| word(token, s))
}
fn column(tokens: &[Token]) -> Result<(String, Option<(String, Type)>)> {
    let Some(first) = tokens.first() else {
        return Err(Error::new("empty column definition".into()));
    };
    if ["CONSTRAINT", "PRIMARY", "UNIQUE", "CHECK", "FOREIGN"]
        .iter()
        .any(|s| word(first, s))
    {
        return Ok((render(tokens), None));
    }
    let name = identifier(first)?;
    super::validate_column_name(&name)?;
    let mut end = 1;
    let mut depth = 0usize;
    while let Some(token) = tokens.get(end) {
        if depth == 0 && constraint(token) {
            break;
        }
        match token {
            Token::LParen => depth += 1,
            Token::RParen => depth = depth.saturating_sub(1),
            _ => (),
        }
        end += 1;
    }
    let mut datatype = String::new();
    for token in &tokens[1..end] {
        match token {
            Token::Word(w) if w.quote_style == Some('[') && w.value.trim().is_empty() => {
                datatype.push_str("[]")
            }
            Token::Word(w) => {
                if !datatype.is_empty() {
                    datatype.push(' ');
                }
                datatype.push_str(&w.value);
            }
            Token::SingleQuotedString(s) => {
                if datatype.is_empty() {
                    // SQLite also accepts a quoted type name, e.g. 'INT64'.
                    datatype.push_str(s);
                } else {
                    datatype.push(' ');
                    datatype.push_str(&format!("'{}'", s.replace('\'', "''")));
                }
            }
            other => datatype.push_str(&other.to_string()),
        }
    }
    let mut definition = ColumnDef {
        name: Ident::new(&name),
        data_type: DataType::Custom(ObjectName(vec![Ident::new(datatype)]), vec![]),
        collation: None,
        options: vec![],
    };
    let mut depth = 0usize;
    for (i, token) in tokens.iter().enumerate().skip(end) {
        if depth == 0 && word(token, "COLLATE") {
            let name = tokens
                .get(i + 1)
                .ok_or_else(|| Error::new("missing collation".into()))?;
            definition.collation = Some(ObjectName(vec![Ident::new(identifier(name)?)]));
        }
        match token {
            Token::LParen => depth += 1,
            Token::RParen => depth = depth.saturating_sub(1),
            _ => (),
        }
    }
    let logical = translate_column(&mut definition)?;
    if ![
        "INT", "INTEGER", "REAL", "TEXT", "BLOB", "ANY", "DECIMAL", "UUID", "INSTANT", "DATE",
        "DATETIME", "INTERVAL", "UINT8", "UINT16", "UINT32", "UINT64",
    ]
    .iter()
    .any(|native| {
        definition
            .data_type
            .to_string()
            .eq_ignore_ascii_case(native)
    }) {
        return Err(Error::new(format!(
            "unsupported column type for {name}: {}",
            definition.data_type
        )));
    }
    let mut output = format!("{} {}", render(&tokens[..1]), definition.data_type);
    if end < tokens.len() {
        output.push(' ');
        output.push_str(&render(&tokens[end..]));
    }
    for option in definition.options {
        output.push(' ');
        output.push_str(&option.to_string());
    }
    Ok((output, logical.map(|ty| (name, ty))))
}
fn tokens(sql: &str) -> Result<Vec<Token>> {
    Ok(Tokenizer::new(&SQLiteDialect {}, sql)
        .tokenize()
        .map_err(Error::validation)?
        .into_iter()
        .filter(|t| !matches!(t, Token::Whitespace(_) | Token::SemiColon))
        .collect())
}

// Preserve literal tokens when adding a column: the generic AST accepts
// non-string ENUM members and renders them as strings, losing that distinction.
pub(super) fn addition(sql: &str) -> Result<Option<Alteration>> {
    let tokens = tokens(sql)?;
    if tokens.len() < 4 || !word(&tokens[0], "ALTER") || !word(&tokens[1], "TABLE") {
        return Ok(None);
    }
    let first = identifier(&tokens[2])?;
    let mut position = 3;
    let (schema, table) = if tokens.get(position) == Some(&Token::Period) {
        position += 2;
        let table = identifier(
            tokens
                .get(position - 1)
                .ok_or_else(|| Error::new("missing table name".into()))?,
        )?;
        (first, table)
    } else {
        ("main".into(), first)
    };
    if !tokens.get(position).is_some_and(|t| word(t, "ADD")) {
        return Ok(None);
    }
    position += 1;
    if tokens.get(position).is_some_and(|t| word(t, "COLUMN")) {
        position += 1;
    }
    let (definition, logical) = column(&tokens[position..])?;
    let main = schema.eq_ignore_ascii_case("main");
    if !main
        && logical
            .as_ref()
            .is_some_and(|(_, ty)| ty.native_kind().is_none())
    {
        return Err(Error::new(
            "extended types require main database tables".into(),
        ));
    }
    Ok(Some(Alteration {
        main,
        sql: format!("{} {definition}", render(&tokens[..position])),
        table,
        columns: if main {
            logical.into_iter().collect()
        } else {
            vec![]
        },
        changes: vec![],
    }))
}

pub(super) fn declaration(sql: &str) -> Result<Option<Declaration>> {
    let tokens = tokens(sql)?;
    let mut position = 0;
    let mut take = |text: &str| {
        if tokens.get(position).is_some_and(|t| word(t, text)) {
            position += 1;
            true
        } else {
            false
        }
    };
    if !take("CREATE") {
        return Ok(None);
    }
    let temporary = take("TEMP") || take("TEMPORARY");
    if !take("TABLE") {
        return Ok(None);
    }
    if take("IF") && !(take("NOT") && take("EXISTS")) {
        return Err(Error::new("expected IF NOT EXISTS".into()));
    }
    let first = identifier(
        tokens
            .get(position)
            .ok_or_else(|| Error::new("missing table name".into()))?,
    )?;
    position += 1;
    let (schema, table) = if tokens.get(position) == Some(&Token::Period) {
        position += 1;
        let table = identifier(
            tokens
                .get(position)
                .ok_or_else(|| Error::new("missing table name".into()))?,
        )?;
        position += 1;
        (first, table)
    } else {
        (
            if temporary {
                "temp".into()
            } else {
                "main".into()
            },
            first,
        )
    };
    if temporary && !schema.eq_ignore_ascii_case("temp") {
        return Err(Error::new(
            "temporary tables must use the temp schema".into(),
        ));
    }
    let header = render(&tokens[..position]);
    if tokens.get(position).is_some_and(|t| word(t, "AS")) {
        return Ok(Some(Declaration {
            sql: sql.into(),
            schema,
            table,
            columns: vec![],
            select: true,
        }));
    }
    if tokens.get(position) != Some(&Token::LParen) {
        return Err(Error::new("CREATE TABLE requires a column list".into()));
    }
    position += 1;
    let mut start = position;
    let mut depth = 0usize;
    let mut definitions = vec![];
    let mut columns = vec![];
    loop {
        let token = tokens
            .get(position)
            .ok_or_else(|| Error::new("unclosed table definition".into()))?;
        if depth == 0 && matches!(token, Token::Comma | Token::RParen) {
            let (definition, logical) = column(&tokens[start..position])?;
            definitions.push(definition);
            if let Some(logical) = logical {
                columns.push(logical);
            }
            if token == &Token::RParen {
                position += 1;
                break;
            }
            start = position + 1;
        } else {
            match token {
                Token::LParen => depth += 1,
                Token::RParen => depth = depth.saturating_sub(1),
                _ => (),
            }
        }
        position += 1;
    }
    if !schema.eq_ignore_ascii_case("main") {
        // Native decimals carry their identity and constraints in SQLite.
        columns.retain(|(_, ty)| ty.native_kind().is_none());
        if !columns.is_empty() {
            return Err(Error::new(
                "extended types require main database tables".into(),
            ));
        }
    }
    let mut without_rowid = false;
    while position < tokens.len() {
        if word(&tokens[position], "STRICT") {
            position += 1;
        } else if word(&tokens[position], "WITHOUT")
            && tokens.get(position + 1).is_some_and(|t| word(t, "ROWID"))
        {
            without_rowid = true;
            position += 2;
        } else {
            return Err(Error::new("unsupported SQLite table option".into()));
        }
        if position < tokens.len() {
            if tokens[position] != Token::Comma {
                return Err(Error::new("table options must be comma-separated".into()));
            }
            position += 1;
            if position == tokens.len() {
                return Err(Error::new("missing table option".into()));
            }
        }
    }
    Ok(Some(Declaration {
        sql: format!(
            "{header} ({}) STRICT{}",
            definitions.join(","),
            if without_rowid { ", WITHOUT ROWID" } else { "" }
        ),
        schema,
        table,
        columns,
        select: false,
    }))
}

impl Declaration {
    pub fn execute(&self, db: &Connection) -> Result<()> {
        db.execute_batch(&self.sql)?;
        if !self.select {
            return Ok(());
        }
        // Let SQLite infer CTAS columns and evaluate the query once. Replace the
        // newly created table inside the caller's savepoint before publishing.
        let columns = db
            .prepare("SELECT name,type FROM pragma_table_xinfo(?1,?2) ORDER BY cid")?
            .query_map([&self.table, &self.schema], |r| {
                Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?))
            })?
            .collect::<rusqlite::Result<Vec<_>>>()?;
        for (name, _) in &columns {
            super::validate_column_name(name)?;
        }
        let mut index = 0usize;
        let scratch = loop {
            let name = format!("__boxdb_ctas_{index}");
            if !db.query_row(
                &format!(
                    "SELECT EXISTS(SELECT 1 FROM {}.sqlite_schema WHERE name=?1 COLLATE NOCASE)",
                    super::quote(&self.schema)
                ),
                [&name],
                |r| r.get::<_, bool>(0),
            )? {
                break name;
            }
            index += 1;
        };
        let target = format!(
            "{}.{}",
            super::quote(&self.schema),
            super::quote(&self.table)
        );
        let source = format!("{}.{}", super::quote(&self.schema), super::quote(&scratch));
        let definitions = columns
            .iter()
            .map(|(name, datatype)| {
                let datatype = match datatype.to_ascii_uppercase().as_str() {
                    "INT" | "INTEGER" => "INT",
                    "REAL" => "REAL",
                    "TEXT" => "TEXT",
                    "BLOB" => "BLOB",
                    "DECIMAL" => "DECIMAL",
                    "UUID" => "UUID",
                    "INSTANT" => "INSTANT",
                    "DATE" => "DATE",
                    "DATETIME" => "DATETIME",
                    "INTERVAL" => "INTERVAL",
                    _ => "ANY",
                };
                format!("{} {datatype}", super::quote(name))
            })
            .collect::<Vec<_>>()
            .join(",");
        let order = ["_rowid_", "rowid", "oid"]
            .into_iter()
            .find(|alias| {
                !columns
                    .iter()
                    .any(|(name, _)| name.eq_ignore_ascii_case(alias))
            })
            .map(|alias| format!(" ORDER BY {}", super::quote(alias)))
            .unwrap_or_default();
        // CTAS has no indexes. If all aliases are shadowed, its plain table scan
        // retains insertion order without sorting a user-defined rowid column.
        db.execute_batch(&format!("ALTER TABLE {target} RENAME TO {}; CREATE TABLE {target}({definitions}) STRICT; INSERT INTO {target} SELECT * FROM {source}{order}; DROP TABLE {source}", super::quote(&scratch)))?;
        Ok(())
    }
}