myTinySQL v0.2.0-2-g5000098
A tiny SQL interpreter over fixed-width text files
Loading...
Searching...
No Matches
Catalog.hpp
Go to the documentation of this file.
1
3
4#pragma once
5
6#include <cstddef>
7#include <filesystem>
8#include <optional>
9#include <string>
10#include <string_view>
11#include <vector>
12
13namespace tinysql {
14
16inline constexpr std::string_view kCatalogTable = "SYSTEM_TABLE";
17
19struct Column {
20 std::string name;
21 std::string type;
22 std::size_t from = 0;
23 std::size_t to = 0;
24
27 std::size_t width() const { return to - from + 1; }
28};
29
32 std::string name;
33 std::vector<Column> columns;
34 std::size_t recordLength = 0;
35
39 std::optional<std::size_t> indexOf(std::string_view columnName) const;
40
43 bool hasAutoId() const;
44};
45
47class Catalog {
48public:
53 static Catalog load(const std::filesystem::path& dataDir);
54
59 const TableSchema& table(std::string_view name) const;
60
63 const std::vector<TableSchema>& tables() const { return tables_; }
64
68 std::filesystem::path dataFile(const TableSchema& table) const;
69
70private:
71 std::filesystem::path dataDir_;
72 std::vector<TableSchema> tables_;
73};
74
75} // namespace tinysql
constexpr std::string_view kCatalogTable
Name of the catalogue table: it describes every table, itself included, and is its own data file.
Definition Catalog.hpp:16
All table definitions, read from the SYSTEM_TABLE file (one 80-character line per column).
Definition Catalog.hpp:47
std::filesystem::path dataFile(const TableSchema &table) const
The file holding a table's rows.
Definition Catalog.cpp:125
std::filesystem::path dataDir_
Where the files are.
Definition Catalog.hpp:71
const std::vector< TableSchema > & tables() const
All tables, in the order they appear in SYSTEM_TABLE.
Definition Catalog.hpp:63
const TableSchema & table(std::string_view name) const
Finds a table by name.
Definition Catalog.cpp:116
std::vector< TableSchema > tables_
Every table, in file order.
Definition Catalog.hpp:72
static Catalog load(const std::filesystem::path &dataDir)
Reads SYSTEM_TABLE from a data directory.
Definition Catalog.cpp:74
One column of a table: the characters it occupies in every fixed-width row.
Definition Catalog.hpp:19
std::size_t width() const
Number of characters the column occupies.
Definition Catalog.hpp:27
std::size_t to
Last character of the column, inclusive.
Definition Catalog.hpp:23
std::size_t from
First character of the column in a row, counted from 0.
Definition Catalog.hpp:22
std::string name
Column name, e.g. NOME.
Definition Catalog.hpp:20
std::string type
N1 or S1: recorded in SYSTEM_TABLE, not enforced.
Definition Catalog.hpp:21
The definition of one table: its name, its columns, and the length of a row.
Definition Catalog.hpp:31
std::string name
Table name, e.g. EMPLOY.
Definition Catalog.hpp:32
std::size_t recordLength
Length of a full row, in characters.
Definition Catalog.hpp:34
std::optional< std::size_t > indexOf(std::string_view columnName) const
Finds a column by name.
Definition Catalog.cpp:60
bool hasAutoId() const
Tells whether rows are numbered automatically.
Definition Catalog.cpp:69
std::vector< Column > columns
Columns in file order.
Definition Catalog.hpp:33