<?php
/*
+----------------------------------------------------------------
|
| MJGUEST
| =============================================================
| Copyright (c) 2002-2008 Giacomo "mdsjack" Menni
| Terms of agreement and support at www.mdsjack.bo.it
|
+----------------------------------------------------------------
| [ DATABASE MODULE ]
| Database driver (Generic PDO Driver)
+----------------------------------------------------------------
*/
class PDO_driver
{
protected $id;
protected $query;
protected $lastquestion;
public $driver = ''; # const
public $answer = '';
public $tot_queries = 0;
public $error = false;
public $tables = array();
function __construct()
{
$this->tables = array
( '[TBL=entries]' => db_flag.'entries'
, '[TBL=settings]' => db_flag.'settings'
);
return true;
}
function __destruct()
{
unset($this->id);
}
public function __call($name, $args)
{
return true;
}
public function escape($value)
{
return $value;
# return $this->id->quote($value);
}
function ask($question, $sql = null, $params = array())
{
$this->lastquestion = $question;
$query = strtr(vsprintf(str_replace('%ยง', $sql, $this->questions[$this->lastquestion]), $params), $this->tables);
# echo $query;
$this->query($query);
# return $this->query->rowCount(); # non compatibile con vecchi drivers - non usare
return true;
}
public function get_field($num = 0)
{
$this->answer = $this->query->fetchColumn($num);
return $this->answer ? $this->answer : false;
}
public function get_row()
{
$this->answer = $this->query->fetch(PDO::FETCH_ASSOC);
return (bool) $this->answer;
}
public function query($sql)
{
$this->query = $this->id->query($sql);
$this->tot_queries++;
return true;
}
public function sql($sqlfile)
{
$sql = explode(';', str_replace(array('mjguest_', "\r\n"), array(db_flag, ' '), file_get_contents($sqlfile)));
try
{
$this->id->beginTransaction();
foreach ($sql as $query)
if (!empty($query))
$this->id->exec($query);
$this->id->commit();
}
catch (Exception $e)
{
$this->error($e);
$this->id->rollBack();
return false;
}
return true;
}
protected function error($e = '') # RIFARE
{
if (!$e) return;
// $e = &$this->id;
echo $this->id->getAttribute(PDO::ATTR_DRIVER_NAME);
echo $e->errorInfo();
echo $e->getCode();
echo $e->getMessage();
echo $this->id->errorCode();
print_r($this->id->errorInfo());
echo $this->query->errorCode();
print_r($this->query->errorInfo());
exit;
/*
if($this->error == true) return false;
if(mysqli_connect_errno()):
echo "
<h3>Database Error!</h3>
<strong>Module:</strong> ".db_type."<br />
<strong>Error:</strong> ".mysqli_connect_error();
$this->error = true;
return false;
endif;
$errno = mysqli_errno($this->id);
if ($errno <> 0):
echo "
<h3>Database Error!</h3>
<strong>Module:</strong> ".db_type."<br />
<strong>Error (#{$errno} - ".$this->lastquestion."):</strong> ".mysqli_error($this->id);
$this->error = true;
return false;
endif;
return true;*/
}
}
?>