Attach a new database.
Initialize the database.
277520 {
277521 string extension = "";
277523 if (options.access_mode == AccessMode::AUTOMATIC) {
277524
277525
277526
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
277533 auto &fs = FileSystem::GetFileSystem(context);
277534 info.path = fs.CanonicalizePath(info.path);
277535 }
277536
277537
277538 if (info.on_conflict == OnCreateConflict::IGNORE_ON_CONFLICT ||
277539 info.on_conflict == OnCreateConflict::REPLACE_ON_CONFLICT) {
277540
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
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
277567 auto profiler = context.client_data->profiler->StartTimer(MetricType::WAITING_TO_ATTACH_LATENCY);
277568
277569 while (
InsertDatabasePath(info, options) == InsertDatabasePathResult::ALREADY_EXISTS) {
277570
277571
277572 auto &meta_transaction = MetaTransaction::Get(context);
277573 auto existing_db = meta_transaction.GetReferencedDatabaseOwning(info.name);
277574 if (existing_db) {
277575
277576 return existing_db;
277577 }
277578
277579
277580
277582 auto entry =
databases.find(info.name);
277584
277585 return entry->second;
277586 }
277587 if (context.interrupted) {
277588 throw InterruptException();
277589 }
277590 }
277591 }
277592 auto &config = DBConfig::GetConfig(context);
277594 if (!options.db_type.empty()) {
277595
277596
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()) {
277604 throw MissingExtensionException("Attaching path '%s' requires extension '%s' to be loaded", info.path,
277605 extension);
277606 }
277607 }
277608
277609
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