myTinySQL v0.2.0-2-g5000098
A tiny SQL interpreter over fixed-width text files
Loading...
Searching...
No Matches
Statement.hpp
Go to the documentation of this file.
1
3
4#pragma once
5
6#include <cstddef>
7#include <optional>
8#include <string>
9#include <string_view>
10#include <variant>
11#include <vector>
12
13namespace tinysql {
14
16struct SelectItem {
18 enum class Kind {
19 Star,
20 Column,
21 Literal,
22 };
24 std::string text;
25};
26
28struct Condition {
29 std::string column;
30 std::string op;
31 std::string value;
32};
33
35struct SelectStmt {
36 std::vector<SelectItem> items;
37 std::string table;
38 std::optional<Condition> where;
39};
40
42struct InsertStmt {
43 std::string table;
44 std::vector<std::string> values;
45};
46
48struct DeleteStmt {
49 std::string table;
51};
52
54struct Assignment {
55 std::string column;
56 std::string value;
57};
58
60struct UpdateStmt {
61 std::string table;
62 std::vector<Assignment> assignments;
64};
65
68 std::string column;
69 std::size_t width;
70};
71
74 std::string name;
75 std::string type;
76 std::size_t width;
77};
78
81 std::string table;
82 std::vector<ColumnDefinition> columns;
83};
84
87 std::optional<std::string> table;
88};
89
91struct CommentStmt {};
92
96using Statement =
99
104Statement parseStatement(std::string_view sql);
105
109std::vector<std::string> splitStatements(std::string_view script);
110
111} // namespace tinysql
std::variant< SelectStmt, InsertStmt, UpdateStmt, DeleteStmt, CreateTableStmt, ColumnFormatStmt, DescribeStmt, CommentStmt > Statement
Any parsed statement: exactly one of the statement types above.
Definition Statement.hpp:98
std::vector< std::string > splitStatements(std::string_view script)
Splits a script into statements.
Definition Parser.cpp:517
Statement parseStatement(std::string_view sql)
Parses one statement.
Definition Parser.cpp:509
One <column> = <value> of an UPDATE's SET list.
Definition Statement.hpp:54
std::string column
The column to change.
Definition Statement.hpp:55
std::string value
Its new value.
Definition Statement.hpp:56
One column of a CREATE TABLE: <name> CHAR(<width>) or <name> NUMBER(<width>).
Definition Statement.hpp:73
std::string name
The column name.
Definition Statement.hpp:74
std::string type
S1 for CHAR, N1 for NUMBER: the codes SYSTEM_TABLE records.
Definition Statement.hpp:75
std::size_t width
Number of characters the column occupies in every row.
Definition Statement.hpp:76
COLUMN <name> FORMAT A<width>: the console display width of a column.
Definition Statement.hpp:67
std::string column
The column name.
Definition Statement.hpp:68
std::size_t width
Display width in characters.
Definition Statement.hpp:69
REM ..., -- ..., or an empty line: nothing to run.
Definition Statement.hpp:91
A WHERE <column> <op> <value> test.
Definition Statement.hpp:28
std::string value
The value to compare with (a number when both sides are integers), or the LIKE pattern.
Definition Statement.hpp:31
std::string column
The column to test.
Definition Statement.hpp:29
std::string op
One of = != <> < <= > >=, or LIKE.
Definition Statement.hpp:30
CREATE TABLE <table> (<column> <type>(<width>)[, ...]): adds a table to SYSTEM_TABLE.
Definition Statement.hpp:80
std::string table
The new table's name.
Definition Statement.hpp:81
std::vector< ColumnDefinition > columns
Its columns, in file order.
Definition Statement.hpp:82
DELETE FROM <table> WHERE <condition>. The WHERE is required, so a table is never emptied by accident...
Definition Statement.hpp:48
Condition where
Which rows to remove.
Definition Statement.hpp:50
std::string table
The table to remove rows from.
Definition Statement.hpp:49
DESCRIBE lists the tables; DESCRIBE <table> lists that table's columns.
Definition Statement.hpp:86
std::optional< std::string > table
The table to describe, or empty to list all tables.
Definition Statement.hpp:87
INSERT INTO <table> VALUES (<values>).
Definition Statement.hpp:42
std::vector< std::string > values
The values, without the ID when it is assigned automatically.
Definition Statement.hpp:44
std::string table
The table to add a row to.
Definition Statement.hpp:43
One entry of a SELECT list: *, a column name, or a 'quoted literal' printed on every row.
Definition Statement.hpp:16
Kind
Which kind of entry this is.
Definition Statement.hpp:18
@ Column
A column name.
@ Star
*: every column of the table.
@ Literal
A 'quoted text', printed as-is on every row.
Kind kind
Which kind of entry this is.
Definition Statement.hpp:23
std::string text
The column name or the literal text (* for Kind::Star).
Definition Statement.hpp:24
SELECT <items> FROM <table> [WHERE <condition>].
Definition Statement.hpp:35
std::vector< SelectItem > items
What to show, in order.
Definition Statement.hpp:36
std::string table
The table to read.
Definition Statement.hpp:37
std::optional< Condition > where
The optional WHERE test.
Definition Statement.hpp:38
UPDATE <table> SET <col> = <value>[, ...] WHERE <condition>. The WHERE is required,...
Definition Statement.hpp:60
std::string table
The table to change.
Definition Statement.hpp:61
std::vector< Assignment > assignments
The columns to set and their new values.
Definition Statement.hpp:62
Condition where
Which rows to change.
Definition Statement.hpp:63