34std::string_view
cut(std::string_view line,
Field field)
36 if (field.
from >= line.size()) {
48std::size_t
number(std::string_view line,
Field field, std::size_t lineNumber)
51 if (!value || *value < 0) {
52 throw SqlError(std::format(
"{} line {}: expected a number in characters {}-{}",
55 return static_cast<std::size_t
>(*value);
66 return static_cast<std::size_t
>(it -
columns.begin());
80 std::ifstream in(path);
82 throw SqlError(std::format(
"cannot open the catalogue {}", path.string()));
86 std::size_t lineNumber = 0;
87 while (std::getline(in, line)) {
89 if (
trim(line).empty()) {
92 std::string tableName(cut(line, kTable));
94 .
name = std::string(cut(line, kName)),
95 .type = std::string(cut(line, kType)),
96 .from = number(line, kFrom, lineNumber),
97 .to = number(line, kTo, lineNumber),
99 if (tableName.empty() || column.name.empty() || column.to < column.from) {
102 const std::size_t recordLength = std::max(number(line, kRecordLength, lineNumber), column.to + 1);
105 [&](
const TableSchema& t) { return equalsIgnoreCase(t.name, tableName); });
120 throw SqlError(std::format(
"unknown table '{}'", name));
The table definitions read from SYSTEM_TABLE: tables, their columns and positions.
constexpr std::string_view kCatalogTable
Name of the catalogue table: it describes every table, itself included, and is its own data file.
The SqlError exception and small string helpers used everywhere.
std::optional< long long > parseInteger(std::string_view text)
Parses a whole string as a base-10 integer.
bool equalsIgnoreCase(std::string_view a, std::string_view b)
Compares two strings, ignoring the case of ASCII letters.
std::string_view trim(std::string_view text)
Removes leading and trailing spaces, tabs and line ends.
All table definitions, read from the SYSTEM_TABLE file (one 80-character line per column).
std::filesystem::path dataFile(const TableSchema &table) const
The file holding a table's rows.
std::filesystem::path dataDir_
Where the files are.
const TableSchema & table(std::string_view name) const
Finds a table by name.
std::vector< TableSchema > tables_
Every table, in file order.
static Catalog load(const std::filesystem::path &dataDir)
Reads SYSTEM_TABLE from a data directory.
A problem with one statement: bad syntax, unknown table, value too long...
constexpr Field kName
Column name.
constexpr Field kType
Column type (N1 or S1).
constexpr Field kTo
Last character of the column.
constexpr Field kTable
Table name.
std::size_t number(std::string_view line, Field field, std::size_t lineNumber)
Reads one numeric field of a SYSTEM_TABLE line.
constexpr Field kFrom
First character of the column in a data row.
constexpr Field kRecordLength
Length of a full data row.
std::string_view cut(std::string_view line, Field field)
Cuts one field out of a SYSTEM_TABLE line.
One column of a table: the characters it occupies in every fixed-width row.
std::string name
Column name, e.g. NOME.
The definition of one table: its name, its columns, and the length of a row.
std::string name
Table name, e.g. EMPLOY.
std::size_t recordLength
Length of a full row, in characters.
std::optional< std::size_t > indexOf(std::string_view columnName) const
Finds a column by name.
bool hasAutoId() const
Tells whether rows are numbered automatically.
std::vector< Column > columns
Columns in file order.
The position of one field in a SYSTEM_TABLE line.
std::size_t to
Last character, inclusive.
std::size_t from
First character, counted from 0.