myTinySQL v0.2.0-2-g5000098
A tiny SQL interpreter over fixed-width text files
Loading...
Searching...
No Matches
Session.hpp
Go to the documentation of this file.
1
3
4#pragma once
5
6#include <cstddef>
7#include <filesystem>
8#include <map>
9#include <optional>
10#include <string>
11#include <string_view>
12#include <vector>
13
14#include "Statement.hpp"
15
16namespace tinysql {
17
19struct ResultSet {
20 std::vector<std::string> headers;
21 std::vector<std::size_t> widths;
22 std::vector<bool> literal;
23 std::vector<std::size_t> storageWidths;
24 std::vector<std::vector<std::string>> rows;
25 std::optional<std::size_t> idColumn;
26};
27
30 std::string sql;
31 std::string tableName;
32 std::optional<ResultSet> table;
33 std::string message;
34 bool failed = false;
35 bool silent = false;
36};
37
42class Session {
43public:
46 explicit Session(std::filesystem::path dataDir);
47
52 StatementResult execute(std::string_view sql);
53
57 std::vector<StatementResult> executeScript(std::string_view script);
58
59private:
64 StatementResult run(const SelectStmt& stmt);
65
70 StatementResult run(const InsertStmt& stmt);
71
77 StatementResult run(const UpdateStmt& stmt);
78
83 StatementResult run(const DeleteStmt& stmt);
84
91
96
101 StatementResult run(const DescribeStmt& stmt);
102
106
107 std::filesystem::path dataDir_;
108 std::map<std::string, std::size_t> displayWidths_;
109};
110
111} // namespace tinysql
The parsed form of every statement, and the functions that build it from text.
Runs statements against the tables in one data directory.
Definition Session.hpp:42
std::filesystem::path dataDir_
Where the tables are.
Definition Session.hpp:107
StatementResult run(const SelectStmt &stmt)
Runs a SELECT.
Definition Session.cpp:132
std::map< std::string, std::size_t > displayWidths_
Upper-case column name to display width.
Definition Session.hpp:108
std::vector< StatementResult > executeScript(std::string_view script)
Runs every statement of a script, in order.
Definition Session.cpp:123
StatementResult execute(std::string_view sql)
Parses and runs one statement.
Definition Session.cpp:106
COLUMN <name> FORMAT A<width>: the console display width of a column.
Definition Statement.hpp:67
REM ..., -- ..., or an empty line: nothing to run.
Definition Statement.hpp:91
CREATE TABLE <table> (<column> <type>(<width>)[, ...]): adds a table to SYSTEM_TABLE.
Definition Statement.hpp:80
DELETE FROM <table> WHERE <condition>. The WHERE is required, so a table is never emptied by accident...
Definition Statement.hpp:48
DESCRIBE lists the tables; DESCRIBE <table> lists that table's columns.
Definition Statement.hpp:86
INSERT INTO <table> VALUES (<values>).
Definition Statement.hpp:42
The rows produced by a SELECT or DESCRIBE. All per-column vectors have the same length.
Definition Session.hpp:19
std::vector< bool > literal
True for a 'literal' SELECT item.
Definition Session.hpp:22
std::vector< std::size_t > widths
Display width of each column in console output.
Definition Session.hpp:21
std::vector< std::string > headers
Column titles.
Definition Session.hpp:20
std::vector< std::size_t > storageWidths
Width of each column in the data file (0 for a literal).
Definition Session.hpp:23
std::vector< std::vector< std::string > > rows
The values, one inner vector per row.
Definition Session.hpp:24
std::optional< std::size_t > idColumn
Output column holding the row ID, if rows can be edited and deleted by ID.
Definition Session.hpp:25
SELECT <items> FROM <table> [WHERE <condition>].
Definition Statement.hpp:35
What running one statement produced: a table, a message, or an error.
Definition Session.hpp:29
std::string sql
The statement as it was run.
Definition Session.hpp:30
std::string tableName
The table the statement worked on, if any.
Definition Session.hpp:31
bool failed
True if the statement failed; message holds the error.
Definition Session.hpp:34
std::string message
E.g. "1 row inserted ...", or the error text.
Definition Session.hpp:33
std::optional< ResultSet > table
The rows, for SELECT and DESCRIBE.
Definition Session.hpp:32
bool silent
True for COLUMN and comments: nothing to show.
Definition Session.hpp:35
UPDATE <table> SET <col> = <value>[, ...] WHERE <condition>. The WHERE is required,...
Definition Statement.hpp:60