csvzall 0.4.0
CSV command-line tool and emerging C++ library
Loading...
Searching...
No Matches
csv_loader.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <functional>
5#include <ostream>
6#include <string>
7#include <vector>
8
9// Forward declarations — consumers that need to call db methods must include
10// the full SQLiteCpp and csv-parser headers themselves.
11namespace csv { class CSVReader; }
12namespace SQLite {
13class Database;
14class Statement;
15}
16
17namespace csvzall::sqlite {
18
20 std::function<void(const std::string&)> error;
21 std::function<void(const std::string&)> verbose;
22};
23
26};
27
29 Numeric,
30 Text,
31};
32
33// Returns a double-quoted SQL identifier with embedded '"' escaped by doubling.
34// E.g. my"col → "my""col"
35// Use this wherever column or table names appear in generated SQL.
36std::string QuoteIdentifier(const std::string& name);
37
38// Infer SQLite column affinities from the remaining rows in reader.
39//
40// Numeric columns stay NUMERIC so SQL comparisons and arithmetic keep working.
41// Columns with text, booleans, timestamps, or CSV_BIGINT values become TEXT so
42// projections preserve exact lexical cell values such as long identifier IDs.
43std::vector<SqliteColumnAffinity> InferColumnAffinities(
44 csv::CSVReader& reader,
45 const std::vector<std::string>& headers);
46
47// Load all rows from reader into a newly-created SQLite table.
48//
49// Schema conventions:
50// - Column affinity is supplied by the caller, normally from
51// InferColumnAffinities() followed by a reader reset. NUMERIC columns let
52// SQLite compare numeric values naturally; TEXT columns preserve exact CSV
53// cells for identifiers and mixed/non-numeric data.
54// - Column names are double-quoted in generated DDL; embedded " are escaped
55// by doubling ("my""col").
56//
57// Parameters:
58// reader — positioned after the header row (csv-parser has parsed it).
59// headers — the column names to use; must match the row field count.
60// db — open, writable SQLite::Database.
61// table_name — name of the table to create (must not already exist).
62// logger — error/verbose callbacks; only error is used on failure.
63//
64// Returns true on success, false on any SQLite or structural error.
65bool LoadCsvIntoTable(csv::CSVReader& reader,
66 const std::vector<std::string>& headers,
67 SQLite::Database& db,
68 const std::string& table_name,
69 const std::vector<SqliteColumnAffinity>& column_affinities,
70 const CsvLoadOptions& options,
71 const SqliteLogCallbacks& logger);
72
74 const std::function<csv::CSVReader&()>& reader,
75 const std::vector<std::string>& headers,
76 SQLite::Database& db,
77 const std::string& table_name,
78 const std::function<bool()>& reset_reader,
79 const CsvLoadOptions& options,
80 const SqliteLogCallbacks& logger);
81
82std::vector<std::string> StatementColumnNames(SQLite::Statement& statement);
83std::vector<std::string> StatementRowValues(SQLite::Statement& statement);
84
85std::uint64_t WriteStatementRowsAsCsv(SQLite::Statement& statement,
86 const std::vector<std::string>& headers,
87 std::ostream& output);
88
89} // namespace csvzall::sqlite
90
91namespace csvzall::pipeline {
92namespace sqlite = ::csvzall::sqlite;
93}
std::string name
Definition json_extract.cpp:347
std::vector< JsonValue > array
Definition json_extract.cpp:33
Definition csv_loader.hpp:12
Definition schema_inference.hpp:8
Definition chart_spec.hpp:100
Definition csv_loader.cpp:17
SqliteColumnAffinity
Definition csv_loader.hpp:28
std::vector< std::string > StatementRowValues(SQLite::Statement &statement)
Definition csv_loader.cpp:191
bool LoadCsvIntoTableWithInferredAffinities(const std::function< csv::CSVReader &()> &reader, const std::vector< std::string > &headers, SQLite::Database &db, const std::string &table_name, const std::function< bool()> &reset_reader, const CsvLoadOptions &options, const SqliteLogCallbacks &logger)
Definition csv_loader.cpp:166
std::uint64_t WriteStatementRowsAsCsv(SQLite::Statement &statement, const std::vector< std::string > &headers, std::ostream &output)
Definition csv_loader.cpp:202
std::vector< SqliteColumnAffinity > InferColumnAffinities(csv::CSVReader &reader, const std::vector< std::string > &headers)
Definition csv_loader.cpp:91
bool LoadCsvIntoTable(csv::CSVReader &reader, const std::vector< std::string > &headers, SQLite::Database &db, const std::string &table_name, const std::vector< SqliteColumnAffinity > &column_affinities, const CsvLoadOptions &options, const SqliteLogCallbacks &logger)
Definition csv_loader.cpp:114
std::vector< std::string > StatementColumnNames(SQLite::Statement &statement)
Definition csv_loader.cpp:181
std::string QuoteIdentifier(const std::string &name)
Definition csv_loader.cpp:20
Definition csv_loader.hpp:24
bool sqlite_journal_enabled
Definition csv_loader.hpp:25
Definition csv_loader.hpp:19
std::function< void(const std::string &)> verbose
Definition csv_loader.hpp:21
std::function< void(const std::string &)> error
Definition csv_loader.hpp:20