From 4773d7a35052df4683b0445c08feae609764bfb0 Mon Sep 17 00:00:00 2001 From: nikrou Date: Sun, 21 Mar 2010 22:51:36 +0000 Subject: Feature 1255 : only one function use exceptions to deal with differents possible errors git-svn-id: http://piwigo.org/svn/trunk@5236 68402e56-0260-453c-a942-63ccdbb3a9ee --- include/common.inc.php | 12 +++++++++--- include/dblayer/functions_mysql.inc.php | 23 ++++++++++++++--------- include/dblayer/functions_pdo-sqlite.inc.php | 15 +++++---------- include/dblayer/functions_pgsql.inc.php | 14 ++++++++++---- include/dblayer/functions_sqlite.inc.php | 16 ++++++---------- install.php | 25 ++++++++----------------- upgrade.php | 12 +++++++++--- upgrade_feed.php | 13 +++++++++---- 8 files changed, 70 insertions(+), 60 deletions(-) diff --git a/include/common.inc.php b/include/common.inc.php index 7ef43c281..0da96c7c6 100644 --- a/include/common.inc.php +++ b/include/common.inc.php @@ -104,9 +104,15 @@ include(PHPWG_ROOT_PATH . 'include/functions.inc.php'); include( PHPWG_ROOT_PATH .'include/template.class.php'); // Database connection -$pwg_db_link = pwg_db_connect($conf['db_host'], $conf['db_user'], - $conf['db_password'], $conf['db_base']); -pwg_select_db($conf['db_base'], $pwg_db_link); +try +{ + $pwg_db_link = pwg_db_connect($conf['db_host'], $conf['db_user'], + $conf['db_password'], $conf['db_base']); +} +catch (Exception $e) +{ + my_error(l10n($e->getMessage(), true); +} pwg_db_check_charset(); diff --git a/include/dblayer/functions_mysql.inc.php b/include/dblayer/functions_mysql.inc.php index 6ada73a59..c21c697ae 100644 --- a/include/dblayer/functions_mysql.inc.php +++ b/include/dblayer/functions_mysql.inc.php @@ -32,16 +32,21 @@ define('DB_RANDOM_FUNCTION', 'RAND'); * */ -function pwg_db_connect($host, $user, $password, $database=null, $die=true) +function pwg_db_connect($host, $user, $password, $database) { - $link = @mysql_connect($host, $user, $password) or my_error('mysql_connect', $die); - - return $link; -} - -function pwg_select_db($database, $link, $die=true) -{ - return @mysql_select_db($database, $link) or my_error('mysql_select_db', $die); + $link = @mysql_connect($host, $user, $password); + if (!$link) + { + throw new Exception("Can't connect to server"); + } + if (mysql_select_db($database, $link)) + { + return $link; + } + else + { + throw new Exception('Connection to server succeed, but it was impossible to connect to database'); + } } function pwg_db_check_charset() diff --git a/include/dblayer/functions_pdo-sqlite.inc.php b/include/dblayer/functions_pdo-sqlite.inc.php index aba554341..33a864980 100644 --- a/include/dblayer/functions_pdo-sqlite.inc.php +++ b/include/dblayer/functions_pdo-sqlite.inc.php @@ -33,16 +33,16 @@ define('DB_RANDOM_FUNCTION', 'RANDOM'); * */ -function pwg_db_connect($host, $user, $password, $database, $die=true) +function pwg_db_connect($host, $user, $password, $database) { global $conf; $db_file = sprintf('sqlite:%s/%s.db', $conf['local_data_dir'], $database); - try { - $link = new PDO($db_file); - } catch (Exception $e) { - my_error('sqlite::open', $die); + $link = new PDO($db_file); + if (!$link) + { + throw new Exception('Connection to server succeed, but it was impossible to connect to database'); } $link->sqliteCreateFunction('now', 'pwg_now', 0); @@ -56,11 +56,6 @@ function pwg_db_connect($host, $user, $password, $database, $die=true) return $link; } -function pwg_select_db($database=null, $link=null, $die=null) -{ - return true; -} - function pwg_db_check_charset() { return true; diff --git a/include/dblayer/functions_pgsql.inc.php b/include/dblayer/functions_pgsql.inc.php index fe350d090..5c928c53c 100644 --- a/include/dblayer/functions_pgsql.inc.php +++ b/include/dblayer/functions_pgsql.inc.php @@ -33,7 +33,7 @@ define('DB_RANDOM_FUNCTION', 'RANDOM'); * */ -function pwg_db_connect($host, $user, $password, $database, $die=true) +function pwg_db_connect($host, $user, $password, $database) { $connection_string = ''; if (strpos($host,':') !== false) @@ -49,9 +49,15 @@ function pwg_db_connect($host, $user, $password, $database, $die=true) $user, $password, $database); - $link = pg_connect($connection_string) or my_error('pg_connect', $die); - - return $link; + $link = pg_connect($connection_string); + if (!$link) + { + throw new Exception("Can't connect to server"); + } + else + { + return $link; + } } function pwg_select_db($database=null, $link=null, $die=null) diff --git a/include/dblayer/functions_sqlite.inc.php b/include/dblayer/functions_sqlite.inc.php index ce426b9e0..7f02a06d1 100644 --- a/include/dblayer/functions_sqlite.inc.php +++ b/include/dblayer/functions_sqlite.inc.php @@ -33,7 +33,7 @@ define('DB_RANDOM_FUNCTION', 'RANDOM'); * */ -function pwg_db_connect($host, $user, $password, $database, $die=true) +function pwg_db_connect($host, $user, $password, $database) { global $conf; @@ -47,10 +47,11 @@ function pwg_db_connect($host, $user, $password, $database, $die=true) { $sqlite_open_mode = SQLITE3_OPEN_READWRITE; } - try { - $link = new SQLite3($db_file, $sqlite_open_mode); - } catch (Exception $e) { - my_error('sqlite::open', $die); + + $link = new SQLite3($db_file, $sqlite_open_mode); + if (!$link) + { + throw new Exception('Connection to server succeed, but it was impossible to connect to database'); } $link->createFunction('now', 'pwg_now', 0); @@ -64,11 +65,6 @@ function pwg_db_connect($host, $user, $password, $database, $die=true) return $link; } -function pwg_select_db($database=null, $link=null, $die=null) -{ - return true; -} - function pwg_db_check_charset() { return true; diff --git a/install.php b/install.php index ea8bdf402..d722288f5 100644 --- a/install.php +++ b/install.php @@ -236,21 +236,13 @@ if (!isset($step)) //---------------------------------------------------------------- form analyze if ( isset( $_POST['install'] )) { - ob_start(); - if (($pwg_db_link = pwg_db_connect($_POST['dbhost'], $_POST['dbuser'], - $_POST['dbpasswd'], $_POST['dbname'], false))!==false) + try { - if (pwg_select_db($_POST['dbname'], $pwg_db_link, false)!==false) - { - array_push( $infos, l10n('Parameters are correct') ); - } - else - { - array_push( $errors, - l10n('Connection to server succeed, but it was impossible to connect to database') ); - } - ob_end_clean(); - + $pwg_db_link = pwg_db_connect($_POST['dbhost'], $_POST['dbuser'], + $_POST['dbpasswd'], $_POST['dbname']); + + array_push( $infos, l10n('Parameters are correct') ); + $required_version = constant('REQUIRED_'.strtoupper($dblayer).'_VERSION'); if ( version_compare(pwg_get_db_version(), $required_version, '>=') ) { @@ -276,10 +268,9 @@ if ( isset( $_POST['install'] )) } } } - else + catch (Exception $e) { - array_push( $errors, l10n('Can\'t connect to server') ); - ob_end_clean(); + array_push( $errors, l10n($e->getMessage())); } $webmaster = trim(preg_replace( '/\s{2,}/', ' ', $admin_name )); if ( empty($webmaster)) diff --git a/upgrade.php b/upgrade.php index 4e01b9272..278f75da4 100644 --- a/upgrade.php +++ b/upgrade.php @@ -51,9 +51,15 @@ include_once(PHPWG_ROOT_PATH.'include/constants.php'); define('PREFIX_TABLE', $prefixeTable); // Database connection -$pwg_db_link = pwg_db_connect($conf['db_host'], $conf['db_user'], - $conf['db_password'], $conf['db_base']); -pwg_select_db($conf['db_base'], $pwg_db_link); +try +{ + $pwg_db_link = pwg_db_connect($conf['db_host'], $conf['db_user'], + $conf['db_password'], $conf['db_base']); +} +catch (Exception $e) +{ + my_error(l10n($e->getMessage(), true); +} pwg_db_check_charset(); diff --git a/upgrade_feed.php b/upgrade_feed.php index 52f0cfb9f..6b991418d 100644 --- a/upgrade_feed.php +++ b/upgrade_feed.php @@ -54,10 +54,15 @@ define('UPGRADES_PATH', PHPWG_ROOT_PATH.'install/db'); // +-----------------------------------------------------------------------+ // | Database connection | // +-----------------------------------------------------------------------+ - -$pwg_db_link = pwg_db_connect($conf['db_host'], $conf['db_user'], - $conf['db_password'], $conf['db_base']); -pwg_select_db($conf['db_base'], $pwg_db_link); +try +{ + $pwg_db_link = pwg_db_connect($conf['db_host'], $conf['db_user'], + $conf['db_password'], $conf['db_base']); +} +catch (Exception $e) +{ + my_error(l10n($e->getMessage(), true); +} pwg_db_check_charset(); -- cgit v1.2.3