Serenity Operating System
at master 86 lines 3.2 kB view raw
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 14namespace 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 */ 27class Tuple { 28public: 29 Tuple(); 30 explicit Tuple(NonnullRefPtr<TupleDescriptor> const&, u32 pointer = 0); 31 Tuple(NonnullRefPtr<TupleDescriptor> const&, Serializer&); 32 Tuple(Tuple const&); 33 virtual ~Tuple() = default; 34 35 Tuple& operator=(Tuple const&); 36 37 [[nodiscard]] DeprecatedString to_deprecated_string() const; 38 explicit operator DeprecatedString() const { return to_deprecated_string(); } 39 40 bool operator<(Tuple const& other) const { return compare(other) < 0; } 41 bool operator<=(Tuple const& other) const { return compare(other) <= 0; } 42 bool operator==(Tuple const& other) const { return compare(other) == 0; } 43 bool operator!=(Tuple const& other) const { return compare(other) != 0; } 44 bool operator>(Tuple const& other) const { return compare(other) > 0; } 45 bool operator>=(Tuple const& other) const { return compare(other) >= 0; } 46 47 [[nodiscard]] bool is_null() const { return m_data.is_empty(); } 48 [[nodiscard]] bool has(DeprecatedString const& name) const { return index_of(name).has_value(); } 49 50 Value const& operator[](size_t ix) const { return m_data[ix]; } 51 Value& operator[](size_t ix) { return m_data[ix]; } 52 Value const& operator[](DeprecatedString const& name) const; 53 Value& operator[](DeprecatedString const& name); 54 void append(Value const&); 55 Tuple& operator+=(Value const&); 56 void extend(Tuple const&); 57 [[nodiscard]] bool is_compatible(Tuple const&) const; 58 59 [[nodiscard]] u32 pointer() const { return m_pointer; } 60 void set_pointer(u32 ptr) { m_pointer = ptr; } 61 62 [[nodiscard]] size_t size() const { return m_data.size(); } 63 [[nodiscard]] virtual size_t length() const; 64 void clear() { m_data.clear(); } 65 [[nodiscard]] NonnullRefPtr<TupleDescriptor> descriptor() const { return m_descriptor; } 66 [[nodiscard]] int compare(Tuple const&) const; 67 [[nodiscard]] int match(Tuple const&) const; 68 [[nodiscard]] u32 hash() const; 69 70 [[nodiscard]] Vector<Value> take_data() { return move(m_data); } 71 72protected: 73 [[nodiscard]] Optional<size_t> index_of(StringView) const; 74 void copy_from(Tuple const&); 75 virtual void serialize(Serializer&) const; 76 virtual void deserialize(Serializer&); 77 78private: 79 NonnullRefPtr<TupleDescriptor> m_descriptor; 80 Vector<Value> m_data; 81 u32 m_pointer { 2 * sizeof(u32) }; 82 83 friend Serializer; 84}; 85 86}