1 | //===----------------------------------------------------------------------===// |
---|---|
2 | // DuckDB |
3 | // |
4 | // duckdb/main/database_manager.hpp |
5 | // |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #pragma once |
10 | |
11 | #include "duckdb/common/common.hpp" |
12 | #include "duckdb/common/case_insensitive_map.hpp" |
13 | #include "duckdb/common/mutex.hpp" |
14 | #include "duckdb/common/atomic.hpp" |
15 | #include "duckdb/common/optional_ptr.hpp" |
16 | #include "duckdb/common/enums/on_entry_not_found.hpp" |
17 | |
18 | namespace duckdb { |
19 | class AttachedDatabase; |
20 | class Catalog; |
21 | class CatalogSet; |
22 | class ClientContext; |
23 | class DatabaseInstance; |
24 | |
25 | //! The DatabaseManager is a class that sits at the root of all attached databases |
26 | class DatabaseManager { |
27 | friend class Catalog; |
28 | |
29 | public: |
30 | explicit DatabaseManager(DatabaseInstance &db); |
31 | ~DatabaseManager(); |
32 | |
33 | public: |
34 | static DatabaseManager &Get(DatabaseInstance &db); |
35 | static DatabaseManager &Get(ClientContext &db); |
36 | static DatabaseManager &Get(AttachedDatabase &db); |
37 | |
38 | void InitializeSystemCatalog(); |
39 | //! Get an attached database with the given name |
40 | optional_ptr<AttachedDatabase> GetDatabase(ClientContext &context, const string &name); |
41 | //! Add a new attached database to the database manager |
42 | void AddDatabase(ClientContext &context, unique_ptr<AttachedDatabase> db); |
43 | void DetachDatabase(ClientContext &context, const string &name, OnEntryNotFound if_not_found); |
44 | //! Returns a reference to the system catalog |
45 | Catalog &GetSystemCatalog(); |
46 | static const string &GetDefaultDatabase(ClientContext &context); |
47 | void SetDefaultDatabase(ClientContext &context, const string &new_value); |
48 | |
49 | optional_ptr<AttachedDatabase> GetDatabaseFromPath(ClientContext &context, const string &path); |
50 | vector<reference<AttachedDatabase>> GetDatabases(ClientContext &context); |
51 | |
52 | transaction_t GetNewQueryNumber() { |
53 | return current_query_number++; |
54 | } |
55 | transaction_t ActiveQueryNumber() const { |
56 | return current_query_number; |
57 | } |
58 | idx_t ModifyCatalog() { |
59 | return catalog_version++; |
60 | } |
61 | bool HasDefaultDatabase() { |
62 | return !default_database.empty(); |
63 | } |
64 | |
65 | private: |
66 | //! The system database is a special database that holds system entries (e.g. functions) |
67 | unique_ptr<AttachedDatabase> system; |
68 | //! The set of attached databases |
69 | unique_ptr<CatalogSet> databases; |
70 | //! The global catalog version, incremented whenever anything changes in the catalog |
71 | atomic<idx_t> catalog_version; |
72 | //! The current query number |
73 | atomic<transaction_t> current_query_number; |
74 | //! The current default database |
75 | string default_database; |
76 | }; |
77 | |
78 | } // namespace duckdb |
79 |