/*
* $Id: dbi_pgsql.php,v 1.13 2001/04/04 09:30:15 erwin Exp $
*
* This class is based on the Perl DBI.
* New functionality should be added accordingly.
* Please refer to the DBI documentation (perldoc DBI).
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
class DBI {
var $session;
var $autocommit = 1;
var $debug = 1;
var $transaction = 0;
var $errstr="";
var $RaiseError = 1;
var $Driver_name = "pgsql";
function DBI ($db, $user, $password) {
$this->session = pg_connect("dbname=$db user=$user password=$password");
}
function prepare ($query) {
if (!$this->autocommit && !$this->transaction) {
$this->transaction = 1;
pg_exec("BEGIN");
}
return new STH($this, $this->session, $query, $this->autocommit, $this->debug);
}
function autocommit ($value) {
if ($this->transaction && $value) {
pg_exec("COMMIT");
$this->transaction = 0;
} elseif ($value) {
$this->transaction = 0;
}
$this->autocommit = $value;
}
function commit () {
return pg_exec("COMMIT");
$this->transaction = 0;
}
function rollback () {
return pg_exec("ROLLBACK");
$this->transaction = 0;
}
function quote ($str) {
if (get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return "'".AddSlashes($str)."'";
}
function insert_id ($sequence) {
$sth = new STH($this, $this->session, "SELECT currval('$sequence')", $this->mode, $this->debug);
$sth->execute();
list($res) = $sth->fetchrow_array();
return $res;
}
}
class STH {
var $query;
var $res;
var $autocommit;
var $debug;
var $row;
var $session;
var $placeholders;
var $dbi;
function STH (&$dbi, $session, $query, $autocommit, $debug) {
$this->dbi = &$dbi;
$this->query = $query;
$this->autocommit = $autocommit;
$this->debug = $debug;
$this->session = $session;
// Scan for placeholders
$this->placeholders = array();
$quote = '';
for ($i = 0; $i < strlen($query); ++$i) {
if ($query[$i] == "'") {
if (empty($quote)) {
$quote = "'";
} elseif ($quote == "'") {
$quote = '';
}
} elseif ($query[$i] == '"') {
if (empty($quote)) {
$quote = '"';
} elseif ($quote == '"') {
$quote = '';
}
} elseif ($query[$i] == '?') {
if (empty($quote)) {
array_push($this->placeholders, $i);
}
}
}
}
function execute () {
global $SERVER_NAME;
global $SCRIPT_FILENAME;
$numargs = func_num_args();
$arg_list = func_get_args();
$parms = array();
for ($i = 0; $i < $numargs; $i++) {
if (is_array($arg_list[$i])) {
while (list($dummy,$parm) = each ($arg_list[$i])) {
array_push($parms, $parm);
}
} else {
array_push($parms,$arg_list[$i]);
}
}
if (sizeof($parms) != sizeof($this->placeholders)) {
trigger_error("
SQL Query (".$this->query.") contains ".sizeof($this->placeholders)." placeholders but ".sizeof($parms)." was passed on page $SCRIPT_FILENAME
", E_USER_ERROR);
}
if (sizeof($parms) > 0) {
$query = substr($this->query, 0, $this->placeholders[0]);
for ($i = 0; $i < sizeof($parms) - 1; ++$i) {
$query .= $this->dbi->quote($parms[$i]) . substr($this->query, $this->placeholders[$i] + 1, $this->placeholders[$i + 1] - $this->placeholders[$i] - 1);
}
$query .= $this->dbi->quote($parms[$i]) . substr($this->query, $this->placeholders[$i] + 1);
} else {
$query = $this->query;
}
if ($this->debug) { // Log the query
$fd = fopen("/tmp/dbi.$SERVER_NAME.log", "a") or die ("Couldn't append to file");
fputs($fd, date("M d H:i:s",time()).": ".$query."\n==================\n");
fclose($fd);
}
$this->res = @pg_exec($this->session, $query);
if (!$this->res) {
if ($this->dbi->RaiseError) {
trigger_error("
Could not execute SQL query: \"".$query."\"
".pg_errormessage($this->session)."
", E_USER_ERROR);
}
$this->dbi->errstr = pg_errormessage($this->session);
}
$this->row = 0;
return $this->res;
}
function fetchrow_array () {
if($this->row<$this->rows())
return @pg_fetch_row($this->res, $this->row++);
}
function fetchrow_hash () {
if($this->row<$this->rows())
return @pg_fetch_array($this->res, $this->row++);
}
function finish () {
pg_freeresult($this->res);
}
function rows () {
return pg_numrows($this->res);
}
}
?>