Skip to content
This repository was archived by the owner on Jan 9, 2025. It is now read-only.

Multiple database support #3

Open
CommandString opened this issue May 4, 2023 · 2 comments
Open

Multiple database support #3

CommandString opened this issue May 4, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@CommandString
Copy link
Member

public function switchDatabase(??? $database): void;
@CommandString CommandString added the enhancement New feature or request label May 4, 2023
@freezemage0
Copy link
Contributor

I think this method should instantiate a new DriverInterface instance instead of changing the internal PDO object state.

Consider the situation where multiple high-level classes depend on the same PDODriver instance, which was injected at the entry-point.

Changing database in one of those high-level classes will change database everywhere, because objects are always passed-by-reference. This will introduce an unexpected behavior.

However, this behavior cannot be achieved easily in current PDODriver state, because database name is a part of DSN, which is a string. The only way to implement this switchDatabase() method is to make DSN value object. Then we could re-use this DSN to make a new PDO connection inside DriverInterface::switchDatabase() method and return a new DriverInterface instance.

@CommandString
Copy link
Member Author

Then we could re-use this DSN to make a new PDO connection inside DriverInterface::switchDatabase()

We'd also need to store the username and password if we're attempting to reconnect. I was personally thinking of having DriverCollection since it seems wrong to have the Driver deal with juggling two connections

interface DriverCollection {
   public function addDriver(string $name, DriverInterface $driver): self;
   public function getDriver(string $name): ?DriverInterface;
   // etc etc
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants