Database.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. explicit Database(String);
  23. ~Database() override = default;
  24. void commit() { m_heap->flush(); }
  25. void add_schema(SchemaDef const&);
  26. static Key get_schema_key(String const&);
  27. RefPtr<SchemaDef> get_schema(String const&);
  28. void add_table(TableDef& table);
  29. static Key get_table_key(String const&, String const&);
  30. RefPtr<TableDef> get_table(String const&, String const&);
  31. Vector<Row> select_all(TableDef const&);
  32. Vector<Row> match(TableDef const&, Key const&);
  33. bool insert(Row&);
  34. bool update(Row&);
  35. private:
  36. NonnullRefPtr<Heap> m_heap;
  37. Serializer m_serializer;
  38. RefPtr<BTree> m_schemas;
  39. RefPtr<BTree> m_tables;
  40. RefPtr<BTree> m_table_columns;
  41. HashMap<u32, RefPtr<SchemaDef>> m_schema_cache;
  42. HashMap<u32, RefPtr<TableDef>> m_table_cache;
  43. };
  44. }