. ======================================================================*/ if (!function_exists('pg_connect')) trigger_error('pg_* functions not defined. Install php*-pgsql.', E_USER_ERROR); require_once(dirname(__FILE__) . '/KjwSql.php'); require_once(dirname(__FILE__) . '/KjwPgResultSet.php'); /** @ingroup sql * * KjwPgSql is the postgresql implementation of the KjwSql abstract class. */ class KjwPgSql extends KjwSql { var $_dbconn; /**< Database resource */ /** * Construct a KjwPgSql object. * * @param $hostname Server name or IP. * @param $portnum Port number of 0 if default. * @param $username User name. * @param $password Password. * @param $database Database name. */ function KjwPgSql($hostname, $portnum, $username, $password, $database) { parent::KjwSql('pgsql', $hostname, $portnum ? $portnum : 5432, $username, $password, $database); $this->_dbconn = null; // null = unconnected, false = failed connect } /* [INHERITED DOCS] */ function destroy() { $this->disconnect(); parent::destroy(); } /* [INHERITED DOCS] */ function connect() { if ($this->_dbconn) return true; // XXX may add 'connect_timeout=' option $connect_string = "host='" . addslashes($this->_hostname) . "' " . "port={$this->_portnum} " . "user='" . addslashes($this->_username) . "' " . "password='" . addslashes($this->_password) . "' " . "dbname='" . addslashes($this->_database) . "'"; if (($res = @pg_connect($connect_string, PGSQL_CONNECT_FORCE_NEW)) == false) $this->croak('Database Connection Failure', 'pg_connect: Connection failed. Wrong host/user/pass?', 2); // Most likely we'll be using UTF-8 all over. if (pg_set_client_encoding($res, 'utf8')) $this->croak('encoding failed!', 2); $this->_dbconn = $res; return true; } /* [INHERITED DOCS] */ function disconnect() { if (!$this->_dbconn) return true; if (($ret = @pg_close($this->_dbconn)) == true) $this->_dbconn = null; return $ret; } /* [INHERITED DOCS] */ function beginWork() { // "START TRANSACTION is standard SQL syntax and is the recommended way to start an ad-hoc transaction." return $this->execute('START TRANSACTION'); } /* [INHERITED DOCS] */ function execute($query) { $this->_setLastQuery($query); if ($this->_dbconn === null) $this->connect(); // Auto-connect once. if (($res = @pg_query($this->_dbconn, $query)) === false) $this->croak('Database Data Transfer Failure', "pg_query: " . pg_last_error($this->_dbconn), 2); if (is_bool($res)) return true; return new KjwPgResultSet($res); } /* [INHERITED DOCS] */ function safeQuote($mixed) { if (is_string($mixed)) { if ($this->_dbconn === null) $this->connect(); // Try to autoconnect once. return "'" . pg_escape_string($this->_dbconn, $mixed) . "'"; } return parent::safeQuote($mixed); } /* [INHERITED DOCS] */ function nameQuote($string) { return '"' . str_replace('"', '""', preg_replace("/[\x01-\x1f]/", '', str_replace("\x00", '', $string))) . '"'; } /* [INHERITED DOCS] */ function affectedRows() { return pg_affected_rows($this->_dbconn); } /* [INHERITED DOCS] */ function insertId() { // LASTVAL() is new in postgresql 8.1 return $this->seletAtom('LASTVAL()'); } } ?>