Changeset 5236


Ignore:
Timestamp:
Mar 21, 2010, 11:51:36 PM (14 years ago)
Author:
nikrou
Message:

Feature 1255 :
only one function
use exceptions to deal with differents possible errors

Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/common.inc.php

    r5235 r5236  
    105105
    106106// Database connection
    107 $pwg_db_link = pwg_db_connect($conf['db_host'], $conf['db_user'],
    108                               $conf['db_password'], $conf['db_base']);
    109 pwg_select_db($conf['db_base'], $pwg_db_link);
     107try
     108{
     109  $pwg_db_link = pwg_db_connect($conf['db_host'], $conf['db_user'],
     110                                $conf['db_password'], $conf['db_base']);
     111}
     112catch (Exception $e)
     113{
     114  my_error(l10n($e->getMessage(), true);
     115}
    110116
    111117pwg_db_check_charset();
  • trunk/include/dblayer/functions_mysql.inc.php

    r5230 r5236  
    3333 */
    3434
    35 function pwg_db_connect($host, $user, $password, $database=null, $die=true)
     35function pwg_db_connect($host, $user, $password, $database)
    3636
    37   $link = @mysql_connect($host, $user, $password) or my_error('mysql_connect', $die);
    38 
    39   return $link;
    40 }
    41 
    42 function pwg_select_db($database, $link, $die=true)
    43 {
    44   return @mysql_select_db($database, $link) or my_error('mysql_select_db', $die);
     37  $link = @mysql_connect($host, $user, $password);
     38  if (!$link)
     39  {
     40    throw new Exception("Can't connect to server");
     41  }
     42  if (mysql_select_db($database, $link))
     43  {
     44    return $link;
     45  }
     46  else
     47  {
     48    throw new Exception('Connection to server succeed, but it was impossible to connect to database');
     49  }
    4550}
    4651
  • trunk/include/dblayer/functions_pdo-sqlite.inc.php

    r5230 r5236  
    3434 */
    3535
    36 function pwg_db_connect($host, $user, $password, $database, $die=true)
     36function pwg_db_connect($host, $user, $password, $database)
    3737{
    3838  global $conf;
     
    4040  $db_file = sprintf('sqlite:%s/%s.db', $conf['local_data_dir'], $database);
    4141
    42   try {
    43     $link = new PDO($db_file);
    44   } catch (Exception $e) {
    45     my_error('sqlite::open', $die);
     42  $link = new PDO($db_file);
     43  if (!$link)
     44  {
     45    throw new  Exception('Connection to server succeed, but it was impossible to connect to database');
    4646  }
    4747
     
    5555
    5656  return $link;
    57 }
    58 
    59 function pwg_select_db($database=null, $link=null, $die=null)
    60 {
    61   return true;
    6257}
    6358
  • trunk/include/dblayer/functions_pgsql.inc.php

    r5230 r5236  
    3434 */
    3535
    36 function pwg_db_connect($host, $user, $password, $database, $die=true)
     36function pwg_db_connect($host, $user, $password, $database)
    3737{
    3838  $connection_string = '';
     
    5050                                $password,
    5151                                $database);
    52   $link = pg_connect($connection_string) or my_error('pg_connect', $die); 
    53 
    54   return $link;
     52  $link = pg_connect($connection_string);
     53  if (!$link)
     54  {
     55    throw new Exception("Can't connect to server");
     56  }
     57  else
     58  {
     59    return $link;
     60  }
    5561}
    5662
  • trunk/include/dblayer/functions_sqlite.inc.php

    r5230 r5236  
    3434 */
    3535
    36 function pwg_db_connect($host, $user, $password, $database, $die=true)
     36function pwg_db_connect($host, $user, $password, $database)
    3737{
    3838  global $conf;
     
    4848    $sqlite_open_mode = SQLITE3_OPEN_READWRITE;
    4949  }
    50   try {
    51     $link = new SQLite3($db_file, $sqlite_open_mode);
    52   } catch (Exception $e) {
    53     my_error('sqlite::open', $die);
     50 
     51  $link = new SQLite3($db_file, $sqlite_open_mode);
     52  if (!$link)
     53  {
     54    throw new  Exception('Connection to server succeed, but it was impossible to connect to database');
    5455  }
    5556
     
    6364
    6465  return $link;
    65 }
    66 
    67 function pwg_select_db($database=null, $link=null, $die=null)
    68 {
    69   return true;
    7066}
    7167
  • trunk/install.php

    r5234 r5236  
    237237if ( isset( $_POST['install'] ))
    238238{
    239   ob_start();
    240   if (($pwg_db_link = pwg_db_connect($_POST['dbhost'], $_POST['dbuser'],
    241                                      $_POST['dbpasswd'], $_POST['dbname'], false))!==false)
    242   {
    243     if (pwg_select_db($_POST['dbname'], $pwg_db_link, false)!==false)
    244     {
    245       array_push( $infos, l10n('Parameters are correct') );
    246     }
    247     else
    248     {
    249       array_push( $errors,
    250         l10n('Connection to server succeed, but it was impossible to connect to database') );
    251     }
    252     ob_end_clean();
    253 
     239  try
     240  {
     241    $pwg_db_link = pwg_db_connect($_POST['dbhost'], $_POST['dbuser'],
     242                                  $_POST['dbpasswd'], $_POST['dbname']);
     243 
     244    array_push( $infos, l10n('Parameters are correct') );
     245   
    254246    $required_version = constant('REQUIRED_'.strtoupper($dblayer).'_VERSION');
    255247    if ( version_compare(pwg_get_db_version(), $required_version, '>=') )
     
    277269    }
    278270  }
    279   else
    280   {
    281     array_push( $errors, l10n('Can\'t connect to server') );
    282     ob_end_clean();
     271  catch (Exception $e)
     272  {
     273    array_push( $errors, l10n($e->getMessage()));
    283274  }
    284275  $webmaster = trim(preg_replace( '/\s{2,}/', ' ', $admin_name ));
  • trunk/upgrade.php

    r5234 r5236  
    5252
    5353// Database connection
    54 $pwg_db_link = pwg_db_connect($conf['db_host'], $conf['db_user'],
    55                               $conf['db_password'], $conf['db_base']);
    56 pwg_select_db($conf['db_base'], $pwg_db_link);
     54try
     55{
     56  $pwg_db_link = pwg_db_connect($conf['db_host'], $conf['db_user'],
     57                                $conf['db_password'], $conf['db_base']);
     58}
     59catch (Exception $e)
     60{
     61  my_error(l10n($e->getMessage(), true);
     62}
    5763
    5864pwg_db_check_charset();
  • trunk/upgrade_feed.php

    r5230 r5236  
    5555// |                         Database connection                           |
    5656// +-----------------------------------------------------------------------+
    57 
    58 $pwg_db_link = pwg_db_connect($conf['db_host'], $conf['db_user'],
    59                               $conf['db_password'], $conf['db_base']);
    60 pwg_select_db($conf['db_base'], $pwg_db_link);
     57try
     58{
     59  $pwg_db_link = pwg_db_connect($conf['db_host'], $conf['db_user'],
     60                                $conf['db_password'], $conf['db_base']);
     61}
     62catch (Exception $e)
     63{
     64  my_error(l10n($e->getMessage(), true);
     65}
    6166
    6267pwg_db_check_charset();
Note: See TracChangeset for help on using the changeset viewer.