00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 if (!function_exists('pg_connect'))
00021 trigger_error('pg_* functions not defined. Install php*-pgsql.', E_USER_ERROR);
00022 require_once(dirname(__FILE__) . '/KjwSql.php');
00023 require_once(dirname(__FILE__) . '/KjwPgResultSet.php');
00024
00029 class KjwPgSql extends KjwSql {
00030 var $_dbconn;
00041 function KjwPgSql($hostname, $portnum, $username, $password, $database) {
00042 parent::KjwSql('pgsql', $hostname, $portnum ? $portnum : 5432, $username, $password, $database);
00043 $this->_dbconn = null;
00044 }
00045
00046
00047 function destroy() {
00048 $this->disconnect();
00049 parent::destroy();
00050 }
00051
00052
00053 function connect() {
00054 if ($this->_dbconn)
00055 return true;
00056
00057 $connect_string = "host='" . addslashes($this->_hostname) . "' "
00058 . "port={$this->_portnum} "
00059 . "user='" . addslashes($this->_username) . "' "
00060 . "password='" . addslashes($this->_password) . "' "
00061 . "dbname='" . addslashes($this->_database) . "'";
00062 if (($res = @pg_connect($connect_string, PGSQL_CONNECT_FORCE_NEW)) == false)
00063 $this->croak('Database Connection Failure', 'pg_connect: Connection failed. Wrong host/user/pass?', 2);
00064
00065
00066 if (pg_set_client_encoding($res, 'utf8'))
00067 $this->croak('encoding failed!', 2);
00068 $this->_dbconn = $res;
00069 return true;
00070 }
00071
00072
00073 function disconnect() {
00074 if (!$this->_dbconn)
00075 return true;
00076 if (($ret = @pg_close($this->_dbconn)) == true)
00077 $this->_dbconn = null;
00078 return $ret;
00079 }
00080
00081
00082 function beginWork() {
00083
00084 return $this->execute('START TRANSACTION');
00085 }
00086
00087
00088 function execute($query) {
00089 $this->_setLastQuery($query);
00090 if ($this->_dbconn === null)
00091 $this->connect();
00092 if (($res = @pg_query($this->_dbconn, $query)) === false)
00093 $this->croak('Database Data Transfer Failure', "pg_query: " . pg_last_error($this->_dbconn), 2);
00094 if (is_bool($res))
00095 return true;
00096 return new KjwPgResultSet($res);
00097 }
00098
00099
00100 function safeQuote($mixed) {
00101 if (is_string($mixed)) {
00102 if ($this->_dbconn === null)
00103 $this->connect();
00104 return "'" . pg_escape_string($this->_dbconn, $mixed) . "'";
00105 }
00106 return parent::safeQuote($mixed);
00107 }
00108
00109
00110 function nameQuote($string) {
00111 return '"' . str_replace('"', '""', preg_replace("/[\x01-\x1f]/", '',
00112 str_replace("\x00", '', $string))) . '"';
00113 }
00114
00115
00116 function affectedRows() {
00117 return pg_affected_rows($this->_dbconn);
00118 }
00119
00120
00121 function insertId() {
00122
00123 return $this->seletAtom('LASTVAL()');
00124 }
00125
00126 }
00127 ?>