note 64204 deleted from ref.pdo-pgsql by bjori
| From: | bjori@php.net | Date: | Sun, 09 Apr 2006 23:16:30 +0000 |
| Subject: | note 64204 deleted from ref.pdo-pgsql by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-107671@lists.php.net to get a copy of this message | ||
Note Submitter: lauris at nix dot lv
Reason: bad code
----
simple postgresql PDO implementation for php 5.0 unsing standard pg_* a lot of stuff is missing but
can be easily added if needed.
if ( ! class_exists('PDO'))
{
class PDOStatement
{
private $_con;
private $_sql;
private $_exec;
private $_bind = array();
public function __construct($con, $sql)
{
$this->_con = $con;
$this->_sql = $sql;
}
public function bindValue($parameter, $value, $data_type = null)
{
$val = null;
switch($data_type)
{
case PDO::PARAM_INT:
$val = intval($value);
break;
case PDO::PARAM_STR:
$val = '\'' . pg_escape_string($value) . '\'';
break;
case PDO::PARAM_BOOL:
$val = ($value ? 'true' : 'false');
break;
case PDO::PARAM_LOB:
$val = pg_escape_bytea($value);
break;
default:
$val = '\'' . $value . '\'';
break;
}
$this->_bind[$parameter] = $val;
}
public function execute()
{
$sql_do = $this->_sql;
foreach ($this->_bind as $pname => $pval)
{
$sql_do = str_replace($pname, $pval, $sql_do);
}
$this->_exec = @pg_query($this->_con, $sql_do);
return ($this->_exec !== false);
}
public function fetch($fetch_style = PDO::FETCH_BOTH)
{
if ($fetch_style == PDO::FETCH_BOTH)
{
return pg_fetch_array($this->_exec, null, PGSQL_BOTH);
}
elseif ($fetch_style == PDO::FETCH_OBJ)
{
return pg_fetch_object($this->_exec);
}
}
public function fetchAll($fetch_style = PDO::FETCH_BOTH)
{
$rez = array();
while ($row = $this->fetch($fetch_style))
{
array_push($rez, $row);
}
return $rez;
}
public function errorInfo()
{
return array(0, 0, pg_last_error($this->_con));
}
}
class PDO
{
const PARAM_NULL = 0;
const PARAM_INT = 1;
const PARAM_STR = 2;
const PARAM_LOB = 3;
const PARAM_STMT = 4;
const PARAM_BOOL = 5;
const FETCH_BOTH = 10;
const FETCH_OBJ = 11;
private $_con = null;
private $_sql = null;
public function __construct($dsn, $username = null, $password = null)
{
$s = strpos($dsn, ':');
$ncon = str_replace(';', ' ', substr($dsn, $s+1));
if ($username != null)
{
$ncon .= ' user=' . $username;
}
if ($password != null)
{
$ncon .= ' password=' . $password;
}
$this->_con = pg_connect($ncon);
}
public function prepare($sql)
{
return new PDOStatement($this->_con, $sql);
}
public function errorInfo()
{
return array(0, 0, pg_last_error($this->_con));
}
}
}