/* * Copyright (c) 2021, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include namespace SQL { template static inline NonnullRefPtr create_ast_node(Args&&... args) { return adopt_ref(*new T(forward(args)...)); } class ASTNode : public RefCounted { public: virtual ~ASTNode() { } protected: ASTNode() = default; }; //================================================================================================== // Language types //================================================================================================== class SignedNumber final : public ASTNode { public: explicit SignedNumber(double value) : m_value(value) { } double value() const { return m_value; } private: double m_value; }; class TypeName : public ASTNode { public: TypeName(String name, NonnullRefPtrVector signed_numbers) : m_name(move(name)) , m_signed_numbers(move(signed_numbers)) { VERIFY(m_signed_numbers.size() <= 2); } const String& name() const { return m_name; } const NonnullRefPtrVector& signed_numbers() const { return m_signed_numbers; } private: String m_name; NonnullRefPtrVector m_signed_numbers; }; class ColumnDefinition : public ASTNode { public: ColumnDefinition(String name, NonnullRefPtr type_name) : m_name(move(name)) , m_type_name(move(type_name)) { } const String& name() const { return m_name; } const NonnullRefPtr& type_name() const { return m_type_name; } private: String m_name; NonnullRefPtr m_type_name; }; class CommonTableExpression : public ASTNode { public: CommonTableExpression(String table_name, Vector column_names, NonnullRefPtr& select_statement() const { return m_select_statement; } private: String m_table_name; Vector m_column_names; NonnullRefPtr select_statement, bool invert_expression) : m_select_statement(move(select_statement)) , m_invert_expression(invert_expression) { } const NonnullRefPtr m_select_statement; bool m_invert_expression; }; class CollateExpression : public NestedExpression { public: CollateExpression(NonnullRefPtr expression, String collation_name) : NestedExpression(move(expression)) , m_collation_name(move(collation_name)) { } const String& collation_name() const { return m_collation_name; } private: String m_collation_name; }; enum class MatchOperator { Like, Glob, Match, Regexp, }; class MatchExpression : public InvertibleNestedDoubleExpression { public: MatchExpression(MatchOperator type, NonnullRefPtr lhs, NonnullRefPtr rhs, RefPtr escape, bool invert_expression) : InvertibleNestedDoubleExpression(move(lhs), move(rhs), invert_expression) , m_type(type) , m_escape(move(escape)) { } MatchOperator type() const { return m_type; } const RefPtr& escape() const { return m_escape; } private: MatchOperator m_type; RefPtr m_escape; }; class NullExpression : public InvertibleNestedExpression { public: NullExpression(NonnullRefPtr expression, bool invert_expression) : InvertibleNestedExpression(move(expression), invert_expression) { } }; class IsExpression : public InvertibleNestedDoubleExpression { public: IsExpression(NonnullRefPtr lhs, NonnullRefPtr rhs, bool invert_expression) : InvertibleNestedDoubleExpression(move(lhs), move(rhs), invert_expression) { } }; class BetweenExpression : public InvertibleNestedDoubleExpression { public: BetweenExpression(NonnullRefPtr expression, NonnullRefPtr lhs, NonnullRefPtr rhs, bool invert_expression) : InvertibleNestedDoubleExpression(move(lhs), move(rhs), invert_expression) , m_expression(move(expression)) { } const NonnullRefPtr& expression() const { return m_expression; } private: NonnullRefPtr m_expression; }; class InSelectionExpression : public InvertibleNestedExpression { public: InSelectionExpression(NonnullRefPtr expression, NonnullRefPtr& select_statement() const { return m_select_statement; } private: NonnullRefPtr select_statement, bool is_temporary, bool is_error_if_table_exists) : m_schema_name(move(schema_name)) , m_table_name(move(table_name)) , m_select_statement(move(select_statement)) , m_is_temporary(is_temporary) , m_is_error_if_table_exists(is_error_if_table_exists) { } CreateTable(String schema_name, String table_name, NonnullRefPtrVector columns, bool is_temporary, bool is_error_if_table_exists) : m_schema_name(move(schema_name)) , m_table_name(move(table_name)) , m_columns(move(columns)) , m_is_temporary(is_temporary) , m_is_error_if_table_exists(is_error_if_table_exists) { } const String& schema_name() const { return m_schema_name; } const String& table_name() const { return m_table_name; } bool has_selection() const { return !m_select_statement.is_null(); } const RefPtr m_select_statement; NonnullRefPtrVector m_columns; bool m_is_temporary; bool m_is_error_if_table_exists; }; class AlterTable : public Statement { public: const String& schema_name() const { return m_schema_name; } const String& table_name() const { return m_table_name; } protected: AlterTable(String schema_name, String table_name) : m_schema_name(move(schema_name)) , m_table_name(move(table_name)) { } private: String m_schema_name; String m_table_name; }; class RenameTable : public AlterTable { public: RenameTable(String schema_name, String table_name, String new_table_name) : AlterTable(move(schema_name), move(table_name)) , m_new_table_name(move(new_table_name)) { } const String& new_table_name() const { return m_new_table_name; } private: String m_new_table_name; }; class RenameColumn : public AlterTable { public: RenameColumn(String schema_name, String table_name, String column_name, String new_column_name) : AlterTable(move(schema_name), move(table_name)) , m_column_name(move(column_name)) , m_new_column_name(move(new_column_name)) { } const String& column_name() const { return m_column_name; } const String& new_column_name() const { return m_new_column_name; } private: String m_column_name; String m_new_column_name; }; class AddColumn : public AlterTable { public: AddColumn(String schema_name, String table_name, NonnullRefPtr column) : AlterTable(move(schema_name), move(table_name)) , m_column(move(column)) { } const NonnullRefPtr& column() const { return m_column; } private: NonnullRefPtr m_column; }; class DropColumn : public AlterTable { public: DropColumn(String schema_name, String table_name, String column_name) : AlterTable(move(schema_name), move(table_name)) , m_column_name(move(column_name)) { } const String& column_name() const { return m_column_name; } private: String m_column_name; }; class DropTable : public Statement { public: DropTable(String schema_name, String table_name, bool is_error_if_table_does_not_exist) : m_schema_name(move(schema_name)) , m_table_name(move(table_name)) , m_is_error_if_table_does_not_exist(is_error_if_table_does_not_exist) { } const String& schema_name() const { return m_schema_name; } const String& table_name() const { return m_table_name; } bool is_error_if_table_does_not_exist() const { return m_is_error_if_table_does_not_exist; } private: String m_schema_name; String m_table_name; bool m_is_error_if_table_does_not_exist; }; enum class ConflictResolution { Abort, Fail, Ignore, Replace, Rollback, }; class Insert : public Statement { public: Insert(RefPtr common_table_expression_list, ConflictResolution conflict_resolution, String schema_name, String table_name, String alias, Vector column_names, NonnullRefPtrVector chained_expressions) : m_common_table_expression_list(move(common_table_expression_list)) , m_conflict_resolution(conflict_resolution) , m_schema_name(move(schema_name)) , m_table_name(move(table_name)) , m_alias(move(alias)) , m_column_names(move(column_names)) , m_chained_expressions(move(chained_expressions)) { } Insert(RefPtr common_table_expression_list, ConflictResolution conflict_resolution, String schema_name, String table_name, String alias, Vector column_names, RefPtr& select_statement() const { return m_select_statement; } private: RefPtr m_common_table_expression_list; ConflictResolution m_conflict_resolution; String m_schema_name; String m_table_name; String m_alias; Vector m_column_names; NonnullRefPtrVector m_chained_expressions; RefPtr