00001 <?php
00011 class DB_PostgreSQL extends DB_Base
00012 {
00013 var $table_defns = array();
00014
00015 function DB_PostgreSQL($conn, $persistent) {
00016 $this->safesql = new SafeSQL_ANSI;
00017 $this->type = 'postgresql';
00018
00019 $connstr = "dbname={$conn['name']}";
00020 if($conn['user']) $connstr .= " user={$conn['user']}";
00021 if($conn['pass']) $connstr .= " password={$conn['pass']}";
00022 if($conn['host']) $connstr .= " host={$conn['host']}";
00023 if($conn['port']) $connstr .= " port={$conn['port']}";
00024
00025 if($persistent) {
00026 $this->conn = pg_pconnect($connstr);
00027 } else {
00028 $this->conn = pg_connect($connstr);
00029 }
00030
00031 if(!$this->conn) {
00032 $this->_catch("Unable to connect to the database!");
00033 return false;
00034 };
00035
00036 return true;
00037 }
00038
00039 function _catch($msg="") {
00040 if(!$this->error = pg_last_error()) return true;
00041 $this->error($msg."<br>{$this->query} \n {$this->error}");
00042 }
00043
00044 function get_table_defn($table) {
00045 if(isset($this->table_defns[$table])) return $this->table_defns[$table];
00046
00047
00048 $fs = array();
00049 $res = pg_query($this->conn, "SELECT * FROM $table LIMIT 1");
00050 $i = pg_num_fields($res);
00051 for($j = 0; $j < $i; $j++) $fs[pg_field_name($res, $j)] = '';
00052
00053 $this->table_defns[$table] = $fs;
00054 return $fs;
00055 }
00056
00057 function run_query($sql) {
00058 return pg_query($this->conn, $sql);
00059 }
00060
00061 function fetch_row(&$q) {
00062 return pg_fetch_row($q);
00063 }
00064
00065 function fetch_array(&$q) {
00066 return pg_fetch_assoc($q);
00067 }
00068
00069 function fetch_field(&$q, $num) {
00070 return pg_fetch_row($q, $num);
00071 }
00072
00073 function num_fields(&$q) {
00074 return pg_num_fields($q);
00075 }
00076
00077 function num_rows(&$q) {
00078 return pg_num_rows($q);
00079 }
00080
00081 function get_insert_id() {
00082
00083
00084 $q = @pg_query($this->conn, "SELECT lastval()");
00085 return $q ? array_shift($this->fetch_row($q)) : 0;
00086 }
00087
00088 function free_result($q) {
00089 return @pg_free_result($q);
00090 }
00091 }
00092
00093 ?>