Result.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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(AmbiguousColumnName, "Column name '{}' is ambiguous") \
  39. S(BooleanOperatorTypeMismatch, "Cannot apply '{}' operator to non-boolean operands") \
  40. S(ColumnDoesNotExist, "Column '{}' does not exist") \
  41. S(DatabaseDoesNotExist, "Database '{}' does not exist") \
  42. S(DatabaseUnavailable, "Database Unavailable") \
  43. S(IntegerOperatorTypeMismatch, "Cannot apply '{}' operator to non-numeric operands") \
  44. S(IntegerOverflow, "Operation would cause integer overflow") \
  45. S(InternalError, "{}") \
  46. S(InvalidDatabaseName, "Invalid database name '{}'") \
  47. S(InvalidNumberOfPlaceholderValues, "Number of values does not match number of placeholders") \
  48. S(InvalidNumberOfValues, "Number of values does not match number of columns") \
  49. S(InvalidOperator, "Invalid operator '{}'") \
  50. S(InvalidType, "Invalid type '{}'") \
  51. S(InvalidValueType, "Invalid type for attribute '{}'") \
  52. S(NoError, "No error") \
  53. S(NotYetImplemented, "{}") \
  54. S(NumericOperatorTypeMismatch, "Cannot apply '{}' operator to non-numeric operands") \
  55. S(SchemaDoesNotExist, "Schema '{}' does not exist") \
  56. S(SchemaExists, "Schema '{}' already exist") \
  57. S(StatementUnavailable, "Statement with id '{}' Unavailable") \
  58. S(SyntaxError, "Syntax Error") \
  59. S(TableDoesNotExist, "Table '{}' does not exist") \
  60. S(TableExists, "Table '{}' already exist")
  61. enum class SQLErrorCode {
  62. #undef __ENUMERATE_SQL_ERROR
  63. #define __ENUMERATE_SQL_ERROR(error, description) error,
  64. ENUMERATE_SQL_ERRORS(__ENUMERATE_SQL_ERROR)
  65. #undef __ENUMERATE_SQL_ERROR
  66. };
  67. class [[nodiscard]] Result {
  68. public:
  69. ALWAYS_INLINE Result(SQLCommand command)
  70. : m_command(command)
  71. {
  72. }
  73. ALWAYS_INLINE Result(SQLCommand command, SQLErrorCode error)
  74. : m_command(command)
  75. , m_error(error)
  76. {
  77. }
  78. ALWAYS_INLINE Result(SQLCommand command, SQLErrorCode error, DeprecatedString error_message)
  79. : m_command(command)
  80. , m_error(error)
  81. , m_error_message(move(error_message))
  82. {
  83. }
  84. ALWAYS_INLINE Result(Error error)
  85. : m_error(static_cast<SQLErrorCode>(error.code()))
  86. , m_error_message(error.string_literal())
  87. {
  88. }
  89. Result(Result&&) = default;
  90. Result& operator=(Result&&) = default;
  91. SQLCommand command() const { return m_command; }
  92. SQLErrorCode error() const { return m_error; }
  93. DeprecatedString error_string() const;
  94. // These are for compatibility with the TRY() macro in AK.
  95. [[nodiscard]] bool is_error() const { return m_error != SQLErrorCode::NoError; }
  96. [[nodiscard]] Result release_value() { return move(*this); }
  97. Result release_error()
  98. {
  99. VERIFY(is_error());
  100. if (m_error_message.has_value())
  101. return { m_command, m_error, m_error_message.release_value() };
  102. return { m_command, m_error };
  103. }
  104. private:
  105. AK_MAKE_NONCOPYABLE(Result);
  106. SQLCommand m_command { SQLCommand::Unknown };
  107. SQLErrorCode m_error { SQLErrorCode::NoError };
  108. Optional<DeprecatedString> m_error_message {};
  109. };
  110. template<typename ValueType>
  111. using ResultOr = ErrorOr<ValueType, Result>;
  112. }