Database.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/RefPtr.h>
  8. #include <AK/String.h>
  9. #include <LibCore/Object.h>
  10. #include <LibSQL/Forward.h>
  11. #include <LibSQL/Heap.h>
  12. #include <LibSQL/Meta.h>
  13. namespace SQL {
  14. /**
  15. * A Database object logically connects a Heap with the SQL data we want
  16. * to store in it. It has BTree pointers for B-Trees holding the definitions
  17. * of tables, columns, indexes, and other SQL objects.
  18. */
  19. class Database : public Core::Object {
  20. C_OBJECT(Database);
  21. public:
  22. ~Database() override;
  23. ErrorOr<void> open();
  24. bool is_open() const { return m_open; }
  25. ErrorOr<void> commit();
  26. ErrorOr<void> add_schema(SchemaDef const&);
  27. static Key get_schema_key(String const&);
  28. ErrorOr<RefPtr<SchemaDef>> get_schema(String const&);
  29. ErrorOr<void> add_table(TableDef& table);
  30. static Key get_table_key(String const&, String const&);
  31. ErrorOr<RefPtr<TableDef>> get_table(String const&, String const&);
  32. ErrorOr<Vector<Row>> select_all(TableDef const&);
  33. ErrorOr<Vector<Row>> match(TableDef const&, Key const&);
  34. ErrorOr<void> insert(Row&);
  35. ErrorOr<void> update(Row&);
  36. private:
  37. explicit Database(String);
  38. bool m_open { false };
  39. NonnullRefPtr<Heap> m_heap;
  40. Serializer m_serializer;
  41. RefPtr<BTree> m_schemas;
  42. RefPtr<BTree> m_tables;
  43. RefPtr<BTree> m_table_columns;
  44. HashMap<u32, RefPtr<SchemaDef>> m_schema_cache;
  45. HashMap<u32, RefPtr<TableDef>> m_table_cache;
  46. };
  47. }