note 64204 added to ref.pdo-pgsql
| From: | lauris at nix dot lv | Date: | Sun, 09 Apr 2006 22:03:38 +0000 |
| Subject: | note 64204 added to ref.pdo-pgsql | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-107666@lists.php.net to get a copy of this message | ||
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));
}
}
}
----
Server IP: 81.94.227.69
Probable Submitter: 213.21.198.203 (proxied: unknown)
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/ref.pdo-pgsql.php
Edit -- http://master.php.net/note/edit/64204
Del: integrated -- http://master.php.net/note/delete/64204/integrated
Del: useless -- http://master.php.net/note/delete/64204/useless
Del: bad code -- http://master.php.net/note/delete/64204/bad+code
Del: spam -- http://master.php.net/note/delete/64204/spam
Del: non-english -- http://master.php.net/note/delete/64204/non-english
Del: in docs -- http://master.php.net/note/delete/64204/in+docs
Del: other reasons-- http://master.php.net/note/delete/64204
Reject -- http://master.php.net/note/reject/64204
Search -- http://master.php.net/manage/user-notes.php