myTinySQL v0.2.0-2-g5000098
A tiny SQL interpreter over fixed-width text files
Loading...
Searching...
No Matches
Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
tinysql::TableFile Class Reference

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.
 

Detailed Description

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.

Member Enumeration Documentation

◆ Change

enum class tinysql::TableFile::Change
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 decide edited it.

Definition at line 71 of file TableFile.hpp.

Constructor & Destructor Documentation

◆ TableFile()

tinysql::TableFile::TableFile ( const TableSchema &  schema,
std::filesystem::path  path 
)

Prepares access to one data file.

Parameters
schemaThe table's definition; it must outlive this object.
pathThe data file, usually from Catalog::dataFile().

Definition at line 43 of file TableFile.cpp.

Member Function Documentation

◆ append()

void tinysql::TableFile::append ( const Row &  values) const

Adds one row at the end of the file.

Parameters
valuesOne value per column, ID included.
Exceptions
SqlErrorIf 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.

◆ appendRows()

void tinysql::TableFile::appendRows ( const std::vector< Row > &  rows) const

Adds several rows at the end of the file, in one write.

Parameters
rowsThe rows; each has one value per column, ID included.
Exceptions
SqlErrorAs append(); every row is checked before the file is touched, so a bad row adds nothing.

Definition at line 167 of file TableFile.cpp.

◆ nextId()

long long tinysql::TableFile::nextId ( ) const

The ID to give the next new row.

Returns
The highest numeric ID in the file plus one, or 1 for an empty or missing file.

Definition at line 149 of file TableFile.cpp.

◆ readRows()

std::vector< Row > tinysql::TableFile::readRows ( ) const

Reads every row of the file.

Returns
The rows, each split into one value per column; blank lines are skipped.
Exceptions
SqlErrorIf the file does not exist.

Definition at line 48 of file TableFile.cpp.

◆ removeRows()

std::size_t tinysql::TableFile::removeRows ( const std::function< bool(const Row &)> &  shouldRemove) const

Removes the rows that match a test.

Parameters
shouldRemoveCalled for every row; returns true to remove it.
Returns
How many rows were removed.
Exceptions
SqlErrorIf the file does not exist or cannot be rewritten.

Definition at line 77 of file TableFile.cpp.

◆ rewrite()

std::size_t tinysql::TableFile::rewrite ( const std::function< Change(std::string &line, const Row &row)> &  decide) const
private

Rewrites the file through a temporary copy, line by line.

Parameters
decideCalled 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.
Returns
How many lines were removed or edited; when 0, the file is left untouched.
Exceptions
SqlErrorIf the file does not exist or the temporary file cannot be written.

Definition at line 103 of file TableFile.cpp.

◆ splitLine()

Row tinysql::TableFile::splitLine ( std::string_view  line) const
private

Cuts one line of the file into its column values.

Parameters
lineA raw line; a line shorter than the row length gives empty values at the end.
Returns
One value per column, trailing spaces removed.

Definition at line 66 of file TableFile.cpp.

◆ updateRows()

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.

Parameters
shouldUpdateCalled for every row; returns true to change it.
newValuesPairs of (column index, new value) to set in every matching row.
Returns
How many rows were changed.
Exceptions
SqlErrorIf 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.

Member Data Documentation

◆ path_

std::filesystem::path tinysql::TableFile::path_
private

The data file.

Definition at line 90 of file TableFile.hpp.

◆ schema_

const TableSchema& tinysql::TableFile::schema_
private

The table's definition.

Definition at line 89 of file TableFile.hpp.


The documentation for this class was generated from the following files: