custom DBConnector system
git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/trunk@75 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
This commit is contained in:
@ -3,38 +3,25 @@
|
||||
* Класс базы данных.
|
||||
* Возвращает идентификатор соединения
|
||||
*
|
||||
* @copyright
|
||||
* @link
|
||||
* @copyright
|
||||
* @link
|
||||
* @package Majestic
|
||||
* @subpackage DB
|
||||
* @since
|
||||
* @since
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
class DBConnector
|
||||
{
|
||||
private $handlers = array();
|
||||
static private $instance = null;
|
||||
static private $handlers = array();
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Получение соединения.
|
||||
@ -43,25 +30,82 @@ class DBConnector
|
||||
* @param array $db_settings - массив настроек
|
||||
* @return resource - идентификатор соединения
|
||||
*/
|
||||
public function getConnect($db_settings)
|
||||
static public function getConnect($db_settings)
|
||||
{
|
||||
$handler_name = $this->getConnectionName($db_settings);
|
||||
$handler_name = self::getConnectionName($db_settings);
|
||||
|
||||
if (isset($this->handlers[$handler_name])) {
|
||||
return $this->handlers[$handler_name];
|
||||
if (isset(self::$handlers[$handler_name])) {
|
||||
return self::$handlers[$handler_name];
|
||||
}
|
||||
|
||||
if (!$handler = mysqli_connect($db_settings['host'], $db_settings['user'], $db_settings['password'], $db_settings['database'])) {
|
||||
throw new MJException('Can\'t connect to DB '.mysqli_connect_error(), 2);
|
||||
}
|
||||
|
||||
mysqli_query($handler, "SET NAMES 'utf8'"); //cheat!!!
|
||||
return $this->handlers[$handler_name] = $handler;
|
||||
self::query($handler, "SET NAMES 'utf8'"); //cheat!!!
|
||||
return self::$handlers[$handler_name] = $handler;
|
||||
}
|
||||
|
||||
protected function getConnectionName($db_settings)
|
||||
static protected function getConnectionName($db_settings)
|
||||
{
|
||||
return $db_settings['host'] . '-' . $db_settings['database'];
|
||||
}
|
||||
|
||||
/////////
|
||||
|
||||
static public function query($handler, $sql)
|
||||
{
|
||||
return mysqli_query($handler, $sql);
|
||||
}
|
||||
|
||||
static public function escape($handler, $str)
|
||||
{
|
||||
return mysqli_real_escape_string($handler, $str);
|
||||
}
|
||||
|
||||
static public function error($handler)
|
||||
{
|
||||
return mysqli_error($handler);
|
||||
}
|
||||
|
||||
static public function free($result)
|
||||
{
|
||||
return mysqli_free_result($result);
|
||||
}
|
||||
|
||||
static public function fetchObject($result, $class_name = false)
|
||||
{
|
||||
return $class_name ? mysqli_fetch_object($result, $class_name) : mysqli_fetch_object($result);
|
||||
}
|
||||
|
||||
static public function numRows($result)
|
||||
{
|
||||
return mysqli_num_rows($result);
|
||||
}
|
||||
|
||||
static public function affectedRows($handler)
|
||||
{
|
||||
return mysqli_affected_rows($handler);
|
||||
}
|
||||
|
||||
static public function getId($handler)
|
||||
{
|
||||
return mysqli_insert_id($handler);
|
||||
}
|
||||
|
||||
static public function autocommit($handler, $switch)
|
||||
{
|
||||
return mysqli_autocommit($handler, (bool) $switch);
|
||||
}
|
||||
|
||||
static public function commit($handler)
|
||||
{
|
||||
return mysqli_commit($handler);
|
||||
}
|
||||
|
||||
static public function rollback($handler)
|
||||
{
|
||||
return mysqli_rollback($handler);
|
||||
}
|
||||
}
|
||||
?>
|
@ -19,7 +19,7 @@ abstract class Model
|
||||
|
||||
function __construct()
|
||||
{
|
||||
$this->handler = DBConnector::getInstance()->getConnect(Env::getParam('db_settings'));
|
||||
$this->handler = DBConnector::getConnect(Env::getParam('db_settings'));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -33,9 +33,9 @@ abstract class Model
|
||||
if (DEBUG_ENABLE) {
|
||||
$time = microtime(true);
|
||||
}
|
||||
$res = mysqli_query($this->handler, $sql);
|
||||
if (mysqli_errno($this->handler)) {
|
||||
throw new MJException("<b>Query Error:</b>\n".$sql."\n<b>Error:</b>\n".mysqli_error($this->handler), 1);
|
||||
$res = DBConnector::query($this->handler, $sql);
|
||||
if ($error = DBConnector::error($this->handler)) {
|
||||
throw new MJException("<b>Query Error:</b>\n".$sql."\n<b>Error:</b>\n".$error, 1);
|
||||
}
|
||||
|
||||
if (DEBUG_ENABLE) {
|
||||
@ -77,11 +77,11 @@ abstract class Model
|
||||
{
|
||||
if(is_array($data)){
|
||||
foreach($data as $id => $val){
|
||||
$data[$id] = mysqli_real_escape_string($this->handler, $val);
|
||||
$data[$id] = DBConnector::escape($this->handler, $val);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
return mysqli_real_escape_string($this->handler, $data);
|
||||
return DBConnector::escape($this->handler, $data);
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
@ -123,17 +123,17 @@ abstract class Model
|
||||
|
||||
function setAutocommit($set)
|
||||
{
|
||||
return mysqli_autocommit($this->handler, (bool) $set);
|
||||
return DBConnector::autocommit($this->handler, (bool) $set);
|
||||
}
|
||||
|
||||
function commit()
|
||||
{
|
||||
return mysqli_commit($this->handler);
|
||||
return DBConnector::commit($this->handler);
|
||||
}
|
||||
|
||||
function rollback()
|
||||
{
|
||||
return mysqli_rollback($this->handler);
|
||||
return DBConnector::rollback($this->handler);
|
||||
}
|
||||
}
|
||||
|
||||
@ -156,7 +156,7 @@ class ModelSelectResult extends ModelResult
|
||||
|
||||
function fetch($class_name = false)
|
||||
{
|
||||
return $class_name ? mysqli_fetch_object($this->result, $class_name) : mysqli_fetch_object($this->result);
|
||||
return DBConnector::fetchObject($this->result, $class_name);
|
||||
}
|
||||
|
||||
function fetchField($field, $default = false)
|
||||
@ -169,11 +169,11 @@ class ModelSelectResult extends ModelResult
|
||||
{
|
||||
$array = array();
|
||||
if ($key) {
|
||||
while ($row = mysqli_fetch_object($this->result)) {
|
||||
while ($row = DBConnector::fetchObject($this->result)) {
|
||||
$array[$row->$key] = $row;
|
||||
}
|
||||
} else {
|
||||
while ($row = mysqli_fetch_object($this->result)) {
|
||||
while ($row = DBConnector::fetchObject($this->result)) {
|
||||
$array[] = $row;
|
||||
}
|
||||
}
|
||||
@ -182,12 +182,12 @@ class ModelSelectResult extends ModelResult
|
||||
|
||||
function count()
|
||||
{
|
||||
return mysqli_num_rows($this->result);
|
||||
return DBConnector::numRows($this->result);
|
||||
}
|
||||
|
||||
function free()
|
||||
{
|
||||
mysqli_free_result($this->result);
|
||||
DBConnector::free($this->result);
|
||||
}
|
||||
|
||||
function __destruct() {
|
||||
@ -201,7 +201,7 @@ class ModelChangeResult extends ModelResult
|
||||
|
||||
function __construct($resource)
|
||||
{
|
||||
$this->affected = mysqli_affected_rows($resource);
|
||||
$this->affected = DBConnector::affectedRows($resource);
|
||||
}
|
||||
|
||||
function count()
|
||||
@ -217,7 +217,7 @@ class ModelInsertResult extends ModelChangeResult
|
||||
function __construct($resource)
|
||||
{
|
||||
parent::__construct($resource);
|
||||
$this->id = mysqli_insert_id($resource);
|
||||
$this->id = DBConnector::getId($resource);
|
||||
}
|
||||
|
||||
function getId()
|
||||
|
Reference in New Issue
Block a user