commit 7431a6d120c2dcd0ae077acc0e554dc82cc45170
parent ea980c1fdd0d401ae5819f677775ccd13e0957ad
Author: dankert <devnull@localhost>
Date: Thu, 14 Jan 2010 23:27:43 +0100
Überarbeitung der Datenbank-Funktionen: Klasse 'DB_Result' entfernt, kein numRows() und numCols() mehr, da von PDO nicht unterstützt.
Diffstat:
8 files changed, 100 insertions(+), 236 deletions(-)
diff --git a/db/db.class.php b/db/db.class.php
@@ -99,7 +99,7 @@ class DB
*/
function connect()
{
- // Ausf�hren des Systemkommandos.
+ // Ausfuehren des Systemkommandos.
if ( !empty($this->conf['cmd']))
{
$ausgabe = array();
@@ -170,7 +170,10 @@ class DB
*/
function query( $query )
{
- if ( is_object($query) && isset($this->conf['prepare']) && $this->conf['prepare'] )
+ if ( !is_object($query) )
+ die('SQL-Query must be an object');
+
+ if ( isset($this->conf['prepare']) && $this->conf['prepare'] )
{
$this->client->clear();
$this->client->prepare( $query->raw,$query->param );
@@ -208,12 +211,9 @@ class DB
}
}
- return new DB_result( $this->client,$result );
+ return $result;
}
- if ( !is_object($query) )
- die('SQL-Query must be an object');
-
$flatQuery = $query->getQuery();
Logger::trace('DB query: '.$query->raw);
@@ -236,18 +236,7 @@ class DB
if ( method_exists($this->client,'commit') )
$this->client->commit();
- return new DB_result( $this->client,$result );
- }
-
-
- /**
- * Ermittelt die Anzahl der betroffenen Zeilen nach einer Datebank-Anfrage.
- *
- * @return unknown
- */
- function affectedRows()
- {
- return $this->client->affectedRows( $query );
+ return $result;
}
@@ -260,23 +249,18 @@ class DB
*/
function &getOne( $query )
{
- $res = $this->query($query);
+ $none = '';
+ $result = $this->query($query);
+
+ $row = $this->client->fetchRow( $result,0 );
+ $this->client->freeResult($result);
- if ( $res->numRows() > 0 )
- {
- $row = $res->fetchRow( 0 );
- $res->free();
-
- $keys = array_keys($row);
-
- return $row[ $keys[0] ];
- }
- else
- {
- $res->free();
- $leer = '';
- return $leer;
- }
+ if ( ! is_array($row) )
+ return $none;
+
+ $keys = array_keys($row);
+
+ return $row[ $keys[0] ];
}
@@ -288,14 +272,25 @@ class DB
*/
function &getRow( $query )
{
- $res = $this->query($query);
+ $result = $this->query($query);
+
+ if ( $result === FALSE )
+ {
+ $this->error = $this->client->error;
+
+ if ( true )
+ {
+ debug_print_backtrace();
+ Logger::warn('Database error: '.$this->error);
+ die('Database Error (not prepared):<pre style="color:red">'.$this->error.'</pre>');
+ }
+ }
- if ( $res->numRows() > 0 )
- $row = $res->fetchRow( 0 );
- else
- $row = array();
+ $row = $this->client->fetchRow( $result,0 );
+ $this->client->freeResult($result);
- $res->free();
+ if ( ! is_array($row) )
+ $row = array();
return $row;
}
@@ -309,23 +304,22 @@ class DB
*/
function &getCol( $query )
{
- $res = $this->query( $query );
-
- $ret = array();
+ $result = $this->query($query);
- $numRows = $res->numRows();
-
- for( $i=0; $i<$numRows; $i++ )
+ $i = 0;
+ $col = array();
+ while( $row = $this->client->fetchRow( $result,$i++ ) )
{
- $row = $res->fetchRow($i);
-
+ if ( empty($row) )
+ break;
+
$keys = array_keys($row);
- $ret[] = $row[ $keys[0] ];
+ $col[] = $row[ $keys[0] ];
}
-
- $res->free();
-
- return $ret;
+
+ $this->client->freeResult($result);
+
+ return $col;
}
@@ -338,16 +332,17 @@ class DB
*/
function &getAssoc( $query, $force_array = false )
{
- $res = $this->query($query);
-
- $numCols = $res->numCols();
- $numRows = $res->numRows();
-
$results = array();
+ $result = $this->query($query);
- if ( $numCols > 2 || $force_array )
+ $i = 0;
+
+ while( $row = $this->client->fetchRow( $result,$i++ ) )
{
- for( $i=0; $i<$numRows; $i++ )
+ if ( empty($row) )
+ break;
+
+ if ( count($row) > 2 || $force_array )
{
$row = $res->fetchRow($i);
@@ -357,13 +352,8 @@ class DB
unset( $row[$key1] );
$results[ $row[$key1] ] = $row;
}
- }
- else
- {
- for( $i=0; $i<$numRows; $i++ )
+ else
{
- $row = $res->fetchRow($i);
-
$keys = array_keys($row);
$key1 = $keys[0];
$key2 = $keys[1];
@@ -372,7 +362,7 @@ class DB
}
}
- $res->free();
+ $this->client->freeResult( $result );
return $results;
}
@@ -386,19 +376,18 @@ class DB
*/
function &getAll( $query )
{
- $res = $this->query( $query );
+ $result = $this->query( $query );
$results = array();
- $numRows = $res->numRows();
+ $i = 0;
- for( $i=0; $i<$numRows; $i++ )
+ while( $row = $this->client->fetchRow( $result,$i++ ) )
{
- $row = $res->fetchRow($i);
$results[] = $row;
}
- $res->free();
-
+ $this->client->freeResult( $result );
+
return $results;
}
@@ -448,55 +437,4 @@ class DB
}
-
-
-/**
- * Darstellung eines Datenbank-Ergebnisses.
- *
- * @author Jan Dankert
- * @version $Revision: 1.9 $
- * @package openrat.database
- */
-
-class DB_result
-{
- var $client;
- var $result;
-
-
- function DB_result( $client, $result )
- {
- $this->client = $client;
- $this->result = $result;
- }
-
-
- function fetchRow( $rownum = 0 )
- {
- $arr = $this->client->fetchRow( $this->result, $rownum );
-
- return $arr;
- }
-
-
- function numCols()
- {
- return $this->client->numCols($this->result);
- }
-
-
- function numRows()
- {
- return $this->client->numRows( $this->result );
- }
-
-
- function free()
- {
- $err = $this->client->freeResult($this->result);
- return true;
- }
-}
-
-
?>
\ No newline at end of file
diff --git a/db/mysql.class.php b/db/mysql.class.php
@@ -123,19 +123,6 @@ class DB_mysql
}
- function numCols($result)
- {
- return mysql_num_fields( $result );
- }
-
-
-
- function numRows( $result )
- {
- return mysql_num_rows($result);
- }
-
-
/**
* Startet eine Transaktion.
*/
diff --git a/db/mysqli.class.php b/db/mysqli.class.php
@@ -158,19 +158,7 @@ class DB_mysqli
}
- function numCols($result)
- {
- return mysqli_num_fields( $result );
- }
-
-
- function numRows( $result )
- {
- return mysqli_num_rows($result);
- }
-
-
function prepare( $query,$param)
{
foreach( $param as $pos)
diff --git a/db/pdo.class.php b/db/pdo.class.php
@@ -20,8 +20,9 @@
//
/**
- * Datenbank-abhaengige Methoden fuer PDO
- * @author $Author: dankert $
+ * Datenbank-abhaengige Methoden fuer PDO.
+ *
+ * @author Jan Dankert
* @version $Revision: 1.5 $
* @package openrat.database
*/
@@ -52,11 +53,6 @@ class DB_pdo
if ( isset($conf['port']) )
$host .= ':'.$conf['port'];
- if ( $conf['persistent'] )
- $connect_function = 'mysql_pconnect';
- else
- $connect_function = 'mysql_connect';
-
$options = array();
foreach( $conf as $c )
if ( substr($c,0,7) == 'option_' )
@@ -109,20 +105,6 @@ class DB_pdo
}
- function numCols($result)
- {
- die('called NumCols() in PDO');
- }
-
-
-
- function numRows( $result )
- {
- die('called NumRows in PDO()');
- }
-
-
-
function prepare( $query,$param)
{
foreach( $param as $pos)
diff --git a/db/postgresql.class.php b/db/postgresql.class.php
@@ -143,7 +143,6 @@ class DB_postgresql
die('unknown type "'.$data['type'].'"');
}
}
- //Html::debug($this->params,'Parameter');
$result = @pg_execute( $this->connection,$this->stmtid,$ar );
@@ -151,7 +150,6 @@ class DB_postgresql
{
if ( empty($this->error) )
$this->error = 'PostgreSQL (prepared) says: '.@pg_errormessage();
- debug_print_backtrace();
return FALSE;
}
@@ -175,6 +173,9 @@ class DB_postgresql
function fetchRow( $result, $rownum )
{
+ if ( $rownum >= pg_num_rows($result) )
+ return false;
+
return pg_fetch_array( $result,$rownum,PGSQL_ASSOC );
}
@@ -185,19 +186,6 @@ class DB_postgresql
}
- function numCols($result )
- {
- return pg_numfields( $result );
- }
-
-
-
- function numRows( $result )
- {
- return pg_numrows($result);
- }
-
-
function prepare( $query,$param )
{
$nr = 1;
diff --git a/db/sqlite.class.php b/db/sqlite.class.php
@@ -124,19 +124,6 @@ class DB_sqlite
{
return true;
}
-
-
- function numCols($result)
- {
- return sqlite_num_fields( $result );
- }
-
-
-
- function numRows( $result )
- {
- return sqlite_num_rows($result);
- }
}
?>
\ No newline at end of file
diff --git a/db/sqlite3.class.php b/db/sqlite3.class.php
@@ -94,21 +94,8 @@ class DB_sqlite3
}
- function numCols($result)
- {
- return $this->result->numColumns();
- }
-
-
-
- function numRows( $result )
- {
- return $this->result->numRows();
- }
-
-
- /**
+ /**
* Startet eine Transaktion.
*/
function start()
diff --git a/functions/db.inc.php b/functions/db.inc.php
@@ -33,34 +33,45 @@ function table_names( $dbid )
{
$db = Session::getDatabase();
if ( is_object( $db ) )
+ {
$conf_db_prefix = $db->conf['prefix'];
+ if ( isset($db->conf['suffix']))
+ $conf_db_suffix = $db->conf['suffix'];
+ else
+ $conf_db_suffix = '';
+ }
else
+ {
$conf_db_prefix = '';
+ $conf_db_suffix = '';
+ }
}
else
{
- $conf_db_prefix = $conf['database'][$dbid]['prefix'];
+ $conf_db_prefix = $config('database',$dbid,'prefix');
+ $conf_db_suffix = $config('database',$dbid,'suffix');
}
- $t['t_include'] = $conf_db_prefix.'include';
- $t['t_element'] = $conf_db_prefix.'element';
- $t['t_template'] = $conf_db_prefix.'template';
- $t['t_templatemodel'] = $conf_db_prefix.'templatemodel';
- $t['t_projectmodel'] = $conf_db_prefix.'projectmodel';
- $t['t_model'] = $conf_db_prefix.'projectmodel';
- $t['t_page'] = $conf_db_prefix.'page';
- $t['t_language'] = $conf_db_prefix.'language';
- $t['t_value'] = $conf_db_prefix.'value';
- $t['t_user'] = $conf_db_prefix.'user';
- $t['t_usergroup'] = $conf_db_prefix.'usergroup';
- $t['t_project'] = $conf_db_prefix.'project';
- $t['t_group'] = $conf_db_prefix.'group';
- $t['t_folder'] = $conf_db_prefix.'folder';
- $t['t_file'] = $conf_db_prefix.'file';
- $t['t_acl'] = $conf_db_prefix.'acl';
- $t['t_object'] = $conf_db_prefix.'object';
- $t['t_name'] = $conf_db_prefix.'name';
- $t['t_link'] = $conf_db_prefix.'link';
+ foreach( array(
+ 'element',
+ 'template',
+ 'templatemodel',
+ 'projectmodel',
+ 'page',
+ 'language',
+ 'value',
+ 'user',
+ 'usergroup',
+ 'project',
+ 'group',
+ 'folder',
+ 'file',
+ 'acl',
+ 'object',
+ 'name',
+ 'link'
+ ) as $tname )
+ $t['t_'.$tname] = $conf_db_prefix.$tname.$conf_db_suffix;
return $t;
}
@@ -73,9 +84,5 @@ function db_connection()
return Session::getDatabase();
}
-//extract( table_names() );
-
-
-
?>
\ No newline at end of file