Skip to content

Commit

Permalink
WIP: handle parameter existence in createDBhandle
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Abel committed May 4, 2024
1 parent 7ce21cc commit 143afa9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ There are three types of configuration parameters:

### 1. Database

that *User Backend SQL Raw* will connect to. (Starting with version 2.0.0) there are four parameters: `dsn`, `db_user`, `db_password` and `db_password_file`. The DSN (Data Source Name) encompasses almost all db connection parameters and gives you the freedom to customize your connection however you want without user_backend_sql_raw needing to provide configuration parameters for each of them. As [to be expected](https://github.com/PanCakeConnaisseur/user_backend_sql_raw/issues/46#issuecomment-2081741993), MySQL needs special treatment, and the db password and user must be provided separately in `db_user` and `db_password`.
that *User Backend SQL Raw* will connect to. There are four parameters: `dsn`, `db_user`, `db_password` and `db_password_file`. The DSN (Data Source Name) encompasses almost all db connection parameters and gives you the freedom to customize your connection however you want without user_backend_sql_raw needing to provide configuration parameters for each of them. As [to be expected](https://github.com/PanCakeConnaisseur/user_backend_sql_raw/issues/46#issuecomment-2081741993), MySQL needs special treatment, and the db password and user must be provided separately in `db_user` and `db_password`.

* `dsn`: check how to construct DSNs for [PostgreSQL](https://github.com/PanCakeConnaisseur/user_backend_sql_raw/issues/46#issuecomment-2081741993) and [MySQL](https://www.php.net/manual/en/ref.pdo-mysql.connection.php) Examples:
* connect to PostgreSQL via a socket with ident authentication which requires no user or password: `pgsql:host=/var/run/postgresql;dbname=theNameOfYourUserDb`
Expand Down
5 changes: 0 additions & 5 deletions lib/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,6 @@ public function getDsn()
*/
public function getDbUser()
{
if (!$this->getDbPassword()) {
throw new \UnexpectedValueException("You specified a `db_user` but neither provided a "
."`db_password` nor `db_password_file`. When using `db_user` the password can not be "
."read from the DSN and must be specified using either of the two mentioned keys.");
}
return $this->getConfigValueOrFalse(self::CONFIG_KEY_DB_USER);
}

Expand Down
22 changes: 18 additions & 4 deletions lib/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,23 @@ public function getDbHandle()
*/
protected function createDbHandle()
{
$this->logger->debug("dsn: {$this->config->getDsn()} dbuser: {$this->config->getDbUser()} dbpassword: {$this->config->getDbPassword()}"); //TODO: remove
return new PDO($this->config->getDsn(),
$this->config->getDbUser(),
$this->config->getDbPassword());
$this->logger->debug("dsn: {$this->config->getDsn()} db_user: {$this->config->getDbUser()} db_password: {$this->config->getDbPassword()}"); //TODO: remove

if (empty($this->config->getDbUser()) !== empty($this->config->getDbPassword())) {
throw new \UnexpectedValueException("You set only one of `db_user` and `db_password`"
. " but not the other. You must either set both or none. If none are provided, "
. "then both values are used from the DSN.");
} elseif ($this->config->getDbUser() and $this->config->getDbPassword()) {
// for e.g. MySQL
return new PDO($this->config->getDsn(),
$this->config->getDbUser(),
$this->config->getDbPassword());
return new PDO($this->config->getDsn());
} elseif (!$this->config->getDbUser() and !$this->config->getDbPassword()) {
// for e.g. PostgreSQL
return new PDO($this->config->getDsn());
}
throw new \LogicException("This should never happen. If you see this error, please report"
." it as a bug.");
}
}

0 comments on commit 143afa9

Please sign in to comment.