session = pg_pconnect("user=$user port=$port dbname=$db host=$host password=$password"); } /*function DBI ($db, $user, $password) { $this->session = pg_connect("user=$user password=$password dbname=$db"); }*/ /*function DBI ($db, $user, $port, $host) { $this->session = pg_connect("user=$user port=$port dbname=$db host=$host"); }*/ function prepare ($query) { if (!$this->autocommit && !$this->transaction) { $this->transaction = 1; pg_execute("BEGIN"); } return new STH($this, $this->session, $query, $this->autocommit, $this->debug); } function autocommit ($value) { if ($this->transaction && $value) { pg_execute("COMMIT"); $this->transaction = 0; } elseif ($value) { $this->transaction = 0; } $this->autocommit = $value; } function commit () { $this->transaction = 0; return pg_execute("COMMIT"); } function rollback () { $this->transaction = 0; return pg_execute("ROLLBACK"); } function quote ($str) { if (isset($str)) { if (get_magic_quotes_gpc()) { $str = stripslashes($str); } return "'".AddSlashes($str)."'"; } else { return "NULL"; } } function insert_id ($sequence) { $sth = new STH($this, $this->session, "SELECT currval('$sequence')", $this->autocommit, $this->debug); $sth->execute(); list($res) = $sth->fetchrow_array(); return $res; } function disconnect () { return pg_close($this->session); } } class STH { public $query; public $autocommit; public $debug; public $session; public $placeholders; public $dbi; function STH (&$dbi, $session, $query, $autocommit, $debug) { $this->dbi = &$dbi; $this->query = $query; $this->autocommit = $autocommit; $this->debug = $debug; $this->session = $session; //print($query."
"); // Scan for placeholders $this->placeholders = array(); $quote = ''; $alterflag = 0; for ($i = 0; $i < strlen($query); ++$i) { if ($query[$i] == "'") { if (empty($quote)) { $quote = "'"; } elseif ($quote == "'" && $i > 0 && $query[$i - 1] != "\\") { $quote = ''; } } elseif ($query[$i] == '"') { if (empty($quote)) { $quote = '"'; } elseif ($quote == '"' && $i > 0 && $query[$i - 1] != "\\") { $quote = ''; } } elseif ($query[$i] == '?') { if (empty($quote)) { array_push($this->placeholders, $i); } } } } function execute () { global $_SERVER; $SERVER_NAME = $_SERVER['SERVER_NAME']; $SCRIPT_FILENAME = $_SERVER['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)) { die("
SQL Query (".$this->query.") contains ".sizeof($this->placeholders)." placeholders but ".sizeof($parms)." was passed on page $SCRIPT_FILENAME
"); } if (sizeof($parms) > 0) { $query = substr($this->query, 0, $this->placeholders[0]); $userflag = 0; if (strstr($query,"create user") || strstr($query,"alter user") || strstr($query,"drop user")) { $userflag = 1; } for ($i = 0; $i < sizeof($parms) - 1; ++$i) { if ($userflag) { $query .= $parms[$i] . substr($this->query, $this->placeholders[$i] + 1, $this->placeholders[$i + 1] - $this->placeholders[$i] - 1); $userflag = 0; } else { $query .= $this->dbi->quote($parms[$i]) . substr($this->query, $this->placeholders[$i] + 1, $this->placeholders[$i + 1] - $this->placeholders[$i] - 1); } } if ($userflag) { $query .= $parms[$i] . substr($this->query, $this->placeholders[$i] +1); $userflag = 0; } else { $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_execute($this->session, $query); if (!$this->res) { if ($this->dbi->RaiseError) { die("
Could not execute SQL query: \"".$query."\"

".pg_last_error($this->session)."
"); } $this->dbi->errstr = pg_last_error($this->session); } $this->row = 0; return $this->res; } function fetchrow_array () { if($this->row<$this->rows()) return @pg_fetch_row($this->res); } function fetchrow_hash () { if($this->row<$this->rows()) return @pg_fetch_array($this->res); } function finish () { pg_free_result($this->res); } function rows () { return pg_num_rows($this->res); } function arows () { return pg_affected_rows($this->res); } } ?>