Skip to main content

rs_tar2sqlite/
tar2sqlite.rs

1use std::io;
2use std::path::Path;
3
4use io::Read;
5
6use rusqlite::Connection;
7use rusqlite::Transaction;
8
9use crate::core::TarEntry;
10use crate::sqlite::Stmt;
11use crate::tar::reader2tar2entries2wtr;
12
13pub fn reader2tar2entries2sqlite<R>(
14    rdr: R,
15    csql: &str, // create sql
16    isql: &str, // insert sql
17    con: &mut Connection,
18    limit: u64,
19) -> Result<(), io::Error>
20where
21    R: Read,
22{
23    con.execute(csql, []).map_err(io::Error::other)?;
24    let tx: Transaction = con.transaction().map_err(io::Error::other)?;
25    {
26        let stmt = tx.prepare(isql).map_err(io::Error::other)?;
27
28        let mut statement = Stmt(stmt);
29        reader2tar2entries2wtr(
30            rdr,
31            &mut |ent: &TarEntry| {
32                statement.upsert(ent)?;
33                Ok(())
34            },
35            limit,
36        )?;
37    }
38    tx.commit().map_err(io::Error::other)?;
39    Ok(())
40}
41
42pub fn stdin2tar2entries2sqlite2fs<P>(
43    csql: &str, // create sql
44    isql: &str, // upsert sql
45    dbpath: P,
46    limit: u64,
47) -> Result<(), io::Error>
48where
49    P: AsRef<Path>,
50{
51    let mut con = Connection::open(dbpath).map_err(io::Error::other)?;
52    reader2tar2entries2sqlite(io::stdin().lock(), csql, isql, &mut con, limit)
53}
54
55pub const CREATE_TABLE_DEFAULT: &str = include_str!("create.sql");
56pub const INSERT_TABLE_DEFAULT: &str = include_str!("insert.sql");
57
58pub const BLOB_SIZE_MAX_DEFAULT: u64 = 16777216;
59
60pub fn stdin2tar2entries2sqlite2fs_default<P>(dbpath: P) -> Result<(), io::Error>
61where
62    P: AsRef<Path>,
63{
64    stdin2tar2entries2sqlite2fs(
65        CREATE_TABLE_DEFAULT,
66        INSERT_TABLE_DEFAULT,
67        dbpath,
68        BLOB_SIZE_MAX_DEFAULT,
69    )
70}