// Category: Databases //**************************************************************************** // The lib is FREEWARE. This means you may use it anywhere you want, you may // do anything with it. The Author mentioned above is NOT responsible for any // consequences of using this library. // If you don't agree with this, you MAY NOT use the lib! //**************************************************************************** // All improvings, feature requests, bug reports, etc. are gladly accepted. //**************************************************************************** // Note: For best viewing of the code Tab size 4 is recommended //**************************************************************************** class CDatabase { var $link; var $db; var $host, $user, $pass; function CDatabase($db, $host = "localhost", $user = "", $pass = "") { $this->db = $db; $this->host = $host; $this->user = $user; $this->pass = $pass; $str = "host={$host} port=5432 dbname={$db} user={$user} password={$pass}"; $this->link = pg_connect($str); } function query($sql) { if (!$this->link) return 0; return pg_query($this->link, $sql); } function affected_rows() { return pg_affected_rows($this->link); } function num_rows($q) { return pg_num_rows($q); } function fetch_array($q) // fetchAll { return pg_fetch_array($q, NULL); } function fetch_object($q) // fetchObject { return pg_fetch_object($q); } /* function data_seek($q, $n) { return pg_data_seek($q, $n); } */ function free_result($q) { return pg_free_result($q); } function insert_id($seq) { $query = "SELECT currval('$seq')"; $res = $this->query($query); $row = pg_fetch_array($res, NULL, PGSQL_ASSOC); pg_free_result($res); return ($row) ? $row['currval'] : 0; } function error() { return pg_last_error($this->link); } function error_die($msg = '') { die(((empty($msg)) ? '' : $msg . ': ') . $this->error()); } function sql2var($sql) { if ((empty($sql)) || (!($query = $this->query($sql)))) return false; if ($this->num_rows($query) < 1) return false; return $this->result2var($query); } function result2var($q) { if (!($Data = $this->fetch_array($q))) return false; $this->free_result($q); foreach($Data as $k => $v) $GLOBALS[$k] = $v; return true; } /*function sql2array($sql, $keyField = '') { if ((empty($sql)) || (!($query = $this->query($sql)))) return false; if ($this->num_rows($query) < 1) return false; return $this->result2array($query, $keyField); }*/ function result2array($q, $keyField = '') { $Result = array(); while ($Data = $this->fetch_array($q)) if (empty($keyField)) $Result[] = $Data; else $Result[$Data[$keyField]] = $Data; $this->free_result($q); return $Result; } /*function list_tables() { return mysql_list_tables($this->db, $this->link); } function list_fields($table_name) { return mysql_list_fields($this->db, $table_name, $this->link); }*/ };