Database.h 1.9 KB

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