| Current Path : /home/balossw/www/temp/common/includes/ |
| Current File : /home/balossw/www/temp/common/includes/db.inc.php |
<?php
/*---------------------------------------------------------------------------
File name ............ common/includes/db.class.php
Author ............... Antoine Lemoine
Creation date ........ 02.09.2009
Modification date .... 21.10.2009
---------------------------------------------------------------------------*/
function buildSafeQuery()
{
global $global;
$args = func_get_args();
return $global->mysql_connection->buildSafeQuery($args);
}
class MysqlConnection
{
var $link;
var $dbconfig;
var $var_types;
function MysqlConnection($dbconfig)
{
$this->dbconfig = $dbconfig;
$this->var_types = array('string','boolean','integer','double');
}
function connect()
{
if (!isset($this->link))
{
$this->link = mysql_connect($this->dbconfig['hostname'],$this->dbconfig['user'],$this->dbconfig['password']);
if (!$this->link) trigger_error('MysqlConnection.buildSafeQuery() - mysql error : '.mysql_error(),E_USER_ERROR);
mysql_select_db($this->dbconfig['base'], $this->link);
}
}
function close()
{
if (isset($this->link)) mysql_close($this->link);
}
function buildSafeQuery()
{
$args = func_get_args();
if (count($args)==0) trigger_error('buildSafeQuery() - not enough arguments',E_USER_ERROR);
if (count($args)==1) $args = $args[0];
if (gettype($args[0])!='string') trigger_error('buildSafeQuery() - first argument must be a string',E_USER_ERROR);
$this->connect();
if (count($args)==2 && gettype($args[1])=='array')
{
$params = array();
for ($i=0; $i<count($args[1]); $i++)
{
$value = $args[1][$i];
$type = gettype($value);
if (in_array($type,$this->var_types)) $params[] = escape($value);
else trigger_error('buildSafeQuery() - unsupported data type `'.$type.'` for argument[1]['.$i.']',E_USER_ERROR);
}
}
else
{
$params = array();
for ($i=1; $i<count($args); $i++)
{
$value = $args[$i];
$type = gettype($value);
if (in_array($type,$this->var_types)) $params[] = escape($value);
else trigger_error('buildSafeQuery() - unsupported data type `'.$type.'` for argument['.$i.']',E_USER_ERROR);
}
}
return new Query(vsprintf($args[0],$params));
}
}
class QueryResult
{
var $numrows;
var $error;
var $rows;
var $success;
var $affected_rows;
var $insert_id;
function QueryResult($result, $query_string)
{
$this->rows = array();
if (is_bool($result))
{
$this->result = null;
$this->success = $result;
$this->error = mysql_error();
$this->numrows = 0;
if ($this->error) trigger_error('QueryResult.QueryResult() - mysql error : '.$this->error.' ['.$query_string.']',E_USER_ERROR);
else
{
$this->affected_rows = mysql_affected_rows();
$this->insert_id = mysql_insert_id();
}
}
else
{
$this->success = true;
$this->affected_rows = mysql_affected_rows();
$this->insert_id = mysql_insert_id();
$this->numrows = mysql_numrows($result);
for ($i=0; $i<$this->numrows; $i++)
{
$this->rows[$i] = mysql_fetch_array($result, MYSQL_ASSOC);
}
}
}
function getRow($i)
{
return isset($this->rows[$i]) ? $this->rows[$i] : null;
}
function isSuccess()
{
return $this->success;
}
function getAffectedRowsCount()
{
return $this->affected_rows;
}
function getInsertId()
{
return $this->insert_id;
}
}
class Query
{
var $query_str;
var $result;
function Query($query)
{
$this->query_str = $query;
$this->mysql_connection = $mysql_connection;
}
function getResult()
{
$mysql_result = @mysql_query($this->query_str);
$this->result = new QueryResult($mysql_result,$this->query_str);
return $this->result;
}
}
function escape($value)
{
$type = gettype($value);
if ($type=='string') return mysql_real_escape_string($value);
else if (in_array($type,array('boolean','integer','double'))) return $value;
else trigger_error('escape() - unsupported data type `'.$type.'`',E_USER_ERROR);
}
?>