|
|
@ -13,22 +13,42 @@ |
|
|
|
*/ |
|
|
|
class DBConnector |
|
|
|
{ |
|
|
|
static private $handlers = array(); |
|
|
|
private $handlers = array(); |
|
|
|
static private $instance = null; |
|
|
|
static public $queries = array(); |
|
|
|
|
|
|
|
/** |
|
|
|
* Запрещаем new и клонирование |
|
|
|
*/ |
|
|
|
private function __construct(){} |
|
|
|
|
|
|
|
private function __clone(){} |
|
|
|
|
|
|
|
/** |
|
|
|
* Единственный способ инициализации. Singletone как-никак. |
|
|
|
* @return DBConnector |
|
|
|
*/ |
|
|
|
static public function getInstance() |
|
|
|
{ |
|
|
|
if (self::$instance == null) { |
|
|
|
self::$instance = new DBConnector(); |
|
|
|
} |
|
|
|
return self::$instance; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Получение соединения. |
|
|
|
* Если соединение с такими параметрами уже есть - новое не создается. |
|
|
|
* |
|
|
|
* @param array $db_settings - массив настроек |
|
|
|
* @return resource - идентификатор соединения |
|
|
|
*/ |
|
|
|
static function getConnect($db_settings) |
|
|
|
public function getConnect($db_settings) |
|
|
|
{ |
|
|
|
$handler_name = self::getConnectionName($db_settings); |
|
|
|
$handler_name = $this->getConnectionName($db_settings); |
|
|
|
|
|
|
|
if (isset(self::$handlers[$handler_name])) { |
|
|
|
return self::$handlers[$handler_name]; |
|
|
|
if (isset($this->handlers[$handler_name])) { |
|
|
|
return $this->handlers[$handler_name]; |
|
|
|
} |
|
|
|
|
|
|
|
if (!$handler = mysqli_connect($db_settings['host'], $db_settings['user'], $db_settings['password'], $db_settings['database'])) { |
|
|
@ -36,12 +56,12 @@ class DBConnector |
|
|
|
} |
|
|
|
|
|
|
|
mysqli_query($handler, "SET NAMES 'utf8'"); //cheat!!!
|
|
|
|
return self::$handlers[$handler_name] = $handler; |
|
|
|
return $this->handlers[$handler_name] = $handler; |
|
|
|
} |
|
|
|
|
|
|
|
static function getConnectionName($db_settings) |
|
|
|
protected function getConnectionName($db_settings) |
|
|
|
{ |
|
|
|
return $db_settings['host'].'-'.$db_settings['database']; |
|
|
|
return $db_settings['host'] . '-' . $db_settings['database']; |
|
|
|
} |
|
|
|
} |
|
|
|
?>
|