Просмотр файла system/inc/classes/mysql.class.php

Размер файла: 4.17Kb
<?

class mysql_base
{
#
# Private properties
#

private $host;
private $user;
private $password;
private $db_name;
private $prefix;

private $connect_id;

private $last_query_result;

private $sql_substitutions_args;

#
# Public properties
#

public $num_queries = 0;

#
# Public methods
#

public function __construct($host, $user, $password, $db_name, $charset)
{
$this->host = $host;
$this->user = $user;
$this->password = $password;
$this->db_name = $db_name;
$this->prefix = $prefix;
$this->charset = $charset;

$this->connect_p();
}

public function close()
{
if ($this->connect_id)
{
mysql_close($this->connect_id);
$this->connect_id = null;
}
}

public function connect()
{
if (!$this->connect_id)
{
$this->connect_p();
}
}

public function query($sql)
{
if ($this->connect_id)
{
unset($this->last_query_result);

$args = func_get_args();
$sql = $this->parse_sql_substitutions($sql, $args);

$this->last_query_result = mysql_query($sql, $this->connect_id);
if ($this->last_query_result)
{
$this->num_queries++;
} else {
unset($this->last_query_result);
throw new SQLDBException("Запрос: '" . $sql . "' не выполнен!");
}
} else {
throw new ConnectDBException("Нет коннекта к базе (метод query)");
}
}

public function insert($table, $data)
{
$this->query("INSERT INTO `?_?c` (?k) VALUES (?s);", $table, array_keys($data), array_values($data));
}

public function num_rows()
{
if ($this->connect_id && isset($this->last_query_result))
{
return mysql_num_rows($this->last_query_result);
}
}

public function fetch_array()
{
if ($this->connect_id && isset($this->last_query_result))
{
return mysql_fetch_array($this->last_query_result);
}
}

#
# Private methods
#

private function sql_substitutions_filter($result, $type)
{
switch ($type)
{
case 's':
$result = "'" . mysql_real_escape_string($result, $this->connect_id) . "'";
break;

case 'd':
$result = (int)$result;
break;

case 'k':
$result = '`' . $result . '`';
break;

case '_': case 'c':
$result = $result;
break;

default:
$result = $result;
}

return $result;
}

private function parse_sql_substitutions_callback($matches)
{
static $count = 1;

if ($matches[1] == '_')
{
$result = $this->prefix;
} else {
$result = $this->sql_substitutions_args[$count++];
}

if (gettype($result) == 'array')
{
$tmp = '';

foreach ($result as $k => $v)
{
$tmp .= $this->sql_substitutions_filter($v, $matches[1]) . ((isset($result[$k + 1])) ? ', ' : '');
}

$result = $tmp;
} else {
$result = $this->sql_substitutions_filter($result, $matches[1]);
}

return $result;
}

private function parse_sql_substitutions($sql, $args)
{
$this->sql_substitutions_args = $args;

$sql = preg_replace_callback('#\\?([a-z_])#', array(&$this, "parse_sql_substitutions_callback"), $sql);

$this->sql_substitutions_args = array();

return $sql;
}

private function connect_p()
{
$this->connect_id = mysql_connect($this->host, $this->user, $this->password);

if ($this->connect_id)
{
mysql_query("SET NAMES '" . $this->charset . "'", $this->connect_id);

$db_select = mysql_select_db($this->db_name, $this->connect_id);

if (!$db_select)
{
mysql_close($this->connect_id);
$this->connect_id = null;
throw new ConnectDBException("Не могу выбрать БД!");
}
} else {
throw new ConnectDBException("Не смог подконнектиться к базе!");
}
}
}

#
# Exceptions classes
#

// Тут мы по сути не расширяем, а просто объявляем новый тип исключения, для более удобной работы при их отлове.

//Генерируется, при отстутствующем коннекте к базе.
class ConnectDBException extends Exception
{
public function __construct($message)
{
parent::__construct($message);
}
}

//Генерируется, при невыполненом SQL-запросе.
class SQLDBException extends Exception
{
public function __construct($message)
{
parent::__construct($message);
}
}

$dbname = new mysql_base(DBHOST, DBUSER, DBPASS, DBNAME,'utf8');
?>