commit a9259440721b538aa78647a279b8cd4323ee05e3
parent 31df83d8bb6618b961b9c8626218fc58090b4f72
Author: dankert <devnull@localhost>
Date: Sat, 10 Nov 2007 01:54:45 +0100
Refactoring der Datenbank-Klassen. Keinen Fehler anzeigen, aber Fehler merken und ggf. FALSE zur?ckgeben.
Diffstat:
3 files changed, 223 insertions(+), 80 deletions(-)
diff --git a/db/db.class.php b/db/db.class.php
@@ -31,62 +31,126 @@
* @package openrat.database
*/
class DB
-{
- var $dbh;
- var $conf;
+{
+ /**
+ * Datenbank-Id.
+ *
+ * @var String
+ */
var $id;
-
-
- function DB( $conf = array() )
- {
- $this->connect( $conf );
- }
+
+ /**
+ * Konfiguration der Datenbank-Verbindung
+ *
+ * @var Array
+ */
+ var $conf;
+
+ /**
+ * Kennzeichen, ob die Datenbank verfügbar ist.
+ *
+ * @var Boolean
+ */
+ var $available;
+
+ /**
+ * Enthält eine Fehlermeldung (sofern verfügbar).
+ *
+ * @var String
+ */
+ var $error;
+
+ /**
+ * Client.
+ *
+ * @var Object
+ */
+ var $client;
+
+ /**
+ * Kontruktor.
+ * Erwartet die Datenbank-Konfiguration als Parameter.
+ *
+ * @param Array $conf
+ * @return Status
+ */
+ function DB( $conf )
+ {
+ $this->available = false;
+ $this->conf = $conf;
+
+ $this->available = $this->connect();
+
+ return $this->available;
+ }
- function connect( $conf = array() )
+
+ /**
+ * Verbindung zur Datenbank aufbauen.
+ *
+ * @return Status
+ */
+ function connect()
{
- if ( count($conf)>0 )
- $this->conf = $conf;
-// print_r($this->conf);
-
$type = $this->conf['type'];
$classname = 'db_'.$type;
- $this->dbh = & new $classname;
-
- $this->dbh->connect( $this->conf );
-
- return true;
- }
-
-
- function isError( $value )
- {
- return $this->isError;
- }
-
+ $this->client = & new $classname;
+
+ $ok = $this->client->connect( $this->conf );
+
+ if ( ! $ok )
+ {
+ $this->error = $this->client->error;
+
+ if ( empty($this->error) )
+ $this->error = 'Error while connecting to database.';
+ }
- function nextId( $sequenceName )
- {
- return $this->dbh->nextId( $sequenceName );
+ return $ok;
}
function query( $query )
- {
- Logger::trace('DB query: '.substr($query,0,45));
- $result = $this->dbh->simpleQuery($query);
+ {
+ Logger::trace('DB query: '.substr($query,0,45).'...');
+
+ $result = $this->client->query($query);
+
+ if ( $result === FALSE )
+ {
+ $this->error = $this->client->error;
+
+ if ( true )
+ {
+ die('Database Error:<pre style="color:red">'.$this->error.'</pre>');
+ }
+ }
+
- return new DB_result( $this->dbh,$result );
+ return new DB_result( $this->client,$result );
}
-
+
+ /**
+ * Ermittelt die Anzahl der betroffenen Zeilen nach einer Datebank-Anfrage.
+ *
+ * @return unknown
+ */
function affectedRows()
{
- return $this->dbh->affectedRows( $query );
+ return $this->client->affectedRows( $query );
}
-
+
+ /**
+ * Ermittelt genau 1 Datenbankergebnis aus einer SQL-Anfrage.
+ * Falls es mehrere Treffer gibt, wird die 1. Spalte aus der 1. Zeile genommen.
+ *
+ * @param String $query
+ * @return String
+ */
function &getOne( $query )
{
$res = $this->query($query);
@@ -102,13 +166,19 @@ class DB
}
else
{
- $res->free();
- $leer = array();
+ $res->free();
+ $leer = '';
return $leer;
}
}
-
+
+ /**
+ * Ermittelt eine Zeile aus der Datenbank.
+ *
+ * @param String $query
+ * @return Array
+ */
function &getRow( $query )
{
$res = $this->query($query);
@@ -123,7 +193,13 @@ class DB
return $row;
}
-
+
+ /**
+ * Ermittelt eine (die 1.) Spalte aus dem Datenbankergebnis.
+ *
+ * @param String $query
+ * @return Array
+ */
function &getCol( $query )
{
$res = $this->query( $query );
@@ -145,7 +221,14 @@ class DB
return $ret;
}
-
+
+ /**
+ * Ermittelt ein assoziatives Array aus der Datenbank.
+ *
+ * @param String $query
+ * @param Boolean $force_array
+ * @return Array
+ */
function &getAssoc( $query, $force_array = false )
{
$res = $this->query($query);
@@ -187,7 +270,13 @@ class DB
return $results;
}
-
+
+ /**
+ * Ermittelt alle Datenbankergebniszeilen.
+ *
+ * @param String $query
+ * @return Array
+ */
function &getAll( $query )
{
$res = $this->query( $query );
@@ -211,28 +300,29 @@ class DB
/**
- * Darstellung eines Datenbank-Ergebnisses
- * @author $Author$
+ * Darstellung eines Datenbank-Ergebnisses.
+ *
+ * @author Jan Dankert
* @version $Revision$
* @package openrat.database
*/
class DB_result
{
- var $dbh;
+ var $client;
var $result;
- function DB_result( $dbh, $result )
+ function DB_result( $client, $result )
{
- $this->dbh = $dbh;
+ $this->client = $client;
$this->result = $result;
}
function fetchRow( $rownum = 0 )
{
- $arr = $this->dbh->fetchRow( $this->result, $rownum );
+ $arr = $this->client->fetchRow( $this->result, $rownum );
return $arr;
}
@@ -240,19 +330,19 @@ class DB_result
function numCols()
{
- return $this->dbh->numCols($this->result);
+ return $this->client->numCols($this->result);
}
function numRows()
{
- return $this->dbh->numRows( $this->result );
+ return $this->client->numRows( $this->result );
}
function free()
{
- $err = $this->dbh->freeResult($this->result);
+ $err = $this->client->freeResult($this->result);
return true;
}
}
diff --git a/db/mysql.class.php b/db/mysql.class.php
@@ -26,8 +26,20 @@
* @package openrat.database
*/
class DB_mysql
-{
- var $connection;
+{
+ /**
+ * Die MySql-Verbindung.
+ *
+ * @var Resource
+ */
+ var $connection;
+
+ /**
+ * Datenbank-Fehler.
+ *
+ * @var String
+ */
+ var $error;
function connect( $conf )
@@ -35,25 +47,38 @@ class DB_mysql
$host = $conf['host'];
$user = $conf['user'];
$pw = $conf['password'];
- $db = $conf['database'];
+ $db = $conf['database'];
+
+ if ( isset($conf['port']) )
+ $host .= ':'.$conf['port'];
if ( $conf['persistent'] )
$connect_function = 'mysql_pconnect';
- else $connect_function = 'mysql_connect';
+ else
+ $connect_function = 'mysql_connect';
if ( $pw != '' )
- $this->connection = $connect_function( $host,$user,$pw );
+ $this->connection = @$connect_function( $host,$user,$pw );
elseif ( $user != '' )
- $this->connection = $connect_function( $host,$user );
+ $this->connection = @$connect_function( $host,$user );
elseif ( $host != '' )
- $this->connection = $connect_function( $host );
+ $this->connection = @$connect_function( $host );
else
- $this->connection = $connect_function();
-
- if ( $db != '' )
- {
- if ( !mysql_select_db( $db,$this->connection ) )
- Http::serverError("Database error while connecting to $host, database '$db' is not available." );
+ $this->connection = @$connect_function();
+
+ if ( !is_resource($this->connection) )
+ {
+ $this->error = "Could not connect to database on host $host.";
+ return false;
+ }
+
+ if ( $db != '' )
+ {
+ if ( !@mysql_select_db( $db,$this->connection ) )
+ {
+ $this->error = "Could not select database '$db' on host $host.";
+ return false;
+ }
}
return true;
@@ -70,14 +95,17 @@ class DB_mysql
- function simpleQuery($query)
+ function query($query)
{
$result = mysql_query($query, $this->connection);
- if ( ! $result )
- Http::serverError( 'Database Error<pre>'.$query."\n".'<span style="color:red;">'.mysql_error().'</span></pre>' );
+ if ( ! $result )
+ {
+ $this->error = 'Database error: '.mysql_error();
+ return FALSE;
+ }
- return $result;;
+ return $result;
}
diff --git a/db/postgresql.class.php b/db/postgresql.class.php
@@ -29,32 +29,53 @@ class DB_postgresql
{
var $connection;
-
+
+ /**
+ * Verbinden zum POSTGRES-Server.
+ *
+ * @param Array $conf
+ * @return boolean
+ */
function connect( $conf )
{
$host = $conf['host'];
$user = $conf['user'];
$pw = $conf['password'];
$db = $conf['database'];
-
+
+ if ( isset($conf['port']) )
+ $host .= ':'.$conf['port'];
+
if ( $conf['persistent'] )
$connect_function = 'pg_pconnect';
- else $connect_function = 'pg_connect';
+ else
+ $connect_function = 'pg_connect';
if ( $pw != '' )
- $this->connection = $connect_function( "host=$host dbname=$db user=$user password=$pw" );
+ $this->connection = @$connect_function( "host=$host dbname=$db user=$user password=$pw" );
elseif ( $user != '' )
- $this->connection = $connect_function( "host=$host dbname=$db user=$user" );
+ $this->connection = @$connect_function( "host=$host dbname=$db user=$user" );
elseif ( $host != '' )
- $this->connection = $connect_function( "host=$host dbname=$db" );
+ $this->connection = @$connect_function( "host=$host dbname=$db" );
else
- $this->connection = $connect_function( "dbname=$db");
+ $this->connection = @$connect_function( "dbname=$db");
+
+ if ( ! is_resource($this->connection) )
+ {
+ $this->error = 'could not connect to database on host '.$host;
+ return false;
+ }
return true;
}
-
+
+ /**
+ * Verbindung schließen.
+ *
+ * @return unknown
+ */
function disconnect()
{
$ret = pg_close( $this->connection );
@@ -64,12 +85,16 @@ class DB_postgresql
- function simpleQuery($query)
+ function query($query)
{
$result = @pg_exec( $this->connection,$query );
- if ( ! $result )
- Http::serverError('Database Error'.'<pre>'.$query."\n".'<span style="color:red;">'.pg_errormessage().'</span></pre>' );
+ if ( ! $result )
+ {
+ if ( empty($this->error) )
+ $this->error = 'PostgreSQL says: '.@pg_errormessage();
+ return FALSE;
+ }
return $result;;
}