1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:47:36 +00:00

LibSQL: Store a NonnullRefPtr to the table definition in SQL::Row

Also return a direct reference to the table from its getter.
This commit is contained in:
Timothy Flynn 2022-11-29 08:52:09 -05:00 committed by Linus Groh
parent 5336f23c7e
commit 6d3f68cc11
3 changed files with 13 additions and 12 deletions

View file

@ -199,25 +199,25 @@ ErrorOr<Vector<Row>> Database::match(TableDef const& table, Key const& key)
ErrorOr<void> Database::insert(Row& row) ErrorOr<void> Database::insert(Row& row)
{ {
VERIFY(m_table_cache.get(row.table()->key().hash()).has_value()); VERIFY(m_table_cache.get(row.table().key().hash()).has_value());
// TODO Check constraints // TODO Check constraints
row.set_pointer(m_heap->new_record_pointer()); row.set_pointer(m_heap->new_record_pointer());
row.set_next_pointer(row.table()->pointer()); row.set_next_pointer(row.table().pointer());
TRY(update(row)); TRY(update(row));
// TODO update indexes defined on table. // TODO update indexes defined on table.
auto table_key = row.table()->key(); auto table_key = row.table().key();
table_key.set_pointer(row.pointer()); table_key.set_pointer(row.pointer());
VERIFY(m_tables->update_key_pointer(table_key)); VERIFY(m_tables->update_key_pointer(table_key));
row.table()->set_pointer(row.pointer()); row.table().set_pointer(row.pointer());
return {}; return {};
} }
ErrorOr<void> Database::update(Row& tuple) ErrorOr<void> Database::update(Row& tuple)
{ {
VERIFY(m_table_cache.get(tuple.table()->key().hash()).has_value()); VERIFY(m_table_cache.get(tuple.table().key().hash()).has_value());
// TODO Check constraints // TODO Check constraints
m_serializer.reset(); m_serializer.reset();
m_serializer.serialize_and_write<Tuple>(tuple); m_serializer.serialize_and_write<Tuple>(tuple);

View file

@ -9,9 +9,9 @@
namespace SQL { namespace SQL {
Row::Row(RefPtr<TableDef> table, u32 pointer) Row::Row(NonnullRefPtr<TableDef> table, u32 pointer)
: Tuple(table->to_tuple_descriptor()) : Tuple(table->to_tuple_descriptor())
, m_table(table) , m_table(move(table))
{ {
set_pointer(pointer); set_pointer(pointer);
} }

View file

@ -6,8 +6,7 @@
#pragma once #pragma once
#include <AK/ByteBuffer.h> #include <AK/NonnullRefPtr.h>
#include <AK/RefPtr.h>
#include <LibSQL/Forward.h> #include <LibSQL/Forward.h>
#include <LibSQL/Meta.h> #include <LibSQL/Meta.h>
#include <LibSQL/Tuple.h> #include <LibSQL/Tuple.h>
@ -26,19 +25,21 @@ namespace SQL {
*/ */
class Row : public Tuple { class Row : public Tuple {
public: public:
explicit Row(RefPtr<TableDef>, u32 pointer = 0); explicit Row(NonnullRefPtr<TableDef>, u32 pointer = 0);
virtual ~Row() override = default; virtual ~Row() override = default;
[[nodiscard]] u32 next_pointer() const { return m_next_pointer; } [[nodiscard]] u32 next_pointer() const { return m_next_pointer; }
void set_next_pointer(u32 ptr) { m_next_pointer = ptr; } void set_next_pointer(u32 ptr) { m_next_pointer = ptr; }
RefPtr<TableDef> table() const { return m_table; } TableDef const& table() const { return *m_table; }
TableDef& table() { return *m_table; }
[[nodiscard]] virtual size_t length() const override { return Tuple::length() + sizeof(u32); } [[nodiscard]] virtual size_t length() const override { return Tuple::length() + sizeof(u32); }
virtual void serialize(Serializer&) const override; virtual void serialize(Serializer&) const override;
virtual void deserialize(Serializer&) override; virtual void deserialize(Serializer&) override;
private: private:
RefPtr<TableDef> m_table; NonnullRefPtr<TableDef> m_table;
u32 m_next_pointer { 0 }; u32 m_next_pointer { 0 };
}; };