SQLStatement.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCore/Object.h>
  7. #include <LibSQL/AST/Parser.h>
  8. #include <SQLServer/ConnectionFromClient.h>
  9. #include <SQLServer/DatabaseConnection.h>
  10. #include <SQLServer/SQLStatement.h>
  11. namespace SQLServer {
  12. static HashMap<u64, NonnullRefPtr<SQLStatement>> s_statements;
  13. static u64 s_next_statement_id = 0;
  14. RefPtr<SQLStatement> SQLStatement::statement_for(u64 statement_id)
  15. {
  16. if (s_statements.contains(statement_id))
  17. return *s_statements.get(statement_id).value();
  18. dbgln_if(SQLSERVER_DEBUG, "Invalid statement_id {}", statement_id);
  19. return nullptr;
  20. }
  21. SQL::ResultOr<NonnullRefPtr<SQLStatement>> SQLStatement::create(DatabaseConnection& connection, StringView sql)
  22. {
  23. auto parser = SQL::AST::Parser(SQL::AST::Lexer(sql));
  24. auto statement = parser.next_statement();
  25. if (parser.has_errors())
  26. return SQL::Result { SQL::SQLCommand::Unknown, SQL::SQLErrorCode::SyntaxError, parser.errors()[0].to_deprecated_string() };
  27. return TRY(adopt_nonnull_ref_or_enomem(new (nothrow) SQLStatement(connection, move(statement))));
  28. }
  29. SQLStatement::SQLStatement(DatabaseConnection& connection, NonnullRefPtr<SQL::AST::Statement> statement)
  30. : Core::Object(&connection)
  31. , m_statement_id(s_next_statement_id++)
  32. , m_statement(move(statement))
  33. {
  34. dbgln_if(SQLSERVER_DEBUG, "SQLStatement({})", connection.connection_id());
  35. s_statements.set(m_statement_id, *this);
  36. }
  37. void SQLStatement::report_error(SQL::Result result, u64 execution_id)
  38. {
  39. dbgln_if(SQLSERVER_DEBUG, "SQLStatement::report_error(statement_id {}, error {}", statement_id(), result.error_string());
  40. auto client_connection = ConnectionFromClient::client_connection_for(connection()->client_id());
  41. s_statements.remove(statement_id());
  42. remove_from_parent();
  43. if (client_connection)
  44. client_connection->async_execution_error(statement_id(), execution_id, result.error(), result.error_string());
  45. else
  46. warnln("Cannot return execution error. Client disconnected");
  47. }
  48. Optional<u64> SQLStatement::execute(Vector<SQL::Value> placeholder_values)
  49. {
  50. dbgln_if(SQLSERVER_DEBUG, "SQLStatement::execute(statement_id {}", statement_id());
  51. auto client_connection = ConnectionFromClient::client_connection_for(connection()->client_id());
  52. if (!client_connection) {
  53. warnln("Cannot yield next result. Client disconnected");
  54. return {};
  55. }
  56. auto execution_id = m_next_execution_id++;
  57. m_ongoing_executions.set(execution_id);
  58. deferred_invoke([this, placeholder_values = move(placeholder_values), execution_id] {
  59. auto execution_result = m_statement->execute(connection()->database(), placeholder_values);
  60. m_ongoing_executions.remove(execution_id);
  61. if (execution_result.is_error()) {
  62. report_error(execution_result.release_error(), execution_id);
  63. return;
  64. }
  65. auto client_connection = ConnectionFromClient::client_connection_for(connection()->client_id());
  66. if (!client_connection) {
  67. warnln("Cannot return statement execution results. Client disconnected");
  68. return;
  69. }
  70. auto result = execution_result.release_value();
  71. if (should_send_result_rows(result)) {
  72. client_connection->async_execution_success(statement_id(), execution_id, true, 0, 0, 0);
  73. auto result_size = result.size();
  74. next(execution_id, move(result), result_size);
  75. } else {
  76. if (result.command() == SQL::SQLCommand::Insert)
  77. client_connection->async_execution_success(statement_id(), execution_id, false, result.size(), 0, 0);
  78. else if (result.command() == SQL::SQLCommand::Update)
  79. client_connection->async_execution_success(statement_id(), execution_id, false, 0, result.size(), 0);
  80. else if (result.command() == SQL::SQLCommand::Delete)
  81. client_connection->async_execution_success(statement_id(), execution_id, false, 0, 0, result.size());
  82. else
  83. client_connection->async_execution_success(statement_id(), execution_id, false, 0, 0, 0);
  84. }
  85. });
  86. return execution_id;
  87. }
  88. bool SQLStatement::should_send_result_rows(SQL::ResultSet const& result) const
  89. {
  90. if (result.is_empty())
  91. return false;
  92. switch (result.command()) {
  93. case SQL::SQLCommand::Describe:
  94. case SQL::SQLCommand::Select:
  95. return true;
  96. default:
  97. return false;
  98. }
  99. }
  100. void SQLStatement::next(u64 execution_id, SQL::ResultSet result, size_t result_size)
  101. {
  102. auto client_connection = ConnectionFromClient::client_connection_for(connection()->client_id());
  103. if (!client_connection) {
  104. warnln("Cannot yield next result. Client disconnected");
  105. return;
  106. }
  107. if (!result.is_empty()) {
  108. auto result_row = result.take_first();
  109. client_connection->async_next_result(statement_id(), execution_id, result_row.row.take_data());
  110. deferred_invoke([this, execution_id, result = move(result), result_size]() {
  111. next(execution_id, move(result), result_size);
  112. });
  113. } else {
  114. client_connection->async_results_exhausted(statement_id(), execution_id, result_size);
  115. }
  116. }
  117. }