summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibSQL/Value.h
blob: ad65999dcc0797d2b59dc1fc692f66a80b449cb9 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
 * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Checked.h>
#include <AK/DeprecatedString.h>
#include <AK/Format.h>
#include <AK/Optional.h>
#include <AK/StringView.h>
#include <AK/Variant.h>
#include <AK/Vector.h>
#include <LibIPC/Forward.h>
#include <LibSQL/Forward.h>
#include <LibSQL/Result.h>
#include <LibSQL/Type.h>
#include <math.h>

namespace SQL {

template<typename T>
concept Boolean = SameAs<RemoveCVReference<T>, bool>;

template<typename T>
concept Integer = (Integral<T> && !Boolean<T>);

/**
 * A `Value` is an atomic piece of SQL data`. A `Value` has a basic type
 * (Text/String, Integer, Float, etc). Richer types are implemented in higher
 * level layers, but the resulting data is stored in these `Value` objects.
 */
class Value {
    template<Integer T>
    using IntegerType = Conditional<IsSigned<T>, i64, u64>;

public:
    explicit Value(SQLType sql_type = SQLType::Null);
    explicit Value(DeprecatedString);
    explicit Value(double);
    Value(Value const&);
    Value(Value&&);
    ~Value();

    explicit Value(Integer auto value)
        : m_type(SQLType::Integer)
        , m_value(static_cast<IntegerType<decltype(value)>>(value))
    {
    }

    explicit Value(Boolean auto value)
        : m_type(SQLType::Boolean)
        , m_value(value)
    {
    }

    explicit Value(UnixDateTime);
    explicit Value(Duration);

    static ResultOr<Value> create_tuple(NonnullRefPtr<TupleDescriptor>);
    static ResultOr<Value> create_tuple(Vector<Value>);

    [[nodiscard]] SQLType type() const;
    [[nodiscard]] StringView type_name() const;
    [[nodiscard]] bool is_type_compatible_with(SQLType) const;
    [[nodiscard]] bool is_null() const;
    [[nodiscard]] bool is_int() const;

    [[nodiscard]] auto const& value() const
    {
        return *m_value;
    }

    [[nodiscard]] DeprecatedString to_deprecated_string() const;
    [[nodiscard]] Optional<double> to_double() const;
    [[nodiscard]] Optional<bool> to_bool() const;
    [[nodiscard]] Optional<Vector<Value>> to_vector() const;

    template<Integer T>
    [[nodiscard]] Optional<T> to_int() const
    {
        if (is_null())
            return {};

        return m_value->visit(
            [](DeprecatedString const& value) -> Optional<T> {
                if constexpr (IsSigned<T>)
                    return value.to_int<T>();
                else
                    return value.to_uint<T>();
            },
            [](Integer auto value) -> Optional<T> {
                if (!AK::is_within_range<T>(value))
                    return {};
                return static_cast<T>(value);
            },
            [](double value) -> Optional<T> {
                if (!AK::is_within_range<T>(value))
                    return {};
                return static_cast<T>(round(value));
            },
            [](bool value) -> Optional<T> { return static_cast<T>(value); },
            [](TupleValue const&) -> Optional<T> { return {}; });
    }

    Value& operator=(Value);
    Value& operator=(DeprecatedString);
    Value& operator=(double);

    Value& operator=(Integer auto value)
    {
        m_type = SQLType::Integer;
        m_value = static_cast<IntegerType<decltype(value)>>(value);
        return *this;
    }

    ResultOr<void> assign_tuple(NonnullRefPtr<TupleDescriptor>);
    ResultOr<void> assign_tuple(Vector<Value>);

    Value& operator=(Boolean auto value)
    {
        m_type = SQLType::Boolean;
        m_value = value;
        return *this;
    }

    [[nodiscard]] size_t length() const;
    [[nodiscard]] u32 hash() const;
    void serialize(Serializer&) const;
    void deserialize(Serializer&);

    [[nodiscard]] int compare(Value const&) const;
    bool operator==(Value const&) const;
    bool operator==(StringView) const;
    bool operator==(double) const;

    template<Integer T>
    bool operator==(T value)
    {
        return to_int<T>() == value;
    }

    bool operator!=(Value const&) const;
    bool operator<(Value const&) const;
    bool operator<=(Value const&) const;
    bool operator>(Value const&) const;
    bool operator>=(Value const&) const;

    ResultOr<Value> add(Value const&) const;
    ResultOr<Value> subtract(Value const&) const;
    ResultOr<Value> multiply(Value const&) const;
    ResultOr<Value> divide(Value const&) const;
    ResultOr<Value> modulo(Value const&) const;
    ResultOr<Value> negate() const;
    ResultOr<Value> shift_left(Value const&) const;
    ResultOr<Value> shift_right(Value const&) const;
    ResultOr<Value> bitwise_or(Value const&) const;
    ResultOr<Value> bitwise_and(Value const&) const;
    ResultOr<Value> bitwise_not() const;

    [[nodiscard]] TupleElementDescriptor descriptor() const;

private:
    friend Serializer;

    struct TupleValue {
        NonnullRefPtr<TupleDescriptor> descriptor;
        Vector<Value> values;
    };

    using ValueType = Variant<DeprecatedString, i64, u64, double, bool, TupleValue>;

    static ResultOr<NonnullRefPtr<TupleDescriptor>> infer_tuple_descriptor(Vector<Value> const& values);
    Value(NonnullRefPtr<TupleDescriptor> descriptor, Vector<Value> values);

    SQLType m_type { SQLType::Null };
    Optional<ValueType> m_value;
};

}

template<>
struct AK::Formatter<SQL::Value> : Formatter<StringView> {
    ErrorOr<void> format(FormatBuilder& builder, SQL::Value const& value)
    {
        return Formatter<StringView>::format(builder, value.to_deprecated_string());
    }
};

namespace IPC {

template<>
ErrorOr<void> encode(Encoder&, SQL::Value const&);

template<>
ErrorOr<SQL::Value> decode(Decoder&);

}