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); } } ?>