ConnectionFromClient.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. if (auto database_connection = DatabaseConnection::create(database_name, client_id()); !database_connection.is_error())
  34. return { database_connection.value()->connection_id() };
  35. return { {} };
  36. }
  37. void ConnectionFromClient::disconnect(u64 connection_id)
  38. {
  39. dbgln_if(SQLSERVER_DEBUG, "ConnectionFromClient::disconnect(connection_id: {})", connection_id);
  40. auto database_connection = DatabaseConnection::connection_for(connection_id);
  41. if (database_connection)
  42. database_connection->disconnect();
  43. else
  44. dbgln("Database connection has disappeared");
  45. }
  46. Messages::SQLServer::PrepareStatementResponse ConnectionFromClient::prepare_statement(u64 connection_id, DeprecatedString const& sql)
  47. {
  48. dbgln_if(SQLSERVER_DEBUG, "ConnectionFromClient::prepare_statement(connection_id: {}, sql: '{}')", connection_id, sql);
  49. auto database_connection = DatabaseConnection::connection_for(connection_id);
  50. if (!database_connection) {
  51. dbgln("Database connection has disappeared");
  52. return { {} };
  53. }
  54. auto result = database_connection->prepare_statement(sql);
  55. if (result.is_error()) {
  56. dbgln_if(SQLSERVER_DEBUG, "Could not parse SQL statement: {}", result.error().error_string());
  57. return { {} };
  58. }
  59. dbgln_if(SQLSERVER_DEBUG, "ConnectionFromClient::prepare_statement -> statement_id = {}", result.value());
  60. return { result.value() };
  61. }
  62. Messages::SQLServer::ExecuteStatementResponse ConnectionFromClient::execute_statement(u64 statement_id, Vector<SQL::Value> const& placeholder_values)
  63. {
  64. dbgln_if(SQLSERVER_DEBUG, "ConnectionFromClient::execute_query_statement(statement_id: {})", statement_id);
  65. auto statement = SQLStatement::statement_for(statement_id);
  66. if (statement && statement->connection()->client_id() == client_id()) {
  67. // FIXME: Support taking parameters from IPC requests.
  68. return statement->execute(move(const_cast<Vector<SQL::Value>&>(placeholder_values)));
  69. }
  70. dbgln_if(SQLSERVER_DEBUG, "Statement has disappeared");
  71. async_execution_error(statement_id, -1, SQL::SQLErrorCode::StatementUnavailable, DeprecatedString::formatted("{}", statement_id));
  72. return { {} };
  73. }
  74. }