summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibSQL/SQLClient.h
blob: fb8bda0deb6f68e16126bc91ca95f59739417233 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Platform.h>
#include <LibIPC/ConnectionToServer.h>
#include <LibSQL/Result.h>
#include <SQLServer/SQLClientEndpoint.h>
#include <SQLServer/SQLServerEndpoint.h>

namespace SQL {

struct ExecutionSuccess {
    u64 statement_id { 0 };
    u64 execution_id { 0 };

    Vector<DeprecatedString> column_names;
    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 };
};

class SQLClient
    : public IPC::ConnectionToServer<SQLClientEndpoint, SQLServerEndpoint>
    , public SQLClientEndpoint {
    IPC_CLIENT_CONNECTION(SQLClient, "/tmp/session/%sid/portal/sql"sv)

public:
#if !defined(AK_OS_SERENITY)
    static ErrorOr<NonnullRefPtr<SQLClient>> launch_server_and_create_client(Vector<String> candidate_server_paths);
#endif

    virtual ~SQLClient() = default;

    Function<void(ExecutionSuccess)> on_execution_success;
    Function<void(ExecutionError)> on_execution_error;
    Function<void(ExecutionResult)> on_next_result;
    Function<void(ExecutionComplete)> on_results_exhausted;

private:
    explicit SQLClient(NonnullOwnPtr<Core::LocalSocket> socket)
        : IPC::ConnectionToServer<SQLClientEndpoint, SQLServerEndpoint>(*this, move(socket))
    {
    }

    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;
    virtual void execution_error(u64 statement_id, u64 execution_id, SQLErrorCode const& code, DeprecatedString const& message) override;
    virtual void next_result(u64 statement_id, u64 execution_id, Vector<SQL::Value> const&) override;
    virtual void results_exhausted(u64 statement_id, u64 execution_id, size_t total_rows) override;
};

}