<?php
/*****************************************************************************
* NedEngine (c) René Gabriëls 2002 *
* NedEngine (c) Michiel van den Berg 2004 *
* Update 2005 by Mispunt *
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *
* This program is free software. You can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License. *
* -------------------------------------------------------------------------- *
* Database abstraction layer for PostgreSQL *
*****************************************************************************/
class _db {
// Database server
var $server;
// Database name
var $name;
// Database user
var $user;
// Database password of $user
var $password;
// Resource ID of the opened connection
var $connection;
// Number of executed queries per page
var $query_count = 0;
// Contains the result of the last query
var $query_result;
/*************************************************************************
* Constructor *
*************************************************************************/
function _db ($db_server, $db_name, $db_user, $db_password) {
// Set database vars
$this->server = $db_server;
$this->name = $db_name;
$this->user = $db_user;
$this->password = $db_password;
// Connect to the database
$this->connect ();
}
/*************************************************************************
* Connect to the database and displey an error when it fails *
*************************************************************************/
function connect () {
// When no database is specified, assume that just a connect should do it :)
if ($this->name == "") {
$this->connection = pg_pconnect ("host=" . $this->server . " port=5432 dbname=test user=" . $this->user . " password=" . $this->password)
or die ("Kon geen verbinding opbouwen met de database op " . $this->server);
}
else {
$this->connection = pg_pconnect ("host=" . $this->server . " port=5432 dbname=" . $this->name . " user=" . $this->user . " password=" . $this->password)
or die ("Kon geen verbinding opbouwen met de database op " . $this->server);
}
}
/*************************************************************************
* Execute a query on the database through the $conntection ID *
*************************************************************************/
function query ($query) {
global $debug_mode;
// When we are in debug mode -> display the specified query
if ($debug_mode == 1) {
echo "<br>================<br>\n";
echo nl2br (str_replace (" ", " ", $query));
echo "<br>================<br>\n";
}
// Execute query
if ($query != "") {
unset ($this->query_result);
$this->query_count++;
$this->query_result = @pg_query ($this->connection, $query);
}
// Check if the query has been executed and show a message in debug mode
if ($debug_mode == 1 && !isset ($this->query_result)) {
echo "<span class='texterror'>Kon deze query niet uitvoeren.</span><br>";
echo pg_last_error ();
return false;
}
// Return result identifier
return $this->query_result;
}
/*************************************************************************
* Return one row of the resultset (where the pointer is) *
*************************************************************************/
function fetch_array () {
if ($this->query_result) {
return @pg_fetch_array ($this->query_result);
}
else {
return false;
}
}
/*************************************************************************
* Fetches all data and puts it into an array $data *
*************************************************************************/
function fetch_all () {
if ($this->query_result) {
$data = array();
for ($i = 0; $i < $this->num_rows(); $i++)
$data[$i] = @pg_fetch_array ($this->query_result);
return $data;
} else {
return false;
}
}
/*************************************************************************
* Return the number of affected rows *
*************************************************************************/
function affected_rows () {
if ($this->connection) {
$rows_affected = @pg_affected_rows ($this->connection);
return $rows_affected;
}
else {
return false;
}
}
/*************************************************************************
* Return the rowcount of the last executed query *
*************************************************************************/
function num_rows () {
if (isset ($this->query_result)) {
return @pg_numrows ($this->query_result);
}
else {
return 0;
}
}
/*************************************************************************
* Return the fieldcount of the last executed query *
*************************************************************************/
function num_fields () {
if (isset ($this->query_result)) {
return @pg_numfields ($this->result);
}
else {
return false;
}
}
/*************************************************************************
* Return the fieldname of $field_id *
*************************************************************************/
function field_name ($field_id) {
if (isset ($this->query_result)) {
return @pg_fieldname ($this->query_result, $field_id);
}
else {
return false;
}
}
/*************************************************************************
* Return the type of $field_id *
*************************************************************************/
function field_type ($field_id) {
if (isset ($this->query_result)) {
return @pg_fieldtype ($this->query_result, $field_id);
}
else {
return false;
}
}
/*************************************************************************
* Set result pointer to specified row *
*************************************************************************/
function data_seek ($row) {
if (isset ($this->query_result)) {
return @pg_result_seek ($this->query_result, $row);
}
else {
return false;
}
}
/*************************************************************************
* Free resultset before end of script if much memory is used *
*************************************************************************/
function free_result () {
if (isset ($this->query_result)) {
@pg_freeresult ($this->connection);
return true;
}
else {
return false;
}
}
}
?>