summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibSQL/Index.h
blob: 751091b74b8cad6dbfed762300c592eaf7fc75a4 (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
/*
 * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <LibCore/Object.h>
#include <LibSQL/Forward.h>
#include <LibSQL/Meta.h>
#include <LibSQL/Serializer.h>

namespace SQL {

class IndexNode {
public:
    virtual ~IndexNode() = default;
    [[nodiscard]] Block::Index block_index() const { return m_block_index; }
    IndexNode* as_index_node() { return dynamic_cast<IndexNode*>(this); }

protected:
    explicit IndexNode(Block::Index block_index)
        : m_block_index(block_index)
    {
    }

    void set_block_index(Block::Index block_index) { m_block_index = block_index; }

private:
    Block::Index m_block_index;
};

class Index : public Core::Object {
    C_OBJECT_ABSTRACT(Index);

public:
    ~Index() override = default;

    NonnullRefPtr<TupleDescriptor> descriptor() const { return m_descriptor; }
    [[nodiscard]] bool duplicates_allowed() const { return !m_unique; }
    [[nodiscard]] bool unique() const { return m_unique; }
    [[nodiscard]] Block::Index block_index() const { return m_block_index; }

protected:
    Index(Serializer&, NonnullRefPtr<TupleDescriptor> const&, bool unique, Block::Index block_index);
    Index(Serializer&, NonnullRefPtr<TupleDescriptor> const&, Block::Index block_index);

    [[nodiscard]] Serializer& serializer() { return m_serializer; }
    void set_block_index(Block::Index block_index) { m_block_index = block_index; }
    u32 request_new_block_index() { return m_serializer.request_new_block_index(); }

private:
    Serializer m_serializer;
    NonnullRefPtr<TupleDescriptor> m_descriptor;
    bool m_unique { false };
    Block::Index m_block_index { 0 };
};

}