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

Runs statements against the tables in one data directory. More...

#include <Session.hpp>

Public Member Functions

 Session (std::filesystem::path dataDir)
 Starts a session.
 
StatementResult execute (std::string_view sql)
 Parses and runs one statement.
 
std::vector< StatementResult > executeScript (std::string_view script)
 Runs every statement of a script, in order.
 

Private Member Functions

StatementResult run (const SelectStmt &stmt)
 Runs a SELECT.
 
StatementResult run (const InsertStmt &stmt)
 Runs an INSERT, assigning the next ID when it is left out.
 
StatementResult run (const UpdateStmt &stmt)
 Runs an UPDATE.
 
StatementResult run (const DeleteStmt &stmt)
 Runs a DELETE.
 
StatementResult run (const CreateTableStmt &stmt)
 Runs a CREATE TABLE: adds one SYSTEM_TABLE line per column and an empty data file.
 
StatementResult run (const ColumnFormatStmt &stmt)
 Remembers a column's display width for the rest of the session.
 
StatementResult run (const DescribeStmt &stmt)
 Lists the tables, or one table's columns.
 
StatementResult run (const CommentStmt &)
 Does nothing: comments have no effect.
 

Private Attributes

std::filesystem::path dataDir_
 Where the tables are.
 
std::map< std::string, std::size_t > displayWidths_
 Upper-case column name to display width.
 

Detailed Description

Runs statements against the tables in one data directory.

COLUMN ... FORMAT settings last for the whole session. Every statement reloads the catalogue, so changes to the files are seen at once.

Definition at line 42 of file Session.hpp.

Constructor & Destructor Documentation

◆ Session()

tinysql::Session::Session ( std::filesystem::path  dataDir)
explicit

Starts a session.

Parameters
dataDirThe directory holding SYSTEM_TABLE and the data files.

Definition at line 104 of file Session.cpp.

Member Function Documentation

◆ execute()

StatementResult tinysql::Session::execute ( std::string_view  sql)

Parses and runs one statement.

Parameters
sqlThe text of one statement.
Returns
The result. Errors never escape: a failed statement gives a result with StatementResult::failed set and the error in StatementResult::message.

Definition at line 106 of file Session.cpp.

◆ executeScript()

std::vector< StatementResult > tinysql::Session::executeScript ( std::string_view  script)

Runs every statement of a script, in order.

Parameters
scriptStatements one per line, or separated by ;.
Returns
One result per statement; a failed statement does not stop the ones after it.

Definition at line 123 of file Session.cpp.

◆ run() [1/8]

StatementResult tinysql::Session::run ( const ColumnFormatStmt &  stmt)
private

Remembers a column's display width for the rest of the session.

Parameters
stmtThe statement.
Returns
A silent result.

Definition at line 378 of file Session.cpp.

◆ run() [2/8]

StatementResult tinysql::Session::run ( const CommentStmt &  )
private

Does nothing: comments have no effect.

Returns
A silent result.

Definition at line 412 of file Session.cpp.

◆ run() [3/8]

StatementResult tinysql::Session::run ( const CreateTableStmt &  stmt)
private

Runs a CREATE TABLE: adds one SYSTEM_TABLE line per column and an empty data file.

Parameters
stmtThe statement.
Returns
A message with the number of columns and the row length.
Exceptions
SqlErrorOn an invalid or duplicate name, an ID column that is not the first one, a row too long for SYSTEM_TABLE, or a data file that already exists.

Definition at line 304 of file Session.cpp.

◆ run() [4/8]

StatementResult tinysql::Session::run ( const DeleteStmt &  stmt)
private

Runs a DELETE.

Parameters
stmtThe statement.
Returns
A message saying how many rows were removed.
Exceptions
SqlErrorOn an unknown column or an attempt to change SYSTEM_TABLE.

Definition at line 282 of file Session.cpp.

◆ run() [5/8]

StatementResult tinysql::Session::run ( const DescribeStmt &  stmt)
private

Lists the tables, or one table's columns.

Parameters
stmtThe statement.
Returns
The list as a ResultSet.
Exceptions
SqlErrorOn an unknown table.

Definition at line 386 of file Session.cpp.

◆ run() [6/8]

StatementResult tinysql::Session::run ( const InsertStmt &  stmt)
private

Runs an INSERT, assigning the next ID when it is left out.

Parameters
stmtThe statement.
Returns
A message with the new row's ID.
Exceptions
SqlErrorOn a wrong number of values, a duplicate ID, or a value too long for its column.

Definition at line 218 of file Session.cpp.

◆ run() [7/8]

StatementResult tinysql::Session::run ( const SelectStmt &  stmt)
private

Runs a SELECT.

Parameters
stmtThe statement.
Returns
The rows found.
Exceptions
SqlErrorOn an unknown table or column, or a missing data file.

Definition at line 132 of file Session.cpp.

◆ run() [8/8]

StatementResult tinysql::Session::run ( const UpdateStmt &  stmt)
private

Runs an UPDATE.

Parameters
stmtThe statement.
Returns
A message saying how many rows changed.
Exceptions
SqlErrorOn an unknown column, an attempt to change the ID or SYSTEM_TABLE, or a value too long for its column.

Definition at line 249 of file Session.cpp.

Member Data Documentation

◆ dataDir_

std::filesystem::path tinysql::Session::dataDir_
private

Where the tables are.

Definition at line 107 of file Session.hpp.

◆ displayWidths_

std::map<std::string, std::size_t> tinysql::Session::displayWidths_
private

Upper-case column name to display width.

Definition at line 108 of file Session.hpp.


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