csvzall 0.4.0
CSV command-line tool and emerging C++ library
Loading...
Searching...
No Matches
row_loader.hpp
Go to the documentation of this file.
1#pragma once
2
3#ifdef CSVZALL_HAVE_POSTGRESQL
4
5#include <pqxx/pqxx>
6#include <csv.hpp>
7
8#include <memory>
9#include <optional>
10#include <string>
11#include <string_view>
12#include <vector>
13#include "schema_inference.hpp"
14
15namespace csvzall::postgres {
16
17// Batch loader state
18class RowLoader {
19 public:
20 using PgValue = std::optional<std::string_view>;
21
22 RowLoader(pqxx::connection& connection,
23 pqxx::work& transaction,
24 const std::string& table_name,
25 const std::vector<InferredColumn>& columns);
26
27 // Adds rows to the COPY stream.
28 void add_row(const csv::CSVRow& row);
29 void add_row(const std::vector<std::string>& row);
30 void add_rows(const std::vector<csv::CSVRow>& rows);
31
32 void flush();
33
34 [[nodiscard]] std::uint64_t rows_loaded() const { return rows_loaded_; }
35 [[nodiscard]] std::uint64_t rows_skipped_type_mismatch() const { return rows_skipped_type_mismatch_; }
36
37 ~RowLoader();
38
39 private:
40 pqxx::connection& connection_;
41 pqxx::work& transaction_;
42 std::string table_name_;
43 std::vector<InferredColumn> columns_;
44 std::vector<std::string> column_names_;
45 std::unique_ptr<pqxx::stream_to> stream_;
46 bool completed_ = false;
47
48 std::uint64_t rows_loaded_ = 0;
49 std::uint64_t rows_skipped_type_mismatch_ = 0;
50
51 void open_stream();
52
53 void append_value(const csv::CSVRow& row, std::size_t index, std::vector<PgValue>& values) const;
54 void append_value(std::string_view value, std::vector<PgValue>& values) const;
55 void write_values(std::vector<PgValue>& values);
56};
57
58} // namespace csvzall::postgres
59
60namespace csvzall::pipeline {
61namespace postgres = ::csvzall::postgres;
62}
63
64#endif // CSVZALL_HAVE_POSTGRESQL
Definition row_loader.hpp:18
std::uint64_t rows_loaded() const
Definition row_loader.hpp:34
std::optional< std::string_view > PgValue
Definition row_loader.hpp:20
void add_rows(const std::vector< csv::CSVRow > &rows)
Definition row_loader.cpp:80
std::uint64_t rows_skipped_type_mismatch() const
Definition row_loader.hpp:35
~RowLoader()
Definition row_loader.cpp:148
void add_row(const csv::CSVRow &row)
Definition row_loader.cpp:42
void flush()
Definition row_loader.cpp:102
std::vector< ColumnMapping > columns
Definition json_extract.cpp:353
std::vector< JsonValue > array
Definition json_extract.cpp:33
Definition chart_spec.hpp:100
Definition commands.hpp:16
double value
Definition timeseries.cpp:32