sqlitepipe 0.2.1

A simple tool for piping the output of a command into sqlite databases.
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
//! A library for managing SQLite tables dynamically and performing streaming data insertions.

use std::{
    collections::HashSet,
    io::{BufRead, Read, Write},
};

use log::debug;
use rusqlite::{Connection, TEMP_DB, params_from_iter};
use snafu::{ResultExt, Snafu};

use crate::{column::Column, sanitizing::SqlSanitize};

pub mod column;
pub mod sanitizing;
mod stmt;

#[cfg(feature = "tx")]
pub mod txmgmt;

/// Errors that can occur during database schema management or data insertion.
#[derive(Debug, Snafu)]
pub enum Error {
    /// A rusqlite error occurred within a specific context.
    #[snafu(display("Database error (context: {context}): {source}"))]
    DatabaseErrorContext {
        source: rusqlite::Error,
        context: &'static str,
    },

    /// A general rusqlite error.
    #[snafu(display("Database error: {source}"))]
    WhateverDatabaseError { source: rusqlite::Error },

    /// Failed to read from the input.
    #[snafu(display("Read error: {source}"))]
    SourceReadError { source: std::io::Error },

    /// Failed to write chunked data to a SQLite BLOB.
    #[snafu(display("Blob write error: {source}"))]
    BlobWriteError { source: std::io::Error },

    /// An error of unknown origin.
    #[snafu(display("Unknown error: {message}"))]
    Unknown { message: String },
}

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, PartialEq, Eq)]
pub enum StdinMode {
    None,
    Blob,
    Lines,
}

/// Prepares the database schema by creating tables and synchronizing columns.
///
/// If `reset` is true, the existing table is dropped and recreated.
pub fn prepare_db(
    conn: &Connection,
    table_name: &str,
    reset: bool,
    columns: &[Column],
) -> Result<()> {
    let sanitized_table_name = table_name.to_sql_sanitized_string();

    if reset {
        conn.execute(&stmt::drop_table_if_exists(&sanitized_table_name), [])
            .context(DatabaseErrorContextSnafu {
                context: "drop table",
            })?;
    }

    let sanitized_column_names: Vec<_> = columns.iter().map(|v| v.sanitized_name()).collect();

    conn.execute(
        &stmt::create_table_if_not_exists(&sanitized_table_name, &sanitized_column_names),
        [],
    )
    .context(DatabaseErrorContextSnafu {
        context: "create table",
    })?;

    let mut statement =
        conn.prepare(stmt::select_pragram_table_info())
            .context(DatabaseErrorContextSnafu {
                context: "sync columns",
            })?;
    let existing_columns: std::result::Result<HashSet<String>, _> = statement
        .query_map([table_name], |row| row.get(0))
        .context(WhateverDatabaseSnafu)?
        .collect();
    let existing_columns = existing_columns.context(WhateverDatabaseSnafu)?;

    for (col_name, san_col_name) in columns
        .iter()
        .zip(sanitized_column_names.iter())
        .filter(|&(n, _)| !existing_columns.contains(n.name()))
    {
        debug!("adding column {col_name:?}");
        conn.execute(
            &stmt::alter_table_add_column(&sanitized_table_name, &san_col_name),
            [],
        )
        .context(DatabaseErrorContextSnafu {
            context: "add column",
        })?;
    }

    Ok(())
}

/// Inserts a single row of raw data into the specified table.
pub fn insert_row(conn: &Connection, table_name: &str, columns: &[Column]) -> Result<()> {
    let sanitized_table_name = table_name.to_sql_sanitized_string();
    let mut statement = conn
        .prepare(&stmt::insert_row(&sanitized_table_name, &columns))
        .context(DatabaseErrorContextSnafu {
            context: "insert row",
        })?;

    debug!("executing {statement:?} with values {columns:?}");

    statement
        .execute(params_from_iter(columns.iter().map(|n| n.raw_value())))
        .context(DatabaseErrorContextSnafu {
            context: "insert row",
        })?;

    Ok(())
}

/// Inserts binary data from standard input into the table.
///
/// This reads `source` in chunks, stores them in a temporary table, and then aggregates
/// them into the target BLOB column using SQLite's `group_concat`.
pub fn insert_blob(
    conn: &Connection,
    table_name: &str,
    columns: &[Column],
    mut source: impl Read,
) -> Result<()> {
    conn.execute(stmt::create_temporary_blob_table(), [])
        .context(DatabaseErrorContextSnafu {
            context: "insert blob",
        })?;

    let mut total_size = 0usize;
    {
        let mut buf = vec![0; stmt::BLOB_BUF_SIZE];

        loop {
            let count = source.read(&mut buf).context(SourceReadSnafu)?;
            total_size += count;
            debug!("read {count} to a total of {total_size}");
            if count == 0 {
                break;
            }

            conn.execute(&stmt::insert_zero_blob(count), []).context(
                DatabaseErrorContextSnafu {
                    context: "insert blob",
                },
            )?;
            let rowid = conn.last_insert_rowid();
            let mut blob = conn
                .blob_open(TEMP_DB, "blob_insert", "data", rowid, false)
                .context(DatabaseErrorContextSnafu {
                    context: "open blob",
                })?;

            blob.write_all(&buf[..count]).context(BlobWriteSnafu)?;
        }
    }
    debug!("read a total of {total_size} bytes");

    let sanitized_table_name = table_name.to_sql_sanitized_string();

    let mut statement = conn
        .prepare(&stmt::insert_blob(
            &sanitized_table_name,
            &columns,
            total_size,
        ))
        .context(DatabaseErrorContextSnafu {
            context: "insert blob",
        })?;

    let values = columns.iter().filter_map(|n| n.value());

    debug!("executing {statement:?} with values {columns:?}");

    statement
        .execute(params_from_iter(values))
        .context(DatabaseErrorContextSnafu {
            context: "insert blob",
        })?;

    Ok(())
}

/// Reads lines from a source and inserts each as a row into the table.
pub fn insert_lines(
    conn: &Connection,
    table_name: &str,
    columns: &[Column],
    source: impl BufRead,
) -> Result<()> {
    let sanitized_table_name = table_name.to_sql_sanitized_string();

    let mut statement = conn
        .prepare(&stmt::insert_row(&sanitized_table_name, &columns))
        .context(DatabaseErrorContextSnafu {
            context: "insert lines",
        })?;

    debug!("prepared statement {statement:?} with values {columns:?}");

    for line in source.lines() {
        let line = line.context(SourceReadSnafu)?;
        debug!("executing statement with line {line:?}");
        statement
            .execute(params_from_iter(
                columns.iter().map(|v| v.value_or_line(&line)),
            ))
            .context(DatabaseErrorContextSnafu {
                context: "insert lines",
            })?;
    }

    Ok(())
}

#[cfg(test)]
mod test {
    use std::io::{Cursor, Read};

    use rusqlite::Connection;

    use super::*;
    use crate::Column;

    static INIT: std::sync::Once = std::sync::Once::new();

    struct InterruptibleCursor<const N: usize> {
        cursor: Cursor<[u8; N]>,
        interrupt_after: u64,
    }

    impl<const N: usize> Read for InterruptibleCursor<N> {
        fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
            if self.cursor.position() < self.interrupt_after {
                let bytes_to_read =
                    (buf.len() as u64).min(self.interrupt_after - self.cursor.position()) as usize;
                self.cursor.read(&mut buf[..bytes_to_read])
            } else {
                self.cursor.read(buf)
            }
        }
    }

    pub fn init() {
        INIT.call_once(|| {
            env_logger::builder()
                .filter_level(log::LevelFilter::Debug)
                .try_init()
                .expect("expected env logger to init");
        });
    }

    #[test]
    fn test_insert_blob_simple() {
        init();
        let db = Connection::open_in_memory().unwrap();

        let columns = vec![Column::blob_column("blob")];

        prepare_db(&db, "test", true, &columns).unwrap();

        let source_bytes = "Hello World".as_bytes();
        let source = Cursor::new(source_bytes);

        insert_blob(&db, "test", &columns, source).unwrap();

        let blob_type: String = db
            .query_one("SELECT typeof(blob) from test;", [], |row| row.get(0))
            .unwrap();

        assert_eq!(&blob_type, "blob");

        let blob_content: String = db
            .query_one("SELECT cast(blob as text) from test;", [], |row| row.get(0))
            .unwrap();

        assert_eq!(&blob_content, "Hello World");
    }

    #[test]
    fn test_insert_blob_with_zero() {
        init();
        let db = Connection::open_in_memory().unwrap();

        let columns = vec![Column::blob_column("blob")];

        prepare_db(&db, "test", true, &columns).unwrap();

        let mut source_bytes = "Hello World".as_bytes().to_vec();
        source_bytes[5] = 0;

        let test_source_bytes = source_bytes.clone();

        let source = Cursor::new(source_bytes);

        insert_blob(&db, "test", &columns, source).unwrap();

        let blob_type: String = db
            .query_one("SELECT typeof(blob) from test;", [], |row| row.get(0))
            .unwrap();

        assert_eq!(&blob_type, "blob");

        let blob_length: i64 = db
            .query_one("SELECT length(blob) from test;", [], |row| row.get(0))
            .unwrap();

        assert_eq!(blob_length, 11);
        let blob_data: Vec<u8> = db
            .query_one("SELECT blob from test;", [], |row| row.get(0))
            .unwrap();

        assert_eq!(&blob_data, &test_source_bytes);
    }

    #[test]
    fn test_insert_blob_read_smaller_than_buf() {
        init();
        let db = Connection::open_in_memory().unwrap();

        let columns = vec![Column::blob_column("blob")];

        prepare_db(&db, "test", true, &columns).unwrap();

        let mut source_bytes = [0u8; 8192];
        for i in 0..source_bytes.len() {
            source_bytes[i] = (i % 256) as u8;
        }

        let source = Cursor::new(source_bytes);

        let interruptible_source = InterruptibleCursor {
            cursor: source,
            interrupt_after: 5,
        };

        insert_blob(&db, "test", &columns, interruptible_source).unwrap();

        let blob_type: String = db
            .query_one("SELECT typeof(blob) from test;", [], |row| row.get(0))
            .unwrap();

        assert_eq!(&blob_type, "blob");

        let blob_length: i64 = db
            .query_one("SELECT length(blob) from test;", [], |row| row.get(0))
            .unwrap();

        assert_eq!(blob_length, source_bytes.len() as i64);

        let blob_data: Vec<u8> = db
            .query_one("SELECT blob from test;", [], |row| row.get(0))
            .unwrap();

        assert_eq!(&blob_data, &source_bytes)
    }

    #[test]
    fn test_insert_lines_simple() {
        init();
        let db = Connection::open_in_memory().unwrap();

        let columns = vec![Column::line_column("line")];

        prepare_db(&db, "test", true, &columns).unwrap();

        let source_bytes = "Hello World\nHello World".as_bytes();
        let source = Cursor::new(source_bytes);

        insert_lines(&db, "test", &columns, source).unwrap();

        let col_type: String = db
            .query_one("SELECT DISTINCT typeof(line) from test;", [], |row| {
                row.get(0)
            })
            .unwrap();

        assert_eq!(&col_type, "text");

        let mut stmt = db.prepare("SELECT line from test;").unwrap();
        let rows: std::result::Result<Vec<String>, _> =
            stmt.query_map([], |row| row.get(0)).unwrap().collect();

        let rows = rows.unwrap();

        assert_eq!(
            rows,
            vec!["Hello World".to_string(), "Hello World".to_string()]
        );
    }

    #[test]
    fn test_insert_raw_values() {
        init();
        let db = Connection::open_in_memory().unwrap();

        let columns = vec![
            Column::raw_column("test1", "value1"),
            Column::raw_column("test2", "value2"),
        ];

        prepare_db(&db, "test", true, &columns).unwrap();

        insert_row(&db, "test", &columns).unwrap();

        let col1_type: String = db
            .query_one("SELECT DISTINCT typeof(test1) from test;", [], |row| {
                row.get(0)
            })
            .unwrap();

        assert_eq!(&col1_type, "text");

        let col2_type: String = db
            .query_one("SELECT DISTINCT typeof(test1) from test;", [], |row| {
                row.get(0)
            })
            .unwrap();

        assert_eq!(&col2_type, "text");

        let values: (String, String) = db
            .query_one("SELECT test1, test2 from test;", [], |row| {
                Ok((row.get(0).unwrap(), row.get(1).unwrap()))
            })
            .unwrap();

        assert_eq!(values, ("value1".to_string(), "value2".to_string()));
    }

    #[test]
    fn test_insert_jsonb_value() {
        init();
        let db = Connection::open_in_memory().unwrap();

        let column = Column::blob_column("json").set_json(true);
        let columns = vec![column];

        prepare_db(&db, "test", true, &columns).unwrap();

        let source_bytes = "{\"hello\": [1, 2, \"World\"]}".as_bytes();
        let source = Cursor::new(source_bytes);

        insert_blob(&db, "test", &columns, source).unwrap();

        let blob_type: String = db
            .query_one("SELECT typeof(json) from test;", [], |row| row.get(0))
            .unwrap();

        assert_eq!(&blob_type, "blob");

        let json_value: String = db
            .query_one("SELECT json ->> '$.hello[2]' from test;", [], |row| {
                row.get(0)
            })
            .unwrap();

        assert_eq!(&json_value, "World");
    }

    #[test]
    fn test_insert_jsonb_lines() {
        init();
        let db = Connection::open_in_memory().unwrap();

        let column = Column::line_column("json").set_json(true);
        let columns = vec![column];

        prepare_db(&db, "test", true, &columns).unwrap();

        let source_bytes =
            "{\"hello\": [1, 2, \"World\"]}\n{\"hello\": [1, 2, \"World2\"]}".as_bytes();
        let source = Cursor::new(source_bytes);

        insert_lines(&db, "test", &columns, source).unwrap();

        let blob_type: String = db
            .query_one("SELECT DISTINCT typeof(json) from test;", [], |row| {
                row.get(0)
            })
            .unwrap();

        assert_eq!(&blob_type, "blob");

        let count: i64 = db
            .query_one("SELECT count(json) from test;", [], |row| row.get(0))
            .unwrap();

        assert_eq!(count, 2);

        let json_value: String = db
            .query_one(
                "SELECT group_concat(json ->> '$.hello[2]') from test;",
                [],
                |row| row.get(0),
            )
            .unwrap();

        assert_eq!(&json_value, "World,World2");
    }

    #[test]
    fn test_insert_invalid_jsonb_value() {
        init();
        let db = Connection::open_in_memory().unwrap();

        let column = Column::blob_column("json").set_json(true);
        let columns = vec![column];

        prepare_db(&db, "test", true, &columns).unwrap();

        let source_bytes = "{\"hello\": [invalid}".as_bytes();
        let source = Cursor::new(source_bytes);

        let err = insert_blob(&db, "test", &columns, source).expect_err("should throw");

        let Error::DatabaseErrorContext { source, context: _ } = err else {
            panic!("expected database error")
        };

        debug!("sqlite error: {source:?}");

        assert!(source.sqlite_error().is_some());
    }
}