PDOStatement->fetch(PDO::FETCH_ASSOC);
}
function fetch_array()
{
return $this->PDOStatement->fetch(PDO::FETCH_BOTH);
}
function fetch_row()
{
return $this->PDOStatement->fetch(PDO::FETCH_NUM);
}
}
class Database
{
var $Prefix;
var $Functions;
var $Type;
var $PDO;
var $Error;
var $insert_id;
var $LastQuery;
var $ShowSQLError;
var $ShowSQLQuery;
var $LogSQLQuery;
var $LogFile;
function __construct()
{
$this->Prefix = '';
$this->Functions = array('NOW()', 'CURDATE()', 'CURTIME()', 'UUID()');
$this->Type = 'mysql'; // mysql, pgsql
$this->Error = '';
$this->LastQuery = '';
$this->ShowSQLError = false;
$this->ShowSQLQuery = false;
$this->LogSQLQuery = false;
$this->LogFile = dirname(__FILE__).'/../../Query.log';
}
function Connect($Host, $User, $Password, $Database)
{
if ($this->Type == 'mysql') $ConnectionString = 'mysql:host='.$Host.';dbname='.$Database;
else if ($this->Type == 'pgsql') $ConnectionString = 'pgsql:dbname='.$Database.';host='.$Host;
else $ConnectionString = '';
try {
$this->PDO = new PDO($ConnectionString, $User, $Password);
} catch (Exception $E)
{
unset($this->PDO);
throw new Exception($E->getMessage());
}
}
function Disconnect()
{
unset($this->PDO);
}
function Connected()
{
return isset($this->PDO);
}
function select_db($Database)
{
$this->query('USE `'.$Database.'`');
}
function query($Query)
{
if (!$this->Connected()) throw new Exception(T('Not connected to database'));
if (($this->ShowSQLQuery == true) or ($this->LogSQLQuery == true)) $QueryStartTime = microtime_float();
$this->LastQuery = $Query;
//echo('a'.$this->ShowSQLQuery.'<'.$QueryStartTime.', '.microtime_float());
if (($this->ShowSQLQuery == true) or ($this->LogSQLQuery == true))
$Duration = ' ; '.round(microtime_float() - $QueryStartTime, 4). ' s';
if ($this->LogSQLQuery == true)
file_put_contents($this->LogFile, $Query.$Duration."\n", FILE_APPEND);
if ($this->ShowSQLQuery == true)
echo('
'.$Query.$Duration.'
'."\n");
$Result = new DatabaseResult();
$Result->PDOStatement = $this->PDO->query($Query);
if ($Result->PDOStatement)
{
$Result->num_rows = $Result->PDOStatement->rowCount();
$this->insert_id = $this->PDO->lastInsertId();
} else
{
$this->Error = $this->PDO->errorInfo();
$this->Error = $this->Error[2];
if (($this->Error != '') and ($this->ShowSQLError == true))
echo('SQL Error: '.$this->Error.'
'.$Query.'
');
throw new Exception('SQL Error: '.$this->Error.', Query: '.$Query);
}
return $Result;
}
function select($Table, $What = '*', $Condition = 1)
{
return $this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition);
}
function delete($Table, $Condition)
{
$this->query('DELETE FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition);
}
function insert($Table, $Data)
{
$this->query($this->GetInsert($Table, $Data));
$this->insert_id = $this->PDO->lastInsertId();
}
function GetInsert($Table, $Data)
{
$Name = '';
$Values = '';
foreach ($Data as $Key => $Value)
{
$Name .= ',`'.$Key.'`';
if (!in_array($Value, $this->Functions))
{
if (is_null($Value)) $Value = 'NULL';
else $Value = $this->PDO->quote($Value);
}
$Values .= ','.$Value;
}
$Name = substr($Name, 1);
$Values = substr($Values, 1);
return 'INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')';
}
function update($Table, $Condition, $Data)
{
$this->query($this->GetUpdate($Table, $Condition, $Data));
}
function GetUpdate($Table, $Condition, $Data)
{
$Values = '';
foreach ($Data as $Key => $Value)
{
if (!in_array($Value, $this->Functions))
{
if (is_null($Value)) $Value = 'NULL';
else $Value = $this->PDO->quote($Value);
}
$Values .= ', `'.$Key.'`='.$Value;
}
$Values = substr($Values, 2);
return 'UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')';
}
function replace($Table, $Data)
{
$Name = '';
$Values = '';
foreach ($Data as $Key => $Value)
{
if (!in_array($Value, $this->Functions))
{
if (is_null($Value)) $Value = 'NULL';
else $Value = $this->PDO->quote($Value);
}
$Name .= ',`'.$Key.'`';
$Values .= ','.$Value;
}
$Name = substr($Name, 1);
$Values = substr($Values, 1);
//echo('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES ('.$Values.')
');
$this->query('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
//echo($this->error().'
');
}
function charset($Charset)
{
$this->query('SET NAMES "'.$Charset.'"');
}
function real_escape_string($Text)
{
return addslashes($Text);
}
function quote($Text)
{
return $this->PDO->quote($Text);
}
public function __sleep()
{
return array('LastQuery');
}
public function __wakeup()
{
}
public function Transaction($Queries)
{
$this->PDO->beginTransaction();
foreach ($Queries as $Query)
{
$Statement = $this->PDO->prepare($Query);
$Statement->execute();
}
if (!$this->PDO->commit())
{
$this->Error = $this->PDO->errorInfo();
$this->Error = $this->Error[2];
if (($this->Error != '') and ($this->ShowSQLError == true))
echo('SQL Error: '.$this->Error.'
'.$Query.'
');
throw new Exception('SQL Error: '.$this->Error.', Query: '.$Query);
}
}
}
function TimeToMysqlDateTime($Time)
{
if ($Time == NULL) return NULL;
else return date('Y-m-d H:i:s', $Time);
}
function TimeToMysqlDate($Time)
{
if ($Time == NULL) return NULL;
else return date('Y-m-d', $Time);
}
function TimeToMysqlTime($Time)
{
if ($Time == NULL) return NULL;
else return date('H:i:s', $Time);
}
function MysqlDateTimeToTime($DateTime)
{
if ($DateTime == '') return NULL;
$Parts = explode(' ', $DateTime);
$DateParts = explode('-', $Parts[0]);
$TimeParts = explode(':', $Parts[1]);
$Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]);
return $Result;
}
function MysqlDateToTime($Date)
{
if ($Date == '') return NULL;
return MysqlDateTimeToTime($Date.' 0:0:0');
}
function MysqlTimeToTime($Time)
{
if ($Time == '') return NULL;
return MysqlDateTimeToTime('0000-00-00 '.$Time);
}