| 1 | //===----------------------------------------------------------------------===// |
| 2 | // DuckDB |
| 3 | // |
| 4 | // duckdb/main/attached_database.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/main/config.hpp" |
| 15 | #include "duckdb/catalog/catalog_entry.hpp" |
| 16 | |
| 17 | namespace duckdb { |
| 18 | class Catalog; |
| 19 | class DatabaseInstance; |
| 20 | class StorageManager; |
| 21 | class TransactionManager; |
| 22 | class StorageExtension; |
| 23 | |
| 24 | struct AttachInfo; |
| 25 | |
| 26 | enum class AttachedDatabaseType { |
| 27 | READ_WRITE_DATABASE, |
| 28 | READ_ONLY_DATABASE, |
| 29 | SYSTEM_DATABASE, |
| 30 | TEMP_DATABASE, |
| 31 | }; |
| 32 | |
| 33 | //! The AttachedDatabase represents an attached database instance |
| 34 | class AttachedDatabase : public CatalogEntry { |
| 35 | public: |
| 36 | //! Create the built-in system attached database (without storage) |
| 37 | explicit AttachedDatabase(DatabaseInstance &db, AttachedDatabaseType type = AttachedDatabaseType::SYSTEM_DATABASE); |
| 38 | //! Create an attached database instance with the specified name and storage |
| 39 | AttachedDatabase(DatabaseInstance &db, Catalog &catalog, string name, string file_path, AccessMode access_mode); |
| 40 | //! Create an attached database instance with the specified storage extension |
| 41 | AttachedDatabase(DatabaseInstance &db, Catalog &catalog, StorageExtension &ext, string name, AttachInfo &info, |
| 42 | AccessMode access_mode); |
| 43 | ~AttachedDatabase() override; |
| 44 | |
| 45 | void Initialize(); |
| 46 | |
| 47 | Catalog &ParentCatalog() override; |
| 48 | StorageManager &GetStorageManager(); |
| 49 | Catalog &GetCatalog(); |
| 50 | TransactionManager &GetTransactionManager(); |
| 51 | DatabaseInstance &GetDatabase() { |
| 52 | return db; |
| 53 | } |
| 54 | const string &GetName() const { |
| 55 | return name; |
| 56 | } |
| 57 | bool IsSystem() const; |
| 58 | bool IsTemporary() const; |
| 59 | bool IsReadOnly() const; |
| 60 | bool IsInitialDatabase() const; |
| 61 | void SetInitialDatabase(); |
| 62 | |
| 63 | static string (const string &dbpath); |
| 64 | |
| 65 | private: |
| 66 | DatabaseInstance &db; |
| 67 | unique_ptr<StorageManager> storage; |
| 68 | unique_ptr<Catalog> catalog; |
| 69 | unique_ptr<TransactionManager> transaction_manager; |
| 70 | AttachedDatabaseType type; |
| 71 | optional_ptr<Catalog> parent_catalog; |
| 72 | bool is_initial_database = false; |
| 73 | }; |
| 74 | |
| 75 | } // namespace duckdb |
| 76 | |