Database.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (c) 2022-2024, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StringView.h>
  7. #include <LibWebView/Database.h>
  8. namespace WebView {
  9. static constexpr auto database_name = "Browser"sv;
  10. ErrorOr<NonnullRefPtr<Database>> Database::create()
  11. {
  12. auto sql_client = TRY(SQL::SQLClient::try_create());
  13. return create(move(sql_client));
  14. }
  15. ErrorOr<NonnullRefPtr<Database>> Database::create(NonnullRefPtr<SQL::SQLClient> sql_client)
  16. {
  17. auto connection_id = sql_client->connect(database_name);
  18. if (!connection_id.has_value())
  19. return Error::from_string_view("Could not connect to SQL database"sv);
  20. return adopt_nonnull_ref_or_enomem(new (nothrow) Database(move(sql_client), *connection_id));
  21. }
  22. Database::Database(NonnullRefPtr<SQL::SQLClient> sql_client, SQL::ConnectionID connection_id)
  23. : m_sql_client(move(sql_client))
  24. , m_connection_id(connection_id)
  25. {
  26. m_sql_client->on_execution_success = [this](auto result) {
  27. if (result.has_results)
  28. return;
  29. if (auto in_progress_statement = take_pending_execution(result); in_progress_statement.has_value()) {
  30. if (in_progress_statement->on_complete)
  31. in_progress_statement->on_complete();
  32. }
  33. };
  34. m_sql_client->on_next_result = [this](auto result) {
  35. if (auto in_progress_statement = take_pending_execution(result); in_progress_statement.has_value()) {
  36. if (in_progress_statement->on_result)
  37. in_progress_statement->on_result(result.values);
  38. m_pending_executions.set({ result.statement_id, result.execution_id }, in_progress_statement.release_value());
  39. }
  40. };
  41. m_sql_client->on_results_exhausted = [this](auto result) {
  42. if (auto in_progress_statement = take_pending_execution(result); in_progress_statement.has_value()) {
  43. if (in_progress_statement->on_complete)
  44. in_progress_statement->on_complete();
  45. }
  46. };
  47. m_sql_client->on_execution_error = [this](auto result) {
  48. if (auto in_progress_statement = take_pending_execution(result); in_progress_statement.has_value()) {
  49. if (in_progress_statement->on_error)
  50. in_progress_statement->on_error(result.error_message);
  51. }
  52. };
  53. }
  54. ErrorOr<SQL::StatementID> Database::prepare_statement(StringView statement)
  55. {
  56. if (auto statement_id = m_sql_client->prepare_statement(m_connection_id, statement); statement_id.has_value())
  57. return *statement_id;
  58. return Error::from_string_view(statement);
  59. }
  60. void Database::execute_statement(SQL::StatementID statement_id, Vector<SQL::Value> placeholder_values, PendingExecution pending_execution)
  61. {
  62. Core::deferred_invoke([this, statement_id, placeholder_values = move(placeholder_values), pending_execution = move(pending_execution)]() mutable {
  63. auto execution_id = m_sql_client->execute_statement(statement_id, move(placeholder_values));
  64. if (!execution_id.has_value()) {
  65. if (pending_execution.on_error)
  66. pending_execution.on_error("Could not execute statement"sv);
  67. return;
  68. }
  69. m_pending_executions.set({ statement_id, *execution_id }, move(pending_execution));
  70. });
  71. }
  72. }