Database.h 1.6 KB

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