Database.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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/Result.h>
  15. #include <LibSQL/Serializer.h>
  16. namespace SQL {
  17. /**
  18. * A Database object logically connects a Heap with the SQL data we want
  19. * to store in it. It has BTree pointers for B-Trees holding the definitions
  20. * of tables, columns, indexes, and other SQL objects.
  21. */
  22. class Database : public Core::Object {
  23. C_OBJECT(Database);
  24. public:
  25. ~Database() override;
  26. ResultOr<void> open();
  27. bool is_open() const { return m_open; }
  28. ErrorOr<void> commit();
  29. ResultOr<void> add_schema(SchemaDef const&);
  30. static Key get_schema_key(String const&);
  31. ResultOr<NonnullRefPtr<SchemaDef>> get_schema(String const&);
  32. ResultOr<void> add_table(TableDef& table);
  33. static Key get_table_key(String const&, String const&);
  34. ResultOr<NonnullRefPtr<TableDef>> get_table(String const&, String const&);
  35. ErrorOr<Vector<Row>> select_all(TableDef const&);
  36. ErrorOr<Vector<Row>> match(TableDef const&, Key const&);
  37. ErrorOr<void> insert(Row&);
  38. ErrorOr<void> remove(Row&);
  39. ErrorOr<void> update(Row&);
  40. private:
  41. explicit Database(String);
  42. bool m_open { false };
  43. NonnullRefPtr<Heap> m_heap;
  44. Serializer m_serializer;
  45. RefPtr<BTree> m_schemas;
  46. RefPtr<BTree> m_tables;
  47. RefPtr<BTree> m_table_columns;
  48. HashMap<u32, NonnullRefPtr<SchemaDef>> m_schema_cache;
  49. HashMap<u32, NonnullRefPtr<TableDef>> m_table_cache;
  50. };
  51. }