2021-06-28 21:15:17 -04:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
|
2022-03-04 13:22:31 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2021-06-28 21:15:17 -04:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2022-12-09 17:04:27 -05:00
|
|
|
#include <AK/Platform.h>
|
2022-02-25 12:27:37 +02:00
|
|
|
#include <LibIPC/ConnectionToServer.h>
|
2022-12-02 16:25:27 -05:00
|
|
|
#include <LibSQL/Result.h>
|
2021-06-28 21:15:17 -04:00
|
|
|
#include <SQLServer/SQLClientEndpoint.h>
|
|
|
|
|
#include <SQLServer/SQLServerEndpoint.h>
|
|
|
|
|
|
|
|
|
|
namespace SQL {
|
|
|
|
|
|
2023-02-03 10:33:10 -05:00
|
|
|
struct ExecutionSuccess {
|
|
|
|
|
u64 statement_id { 0 };
|
|
|
|
|
u64 execution_id { 0 };
|
|
|
|
|
|
2023-02-03 10:11:15 -05:00
|
|
|
Vector<DeprecatedString> column_names;
|
2023-02-03 10:33:10 -05:00
|
|
|
bool has_results { false };
|
|
|
|
|
size_t rows_created { 0 };
|
|
|
|
|
size_t rows_updated { 0 };
|
|
|
|
|
size_t rows_deleted { 0 };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct ExecutionError {
|
|
|
|
|
u64 statement_id { 0 };
|
|
|
|
|
u64 execution_id { 0 };
|
|
|
|
|
|
|
|
|
|
SQLErrorCode error_code;
|
|
|
|
|
DeprecatedString error_message;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct ExecutionResult {
|
|
|
|
|
u64 statement_id { 0 };
|
|
|
|
|
u64 execution_id { 0 };
|
|
|
|
|
|
|
|
|
|
Vector<Value> values;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct ExecutionComplete {
|
|
|
|
|
u64 statement_id { 0 };
|
|
|
|
|
u64 execution_id { 0 };
|
|
|
|
|
|
|
|
|
|
size_t total_rows { 0 };
|
|
|
|
|
};
|
|
|
|
|
|
2021-06-28 21:15:17 -04:00
|
|
|
class SQLClient
|
2022-02-25 12:27:37 +02:00
|
|
|
: public IPC::ConnectionToServer<SQLClientEndpoint, SQLServerEndpoint>
|
2021-06-28 21:15:17 -04:00
|
|
|
, public SQLClientEndpoint {
|
2022-09-06 00:04:06 -06:00
|
|
|
IPC_CLIENT_CONNECTION(SQLClient, "/tmp/session/%sid/portal/sql"sv)
|
2022-12-05 12:38:38 -05:00
|
|
|
|
|
|
|
|
public:
|
2022-12-09 17:04:27 -05:00
|
|
|
#if !defined(AK_OS_SERENITY)
|
2023-02-02 03:03:46 -07:00
|
|
|
static ErrorOr<NonnullRefPtr<SQLClient>> launch_server_and_create_client(Vector<String> candidate_server_paths);
|
2022-12-09 17:04:27 -05:00
|
|
|
#endif
|
2022-12-05 12:38:38 -05:00
|
|
|
|
2022-03-04 13:22:31 -07:00
|
|
|
virtual ~SQLClient() = default;
|
2021-06-28 21:15:17 -04:00
|
|
|
|
2023-02-03 10:33:10 -05:00
|
|
|
Function<void(ExecutionSuccess)> on_execution_success;
|
|
|
|
|
Function<void(ExecutionError)> on_execution_error;
|
|
|
|
|
Function<void(ExecutionResult)> on_next_result;
|
|
|
|
|
Function<void(ExecutionComplete)> on_results_exhausted;
|
2021-06-28 21:15:17 -04:00
|
|
|
|
|
|
|
|
private:
|
2023-02-08 23:05:44 +01:00
|
|
|
explicit SQLClient(NonnullOwnPtr<Core::LocalSocket> socket)
|
2022-12-09 17:04:27 -05:00
|
|
|
: IPC::ConnectionToServer<SQLClientEndpoint, SQLServerEndpoint>(*this, move(socket))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-03 10:11:15 -05:00
|
|
|
virtual void execution_success(u64 statement_id, u64 execution_id, Vector<DeprecatedString> const& column_names, bool has_results, size_t created, size_t updated, size_t deleted) override;
|
2023-02-03 10:33:10 -05:00
|
|
|
virtual void execution_error(u64 statement_id, u64 execution_id, SQLErrorCode const& code, DeprecatedString const& message) override;
|
2022-12-03 11:34:30 -05:00
|
|
|
virtual void next_result(u64 statement_id, u64 execution_id, Vector<SQL::Value> const&) override;
|
2022-12-02 17:14:56 -05:00
|
|
|
virtual void results_exhausted(u64 statement_id, u64 execution_id, size_t total_rows) override;
|
2021-06-28 21:15:17 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|