From cc198709234dd5c3e5b071269602515483a252f0 Mon Sep 17 00:00:00 2001 From: David Goodwin Date: Fri, 7 Jun 2019 16:23:34 +0100 Subject: [PATCH] drop function: db_connect_with_errors(); just throw from db_connect() if something goes wrong --- functions.inc.php | 30 ++++++++---------------------- public/setup.php | 10 +++++++++- tests/bootstrap.php | 14 +++++++------- 3 files changed, 24 insertions(+), 30 deletions(-) diff --git a/functions.inc.php b/functions.inc.php index aa5e31c1..7ef0ff50 100644 --- a/functions.inc.php +++ b/functions.inc.php @@ -1464,29 +1464,14 @@ EOF; * @return \PDO */ function db_connect() { - list($link, $error_text) = db_connect_with_errors(); - - if (!$link instanceof PDO) { - throw new Exception("Database connection failed: $error_text"); - } - - return $link; -} - -/** - * @param bool $ignore_errors - * @return array [PDO link | false, string $error_text]; - */ -function db_connect_with_errors() { global $CONF; - global $DEBUG_TEXT; - - $error_text = ''; + /* some attempt at not reopening an existing connection */ static $link; if (isset($link) && $link) { - return array($link, $error_text); + return $link; } + $link = false; $options = array( @@ -1520,17 +1505,17 @@ function db_connect_with_errors() { if (!file_exists($db)) { $error_text = 'SQLite database missing: '. $db; - return array($link, $error_text); + throw new Exception($error_text); } if (!is_writeable($db)) { $error_text = 'SQLite database not writeable: '. $db; - return array($link, $error_text); + throw new Exception($error_text); } if (!is_writeable(dirname($db))) { $error_text = 'The directory the SQLite database is in is not writeable: '. dirname($db); - return array($link, $error_text); + throw new Exception($error_text); } $dsn = "sqlite:{$db}"; @@ -1556,7 +1541,8 @@ function db_connect_with_errors() { } } - return array($link, $error_text); + return $link; + } /** diff --git a/public/setup.php b/public/setup.php index d824e782..f044b775 100644 --- a/public/setup.php +++ b/public/setup.php @@ -184,7 +184,15 @@ if ($f_sqlite_open == 1) { // // Database connection // -list($link, $error_text) = db_connect_with_errors(); +$link = null; +$error_text = null; + +try { + $link = db_connect(); +} +catch(Exception $e) { + $error_text = $e->getMessage(); +} if (!empty($link) && $error_text == "") { print "
  • Testing database connection (using {$CONF['database_type']}) - Success
  • "; diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 3a1d4edb..0bafb9d0 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -28,7 +28,7 @@ if (getenv('DATABASE') == 'postgresql') { $user = getenv('PGUSER') ?: 'postgres'; $pass = getenv('PGPASSWORD') ?: ''; $host = getenv('PGHOST') ?: 'localhost'; - + $CONF['database_type'] = 'pgsql'; $CONF['database_user'] = $user; $CONF['database_password'] = $pass; @@ -56,7 +56,7 @@ if (getenv('DATABASE') == 'mysql') { $config = parse_ini_file($expand_tilde('~/.my.cnf')); if (empty($config)) { - $config = ['user'=>'root', 'host' => '127.0.0.1', 'password' => '']; + $config = ['user' => 'root', 'host' => '127.0.0.1', 'password' => '']; } if (isset($config['socket'])) { @@ -79,13 +79,13 @@ if (getenv('DATABASE') == 'mysql') { echo "Using: MySQL database for tests\n"; } - -list($db, $error_text) = db_connect_with_errors(); - -if ($db === false) { +try { + $db = db_connect(); +} catch (Exception $e) { echo "failed to connect to database\n"; - echo $error_text; + echo $e->getMessage(); exit(1); + } require_once(dirname(__FILE__) . '/../public/upgrade.php');