Browse Source

custom DBConnector system

git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/trunk@75 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
master
akulikov 16 years ago
parent
commit
2e3a1e217c
  1. 86
      classes/DBConnector.class.php
  2. 32
      classes/Model.class.php
  3. 3
      init/sys.inc.php

86
classes/DBConnector.class.php

@ -13,8 +13,7 @@
*/ */
class DBConnector class DBConnector
{ {
private $handlers = array();
static private $instance = null;
static private $handlers = array();
static public $queries = array(); static public $queries = array();
/** /**
@ -25,43 +24,88 @@ class DBConnector
private function __clone(){} 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 - массив настроек * @param array $db_settings - массив настроек
* @return resource - идентификатор соединения * @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'])) { 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); 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']; 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);
}
} }
?> ?>

32
classes/Model.class.php

@ -19,7 +19,7 @@ abstract class Model
function __construct() 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) { if (DEBUG_ENABLE) {
$time = microtime(true); $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) { if (DEBUG_ENABLE) {
@ -77,11 +77,11 @@ abstract class Model
{ {
if(is_array($data)){ if(is_array($data)){
foreach($data as $id => $val){ foreach($data as $id => $val){
$data[$id] = mysqli_real_escape_string($this->handler, $val);
$data[$id] = DBConnector::escape($this->handler, $val);
} }
return $data; 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) function setAutocommit($set)
{ {
return mysqli_autocommit($this->handler, (bool) $set);
return DBConnector::autocommit($this->handler, (bool) $set);
} }
function commit() function commit()
{ {
return mysqli_commit($this->handler);
return DBConnector::commit($this->handler);
} }
function rollback() 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) 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) function fetchField($field, $default = false)
@ -169,11 +169,11 @@ class ModelSelectResult extends ModelResult
{ {
$array = array(); $array = array();
if ($key) { if ($key) {
while ($row = mysqli_fetch_object($this->result)) {
while ($row = DBConnector::fetchObject($this->result)) {
$array[$row->$key] = $row; $array[$row->$key] = $row;
} }
} else { } else {
while ($row = mysqli_fetch_object($this->result)) {
while ($row = DBConnector::fetchObject($this->result)) {
$array[] = $row; $array[] = $row;
} }
} }
@ -182,12 +182,12 @@ class ModelSelectResult extends ModelResult
function count() function count()
{ {
return mysqli_num_rows($this->result);
return DBConnector::numRows($this->result);
} }
function free() function free()
{ {
mysqli_free_result($this->result);
DBConnector::free($this->result);
} }
function __destruct() { function __destruct() {
@ -201,7 +201,7 @@ class ModelChangeResult extends ModelResult
function __construct($resource) function __construct($resource)
{ {
$this->affected = mysqli_affected_rows($resource);
$this->affected = DBConnector::affectedRows($resource);
} }
function count() function count()
@ -217,7 +217,7 @@ class ModelInsertResult extends ModelChangeResult
function __construct($resource) function __construct($resource)
{ {
parent::__construct($resource); parent::__construct($resource);
$this->id = mysqli_insert_id($resource);
$this->id = DBConnector::getId($resource);
} }
function getId() function getId()

3
init/sys.inc.php

@ -77,6 +77,9 @@ function __autoload($name)
$class_name = substr($name, 0, -strlen($type)); $class_name = substr($name, 0, -strlen($type));
switch ($type) { switch ($type) {
case 'DBConnector':
require(CORE_PATH.'/'.(defined('CUSTOM_DBCONNECTOR') ? CUSTOM_DBCONNECTOR.'_' : '').'DBConnector.class.php');
break;
case $name: case $name:
case EXCEPTION_POSTFIX: case EXCEPTION_POSTFIX:
require(CORE_PATH.'/'.$name.'.class.php'); require(CORE_PATH.'/'.$name.'.class.php');

Loading…
Cancel
Save