diff --git a/README.md b/README.md index 2e23922..0a52e15 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/lib/Config.php b/lib/Config.php index 8e6166d..abfdf12 100644 --- a/lib/Config.php +++ b/lib/Config.php @@ -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); } diff --git a/lib/Db.php b/lib/Db.php index af88e5e..8f452b5 100644 --- a/lib/Db.php +++ b/lib/Db.php @@ -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."); } }