From 2c8ade748ad674bff5ecc4fde2cd1375e7b0ac9a Mon Sep 17 00:00:00 2001 From: habarnam Date: Mon, 2 Jun 2008 01:27:56 +0300 Subject: [PATCH] * modified the sql factory and null sql to fail gracefully on db problems --- _res/_libs/interfacesql.class.php | 3 ++- _res/_libs/mysqlim.class.php | 19 ++++++++++--------- _res/_libs/nullsql.class.php | 33 +++++++++++++++++---------------- _res/_libs/sqlfactory.class.php | 38 ++++++++++++++++++++------------------ 4 files changed, 49 insertions(+), 44 deletions(-) diff --git a/_res/_libs/interfacesql.class.php b/_res/_libs/interfacesql.class.php index 497d61d..6f17d6d 100644 --- a/_res/_libs/interfacesql.class.php +++ b/_res/_libs/interfacesql.class.php @@ -5,8 +5,9 @@ */ abstract class interfaceSql { public $conn, + $error, $link; - + /** * just a function to trigger an error in the eventuality of using * an unsupported DB_TYPE connection (usually because of a config error) diff --git a/_res/_libs/mysqlim.class.php b/_res/_libs/mysqlim.class.php index bf7f3b9..82128a4 100644 --- a/_res/_libs/mysqlim.class.php +++ b/_res/_libs/mysqlim.class.php @@ -47,14 +47,14 @@ class mySqlIm extends interfaceSql { trigger_error('Database connection data missing!', E_USER_ERROR); if (!empty($this->host) && !empty($this->user) && !empty($this->pass)) { - $this->connect(); + $this->connect (); } } public function __destruct() { // var_dump($this->link); - if (!empty ($this->link) && $this->link instanceof mysqli) - $this->close(); +// if (!empty ($this->link) && $this->link instanceof mysqli) +// $this->close(); } @@ -63,11 +63,12 @@ class mySqlIm extends interfaceSql { * * @return bool */ - private function connect(){ - $this->link = new mysqli ($this->host, $this->user, $this->pass); -// var_dump($this->link); - if($this->link->errno) { - trigger_error($this->link->error, E_USER_ERROR); + private function connect (){ + $this->link = @new mysqli ($this->host, $this->user, $this->pass); + $errNo = mysqli_connect_errno(); + if (!empty($errNo)) { + $this->error = $errNo.' '.mysqli_connect_error(); +// trigger_error ($this->link->error, E_USER_ERROR); return false; } return true; @@ -97,7 +98,7 @@ class mySqlIm extends interfaceSql { if (($this->link instanceof mysqli) && $this->link->select_db($incData)) { return true; } else { - trigger_error($this->link->error, E_USER_ERROR); +// trigger_error($this->link->error, E_USER_ERROR); return false; } } diff --git a/_res/_libs/nullsql.class.php b/_res/_libs/nullsql.class.php index ba5ab91..b550c51 100644 --- a/_res/_libs/nullsql.class.php +++ b/_res/_libs/nullsql.class.php @@ -6,7 +6,7 @@ class nullSql extends interfaceSql { public $conn, $link; - + /** * just a function to trigger an error in the eventuality of using * an unsupported DB_TYPE connection (usually because of a config error) @@ -19,34 +19,35 @@ class nullSql extends interfaceSql { * @param string $dbPass */ public function __construct( $dbHost = null, $dbUser = null, $dbPass = null ) { - trigger_error('This site has all database functionality disabled.
Please check for configuration errors', E_USER_NOTICE); + trigger_error ('This site has all database functionality disabled.
Please check for configuration errors', E_USER_NOTICE); } + public function close () {} - public function _SELECT($incObj) {} - - public function _CREATE() {} + public function _SELECT ($incObj){} - public function _SET() {} + public function _CREATE (){} - public function _INSERT() {} + public function _SET(){} + + public function _INSERT ($incData){} - public function _UPDATE($incOb) {} + public function _UPDATE ($incOb){} - public function _FROM($incData) {} - - public function _AND() {} + public function _FROM ($incData){} - public function _OR() {} + public function _AND (){} + + public function _OR (){} public function _JOIN ($type) {} - public function _AS($str) {} + public function _AS ($str){} - public function _LIMIT($start = 0, $end = 0) {} + public function _LIMIT ($start = 0, $end = 0){} - public function _GROUP($incObj = null) {} + public function _GROUP ($incObj = null){} - public function _ORDER($orderBys = null) {} + public function _ORDER ($orderBys = null){} public function _WHERE ($clause) {} } diff --git a/_res/_libs/sqlfactory.class.php b/_res/_libs/sqlfactory.class.php index 3f6da56..b4256ea 100644 --- a/_res/_libs/sqlfactory.class.php +++ b/_res/_libs/sqlfactory.class.php @@ -6,9 +6,7 @@ class sqlFactory { static public $TYPES = array ('postgresql', 'mysql','mysqli'); static private $instance = false; - - public function __construct(){} - + /** * returning if the set DB type is supported * @@ -28,23 +26,27 @@ class sqlFactory { * @param string $incString * @return interfaceSql */ - - static public function connect($incString){ - if (!sqlFactory::validType($incString)) { - return new nullSql(); + + static public function connect($incString) { + if (!sqlFactory::validType ($incString)) { + sqlFactory::$instance = new nullSql(); } - - if(!sqlFactory::$instance) { + + if(!(sqlFactory::$instance instanceof interfaceSql)) { if (stristr($incString, 'mysqli')) { - return new mySqlIm(); - }elseif (stristr ($incString, 'mysql')) - return new mySql(); - elseif (stristr ($incString, 'postgresql')) - return false; // postgresql not implemented - elseif (stristr ($incString, 'sqlserv')) - return false; // Sql server not implemented - } else { - return sqlFactory::instance; + sqlFactory::$instance = new mySqlIm (); + } elseif (stristr ($incString, 'mysql')) { + sqlFactory::$instance = new mySql (); + } elseif (stristr ($incString, 'postgresql')) { + sqlFactory::$instance = new nullSql (); // postgresql not implemented + } elseif (stristr ($incString, 'sqlserv')) { + sqlFactory::$instance = new nullSql (); // Sql server not implemented + } + + if (sqlFactory::$instance->error) { + sqlFactory::$instance = new nullSql (); + } } + return sqlFactory::$instance; } } -- 2.11.4.GIT