/src/serenity/Userland/Libraries/LibSQL/Tuple.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2021, Jan de Visser <jan@de-visser.net> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/Vector.h> |
10 | | #include <LibSQL/Forward.h> |
11 | | #include <LibSQL/TupleDescriptor.h> |
12 | | #include <LibSQL/Value.h> |
13 | | |
14 | | namespace SQL { |
15 | | |
16 | | /** |
17 | | * A Tuple is an element of a random-access data structure persisted in a Heap. |
18 | | * Tuple objects stored in such a structure have a definition controlling the |
19 | | * number of parts or columns the tuple has, the types of the parts, and the |
20 | | * sort order of these parts. Besides having an optional definition, a Tuple |
21 | | * consists of one Value object per part. In addition, tuples have a u32 pointer |
22 | | * member which points to a Heap location. |
23 | | * |
24 | | * Tuple is a base class; concrete subclasses are Key, which implements the |
25 | | * elements of an index, and Row, which implements the rows in a table. |
26 | | */ |
27 | | class Tuple { |
28 | | public: |
29 | | Tuple(); |
30 | | explicit Tuple(NonnullRefPtr<TupleDescriptor> const&, Block::Index = 0); |
31 | | Tuple(NonnullRefPtr<TupleDescriptor> const&, Serializer&); |
32 | | Tuple(Tuple const&); |
33 | 0 | virtual ~Tuple() = default; |
34 | | |
35 | | Tuple& operator=(Tuple const&); |
36 | | |
37 | | [[nodiscard]] ByteString to_byte_string() const; |
38 | 0 | explicit operator ByteString() const { return to_byte_string(); } |
39 | | |
40 | 0 | bool operator<(Tuple const& other) const { return compare(other) < 0; } |
41 | 0 | bool operator<=(Tuple const& other) const { return compare(other) <= 0; } |
42 | 0 | bool operator==(Tuple const& other) const { return compare(other) == 0; } |
43 | 0 | bool operator!=(Tuple const& other) const { return compare(other) != 0; } |
44 | 0 | bool operator>(Tuple const& other) const { return compare(other) > 0; } |
45 | 0 | bool operator>=(Tuple const& other) const { return compare(other) >= 0; } |
46 | | |
47 | 0 | [[nodiscard]] bool is_null() const { return m_data.is_empty(); } |
48 | 0 | [[nodiscard]] bool has(ByteString const& name) const { return index_of(name).has_value(); } |
49 | | |
50 | 0 | Value const& operator[](size_t ix) const { return m_data[ix]; } |
51 | 0 | Value& operator[](size_t ix) { return m_data[ix]; } |
52 | | Value const& operator[](ByteString const& name) const; |
53 | | Value& operator[](ByteString const& name); |
54 | | void append(Value const&); |
55 | | Tuple& operator+=(Value const&); |
56 | | void extend(Tuple const&); |
57 | | |
58 | 0 | [[nodiscard]] Block::Index block_index() const { return m_block_index; } |
59 | 0 | void set_block_index(Block::Index index) { m_block_index = index; } |
60 | | |
61 | 0 | [[nodiscard]] size_t size() const { return m_data.size(); } |
62 | | [[nodiscard]] virtual size_t length() const; |
63 | 0 | void clear() { m_data.clear(); } |
64 | 0 | [[nodiscard]] NonnullRefPtr<TupleDescriptor> descriptor() const { return m_descriptor; } |
65 | | [[nodiscard]] int compare(Tuple const&) const; |
66 | | [[nodiscard]] int match(Tuple const&) const; |
67 | | [[nodiscard]] u32 hash() const; |
68 | | |
69 | 0 | [[nodiscard]] Vector<Value> take_data() { return move(m_data); } |
70 | | |
71 | | protected: |
72 | | [[nodiscard]] Optional<size_t> index_of(StringView) const; |
73 | | void copy_from(Tuple const&); |
74 | | virtual void serialize(Serializer&) const; |
75 | | virtual void deserialize(Serializer&); |
76 | | |
77 | | private: |
78 | | NonnullRefPtr<TupleDescriptor> m_descriptor; |
79 | | Vector<Value> m_data; |
80 | | Block::Index m_block_index { 0 }; |
81 | | |
82 | | friend Serializer; |
83 | | }; |
84 | | |
85 | | } |