Database.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. ErrorOr<void> add_table(TableDef& table);
  33. static Key get_table_key(String const&, String const&);
  34. ResultOr<RefPtr<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> update(Row&);
  39. private:
  40. explicit Database(String);
  41. bool m_open { false };
  42. NonnullRefPtr<Heap> m_heap;
  43. Serializer m_serializer;
  44. RefPtr<BTree> m_schemas;
  45. RefPtr<BTree> m_tables;
  46. RefPtr<BTree> m_table_columns;
  47. HashMap<u32, NonnullRefPtr<SchemaDef>> m_schema_cache;
  48. HashMap<u32, RefPtr<TableDef>> m_table_cache;
  49. };
  50. }