SQLResult.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/NonnullOwnPtrVector.h>
  9. #include <AK/Vector.h>
  10. #include <LibCore/Object.h>
  11. #include <LibSQL/Tuple.h>
  12. #include <LibSQL/Type.h>
  13. namespace SQL {
  14. #define ENUMERATE_SQL_COMMANDS(S) \
  15. S(Create) \
  16. S(Delete) \
  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(DatabaseUnavailable, "Database Unavailable") \
  40. S(StatementUnavailable, "Statement with id '{}' Unavailable") \
  41. S(SyntaxError, "Syntax Error") \
  42. S(DatabaseDoesNotExist, "Database '{}' does not exist") \
  43. S(SchemaDoesNotExist, "Schema '{}' does not exist") \
  44. S(SchemaExists, "Schema '{}' already exist") \
  45. S(TableDoesNotExist, "Table '{}' does not exist") \
  46. S(ColumnDoesNotExist, "Column '{}' does not exist") \
  47. S(TableExists, "Table '{}' already exist") \
  48. S(InvalidType, "Invalid type '{}'") \
  49. S(InvalidDatabaseName, "Invalid database name '{}'") \
  50. S(InvalidValueType, "Invalid type for attribute '{}'") \
  51. S(InvalidNumberOfValues, "Number of values does not match number of columns")
  52. enum class SQLErrorCode {
  53. #undef __ENUMERATE_SQL_ERROR
  54. #define __ENUMERATE_SQL_ERROR(error, description) error,
  55. ENUMERATE_SQL_ERRORS(__ENUMERATE_SQL_ERROR)
  56. #undef __ENUMERATE_SQL_ERROR
  57. };
  58. struct SQLError {
  59. SQLErrorCode code { SQLErrorCode::NoError };
  60. String error_argument { "" };
  61. String to_string() const
  62. {
  63. String code_string;
  64. String message;
  65. switch (code) {
  66. #undef __ENUMERATE_SQL_ERROR
  67. #define __ENUMERATE_SQL_ERROR(error, description) \
  68. case SQLErrorCode::error: \
  69. code_string = #error; \
  70. message = description; \
  71. break;
  72. ENUMERATE_SQL_ERRORS(__ENUMERATE_SQL_ERROR)
  73. #undef __ENUMERATE_SQL_ERROR
  74. default:
  75. VERIFY_NOT_REACHED();
  76. }
  77. if (!error_argument.is_null() && !error_argument.is_empty()) {
  78. if (message.find("{}").has_value()) {
  79. message = String::formatted(message, error_argument);
  80. } else {
  81. message = String::formatted("{}: {}", message, error_argument);
  82. }
  83. }
  84. if (message.is_null() || (message.is_empty())) {
  85. return code_string;
  86. } else {
  87. return String::formatted("{}: {}", code_string, message);
  88. }
  89. }
  90. };
  91. class SQLResult : public Core::Object {
  92. C_OBJECT(SQLResult)
  93. public:
  94. void append(Tuple const& tuple)
  95. {
  96. m_has_results = true;
  97. m_result_set.append(tuple);
  98. }
  99. SQLCommand command() const { return m_command; }
  100. int updated() const { return m_update_count; }
  101. int inserted() const { return m_insert_count; }
  102. int deleted() const { return m_delete_count; }
  103. SQLError const& error() const { return m_error; }
  104. bool has_results() const { return m_has_results; }
  105. Vector<Tuple> const& results() const { return m_result_set; }
  106. private:
  107. SQLResult() = default;
  108. explicit SQLResult(SQLCommand command, int update_count = 0, int insert_count = 0, int delete_count = 0)
  109. : m_command(command)
  110. , m_update_count(update_count)
  111. , m_insert_count(insert_count)
  112. , m_delete_count(delete_count)
  113. , m_has_results(command == SQLCommand::Select)
  114. {
  115. }
  116. SQLResult(SQLCommand command, SQLErrorCode error_code, String error_argument)
  117. : m_command(command)
  118. , m_error({ error_code, move(error_argument) })
  119. {
  120. }
  121. SQLCommand m_command { SQLCommand::Select };
  122. SQLError m_error { SQLErrorCode::NoError, "" };
  123. int m_update_count { 0 };
  124. int m_insert_count { 0 };
  125. int m_delete_count { 0 };
  126. bool m_has_results { false };
  127. Vector<Tuple> m_result_set;
  128. };
  129. }