1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/main/database.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/main/config.hpp"
12#include "duckdb/main/valid_checker.hpp"
13#include "duckdb/common/winapi.hpp"
14#include "duckdb/main/extension.hpp"
15
16namespace duckdb {
17class BufferManager;
18class DatabaseManager;
19class StorageManager;
20class Catalog;
21class TransactionManager;
22class ConnectionManager;
23class FileSystem;
24class TaskScheduler;
25class ObjectCache;
26struct AttachInfo;
27
28class DatabaseInstance : public std::enable_shared_from_this<DatabaseInstance> {
29 friend class DuckDB;
30
31public:
32 DUCKDB_API DatabaseInstance();
33 DUCKDB_API ~DatabaseInstance();
34
35 DBConfig config;
36
37public:
38 BufferPool &GetBufferPool();
39 DUCKDB_API BufferManager &GetBufferManager();
40 DUCKDB_API DatabaseManager &GetDatabaseManager();
41 DUCKDB_API FileSystem &GetFileSystem();
42 DUCKDB_API TaskScheduler &GetScheduler();
43 DUCKDB_API ObjectCache &GetObjectCache();
44 DUCKDB_API ConnectionManager &GetConnectionManager();
45 DUCKDB_API ValidChecker &GetValidChecker();
46 DUCKDB_API void SetExtensionLoaded(const std::string &extension_name);
47
48 idx_t NumberOfThreads();
49
50 DUCKDB_API static DatabaseInstance &GetDatabase(ClientContext &context);
51
52 DUCKDB_API const unordered_set<std::string> &LoadedExtensions();
53 DUCKDB_API bool ExtensionIsLoaded(const std::string &name);
54
55 DUCKDB_API bool TryGetCurrentSetting(const std::string &key, Value &result);
56
57 unique_ptr<AttachedDatabase> CreateAttachedDatabase(AttachInfo &info, const string &type, AccessMode access_mode);
58
59private:
60 void Initialize(const char *path, DBConfig *config);
61 void CreateMainDatabase();
62
63 void Configure(DBConfig &config);
64
65private:
66 unique_ptr<BufferManager> buffer_manager;
67 unique_ptr<DatabaseManager> db_manager;
68 unique_ptr<TaskScheduler> scheduler;
69 unique_ptr<ObjectCache> object_cache;
70 unique_ptr<ConnectionManager> connection_manager;
71 unordered_set<std::string> loaded_extensions;
72 ValidChecker db_validity;
73};
74
75//! The database object. This object holds the catalog and all the
76//! database-specific meta information.
77class DuckDB {
78public:
79 DUCKDB_API explicit DuckDB(const char *path = nullptr, DBConfig *config = nullptr);
80 DUCKDB_API explicit DuckDB(const string &path, DBConfig *config = nullptr);
81 DUCKDB_API explicit DuckDB(DatabaseInstance &instance);
82
83 DUCKDB_API ~DuckDB();
84
85 //! Reference to the actual database instance
86 shared_ptr<DatabaseInstance> instance;
87
88public:
89 template <class T>
90 void LoadExtension() {
91 T extension;
92 if (ExtensionIsLoaded(name: extension.Name())) {
93 return;
94 }
95 extension.Load(*this);
96 instance->SetExtensionLoaded(extension.Name());
97 }
98
99 DUCKDB_API FileSystem &GetFileSystem();
100
101 DUCKDB_API idx_t NumberOfThreads();
102 DUCKDB_API static const char *SourceID();
103 DUCKDB_API static const char *LibraryVersion();
104 DUCKDB_API static idx_t StandardVectorSize();
105 DUCKDB_API static string Platform();
106 DUCKDB_API bool ExtensionIsLoaded(const std::string &name);
107};
108
109} // namespace duckdb
110