/*------------------------------------------------------------------------------* * Architecture & Implementation of DBMS * *------------------------------------------------------------------------------* * Copyright 2022 Databases and Information Systems Group TU Dortmund * * Visit us at * * http://dbis.cs.tu-dortmund.de/cms/en/home/ * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR * * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * * OTHER DEALINGS IN THE SOFTWARE. * * * * Authors: * * Maximilian Berens * * Roland Kühn * * Jan Mühlig * *------------------------------------------------------------------------------* */ #pragma once #include #include #include #include #include #include #include
#include namespace beedb::parser { using Alias = std::optional; using TableDescr = std::pair; using JoinDescr = std::pair>; using WhereExpression = std::unique_ptr; using GroupByExpression = std::vector; using OrderByItem = std::pair, bool>; using OrderByExpression = std::vector; struct LimitExpression { std::uint64_t limit; std::uint64_t offset; }; class NodeInterface { public: NodeInterface() noexcept = default; virtual ~NodeInterface() noexcept = default; }; class CreateTableStatement final : public NodeInterface { public: CreateTableStatement(std::string &&table_name, const bool if_not_exists, table::Schema &&schema) noexcept : _table_name(std::move(table_name)), _if_not_exists(if_not_exists), _schema(std::move(schema)) { } ~CreateTableStatement() noexcept override = default; [[nodiscard]] std::string &table_name() noexcept { return _table_name; } [[nodiscard]] bool if_not_exists() const noexcept { return _if_not_exists; } [[nodiscard]] table::Schema &schema() { return _schema; } private: std::string _table_name; bool _if_not_exists; table::Schema _schema; }; class CreateIndexStatement final : public NodeInterface { public: CreateIndexStatement(std::string &&index_name, std::string &&table_name, std::string &&column_name, const bool is_unique) noexcept : _index_name(std::move(index_name)), _table_name(std::move(table_name)), _column_name(std::move(column_name)), _is_unique(is_unique) { } ~CreateIndexStatement() noexcept override = default; [[nodiscard]] std::string &index_name() noexcept { return _index_name; } [[nodiscard]] std::string &table_name() noexcept { return _table_name; } [[nodiscard]] std::string &column_name() noexcept { return _column_name; } [[nodiscard]] bool is_unique() const noexcept { return _is_unique; } private: std::string _index_name; std::string _table_name; std::string _column_name; bool _is_unique; }; class InsertStatement final : public NodeInterface { public: InsertStatement(std::string &&table_name, std::vector &&column_names, std::vector> &&values) noexcept : _table_name(std::move(table_name)), _column_names(std::move(column_names)), _values(std::move(values)) { } ~InsertStatement() noexcept override = default; [[nodiscard]] std::string &table_name() noexcept { return _table_name; } [[nodiscard]] std::vector &column_names() noexcept { return _column_names; } [[nodiscard]] std::vector> &values() noexcept { return _values; } private: std::string _table_name; std::vector _column_names; std::vector> _values; }; class UpdateStatement final : public NodeInterface { public: UpdateStatement(std::string &&table_name, std::vector>> &&updates, WhereExpression &&where) noexcept : _table_name(std::move(table_name)), _updates(std::move(updates)), _where(std::move(where)) { } ~UpdateStatement() noexcept override = default; [[nodiscard]] std::string &table_name() noexcept { return _table_name; } [[nodiscard]] std::vector>> &updates() noexcept { return _updates; } [[nodiscard]] WhereExpression &where() noexcept { return _where; } private: std::string _table_name; std::vector>> _updates; WhereExpression _where; }; class DeleteStatement final : public NodeInterface { public: DeleteStatement(std::string &&table_name, WhereExpression &&where) noexcept : _table_name(std::move(table_name)), _where(std::move(where)) { } ~DeleteStatement() noexcept override = default; [[nodiscard]] std::string &table_name() noexcept { return _table_name; } [[nodiscard]] WhereExpression &where() noexcept { return _where; } private: std::string _table_name; WhereExpression _where; }; class TransactionStatement final : public NodeInterface { public: enum Type { BeginTransaction, CommitTransaction, AbortTransaction }; explicit TransactionStatement(const Type type) : _type(type) { } ~TransactionStatement() override = default; [[nodiscard]] bool is_begin() const noexcept { return _type == Type::BeginTransaction; } [[nodiscard]] bool is_commit() const noexcept { return _type == Type::CommitTransaction; } [[nodiscard]] bool is_abort() const noexcept { return _type == Type::AbortTransaction; } private: const Type _type; }; class SelectQuery final : public NodeInterface { public: SelectQuery(std::vector> &&attributes, std::vector &&from, std::optional> &&join, WhereExpression &&where, std::optional &&group_by, std::optional &&order_by, std::optional &&limit) noexcept : _attributes(std::move(attributes)), _from(std::move(from)), _join(std::move(join)), _where(std::move(where)), _group_by(std::move(group_by)), _order_by(std::move(order_by)), _limit(limit) { } ~SelectQuery() noexcept override = default; [[nodiscard]] std::vector> &attributes() noexcept { return _attributes; } [[nodiscard]] std::vector &from() noexcept { return _from; } [[nodiscard]] std::optional> &join() noexcept { return _join; } [[nodiscard]] WhereExpression &where() noexcept { return _where; } [[nodiscard]] std::optional &group_by() noexcept { return _group_by; } [[nodiscard]] std::optional &order_by() noexcept { return _order_by; } [[nodiscard]] std::optional &limit() noexcept { return _limit; } private: std::vector> _attributes; std::vector _from; std::optional> _join; WhereExpression _where; std::optional _group_by; std::optional _order_by; std::optional _limit; }; } // namespace beedb::parser