myTinySQL v0.2.0-2-g5000098
A tiny SQL interpreter over fixed-width text files
Loading...
Searching...
No Matches
TableFile.hpp
Go to the documentation of this file.
1
3
4#pragma once
5
6#include <cstddef>
7#include <filesystem>
8#include <functional>
9#include <string>
10#include <string_view>
11#include <utility>
12#include <vector>
13
14#include "Catalog.hpp"
15
16namespace tinysql {
17
19using Row = std::vector<std::string>;
20
26class TableFile {
27public:
31 TableFile(const TableSchema& schema, std::filesystem::path path);
32
36 std::vector<Row> readRows() const;
37
40 long long nextId() const;
41
46 void append(const Row& values) const;
47
52 void appendRows(const std::vector<Row>& rows) const;
53
58 std::size_t removeRows(const std::function<bool(const Row&)>& shouldRemove) const;
59
66 std::size_t updateRows(const std::function<bool(const Row&)>& shouldUpdate,
67 const std::vector<std::pair<std::size_t, std::string>>& newValues) const;
68
69private:
71 enum class Change {
72 Keep,
73 Remove,
74 Update,
75 };
76
82 std::size_t rewrite(const std::function<Change(std::string& line, const Row& row)>& decide) const;
83
87 Row splitLine(std::string_view line) const;
88
90 std::filesystem::path path_;
91};
92
93} // namespace tinysql
The table definitions read from SYSTEM_TABLE: tables, their columns and positions.
std::vector< std::string > Row
One row of a table: one value per column, trailing spaces removed.
Definition TableFile.hpp:19
Reads and changes the rows of one table's data file.
Definition TableFile.hpp:26
std::size_t removeRows(const std::function< bool(const Row &)> &shouldRemove) const
Removes the rows that match a test.
Definition TableFile.cpp:77
long long nextId() const
The ID to give the next new row.
std::size_t updateRows(const std::function< bool(const Row &)> &shouldUpdate, const std::vector< std::pair< std::size_t, std::string > > &newValues) const
Changes some columns in the rows that match a test.
Definition TableFile.cpp:82
std::size_t rewrite(const std::function< Change(std::string &line, const Row &row)> &decide) const
Rewrites the file through a temporary copy, line by line.
Change
What rewrite() does with one line.
Definition TableFile.hpp:71
@ Keep
Write the line back unchanged.
@ Update
Write the line back as decide edited it.
@ Remove
Leave the line out.
std::filesystem::path path_
The data file.
Definition TableFile.hpp:90
void append(const Row &values) const
Adds one row at the end of the file.
const TableSchema & schema_
The table's definition.
Definition TableFile.hpp:89
std::vector< Row > readRows() const
Reads every row of the file.
Definition TableFile.cpp:48
Row splitLine(std::string_view line) const
Cuts one line of the file into its column values.
Definition TableFile.cpp:66
void appendRows(const std::vector< Row > &rows) const
Adds several rows at the end of the file, in one write.
The definition of one table: its name, its columns, and the length of a row.
Definition Catalog.hpp:31