blob: 74c90a8feabd0cedd49555e74f5b85f5a0613688 (
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
|
/*
* Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/ByteBuffer.h>
#include <AK/Function.h>
#include <AK/String.h>
#include <AK/Variant.h>
#include <LibSQL/Type.h>
namespace SQL {
/**
* 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 {
public:
explicit Value(SQLType sql_type = SQLType::Text);
Value(SQLType sql_type, ByteBuffer& buffer, size_t& offset);
Value(Value const& other);
~Value();
static Value const& null();
Value& operator=(Value&& other) noexcept
{
(*this) = other;
return (*this);
}
Value& operator=(Value const& other);
Value& operator=(String const&);
Value& operator=(String&& string)
{
operator=(string);
return *this;
}
Value& operator=(int);
Value& operator=(u32);
Value& operator=(double);
Value& set_null();
Optional<String> to_string() const;
explicit operator String() const;
Optional<int> to_int() const;
explicit operator int() const;
Optional<double> to_double() const;
explicit operator double() const;
Optional<u32> to_u32() const;
explicit operator u32() const;
[[nodiscard]] SQLType type() const { return m_type; }
[[nodiscard]] const char* type_name() const { return m_type_name(); }
[[nodiscard]] size_t size() const { return m_size(); }
[[nodiscard]] int compare(Value const& other) const { return m_compare(other); }
[[nodiscard]] bool is_null() const { return m_is_null; }
[[nodiscard]] bool can_cast(Value const&) const;
[[nodiscard]] u32 hash() const { return (is_null()) ? 0 : m_hash(); }
bool operator==(Value const& other) const { return m_compare(other) == 0; }
bool operator==(String const& other) const;
bool operator==(int other) const;
bool operator==(double other) const;
bool operator!=(Value const& other) const { return m_compare(other) != 0; }
bool operator<(Value const& other) const { return m_compare(other) < 0; }
bool operator<=(Value const& other) const { return m_compare(other) <= 0; }
bool operator>(Value const& other) const { return m_compare(other) > 0; }
bool operator>=(Value const& other) const { return m_compare(other) >= 0; }
void serialize(ByteBuffer& buffer) const
{
VERIFY(!is_null());
m_serialize(buffer);
}
private:
void setup(SQLType sql_type);
void setup_text();
void setup_int();
void setup_float();
Function<Optional<String>()> m_to_string;
Function<Optional<int>()> m_to_int;
Function<Optional<double>()> m_to_double;
Function<void(Value const&)> m_assign_value;
Function<void(String const&)> m_assign_string;
Function<void(int)> m_assign_int;
Function<void(double)> m_assign_double;
Function<int(Value const&)> m_compare;
Function<void(ByteBuffer&)> m_serialize;
Function<void(ByteBuffer&, size_t& offset)> m_deserialize;
Function<size_t()> m_size;
Function<const char*()> m_type_name;
Function<bool(Value const&)> m_can_cast;
Function<u32()> m_hash;
SQLType m_type { SQLType::Text };
bool m_is_null { true };
Variant<String, int, double> m_impl {};
};
}
|