Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent and unspecified behavior of Connection::getDatabase() #3592

Closed
morozov opened this issue Jun 6, 2019 · 2 comments
Closed

Inconsistent and unspecified behavior of Connection::getDatabase() #3592

morozov opened this issue Jun 6, 2019 · 2 comments

Comments

@morozov
Copy link
Member

morozov commented Jun 6, 2019

Q A
BC Break no
Version 2.9.3

The Connection::getDatabase() method description looks like following:

/**
* Gets the name of the database this Connection is connected to.

This is a bit unclear and doesn't always work in the same way.

1. Depending on the connection parameters

Consider the example:

$conn = DriverManager::getConnection([
    'driver' => 'mysqli',
    'host' => 'localhost',
    'user' => 'root',
]);

var_dump($conn->getDatabase());
// NULL
$conn->exec('USE doctrine');
var_dump($conn->getDatabase());
// string(8) "doctrine"
$conn->exec('USE mysql');
var_dump($conn->getDatabase());
// string(5) "mysql"

The above looks correct since it returns the currently selected database.

Here's another example:

$conn = DriverManager::getConnection([
    'driver' => 'mysqli',
    'host' => 'localhost',
    'user' => 'root',
    'dbname' => 'doctrine',
]);

var_dump($conn->getDatabase());
// string(8) "doctrine"
$conn->exec('USE mysql');
var_dump($conn->getDatabase());
// string(8) "doctrine"

Here, it always returns the originally selected database.

This behavior is consistent across drivers. They all try to use the value from the configuration and fall back to the value reported by the connection which causes the inconsistency.

2. Depending on the authentication method

Most of the drivers support external authentication where most if not all connection parameters including username and database can be omitted and will be picked up by the driver from another place (e.g. php.ini, OS configuration, etc).

For instance, in order to connect to an Oracle database via oci8 using external authentication, one needs to specify / as the user name. If Connection::getDatabase() is called on such a connection, it will return /, not the current database. The same example can be made for most of the supported drivers.

@morozov
Copy link
Member Author

morozov commented Jun 13, 2019

Fixed by #3606.

@morozov morozov closed this as completed Jun 13, 2019
@morozov morozov self-assigned this Jun 13, 2019
@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 31, 2022
@morozov morozov modified the milestones: 4.0.0, 3.0.0 Aug 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant