ConnectionFromClient.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/DeprecatedString.h>
  7. #include <AK/Vector.h>
  8. #include <LibSQL/Result.h>
  9. #include <SQLServer/ConnectionFromClient.h>
  10. #include <SQLServer/DatabaseConnection.h>
  11. #include <SQLServer/SQLStatement.h>
  12. namespace SQLServer {
  13. static HashMap<int, RefPtr<ConnectionFromClient>> s_connections;
  14. RefPtr<ConnectionFromClient> ConnectionFromClient::client_connection_for(int client_id)
  15. {
  16. if (s_connections.contains(client_id))
  17. return *s_connections.get(client_id).value();
  18. dbgln_if(SQLSERVER_DEBUG, "Invalid client_id {}", client_id);
  19. return nullptr;
  20. }
  21. ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<Core::Stream::LocalSocket> socket, int client_id)
  22. : IPC::ConnectionFromClient<SQLClientEndpoint, SQLServerEndpoint>(*this, move(socket), client_id)
  23. {
  24. s_connections.set(client_id, *this);
  25. }
  26. void ConnectionFromClient::die()
  27. {
  28. s_connections.remove(client_id());
  29. }
  30. Messages::SQLServer::ConnectResponse ConnectionFromClient::connect(DeprecatedString const& database_name)
  31. {
  32. dbgln_if(SQLSERVER_DEBUG, "ConnectionFromClient::connect(database_name: {})", database_name);
  33. auto database_connection = DatabaseConnection::construct(database_name, client_id());
  34. return { database_connection->connection_id() };
  35. }
  36. void ConnectionFromClient::disconnect(int connection_id)
  37. {
  38. dbgln_if(SQLSERVER_DEBUG, "ConnectionFromClient::disconnect(connection_id: {})", connection_id);
  39. auto database_connection = DatabaseConnection::connection_for(connection_id);
  40. if (database_connection)
  41. database_connection->disconnect();
  42. else
  43. dbgln("Database connection has disappeared");
  44. }
  45. Messages::SQLServer::PrepareStatementResponse ConnectionFromClient::prepare_statement(int connection_id, DeprecatedString const& sql)
  46. {
  47. dbgln_if(SQLSERVER_DEBUG, "ConnectionFromClient::prepare_statement(connection_id: {}, sql: '{}')", connection_id, sql);
  48. auto database_connection = DatabaseConnection::connection_for(connection_id);
  49. if (!database_connection) {
  50. dbgln("Database connection has disappeared");
  51. return { -1 };
  52. }
  53. auto result = database_connection->prepare_statement(sql);
  54. if (result.is_error()) {
  55. dbgln_if(SQLSERVER_DEBUG, "Could not parse SQL statement: {}", result.error().error_string());
  56. return { -1 };
  57. }
  58. dbgln_if(SQLSERVER_DEBUG, "ConnectionFromClient::prepare_statement -> statement_id = {}", result.value());
  59. return { result.value() };
  60. }
  61. void ConnectionFromClient::execute_statement(int statement_id)
  62. {
  63. dbgln_if(SQLSERVER_DEBUG, "ConnectionFromClient::execute_query_statement(statement_id: {})", statement_id);
  64. auto statement = SQLStatement::statement_for(statement_id);
  65. if (statement && statement->connection()->client_id() == client_id()) {
  66. statement->execute();
  67. } else {
  68. dbgln_if(SQLSERVER_DEBUG, "Statement has disappeared");
  69. async_execution_error(statement_id, (int)SQL::SQLErrorCode::StatementUnavailable, DeprecatedString::formatted("{}", statement_id));
  70. }
  71. }
  72. }