summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibSQL/SQLClient.cpp
blob: 3b1bd35f449a69d0e2bfde9a056e0a6d1953c178 (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
/*
 * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibSQL/SQLClient.h>

namespace SQL {

void SQLClient::connected(u64 connection_id, DeprecatedString const& connected_to_database)
{
    if (on_connected)
        on_connected(connection_id, connected_to_database);
}

void SQLClient::disconnected(u64 connection_id)
{
    if (on_disconnected)
        on_disconnected(connection_id);
}

void SQLClient::connection_error(u64 connection_id, SQLErrorCode const& code, DeprecatedString const& message)
{
    if (on_connection_error)
        on_connection_error(connection_id, code, message);
    else
        warnln("Connection error for connection_id {}: {} ({})", connection_id, message, to_underlying(code));
}

void SQLClient::execution_error(u64 statement_id, u64 execution_id, SQLErrorCode const& code, DeprecatedString const& message)
{
    if (on_execution_error)
        on_execution_error(statement_id, execution_id, code, message);
    else
        warnln("Execution error for statement_id {}: {} ({})", statement_id, message, to_underlying(code));
}

void SQLClient::execution_success(u64 statement_id, u64 execution_id, bool has_results, size_t created, size_t updated, size_t deleted)
{
    if (on_execution_success)
        on_execution_success(statement_id, execution_id, has_results, created, updated, deleted);
    else
        outln("{} row(s) created, {} updated, {} deleted", created, updated, deleted);
}

void SQLClient::next_result(u64 statement_id, u64 execution_id, Vector<DeprecatedString> const& row)
{
    if (on_next_result) {
        on_next_result(statement_id, execution_id, row);
        return;
    }
    bool first = true;
    for (auto& column : row) {
        if (!first)
            out(", ");
        out("\"{}\"", column);
        first = false;
    }
    outln();
}

void SQLClient::results_exhausted(u64 statement_id, u64 execution_id, size_t total_rows)
{
    if (on_results_exhausted)
        on_results_exhausted(statement_id, execution_id, total_rows);
    else
        outln("{} total row(s)", total_rows);
}

}