summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibSQL/SQLResult.h
blob: 8072f67b0a1b384cdfd21e262e2796a4e16cb961 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
 * Copyright (c) 2021, Mahmoud Mandour <ma.mandourr@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/NonnullOwnPtrVector.h>
#include <AK/Vector.h>
#include <LibCore/Object.h>
#include <LibSQL/Tuple.h>
#include <LibSQL/Type.h>

namespace SQL {

#define ENUMERATE_SQL_COMMANDS(S) \
    S(Create)                     \
    S(Delete)                     \
    S(Insert)                     \
    S(Select)                     \
    S(Update)

enum class SQLCommand {
#undef __ENUMERATE_SQL_COMMAND
#define __ENUMERATE_SQL_COMMAND(command) command,
    ENUMERATE_SQL_COMMANDS(__ENUMERATE_SQL_COMMAND)
#undef __ENUMERATE_SQL_COMMAND
};

constexpr char const* command_tag(SQLCommand command)
{
    switch (command) {
#undef __ENUMERATE_SQL_COMMAND
#define __ENUMERATE_SQL_COMMAND(command) \
    case SQLCommand::command:            \
        return #command;
        ENUMERATE_SQL_COMMANDS(__ENUMERATE_SQL_COMMAND)
#undef __ENUMERATE_SQL_COMMAND
    }
}

#define ENUMERATE_SQL_ERRORS(S)                                                          \
    S(NoError, "No error")                                                               \
    S(InternalError, "{}")                                                               \
    S(NotYetImplemented, "{}")                                                           \
    S(DatabaseUnavailable, "Database Unavailable")                                       \
    S(StatementUnavailable, "Statement with id '{}' Unavailable")                        \
    S(SyntaxError, "Syntax Error")                                                       \
    S(DatabaseDoesNotExist, "Database '{}' does not exist")                              \
    S(SchemaDoesNotExist, "Schema '{}' does not exist")                                  \
    S(SchemaExists, "Schema '{}' already exist")                                         \
    S(TableDoesNotExist, "Table '{}' does not exist")                                    \
    S(ColumnDoesNotExist, "Column '{}' does not exist")                                  \
    S(AmbiguousColumnName, "Column name '{}' is ambiguous")                              \
    S(TableExists, "Table '{}' already exist")                                           \
    S(InvalidType, "Invalid type '{}'")                                                  \
    S(InvalidDatabaseName, "Invalid database name '{}'")                                 \
    S(InvalidValueType, "Invalid type for attribute '{}'")                               \
    S(InvalidNumberOfValues, "Number of values does not match number of columns")        \
    S(BooleanOperatorTypeMismatch, "Cannot apply '{}' operator to non-boolean operands") \
    S(NumericOperatorTypeMismatch, "Cannot apply '{}' operator to non-numeric operands") \
    S(IntegerOperatorTypeMismatch, "Cannot apply '{}' operator to non-numeric operands") \
    S(InvalidOperator, "Invalid operator '{}'")

enum class SQLErrorCode {
#undef __ENUMERATE_SQL_ERROR
#define __ENUMERATE_SQL_ERROR(error, description) error,
    ENUMERATE_SQL_ERRORS(__ENUMERATE_SQL_ERROR)
#undef __ENUMERATE_SQL_ERROR
};

struct SQLError {
    SQLErrorCode code { SQLErrorCode::NoError };
    String error_argument { "" };

    String to_string() const
    {
        String code_string;
        String message;
        switch (code) {
#undef __ENUMERATE_SQL_ERROR
#define __ENUMERATE_SQL_ERROR(error, description) \
    case SQLErrorCode::error:                     \
        code_string = #error;                     \
        message = description;                    \
        break;
            ENUMERATE_SQL_ERRORS(__ENUMERATE_SQL_ERROR)
#undef __ENUMERATE_SQL_ERROR
        default:
            VERIFY_NOT_REACHED();
        }
        if (!error_argument.is_null() && !error_argument.is_empty()) {
            if (message.find("{}").has_value()) {
                message = String::formatted(message, error_argument);
            } else {
                message = String::formatted("{}: {}", message, error_argument);
            }
        }
        if (message.is_null() || (message.is_empty())) {
            return code_string;
        } else {
            return String::formatted("{}: {}", code_string, message);
        }
    }
};

class SQLResult : public Core::Object {
    C_OBJECT(SQLResult)

public:
    void append(Tuple const& tuple)
    {
        m_has_results = true;
        m_result_set.append(tuple);
    }

    SQLCommand command() const { return m_command; }
    int updated() const { return m_update_count; }
    int inserted() const { return m_insert_count; }
    int deleted() const { return m_delete_count; }
    void set_error(SQLErrorCode code, String argument = {})
    {
        m_error.code = code;
        m_error.error_argument = argument;
    }

    bool has_error() const { return m_error.code != SQLErrorCode::NoError; }
    SQLError const& error() const { return m_error; }
    bool has_results() const { return m_has_results; }
    Vector<Tuple> const& results() const { return m_result_set; }

private:
    SQLResult() = default;

    explicit SQLResult(SQLCommand command, int update_count = 0, int insert_count = 0, int delete_count = 0)
        : m_command(command)
        , m_update_count(update_count)
        , m_insert_count(insert_count)
        , m_delete_count(delete_count)
        , m_has_results(command == SQLCommand::Select)
    {
    }

    SQLResult(SQLCommand command, SQLErrorCode error_code, String error_argument)
        : m_command(command)
        , m_error({ error_code, move(error_argument) })
    {
    }

    SQLResult(SQLCommand command, SQLErrorCode error_code, AK::Error error)
        : m_command(command)
        , m_error({ error_code, error.string_literal() })
    {
    }

    SQLCommand m_command { SQLCommand::Select };
    SQLError m_error { SQLErrorCode::NoError, "" };
    int m_update_count { 0 };
    int m_insert_count { 0 };
    int m_delete_count { 0 };
    bool m_has_results { false };
    Vector<Tuple> m_result_set;
};

}