/** * @param string $dsn * @return mixed $dsn (if valid) * @throws \RuntimeException (if $dsn is malformed or fails to connect) */ public static function validateDsn($dsn) { if (empty($dsn)) { throw new \RuntimeException("Value is required"); } $datasource = new \Amp\Database\Datasource(array('civi_dsn' => $dsn)); $dbh = $datasource->createPDO(); foreach ($dbh->query('SELECT 99 as value') as $row) { if ($row['value'] == 99) { return $dsn; } } throw new \RuntimeException("Connection failed"); }
/** * @param string $dsn * @return mixed $dsn (if valid) * @throws \RuntimeException (if $dsn is malformed or fails to connect) */ public static function validateDsn($dsn) { if (empty($dsn)) { throw new \RuntimeException("Value is required"); } $datasource = new \Amp\Database\Datasource(array('civi_dsn' => $dsn)); if ($datasource->isValid()) { return $dsn; } else { throw new \RuntimeException("Connection failed"); } }