AST.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/NonnullRefPtr.h>
  28. #include <AK/NonnullRefPtrVector.h>
  29. #include <AK/RefCounted.h>
  30. #include <AK/String.h>
  31. namespace SQL {
  32. template<class T, class... Args>
  33. static inline NonnullRefPtr<T>
  34. create_ast_node(Args&&... args)
  35. {
  36. return adopt(*new T(forward<Args>(args)...));
  37. }
  38. class ASTNode : public RefCounted<ASTNode> {
  39. public:
  40. virtual ~ASTNode() { }
  41. protected:
  42. ASTNode() = default;
  43. };
  44. class Statement : public ASTNode {
  45. };
  46. class ErrorStatement final : public Statement {
  47. };
  48. class SignedNumber final : public ASTNode {
  49. public:
  50. explicit SignedNumber(double value)
  51. : m_value(value)
  52. {
  53. }
  54. double value() const { return m_value; }
  55. private:
  56. double m_value;
  57. };
  58. class TypeName : public ASTNode {
  59. public:
  60. TypeName(String name, NonnullRefPtrVector<SignedNumber> signed_numbers)
  61. : m_name(move(name))
  62. , m_signed_numbers(move(signed_numbers))
  63. {
  64. VERIFY(m_signed_numbers.size() <= 2);
  65. }
  66. const String& name() const { return m_name; }
  67. const NonnullRefPtrVector<SignedNumber> signed_numbers() const { return m_signed_numbers; }
  68. private:
  69. String m_name;
  70. NonnullRefPtrVector<SignedNumber> m_signed_numbers;
  71. };
  72. class ColumnDefinition : public ASTNode {
  73. public:
  74. ColumnDefinition(String name, NonnullRefPtr<TypeName> type_name)
  75. : m_name(move(name))
  76. , m_type_name(move(type_name))
  77. {
  78. }
  79. const String& name() const { return m_name; }
  80. const NonnullRefPtr<TypeName>& type_name() const { return m_type_name; }
  81. private:
  82. String m_name;
  83. NonnullRefPtr<TypeName> m_type_name;
  84. };
  85. class CreateTable : public Statement {
  86. public:
  87. CreateTable(String schema_name, String table_name, NonnullRefPtrVector<ColumnDefinition> columns, bool is_temporary, bool is_error_if_table_exists)
  88. : m_schema_name(move(schema_name))
  89. , m_table_name(move(table_name))
  90. , m_columns(move(columns))
  91. , m_is_temporary(is_temporary)
  92. , m_is_error_if_table_exists(is_error_if_table_exists)
  93. {
  94. }
  95. const String& schema_name() const { return m_schema_name; }
  96. const String& table_name() const { return m_table_name; }
  97. const NonnullRefPtrVector<ColumnDefinition> columns() const { return m_columns; }
  98. bool is_temporary() const { return m_is_temporary; }
  99. bool is_error_if_table_exists() const { return m_is_error_if_table_exists; }
  100. private:
  101. String m_schema_name;
  102. String m_table_name;
  103. NonnullRefPtrVector<ColumnDefinition> m_columns;
  104. bool m_is_temporary;
  105. bool m_is_error_if_table_exists;
  106. };
  107. class DropTable : public Statement {
  108. public:
  109. DropTable(String schema_name, String table_name, bool is_error_if_table_does_not_exist)
  110. : m_schema_name(move(schema_name))
  111. , m_table_name(move(table_name))
  112. , m_is_error_if_table_does_not_exist(is_error_if_table_does_not_exist)
  113. {
  114. }
  115. const String& schema_name() const { return m_schema_name; }
  116. const String& table_name() const { return m_table_name; }
  117. bool is_error_if_table_does_not_exist() const { return m_is_error_if_table_does_not_exist; }
  118. private:
  119. String m_schema_name;
  120. String m_table_name;
  121. bool m_is_error_if_table_does_not_exist;
  122. };
  123. }