Database.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. namespace SQL {
  13. /**
  14. * A Database object logically connects a Heap with the SQL data we want
  15. * to store in it. It has BTree pointers for B-Trees holding the definitions
  16. * of tables, columns, indexes, and other SQL objects.
  17. */
  18. class Database : public Core::Object {
  19. C_OBJECT(Database);
  20. public:
  21. explicit Database(String);
  22. ~Database() override = default;
  23. void commit() { m_heap->flush(); }
  24. void add_schema(SchemaDef const&);
  25. static Key get_schema_key(String const&);
  26. RefPtr<SchemaDef> get_schema(String const&);
  27. void add_table(TableDef& table);
  28. static Key get_table_key(String const&, String const&);
  29. RefPtr<TableDef> get_table(String const&, String const&);
  30. Vector<Row> select_all(TableDef const&);
  31. Vector<Row> match(TableDef const&, Key const&);
  32. bool insert(Row&);
  33. bool update(Row&);
  34. private:
  35. RefPtr<Heap> m_heap;
  36. RefPtr<BTree> m_schemas;
  37. RefPtr<BTree> m_tables;
  38. RefPtr<BTree> m_table_columns;
  39. HashMap<u32, RefPtr<SchemaDef>> m_schema_cache;
  40. HashMap<u32, RefPtr<TableDef>> m_table_cache;
  41. };
  42. }