Файл: adultscript-2.0.3-pro/files/libraries/framework/database/mysql.php
Строк: 179
<?php
defined('_VALID') or die('Restricted Access!');
class VDatabase_Driver_mysql extends VDatabase
{
private $_link;
private $_query;
private $_query_count = 0;
private $_query_string;
private $_query_times = array();
private $_queries = array();
private $_row;
private $_result;
private $_record;
private $debug;
public function __construct()
{
parent::__construct();
$this->check();
$this->connect();
$this->select();
$this->setUTF();
}
public function __destruct()
{
session_write_close();
$this->close();
}
private function connect()
{
$connect = ( $this->db_persistent ) ? 'mysql_pconnect' : 'mysql_connect';
$this->_link = $connect($this->db_host, $this->db_username, $this->db_password);
if ( !$this->_link ) {
$this->error('Could not connect to the MySQL Server!');
}
}
private function select()
{
if ( !$this->_link ) {
$this->connect();
}
if ( !mysql_select_db($this->db_name) ) {
$this->error('Could not select mysql database (' .$this->db_name. ')!');
}
}
private function setUTF()
{
$this->query('SET NAMES utf8');
$this->query('SET CHARACTER SET utf8');
}
public function query($sql, $reconnect=FALSE)
{
// this is required for connection timeout bypass on shared servers :-)
if ($reconnect === TRUE) {
if ($this->_link) {
$this->close();
}
}
if (!$this->_link) {
$this->connect();
$this->select();
$this->setUTF();
}
if ($this->_query) {
$this->_query = NULL;
}
if ($this->_result) {
$this->_result = NULL;
}
$this->_query_string = str_replace('#__', $this->db_prefix, $sql);
$this->_query = mysql_query($this->_query_string, $this->_link);
if ( !$this->_query ) {
$this->error('Could not execute mysql query!');
}
$this->_queries[$this->_query_count] = $this->_query_string;
++$this->_query_count;
}
public function fetch_row()
{
$this->_result = array();
$this->_row = mysql_fetch_row($this->_query);
if ( !$this->_row ) {
$this->error('Could not fetch mysql row!');
}
$this->_result = $this->_row;
$this->free();
return $this->_result;
}
public function get_row($sql)
{
$this->query($sql);
return $this->fetch_row();
}
public function fetch_assoc()
{
$this->_result = array();
$this->_row = mysql_fetch_assoc($this->_query);
if ( !$this->_row ) {
$this->error('Could not fetch mysql row!');
}
$this->_result = $this->_row;
$this->free();
return $this->_result;
}
public function get_assoc($sql)
{
$this->query($sql);
return $this->fetch_assoc();
}
public function fetch_rows()
{
$this->_result = array();
while ( $this->_row = mysql_fetch_array($this->_query) ) {
$this->_result[] = $this->_row;
}
$this->free();
return $this->_result;
}
public function get_rows( $sql )
{
$this->query($sql);
return $this->fetch_rows();
}
public function fetch_field($field)
{
if (!$this->_result OR !isset($this->_result[$field])) {
$this->_result = $this->fetch_assoc();
}
if (isset($this->_result[$field])) {
return $this->_result[$field];
}
}
public function get_field($sql, $field)
{
if ($this->_result) {
$this->_result = null;
}
$this->query($sql);
return $this->fetch_field($field);
}
public function affected_rows()
{
return ( $this->_link ) ? mysql_affected_rows($this->_link) : NULL;
}
public function get_affected_rows($sql)
{
$this->query($sql);
return $this->affected_rows();
}
public function num_rows()
{
return ( $this->_query ) ? mysql_num_rows($this->_query) : NULL;
}
public function get_insert_id()
{
return ( $this->_query ) ? mysql_insert_id($this->_query) : NULL;
}
public function get_last_insert_id($table)
{
// THERE IS A PERFORMANCE BUG HERE!!!
return mysql_insert_id($this->_link);
// $this->query("SELECT LAST_INSERT_ID() as last_insert_id FROM ".str_replace('#__', $this->db_prefix, $table)." LIMIT 1");
// return $this->fetch_field('last_insert_id');
}
public function get_link_id()
{
return ( $this->_link ) ? $this->_link : NULL;
}
public function get_query_id()
{
return ( $this->_query ) ? $this->_query : NULL;
}
public function escape($text)
{
return mysql_real_escape_string($text, $this->_link);
}
private function free()
{
if ( $this->db_free ) {
return ( $this->_query ) ? mysql_free_result($this->_query) : NULL;
}
}
public function close()
{
if ($this->_link && is_resource($this->_link)) {
mysql_close($this->_link);
$this->_link = NULL;
}
$this->_link = NULL;
}
public function connected()
{
if (!$this->_link) {
$this->connect();
$this->select();
$this->setUTF();
}
return mysql_ping($this->_link);
}
public function debug()
{
if ($this->_link) {
$query = str_replace("n", ' ', $this->_query_string);
$query = preg_replace('/ss+/', ' ', $query);
echo var_dump($query). '<br />';
}
}
public function get($var)
{
if (isset($this->$var)) {
return $this->$var;
}
}
private function error($message)
{
if ( $this->_link ) {
$error = array();
$error[] = "Query: ".$this->_query_string;
$error[] = "Error Message: " .mysql_error($this->_link);
$error[] = "Error Number: " .mysql_errno($this->_link);
die(implode("<br />", $error));
$this->close();
}
die($message);
}
private function check()
{
if (!function_exists('mysql_connect'))
$this->error('MySQL adapter "mysql" is not available!');
}
public function dump()
{
return $this->_queries;
}
}
?>