Serenity Operating System
at master 80 lines 2.3 kB view raw
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 8#pragma once 9 10#include <AK/Platform.h> 11#include <LibIPC/ConnectionToServer.h> 12#include <LibSQL/Result.h> 13#include <SQLServer/SQLClientEndpoint.h> 14#include <SQLServer/SQLServerEndpoint.h> 15 16namespace SQL { 17 18struct ExecutionSuccess { 19 u64 statement_id { 0 }; 20 u64 execution_id { 0 }; 21 22 Vector<DeprecatedString> column_names; 23 bool has_results { false }; 24 size_t rows_created { 0 }; 25 size_t rows_updated { 0 }; 26 size_t rows_deleted { 0 }; 27}; 28 29struct ExecutionError { 30 u64 statement_id { 0 }; 31 u64 execution_id { 0 }; 32 33 SQLErrorCode error_code; 34 DeprecatedString error_message; 35}; 36 37struct ExecutionResult { 38 u64 statement_id { 0 }; 39 u64 execution_id { 0 }; 40 41 Vector<Value> values; 42}; 43 44struct ExecutionComplete { 45 u64 statement_id { 0 }; 46 u64 execution_id { 0 }; 47 48 size_t total_rows { 0 }; 49}; 50 51class SQLClient 52 : public IPC::ConnectionToServer<SQLClientEndpoint, SQLServerEndpoint> 53 , public SQLClientEndpoint { 54 IPC_CLIENT_CONNECTION(SQLClient, "/tmp/session/%sid/portal/sql"sv) 55 56public: 57#if !defined(AK_OS_SERENITY) 58 static ErrorOr<NonnullRefPtr<SQLClient>> launch_server_and_create_client(Vector<String> candidate_server_paths); 59#endif 60 61 virtual ~SQLClient() = default; 62 63 Function<void(ExecutionSuccess)> on_execution_success; 64 Function<void(ExecutionError)> on_execution_error; 65 Function<void(ExecutionResult)> on_next_result; 66 Function<void(ExecutionComplete)> on_results_exhausted; 67 68private: 69 explicit SQLClient(NonnullOwnPtr<Core::LocalSocket> socket) 70 : IPC::ConnectionToServer<SQLClientEndpoint, SQLServerEndpoint>(*this, move(socket)) 71 { 72 } 73 74 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; 75 virtual void execution_error(u64 statement_id, u64 execution_id, SQLErrorCode const& code, DeprecatedString const& message) override; 76 virtual void next_result(u64 statement_id, u64 execution_id, Vector<SQL::Value> const&) override; 77 virtual void results_exhausted(u64 statement_id, u64 execution_id, size_t total_rows) override; 78}; 79 80}