Result.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
  3. * Copyright (c) 2021, Mahmoud Mandour <ma.mandourr@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Error.h>
  9. #include <AK/Noncopyable.h>
  10. #include <LibSQL/Type.h>
  11. namespace SQL {
  12. #define ENUMERATE_SQL_COMMANDS(S) \
  13. S(Unknown) \
  14. S(Create) \
  15. S(Delete) \
  16. S(Describe) \
  17. S(Insert) \
  18. S(Select) \
  19. S(Update)
  20. enum class SQLCommand {
  21. #undef __ENUMERATE_SQL_COMMAND
  22. #define __ENUMERATE_SQL_COMMAND(command) command,
  23. ENUMERATE_SQL_COMMANDS(__ENUMERATE_SQL_COMMAND)
  24. #undef __ENUMERATE_SQL_COMMAND
  25. };
  26. constexpr char const* command_tag(SQLCommand command)
  27. {
  28. switch (command) {
  29. #undef __ENUMERATE_SQL_COMMAND
  30. #define __ENUMERATE_SQL_COMMAND(command) \
  31. case SQLCommand::command: \
  32. return #command;
  33. ENUMERATE_SQL_COMMANDS(__ENUMERATE_SQL_COMMAND)
  34. #undef __ENUMERATE_SQL_COMMAND
  35. }
  36. }
  37. #define ENUMERATE_SQL_ERRORS(S) \
  38. S(NoError, "No error") \
  39. S(InternalError, "{}") \
  40. S(NotYetImplemented, "{}") \
  41. S(DatabaseUnavailable, "Database Unavailable") \
  42. S(StatementUnavailable, "Statement with id '{}' Unavailable") \
  43. S(SyntaxError, "Syntax Error") \
  44. S(DatabaseDoesNotExist, "Database '{}' does not exist") \
  45. S(SchemaDoesNotExist, "Schema '{}' does not exist") \
  46. S(SchemaExists, "Schema '{}' already exist") \
  47. S(TableDoesNotExist, "Table '{}' does not exist") \
  48. S(ColumnDoesNotExist, "Column '{}' does not exist") \
  49. S(AmbiguousColumnName, "Column name '{}' is ambiguous") \
  50. S(TableExists, "Table '{}' already exist") \
  51. S(InvalidType, "Invalid type '{}'") \
  52. S(InvalidDatabaseName, "Invalid database name '{}'") \
  53. S(InvalidValueType, "Invalid type for attribute '{}'") \
  54. S(InvalidNumberOfPlaceholderValues, "Number of values does not match number of placeholders") \
  55. S(InvalidNumberOfValues, "Number of values does not match number of columns") \
  56. S(BooleanOperatorTypeMismatch, "Cannot apply '{}' operator to non-boolean operands") \
  57. S(NumericOperatorTypeMismatch, "Cannot apply '{}' operator to non-numeric operands") \
  58. S(IntegerOperatorTypeMismatch, "Cannot apply '{}' operator to non-numeric operands") \
  59. S(InvalidOperator, "Invalid operator '{}'")
  60. enum class SQLErrorCode {
  61. #undef __ENUMERATE_SQL_ERROR
  62. #define __ENUMERATE_SQL_ERROR(error, description) error,
  63. ENUMERATE_SQL_ERRORS(__ENUMERATE_SQL_ERROR)
  64. #undef __ENUMERATE_SQL_ERROR
  65. };
  66. class [[nodiscard]] Result {
  67. public:
  68. ALWAYS_INLINE Result(SQLCommand command)
  69. : m_command(command)
  70. {
  71. }
  72. ALWAYS_INLINE Result(SQLCommand command, SQLErrorCode error)
  73. : m_command(command)
  74. , m_error(error)
  75. {
  76. }
  77. ALWAYS_INLINE Result(SQLCommand command, SQLErrorCode error, DeprecatedString error_message)
  78. : m_command(command)
  79. , m_error(error)
  80. , m_error_message(move(error_message))
  81. {
  82. }
  83. ALWAYS_INLINE Result(Error error)
  84. : m_error(static_cast<SQLErrorCode>(error.code()))
  85. , m_error_message(error.string_literal())
  86. {
  87. }
  88. Result(Result&&) = default;
  89. Result& operator=(Result&&) = default;
  90. SQLCommand command() const { return m_command; }
  91. SQLErrorCode error() const { return m_error; }
  92. DeprecatedString error_string() const;
  93. // These are for compatibility with the TRY() macro in AK.
  94. [[nodiscard]] bool is_error() const { return m_error != SQLErrorCode::NoError; }
  95. [[nodiscard]] Result release_value() { return move(*this); }
  96. Result release_error()
  97. {
  98. VERIFY(is_error());
  99. if (m_error_message.has_value())
  100. return { m_command, m_error, m_error_message.release_value() };
  101. return { m_command, m_error };
  102. }
  103. private:
  104. AK_MAKE_NONCOPYABLE(Result);
  105. SQLCommand m_command { SQLCommand::Unknown };
  106. SQLErrorCode m_error { SQLErrorCode::NoError };
  107. Optional<DeprecatedString> m_error_message {};
  108. };
  109. template<typename ValueType>
  110. using ResultOr = ErrorOr<ValueType, Result>;
  111. }