Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::DatabaseManager Class Reference

The DatabaseManager is a class that sits at the root of all attached databases. More...

#include <duckdb.hpp>

Collaboration diagram for duckdb::DatabaseManager:

Public Member Functions

 DatabaseManager (DatabaseInstance &db)
DatabaseInstance & GetInstance ()
void InitializeSystemCatalog ()
 Initializes the system catalog of the attached SYSTEM_DATABASE.
void FinalizeStartup ()
 Finalize starting up the system.
optional_ptr< AttachedDatabase > GetDatabase (ClientContext &context, const string &name)
 Get an attached database by its name.
shared_ptr< AttachedDatabase > GetDatabase (const string &name)
shared_ptr< AttachedDatabase > AttachDatabase (ClientContext &context, AttachInfo &info, AttachOptions &options)
 Attach a new database.
void DetachDatabase (ClientContext &context, const string &name, OnEntryNotFound if_not_found)
 Detach an existing database.
void Alter (ClientContext &context, AlterInfo &info)
 Alter operation dispatcher.
shared_ptr< AttachedDatabase > DetachInternal (const string &name)
 Rollback the attach of a database.
Catalog & GetSystemCatalog ()
 Returns a reference to the system catalog.
void SetDefaultDatabase (ClientContext &context, const string &new_value)
InsertDatabasePathResult InsertDatabasePath (const AttachInfo &info, AttachOptions &options)
 Inserts a path to name mapping to the database paths map.
void GetDatabaseType (ClientContext &context, AttachInfo &info, const DBConfig &config, AttachOptions &options)
 Returns the database type. This might require checking the header of the file, in which case the file handle is necessary. We can only grab the file handle, if it is not yet held, even for uncommitted changes. Thus, we have to lock for this operation.
vector< shared_ptr< AttachedDatabase > > GetDatabases (ClientContext &context, const optional_idx max_db_count=optional_idx())
 Scans the catalog set and adds each committed database entry, and each database entry of the current transaction, to a vector holding AttachedDatabase references.
vector< shared_ptr< AttachedDatabase > > GetDatabases ()
 Scans the catalog set and returns each committed database entry.
idx_t ApproxDatabaseCount ()
 Returns the approximate count of attached databases.
void ResetDatabases ()
 Removes all databases from the catalog set. This is necessary for the database instance's destructor, as the database manager has to be alive when destroying the catalog set objects.
transaction_t GetNewQueryNumber ()
transaction_t ActiveQueryNumber () const
transaction_t GetNewTransactionNumber ()
transaction_t ActiveTransactionNumber () const
idx_t NextOid ()
bool HasDefaultDatabase ()
vector< string > GetAttachedDatabasePaths ()
 Gets a list of all attached database paths.
shared_ptr< AttachedDatabase > GetDatabaseInternal (const lock_guard< mutex > &, const string &name)

Static Public Member Functions

static DatabaseManager & Get (DatabaseInstance &db)
static DatabaseManager & Get (ClientContext &db)
static DatabaseManager & Get (AttachedDatabase &db)
static const string & GetDefaultDatabase (ClientContext &context)

Private Member Functions

optional_ptr< AttachedDatabase > FinalizeAttach (ClientContext &context, AttachInfo &info, shared_ptr< AttachedDatabase > database)
void RenameDatabase (ClientContext &context, const string &old_name, const string &new_name, OnEntryNotFound if_not_found)
 Rename an existing database.

Private Attributes

DatabaseInstance & db
shared_ptr< AttachedDatabase > system
 The system database is a special database that holds system entries (e.g. functions).
mutex databases_lock
 Lock for databases.
case_insensitive_map_t< shared_ptr< AttachedDatabase > > databases
 The set of attached databases.
atomic< idx_t > next_oid
 The next object id handed out by the NextOid method.
atomic< transaction_t > current_query_number
 The current query number.
atomic< transaction_t > current_transaction_id
 The current transaction number.
string default_database
 The current default database.
shared_ptr< DatabaseFilePathManager > path_manager
 Manager for ensuring we never open the same database file twice in the same program.

Friends

class Catalog

Detailed Description

The DatabaseManager is a class that sits at the root of all attached databases.

Constructor & Destructor Documentation

◆ DatabaseManager()

duckdb::DatabaseManager::DatabaseManager ( DatabaseInstance & db)
explicit
277438 : db(db), next_oid(20000), current_query_number(1), current_transaction_id(0) {
277439 system = make_shared_ptr<AttachedDatabase>(db);
277440 auto &config = DBConfig::GetConfig(db);
277441 path_manager = config.path_manager;
277442 if (!path_manager) {
277443 // no shared path manager
277444 path_manager = make_shared_ptr<DatabaseFilePathManager>();
277445 }
277446}
atomic< idx_t > next_oid
The next object id handed out by the NextOid method.
Definition duckdb.hpp:40852
atomic< transaction_t > current_transaction_id
The current transaction number.
Definition duckdb.hpp:40856
shared_ptr< DatabaseFilePathManager > path_manager
Manager for ensuring we never open the same database file twice in the same program.
Definition duckdb.hpp:40860
shared_ptr< AttachedDatabase > system
The system database is a special database that holds system entries (e.g. functions).
Definition duckdb.hpp:40846
atomic< transaction_t > current_query_number
The current query number.
Definition duckdb.hpp:40854

◆ ~DatabaseManager()

duckdb::DatabaseManager::~DatabaseManager ( )
277448 {
277449}

Member Function Documentation

◆ Get() [1/3]

DatabaseManager & duckdb::DatabaseManager::Get ( DatabaseInstance & db)
static
277101 {
277102 return db.GetDatabaseManager();
277103}

◆ Get() [2/3]

DatabaseManager & duckdb::DatabaseManager::Get ( ClientContext & db)
static
277105 {
277106 return DatabaseManager::Get(*db.db);
277107}

◆ Get() [3/3]

DatabaseManager & duckdb::DatabaseManager::Get ( AttachedDatabase & db)
static
277451 {
277452 return DatabaseManager::Get(db.GetDatabase());
277453}

◆ GetInstance()

DatabaseInstance & duckdb::DatabaseManager::GetInstance ( )
inline
40772 {
40773 return db;
40774 }

◆ InitializeSystemCatalog()

void duckdb::DatabaseManager::InitializeSystemCatalog ( )

Initializes the system catalog of the attached SYSTEM_DATABASE.

277455 {
277456 // The SYSTEM_DATABASE has no persistent storage.
277457 system->Initialize();
277458}

◆ FinalizeStartup()

void duckdb::DatabaseManager::FinalizeStartup ( )

Finalize starting up the system.

277460 {
277461 auto dbs = GetDatabases();
277462 for (auto &db : dbs) {
277463 db->FinalizeLoad(nullptr);
277464 }
277465}
vector< shared_ptr< AttachedDatabase > > GetDatabases()
Scans the catalog set and returns each committed database entry.
Definition duckdb.cpp:277857
Here is the call graph for this function:

◆ GetDatabase() [1/2]

optional_ptr< AttachedDatabase > duckdb::DatabaseManager::GetDatabase ( ClientContext & context,
const string & name )

Get an attached database by its name.

277467 {
277468 auto &meta_transaction = MetaTransaction::Get(context);
277469 // first check if we have a local reference to this database already
277470 auto database = meta_transaction.GetReferencedDatabase(name);
277471 if (database) {
277472 // we do! return it
277473 return database;
277474 }
277475 lock_guard<mutex> guard(databases_lock);
277476 shared_ptr<AttachedDatabase> db;
277477 if (StringUtil::Lower(name) == TEMP_CATALOG) {
277478 db = context.client_data->temporary_objects;
277479 } else {
277480 db = GetDatabaseInternal(guard, name);
277481 }
277482 if (!db) {
277483 return nullptr;
277484 }
277485 return meta_transaction.UseDatabase(db);
277486}
mutex databases_lock
Lock for databases.
Definition duckdb.hpp:40848
static DUCKDB_API string Lower(const string &str)
Convert a string to lowercase.
Definition duckdb.cpp:109630
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetDatabase() [2/2]

shared_ptr< AttachedDatabase > duckdb::DatabaseManager::GetDatabase ( const string & name)
277488 {
277489 lock_guard<mutex> guard(databases_lock);
277490 return GetDatabaseInternal(guard, name);
277491}

◆ AttachDatabase()

shared_ptr< AttachedDatabase > duckdb::DatabaseManager::AttachDatabase ( ClientContext & context,
AttachInfo & info,
AttachOptions & options )

Attach a new database.

Initialize the database.

277520 {
277521 string extension = "";
277522 if (FileSystem::IsRemoteFile(info.path, extension)) {
277523 if (options.access_mode == AccessMode::AUTOMATIC) {
277524 // Attaching of remote files gets bumped to READ_ONLY
277525 // This is due to the fact that on most (all?) remote files writes to DB are not available
277526 // and having this raised later is not super helpful
277527 options.access_mode = AccessMode::READ_ONLY;
277528 }
277529 }
277530 bool requires_tracking_attaches = RequiresTrackingAttaches(info.path, options.db_type);
277531 if (requires_tracking_attaches) {
277532 // canonicalize the path to the database
277533 auto &fs = FileSystem::GetFileSystem(context);
277534 info.path = fs.CanonicalizePath(info.path);
277535 }
277536
277537 // for IGNORE / REPLACE ON CONFLICT - first look for an existing entry
277538 if (info.on_conflict == OnCreateConflict::IGNORE_ON_CONFLICT ||
277539 info.on_conflict == OnCreateConflict::REPLACE_ON_CONFLICT) {
277540 // constant-time lookup in the catalog for the db name
277541 auto existing_db = GetDatabase(info.name);
277542 if (existing_db) {
277543 if ((existing_db->IsReadOnly() && options.access_mode == AccessMode::READ_WRITE) ||
277544 (!existing_db->IsReadOnly() && options.access_mode == AccessMode::READ_ONLY)) {
277545 auto existing_mode = existing_db->IsReadOnly() ? AccessMode::READ_ONLY : AccessMode::READ_WRITE;
277546 auto existing_mode_str = EnumUtil::ToString(existing_mode);
277547 auto attached_mode = EnumUtil::ToString(options.access_mode);
277548 throw BinderException("Database \"%s\" is already attached in %s mode, cannot re-attach in %s mode",
277549 info.name, existing_mode_str, attached_mode);
277550 }
277551 if (!options.default_table.name.empty()) {
277552 existing_db->GetCatalog().SetDefaultTable(options.default_table.schema, options.default_table.name);
277553 }
277554 if (info.on_conflict == OnCreateConflict::REPLACE_ON_CONFLICT) {
277555 // allow custom catalogs to override this behavior
277556 if (!existing_db->GetCatalog().HasConflictingAttachOptions(info.path, options)) {
277557 return existing_db;
277558 }
277559 } else {
277560 return existing_db;
277561 }
277562 }
277563 }
277564
277565 if (requires_tracking_attaches) {
277566 // Start timing the ATTACH-delay step.
277567 auto profiler = context.client_data->profiler->StartTimer(MetricType::WAITING_TO_ATTACH_LATENCY);
277568
277569 while (InsertDatabasePath(info, options) == InsertDatabasePathResult::ALREADY_EXISTS) {
277570 // database with this name and path already exists
277571 // first check if it exists within this transaction
277572 auto &meta_transaction = MetaTransaction::Get(context);
277573 auto existing_db = meta_transaction.GetReferencedDatabaseOwning(info.name);
277574 if (existing_db) {
277575 // it does! return it
277576 return existing_db;
277577 }
277578
277579 // ... but it might not be done attaching yet!
277580 // verify the database has actually finished attaching prior to returning
277581 lock_guard<mutex> guard(databases_lock);
277582 auto entry = databases.find(info.name);
277583 if (entry != databases.end()) {
277584 // The database ACTUALLY exists, so we return it.
277585 return entry->second;
277586 }
277587 if (context.interrupted) {
277588 throw InterruptException();
277589 }
277590 }
277591 }
277592 auto &config = DBConfig::GetConfig(context);
277593 GetDatabaseType(context, info, config, options);
277594 if (!options.db_type.empty()) {
277595 // we only need to prevent duplicate opening of DuckDB files
277596 // if this is not a DuckDB file but e.g. a CSV or Parquet file, we don't need to do this duplicate protection
277597 options.stored_database_path.reset();
277598 }
277599 if (AttachedDatabase::NameIsReserved(info.name)) {
277600 throw BinderException("Attached database name \"%s\" cannot be used because it is a reserved name", info.name);
277601 }
277602 if (!extension.empty()) {
277603 if (!ExtensionHelper::TryAutoLoadExtension(context, extension)) {
277604 throw MissingExtensionException("Attaching path '%s' requires extension '%s' to be loaded", info.path,
277605 extension);
277606 }
277607 }
277608
277609 // now create the attached database
277610 auto &db = DatabaseInstance::GetDatabase(context);
277611 auto attached_db = db.CreateAttachedDatabase(context, info, options);
277612
277614 if (options.is_main_database) {
277615 attached_db->SetInitialDatabase();
277616 attached_db->Initialize(context);
277617 } else {
277618 attached_db->Initialize(context);
277619 if (!options.default_table.name.empty()) {
277620 attached_db->GetCatalog().SetDefaultTable(options.default_table.schema, options.default_table.name);
277621 }
277622 attached_db->FinalizeLoad(context);
277623 }
277624
277625 FinalizeAttach(context, info, attached_db);
277626 return attached_db;
277627}
void GetDatabaseType(ClientContext &context, AttachInfo &info, const DBConfig &config, AttachOptions &options)
Returns the database type. This might require checking the header of the file, in which case the file...
Definition duckdb.cpp:277772
InsertDatabasePathResult InsertDatabasePath(const AttachInfo &info, AttachOptions &options)
Inserts a path to name mapping to the database paths map.
Definition duckdb.cpp:277750
case_insensitive_map_t< shared_ptr< AttachedDatabase > > databases
The set of attached databases.
Definition duckdb.hpp:40850
optional_ptr< AttachedDatabase > GetDatabase(ClientContext &context, const string &name)
Get an attached database by its name.
Definition duckdb.cpp:277467
static DUCKDB_API bool TryAutoLoadExtension(DatabaseInstance &db, const string &extension_name) noexcept
Autoload an extension (depending on config, potentially a nop. Returns false on failure).
Definition duckdb.cpp:278501
static DUCKDB_API bool IsRemoteFile(const string &path)
Whether or not a file is remote or local, based only on file path.
Definition duckdb.cpp:74413
Here is the call graph for this function:

◆ DetachDatabase()

void duckdb::DatabaseManager::DetachDatabase ( ClientContext & context,
const string & name,
OnEntryNotFound if_not_found )

Detach an existing database.

277663 {
277664 if (StringUtil::CIEquals(GetDefaultDatabase(context), name)) {
277665 throw BinderException("Cannot detach database \"%s\" because it is the default database. Select a different "
277666 "database using `USE` to allow detaching this database",
277667 name);
277668 }
277669
277670 auto attached_db = DetachInternal(name);
277671 if (!attached_db) {
277672 if (if_not_found == OnEntryNotFound::THROW_EXCEPTION) {
277673 throw BinderException("Failed to detach database with name \"%s\": database not found", name);
277674 }
277675 return;
277676 }
277677
277678 attached_db->OnDetach(context);
277679
277680 // DetachInternal removes the AttachedDatabase from the list of databases that can be referenced.
277681 AttachedDatabase::InvokeCloseIfLastReference(attached_db);
277682}
shared_ptr< AttachedDatabase > DetachInternal(const string &name)
Rollback the attach of a database.
Definition duckdb.cpp:277732
static DUCKDB_API bool CIEquals(const string &l1, const string &l2)
Case insensitive equals.
Definition duckdb.cpp:109696
Here is the call graph for this function:

◆ Alter()

void duckdb::DatabaseManager::Alter ( ClientContext & context,
AlterInfo & info )

Alter operation dispatcher.

277684 {
277685 auto &db_info = info.Cast<AlterDatabaseInfo>();
277686
277687 switch (db_info.alter_database_type) {
277688 case AlterDatabaseType::RENAME_DATABASE: {
277689 auto &rename_info = db_info.Cast<RenameDatabaseInfo>();
277690 RenameDatabase(context, db_info.catalog, rename_info.new_name, db_info.if_not_found);
277691 break;
277692 }
277693 default:
277694 throw InternalException("Unsupported ALTER DATABASE operation");
277695 }
277696}
void RenameDatabase(ClientContext &context, const string &old_name, const string &new_name, OnEntryNotFound if_not_found)
Rename an existing database.
Definition duckdb.cpp:277698
Here is the call graph for this function:

◆ DetachInternal()

shared_ptr< AttachedDatabase > duckdb::DatabaseManager::DetachInternal ( const string & name)

Rollback the attach of a database.

277732 {
277733 shared_ptr<AttachedDatabase> attached_db;
277734 {
277735 lock_guard<mutex> guard(databases_lock);
277736 auto entry = databases.find(name);
277737 if (entry == databases.end()) {
277738 return nullptr;
277739 }
277740 attached_db = std::move(entry->second);
277741 databases.erase(entry);
277742 }
277743 return attached_db;
277744}
Here is the caller graph for this function:

◆ GetSystemCatalog()

Catalog & duckdb::DatabaseManager::GetSystemCatalog ( )

Returns a reference to the system catalog.

277876 {
277877 D_ASSERT(system);
277878 return system->GetCatalog();
277879}

◆ GetDefaultDatabase()

const string & duckdb::DatabaseManager::GetDefaultDatabase ( ClientContext & context)
static
277805 {
277806 auto &config = ClientData::Get(context);
277807 auto &default_entry = config.catalog_search_path->GetDefault();
277808 if (IsInvalidCatalog(default_entry.catalog)) {
277809 auto &result = DatabaseManager::Get(context).default_database;
277810 if (result.empty()) {
277811 throw InternalException("Calling DatabaseManager::GetDefaultDatabase with no default database set");
277812 }
277813 return result;
277814 }
277815 return default_entry.catalog;
277816}

◆ SetDefaultDatabase()

void duckdb::DatabaseManager::SetDefaultDatabase ( ClientContext & context,
const string & new_value )
277819 {
277820 auto db_entry = GetDatabase(context, new_value);
277821
277822 if (!db_entry) {
277823 throw InternalException("Database \"%s\" not found", new_value);
277824 } else if (db_entry->IsTemporary()) {
277825 throw InternalException("Cannot set the default database to a temporary database");
277826 } else if (db_entry->IsSystem()) {
277827 throw InternalException("Cannot set the default database to a system database");
277828 }
277829
277830 default_database = new_value;
277831}
string default_database
The current default database.
Definition duckdb.hpp:40858

◆ InsertDatabasePath()

InsertDatabasePathResult duckdb::DatabaseManager::InsertDatabasePath ( const AttachInfo & info,
AttachOptions & options )

Inserts a path to name mapping to the database paths map.

277750 {
277751 return path_manager->InsertDatabasePath(*this, info.path, info.name, info.on_conflict, options);
277752}
Here is the caller graph for this function:

◆ GetDatabaseType()

void duckdb::DatabaseManager::GetDatabaseType ( ClientContext & context,
AttachInfo & info,
const DBConfig & config,
AttachOptions & options )

Returns the database type. This might require checking the header of the file, in which case the file handle is necessary. We can only grab the file handle, if it is not yet held, even for uncommitted changes. Thus, we have to lock for this operation.

277773 {
277774 // Test if the database is a DuckDB database file.
277775 if (StringUtil::CIEquals(options.db_type, "duckdb")) {
277776 options.db_type = "";
277777 return;
277778 }
277779
277780 // Try to extract the database type from the path.
277781 if (options.db_type.empty()) {
277782 auto &fs = FileSystem::GetFileSystem(context);
277783 DBPathAndType::CheckMagicBytes(context, fs, info.path, options.db_type);
277784 }
277785
277786 if (options.db_type.empty()) {
277787 return;
277788 }
277789
277790 auto extension_name = ExtensionHelper::ApplyExtensionAlias(options.db_type);
277791 if (StorageExtension::Find(config, extension_name)) {
277792 // If the database type is already registered, we don't need to load it again.
277793 return;
277794 }
277795
277796 // If we are loading a database type from an extension, then we need to check if that extension is loaded.
277797 if (!Catalog::TryAutoLoad(context, options.db_type)) {
277798 // FIXME: Here it might be preferable to use an AutoLoadOrThrow kind of function
277799 // so that either there will be success or a message to throw, and load will be
277800 // attempted only once respecting the auto-loading options
277801 ExtensionHelper::LoadExternalExtension(context, options.db_type);
277802 }
277803}
static string ApplyExtensionAlias(const string &extension_name)
Apply any known extension aliases, return the lowercase name.
Definition duckdb.cpp:278254
static void LoadExternalExtension(ClientContext &context, const string &extension)
Load an extension.
Definition duckdb.cpp:280517
static void CheckMagicBytes(QueryContext context, FileSystem &fs, string &path, string &db_type)
Check the magic bytes of a file and set the database type based on that.
Definition duckdb.cpp:277900
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetDatabases() [1/2]

vector< shared_ptr< AttachedDatabase > > duckdb::DatabaseManager::GetDatabases ( ClientContext & context,
const optional_idx max_db_count = optional_idx() )

Scans the catalog set and adds each committed database entry, and each database entry of the current transaction, to a vector holding AttachedDatabase references.

277835 {
277836 vector<shared_ptr<AttachedDatabase>> result;
277837
277838 lock_guard<mutex> guard(databases_lock);
277839 idx_t count = 2;
277840 for (auto &entry : databases) {
277841 if (max_db_count.IsValid() && count >= max_db_count.GetIndex()) {
277842 break;
277843 }
277844 result.push_back(entry.second);
277845 count++;
277846 }
277847 if (!max_db_count.IsValid() || max_db_count.GetIndex() >= 1) {
277848 result.push_back(system);
277849 }
277850 if (!max_db_count.IsValid() || max_db_count.GetIndex() >= 2) {
277851 result.push_back(context.client_data->temporary_objects);
277852 }
277853
277854 return result;
277855}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ GetDatabases() [2/2]

vector< shared_ptr< AttachedDatabase > > duckdb::DatabaseManager::GetDatabases ( )

Scans the catalog set and returns each committed database entry.

277857 {
277858 vector<shared_ptr<AttachedDatabase>> result;
277859
277860 lock_guard<mutex> guard(databases_lock);
277861 for (auto &entry : databases) {
277862 result.push_back(entry.second);
277863 }
277864 result.push_back(system);
277865 return result;
277866}
Here is the caller graph for this function:

◆ ApproxDatabaseCount()

idx_t duckdb::DatabaseManager::ApproxDatabaseCount ( )

Returns the approximate count of attached databases.

277746 {
277747 return path_manager->ApproxDatabaseCount();
277748}

◆ ResetDatabases()

void duckdb::DatabaseManager::ResetDatabases ( )

Removes all databases from the catalog set. This is necessary for the database instance's destructor, as the database manager has to be alive when destroying the catalog set objects.

277868 {
277869 auto shared_db_pointers = GetDatabases();
277870 for (auto &entry : shared_db_pointers) {
277871 entry->Close(DatabaseCloseAction::TRY_CHECKPOINT);
277872 entry.reset();
277873 }
277874}
Here is the call graph for this function:

◆ GetNewQueryNumber()

transaction_t duckdb::DatabaseManager::GetNewQueryNumber ( )
inline
40816 {
40817 return current_query_number++;
40818 }

◆ ActiveQueryNumber()

transaction_t duckdb::DatabaseManager::ActiveQueryNumber ( ) const
inline
40819 {
40820 return current_query_number;
40821 }

◆ GetNewTransactionNumber()

transaction_t duckdb::DatabaseManager::GetNewTransactionNumber ( )
inline
40822 {
40823 return current_transaction_id++;
40824 }

◆ ActiveTransactionNumber()

transaction_t duckdb::DatabaseManager::ActiveTransactionNumber ( ) const
inline
40825 {
40827 }

◆ NextOid()

idx_t duckdb::DatabaseManager::NextOid ( )
inline
40828 {
40829 return next_oid++;
40830 }

◆ HasDefaultDatabase()

bool duckdb::DatabaseManager::HasDefaultDatabase ( )
inline
40831 {
40832 return !default_database.empty();
40833 }

◆ GetAttachedDatabasePaths()

vector< string > duckdb::DatabaseManager::GetAttachedDatabasePaths ( )

Gets a list of all attached database paths.

277754 {
277755 vector<string> result;
277756 lock_guard<mutex> guard(databases_lock);
277757 for (auto &entry : databases) {
277758 auto &db_ref = *entry.second;
277759 auto &catalog = db_ref.GetCatalog();
277760 if (catalog.InMemory() || catalog.IsSystemCatalog()) {
277761 continue;
277762 }
277763 auto path = catalog.GetDBPath();
277764 if (path.empty()) {
277765 continue;
277766 }
277767 result.push_back(std::move(path));
277768 }
277769 return result;
277770}

◆ GetDatabaseInternal()

shared_ptr< AttachedDatabase > duckdb::DatabaseManager::GetDatabaseInternal ( const lock_guard< mutex > & ,
const string & name )
277493 {
277494 if (StringUtil::Lower(name) == SYSTEM_CATALOG) {
277495 return system;
277496 }
277497 auto entry = databases.find(name);
277498 if (entry == databases.end()) {
277499 // not found
277500 return nullptr;
277501 }
277502 return entry->second;
277503}

◆ FinalizeAttach()

optional_ptr< AttachedDatabase > duckdb::DatabaseManager::FinalizeAttach ( ClientContext & context,
AttachInfo & info,
shared_ptr< AttachedDatabase > database )
private
277630 {
277631 const auto name = attached_db->GetName();
277632 attached_db->oid = NextOid();
277633 if (default_database.empty()) {
277634 default_database = name;
277635 }
277636 shared_ptr<AttachedDatabase> detached_db;
277637 {
277638 lock_guard<mutex> guard(databases_lock);
277639 auto entry = databases.emplace(name, attached_db);
277640 if (!entry.second) {
277641 if (info.on_conflict == OnCreateConflict::REPLACE_ON_CONFLICT) {
277642 // override existing entry
277643 detached_db = std::move(entry.first->second);
277644 databases[name] = attached_db;
277645 } else {
277646 throw BinderException("Failed to attach database: database with name \"%s\" already exists", name);
277647 }
277648 }
277649 }
277650 auto &meta_transaction = MetaTransaction::Get(context);
277651 if (detached_db) {
277652 meta_transaction.DetachDatabase(*detached_db);
277653 detached_db->OnDetach(context);
277654 detached_db.reset();
277655 }
277656 auto &db_ref = meta_transaction.UseDatabase(attached_db);
277657 auto &transaction = DuckTransaction::Get(context, *system);
277658 auto &transaction_manager = DuckTransactionManager::Get(*system);
277659 transaction_manager.PushAttach(transaction, db_ref);
277660 return db_ref;
277661}

◆ RenameDatabase()

void duckdb::DatabaseManager::RenameDatabase ( ClientContext & context,
const string & old_name,
const string & new_name,
OnEntryNotFound if_not_found )
private

Rename an existing database.

277699 {
277700 if (AttachedDatabase::NameIsReserved(new_name)) {
277701 throw BinderException("Database name \"%s\" cannot be used because it is a reserved name", new_name);
277702 }
277703
277704 shared_ptr<AttachedDatabase> attached_db;
277705 {
277706 lock_guard<mutex> guard(databases_lock);
277707 auto old_entry = databases.find(old_name);
277708 if (old_entry == databases.end()) {
277709 if (if_not_found == OnEntryNotFound::THROW_EXCEPTION) {
277710 throw BinderException("Failed to rename database \"%s\": database not found", old_name);
277711 }
277712 return;
277713 }
277714
277715 auto new_entry = databases.find(new_name);
277716 if (new_entry != databases.end()) {
277717 throw BinderException("Failed to rename database \"%s\" to \"%s\": database with new name already exists",
277718 old_name, new_name);
277719 }
277720
277721 attached_db = old_entry->second;
277722 databases.erase(old_entry);
277723 attached_db->SetName(new_name);
277724 databases[new_name] = attached_db;
277725 }
277726
277727 if (StringUtil::CIEquals(default_database, old_name)) {
277728 default_database = new_name;
277729 }
277730}
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files: