|
myTinySQL v0.2.0-2-g5000098
A tiny SQL interpreter over fixed-width text files
|
Reads and changes the rows of one table's data file. More...
#include <TableFile.hpp>
Public Member Functions | |
| TableFile (const TableSchema &schema, std::filesystem::path path) | |
| Prepares access to one data file. | |
| std::vector< Row > | readRows () const |
| Reads every row of the file. | |
| long long | nextId () const |
| The ID to give the next new row. | |
| void | append (const Row &values) const |
| Adds one row at the end of the file. | |
| void | appendRows (const std::vector< Row > &rows) const |
| Adds several rows at the end of the file, in one write. | |
| std::size_t | removeRows (const std::function< bool(const Row &)> &shouldRemove) const |
| Removes the rows that match a test. | |
| 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. | |
Private Types | |
| enum class | Change { Keep , Remove , Update } |
| What rewrite() does with one line. More... | |
Private Member Functions | |
| 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. | |
| Row | splitLine (std::string_view line) const |
| Cuts one line of the file into its column values. | |
Private Attributes | |
| const TableSchema & | schema_ |
| The table's definition. | |
| std::filesystem::path | path_ |
| The data file. | |
Reads and changes the rows of one table's data file.
Each line of the file is one row; each column sits at the fixed positions given by the schema. Changes that rewrite the file (removeRows(), updateRows()) go through a temporary file, so a crash half-way never leaves a half-written table.
Definition at line 26 of file TableFile.hpp.
|
strongprivate |
What rewrite() does with one line.
| Enumerator | |
|---|---|
| Keep | Write the line back unchanged. |
| Remove | Leave the line out. |
| Update | Write the line back as |
Definition at line 71 of file TableFile.hpp.
| tinysql::TableFile::TableFile | ( | const TableSchema & | schema, |
| std::filesystem::path | path | ||
| ) |
Prepares access to one data file.
| schema | The table's definition; it must outlive this object. |
| path | The data file, usually from Catalog::dataFile(). |
Definition at line 43 of file TableFile.cpp.
| void tinysql::TableFile::append | ( | const Row & | values | ) | const |
Adds one row at the end of the file.
| values | One value per column, ID included. |
| SqlError | If the number of values is wrong, a value is too long for its column, or the file cannot be written. |
Definition at line 162 of file TableFile.cpp.
| void tinysql::TableFile::appendRows | ( | const std::vector< Row > & | rows | ) | const |
Adds several rows at the end of the file, in one write.
| rows | The rows; each has one value per column, ID included. |
| SqlError | As append(); every row is checked before the file is touched, so a bad row adds nothing. |
Definition at line 167 of file TableFile.cpp.
| long long tinysql::TableFile::nextId | ( | ) | const |
The ID to give the next new row.
Definition at line 149 of file TableFile.cpp.
| std::vector< Row > tinysql::TableFile::readRows | ( | ) | const |
Reads every row of the file.
| SqlError | If the file does not exist. |
Definition at line 48 of file TableFile.cpp.
| std::size_t tinysql::TableFile::removeRows | ( | const std::function< bool(const Row &)> & | shouldRemove | ) | const |
Removes the rows that match a test.
| shouldRemove | Called for every row; returns true to remove it. |
| SqlError | If the file does not exist or cannot be rewritten. |
Definition at line 77 of file TableFile.cpp.
|
private |
Rewrites the file through a temporary copy, line by line.
| decide | Called for every line with the raw line (which it may edit in place) and the same line split into a Row; returns what to do with that line. |
| SqlError | If the file does not exist or the temporary file cannot be written. |
Definition at line 103 of file TableFile.cpp.
|
private |
Cuts one line of the file into its column values.
| line | A raw line; a line shorter than the row length gives empty values at the end. |
Definition at line 66 of file TableFile.cpp.
| std::size_t tinysql::TableFile::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.
| shouldUpdate | Called for every row; returns true to change it. |
| newValues | Pairs of (column index, new value) to set in every matching row. |
| SqlError | If a new value is too long for its column (checked before the file is touched), or the file does not exist or cannot be rewritten. |
Definition at line 82 of file TableFile.cpp.
|
private |
The data file.
Definition at line 90 of file TableFile.hpp.
|
private |
The table's definition.
Definition at line 89 of file TableFile.hpp.