SQLClient.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibSQL/SQLClient.h>
  8. namespace SQL {
  9. void SQLClient::connected(int connection_id, String const& connected_to_database)
  10. {
  11. if (on_connected)
  12. on_connected(connection_id, connected_to_database);
  13. }
  14. void SQLClient::disconnected(int connection_id)
  15. {
  16. if (on_disconnected)
  17. on_disconnected(connection_id);
  18. }
  19. void SQLClient::connection_error(int connection_id, int code, String const& message)
  20. {
  21. if (on_connection_error)
  22. on_connection_error(connection_id, code, message);
  23. else
  24. warnln("Connection error for connection_id {}: {} ({})", connection_id, message, code);
  25. }
  26. void SQLClient::execution_error(int statement_id, int code, String const& message)
  27. {
  28. if (on_execution_error)
  29. on_execution_error(statement_id, code, message);
  30. else
  31. warnln("Execution error for statement_id {}: {} ({})", statement_id, message, code);
  32. }
  33. void SQLClient::execution_success(int statement_id, bool has_results, int created, int updated, int deleted)
  34. {
  35. if (on_execution_success)
  36. on_execution_success(statement_id, has_results, created, updated, deleted);
  37. else
  38. outln("{} row(s) created, {} updated, {} deleted", created, updated, deleted);
  39. }
  40. void SQLClient::next_result(int statement_id, Vector<String> const& row)
  41. {
  42. if (on_next_result) {
  43. on_next_result(statement_id, row);
  44. return;
  45. }
  46. bool first = true;
  47. for (auto& column : row) {
  48. if (!first)
  49. out(", ");
  50. out("\"{}\"", column);
  51. first = false;
  52. }
  53. outln();
  54. }
  55. void SQLClient::results_exhausted(int statement_id, int total_rows)
  56. {
  57. if (on_results_exhausted)
  58. on_results_exhausted(statement_id, total_rows);
  59. else
  60. outln("{} total row(s)", total_rows);
  61. }
  62. }