csvzall 0.4.0
CSV command-line tool and emerging C++ library
Loading...
Searching...
No Matches
sqlite_db.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <memory>
5#include <string>
6
7#include <SQLiteCpp/SQLiteCpp.h>
8
9namespace csvzall::sqlite {
10
12 std::string db_path;
13 bool input_is_stdin = false;
14 std::string input_path;
15 std::size_t threshold_mb = 256;
16};
17
18// RAII wrapper around SQLite::Database.
19//
20// Optionally owns a temporary on-disk file: when owns_temp_file is true the
21// file is deleted (best-effort) when this object is destroyed. This covers
22// normal exit and std::terminate; SIGKILL may leave orphaned files in the
23// system temp directory.
24class SqliteDb {
25 public:
26 // Opens or creates a database at path.
27 // path == ":memory:" creates an in-process in-memory database.
28 // If owns_temp_file is true, path is deleted when this object is destroyed.
29 explicit SqliteDb(const std::string& path, bool owns_temp_file = false);
30
31 ~SqliteDb();
32
33 SqliteDb(const SqliteDb&) = delete;
34 SqliteDb& operator=(const SqliteDb&) = delete;
35
38
39 SQLite::Database& db();
40
41 private:
43 std::string temp_path_; // non-empty => we own the file and must delete it
44};
45
46// Factory: choose in-memory vs temp-file database according to RunOptions and
47// the size of the input file.
48//
49// Decision rules (evaluated in order):
50// 1. options.sqlite_db_path non-empty -> use that path as-is (not deleted).
51// 2. options.input_is_stdin or options.input_path empty/"-" -> :memory:
52// 3. file_size(options.input_path) <= options.sqlite_threshold_mb MB -> :memory:
53// 4. otherwise -> temp-file database in system temp dir (deleted on exit).
55
56// Register csvzall SQLite scalar functions that must be available anywhere
57// CSV-backed SQL executes.
59
60} // namespace csvzall::sqlite
61
62namespace csvzall::pipeline {
63namespace sqlite = ::csvzall::sqlite;
64}
Definition sqlite_db.hpp:24
SQLite::Database & db()
Definition sqlite_db.cpp:124
SqliteDb & operator=(const SqliteDb &)=delete
~SqliteDb()
Definition sqlite_db.cpp:110
SqliteDb(const SqliteDb &)=delete
std::vector< JsonValue > array
Definition json_extract.cpp:33
common::JsonPath path
Definition json_extract.cpp:348
Definition csv_loader.hpp:12
Definition csv_loader.cpp:17
SqliteDb OpenSqliteDb(const SqliteDbOpenOptions &options)
Definition sqlite_db.cpp:128
void RegisterSqliteRegexFunctions(SQLite::Database &db)
Definition sqlite_db.cpp:163
Definition bar.cpp:11
Definition sqlite_db.hpp:11
std::size_t threshold_mb
Definition sqlite_db.hpp:15
std::string db_path
Definition sqlite_db.hpp:12
std::string input_path
Definition sqlite_db.hpp:14
bool input_is_stdin
Definition sqlite_db.hpp:13