openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 18f87bd13d579cc0bab8806b5cc03fda9c081ca1
parent 9a3ebf0e8c73b33804bd80d48779a783c8c58306
Author: dankert <devnull@localhost>
Date:   Thu, 16 Dec 2004 00:21:36 +0100

diverse Aenderungen

Diffstat:
db/db.class.php | 119++++++++++++++++++++++++++++++++++++-------------------------------------------
db/mysql.class.php | 286+++++++++++++++++++++++++++++++-------------------------------------------------
db/postgresql.class.php | 245+++++++++++++++++++++++++++++++++----------------------------------------------
3 files changed, 266 insertions(+), 384 deletions(-)

diff --git a/db/db.class.php b/db/db.class.php @@ -18,21 +18,9 @@ // +----------------------------------------------------------------------+ // - // This is the database abstraction layer. This class was inspired by the -// great PHP-Pear-DB package. Thanks to its developers. - - -// Column data indexed by numbers, ordered from 0 and up -define('DB_FETCHMODE_ORDERED', 1); - -// Column data indexed by column names -define('DB_FETCHMODE_ASSOC' , 2); - -// Column data as object properties -define('DB_FETCHMODE_OBJECT' , 3); +// PHP-Pear-DB package. Thanks to its developers. -define('DB_FETCHMODE_DEFAULT', DB_FETCHMODE_ASSOC); @@ -44,10 +32,7 @@ define('DB_FETCHMODE_DEFAULT', DB_FETCHMODE_ASSOC); */ class DB { - var $isError = false; - var $error = ''; var $dbh; - var $fetchmode = DB_FETCHMODE_ORDERED; var $conf; var $id; @@ -58,12 +43,6 @@ class DB } - function setFetchMode( $fetchmode ) - { - $this->fetchmode = $fetchmode; - } - - function connect( $conf = array() ) { if ( count($conf)>0 ) @@ -77,9 +56,6 @@ class DB $this->dbh->connect( $this->conf ); - if ( $this->dbh->isError ) - return false; - return true; } @@ -115,10 +91,20 @@ class DB { $res = $this->query($query); - $err = $res->fetchInto( &$row, DB_FETCHMODE_ORDERED ); - $res->free(); - - return $row[0]; + if ( $res->numRows() > 0 ) + { + $row = $res->fetchRow( 0 ); + $res->free(); + + $keys = array_keys($row); + + return $row[ $keys[0] ]; + } + else + { + $res->free(); + return array(); + } } @@ -126,7 +112,10 @@ class DB { $res = $this->query($query); - $err = $res->fetchInto($row); + if ( $res->numRows() > 0 ) + $row = $res->fetchRow( 0 ); + else + $row = array(); $res->free(); @@ -134,19 +123,20 @@ class DB } - function &getCol( $query, $col=0 ) + function &getCol( $query ) { $res = $this->query( $query ); - if ( is_integer( $col ) ) - $fetchmode = DB_FETCHMODE_ORDERED; - else $fetchmode = DB_FETCHMODE_ASSOC; - $ret = array(); - while( is_array($row = $res->fetchRow($fetchmode)) ) + $numRows = $res->numRows(); + + for( $i=0; $i<$numRows; $i++ ) { - $ret[] = $row[$col]; + $row = $res->fetchRow($i); + + $keys = array_keys($row); + $ret[] = $row[ $keys[0] ]; } $res->free(); @@ -159,23 +149,35 @@ class DB { $res = $this->query($query); - $cols = $res->numCols(); + $numCols = $res->numCols(); + $numRows = $res->numRows(); $results = array(); - if ( $cols > 2 || $force_array ) + if ( $numCols > 2 || $force_array ) { - while( is_array($row = $res->fetchRow(DB_FETCHMODE_ORDERED)) ) + for( $i=0; $i<$numRows; $i++ ) { - reset($row); - $results[ $row[0] ] = array_slice($row, 1); + $row = $res->fetchRow($i); + + $keys = array_keys($row); + $key1 = $keys[0]; + + unset( $row[$key1] ); + $results[ $row[$key1] ] = $row; } } else { - while (is_array($row = $res->fetchRow(DB_FETCHMODE_ORDERED))) + for( $i=0; $i<$numRows; $i++ ) { - $results[$row[0]] = $row[1]; + $row = $res->fetchRow($i); + + $keys = array_keys($row); + $key1 = $keys[0]; + $key2 = $keys[1]; + + $results[ $row[$key1] ] = $row[$key2]; } } @@ -184,17 +186,17 @@ class DB return $results; } - function &getAll($query, $fetchmode = DB_FETCHMODE_DEFAULT ) - { - - $fetchmode = (empty($fetchmode)) ? DB_FETCHMODE_DEFAULT : $fetchmode; + function &getAll( $query ) + { $res = $this->query( $query ); $results = array(); + $numRows = $res->numRows(); - while( $row = $res->fetchRow( $fetchmode) ) + for( $i=0; $i<$numRows; $i++ ) { + $row = $res->fetchRow($i); $results[] = $row; } @@ -227,22 +229,14 @@ class DB_result } - function fetchRow( $fetchmode = null, $rownum = null ) + function fetchRow( $rownum = 0 ) { - $arr = $this->dbh->fetchRow( $this->result, $fetchmode, $rownum ); + $arr = $this->dbh->fetchRow( $this->result, $rownum ); return $arr; } - function fetchInto( &$arr, $fetchmode = null, $rownum=null ) - { - $arr = $this->fetchRow( $fetchmode, $rownum ); - - return true; - } - - function numCols() { return $this->dbh->numCols($this->result); @@ -251,12 +245,7 @@ class DB_result function numRows() { - $rows = $this->dbh->numRows( $this->result ); - - if ( $this->dbh->isError ) - echo "Fehler"; - - return $rows; + return $this->dbh->numRows( $this->result ); } diff --git a/db/mysql.class.php b/db/mysql.class.php @@ -1,177 +1,111 @@ -<?php - -// -// +----------------------------------------------------------------------+ -// | PHP version 4.0 | -// +----------------------------------------------------------------------+ -// | Copyright (c) 1997-2001 The PHP Group | -// +----------------------------------------------------------------------+ -// | This source file is subject to version 2.02 of the PHP license, | -// | that is bundled with this package in the file LICENSE, and is | -// | available at through the world-wide-web at | -// | http://www.php.net/license/2_02.txt. | -// | If you did not receive a copy of the PHP license and are unable to | -// | obtain it through the world-wide-web, please send a note to | -// | license@php.net so we can mail you a copy immediately. | -// +----------------------------------------------------------------------+ -// | Authors: Stig Bakken <ssb@fast.no> | -// | Jan Dankert <phpdb@jandankert.de> | -// +----------------------------------------------------------------------+ -// - -/** - * Datenbank-abhaengige Methoden fuer MySQL - * @author $Author$ - * @version $Revision$ - * @package openrat.database - */ -class DB_mysql -{ - var $connection; - var $autocommit = true; - var $fetchmode = DB_FETCHMODE_ORDERED; /* Default fetch mode */ - var $isError = false; - - function connect( $conf ) - { - $host = $conf['host']; - $user = $conf['user']; - $pw = $conf['password']; - $db = $conf['database']; - - if ( $conf['persistent'] ) - $connect_function = 'mysql_pconnect'; - else $connect_function = 'mysql_connect'; - - if ( $pw != '' ) - $this->connection = $connect_function( $host,$user,$pw ); - elseif ( $user != '' ) - $this->connection = $connect_function( $host,$user ); - elseif ( $host != '' ) - $this->connection = $connect_function( $host ); - else - $this->connection = $connect_function(); - - if ( $db != '' ) - { - if ( !mysql_select_db( $db,$this->connection ) ) - die( "cannot select database '$db'. Check rights." ); - } - - return true; - } - - - -// function nextId( $sequenceName ) -// { -// $res = mysql_query("SELECT id FROM `$sequenceName`",$this->connection ); -// if ( mysql_errno($this->connection) != 0 ) -// die( mysql_error($this->connection) ); -// -// $nextId = intval( mysql_result($res,0,0) ) + 1; -// -// mysql_query("UPDATE `$sequenceName` SET id=".$nextId,$this->connection ); -// if ( mysql_errno($this->connection) != 0 ) -// die( mysql_error($this->connection) ); -// -// return $nextId; -// } -// -// -// - function disconnect() - { - $ret = mysql_close( $this->connection ); - $this->connection = null; - return $ret; - } - - - - function simpleQuery($query) - { - $result = mysql_query($query, $this->connection); - - if ( ! $result ) - { - die( '<pre>'.$query."\n".'<span style="color:red;">'.mysql_error().'</span></pre>' ); - } - - return $result;; - } - - - function affectedRows() - { - return mysql_affected_rows(); - } - - - function fetchRow( $result, $fetchmode, $rownum ) - { - if ( $rownum !== null ) - { - if ( ! @mysql_data_seek($result, $rownum) ) - { - return null; - } - } - - if ( $fetchmode == DB_FETCHMODE_ORDERED ) - { - $arr = @mysql_fetch_row( $result ); - } - else - { - $arr = @mysql_fetch_array( $result,MYSQL_ASSOC ); - } - - if ( ! $arr ) - { - $this->isError = true; - } - - return $arr; - } - - - function freeResult($result) - { - if (is_resource($result)) - { - return mysql_free_result($result); - } - return true; - } - - - function numCols($result) - { - $cols = mysql_num_fields( $result ); - - if ( ! $cols ) - { - return $this->mysqlRaiseError(); - } - - return $cols; - } - - - - function numRows( $result ) - { - //echo "yo"; - //print_r($result); - $rows = mysql_num_rows($result); - - if ($rows === null ) - { - $this->isError = true; - } - return $rows; - } -} - +<?php + +// +// +----------------------------------------------------------------------+ +// | PHP version 4.0 | +// +----------------------------------------------------------------------+ +// | Copyright (c) 1997-2001 The PHP Group | +// +----------------------------------------------------------------------+ +// | This source file is subject to version 2.02 of the PHP license, | +// | that is bundled with this package in the file LICENSE, and is | +// | available at through the world-wide-web at | +// | http://www.php.net/license/2_02.txt. | +// | If you did not receive a copy of the PHP license and are unable to | +// | obtain it through the world-wide-web, please send a note to | +// | license@php.net so we can mail you a copy immediately. | +// +----------------------------------------------------------------------+ +// | Authors: Stig Bakken <ssb@fast.no> | +// | Jan Dankert <phpdb@jandankert.de> | +// +----------------------------------------------------------------------+ +// + +/** + * Datenbank-abhaengige Methoden fuer MySQL + * @author $Author$ + * @version $Revision$ + * @package openrat.database + */ +class DB_mysql +{ + var $connection; + + + function connect( $conf ) + { + $host = $conf['host']; + $user = $conf['user']; + $pw = $conf['password']; + $db = $conf['database']; + + if ( $conf['persistent'] ) + $connect_function = 'mysql_pconnect'; + else $connect_function = 'mysql_connect'; + + if ( $pw != '' ) + $this->connection = $connect_function( $host,$user,$pw ); + elseif ( $user != '' ) + $this->connection = $connect_function( $host,$user ); + elseif ( $host != '' ) + $this->connection = $connect_function( $host ); + else + $this->connection = $connect_function(); + + if ( $db != '' ) + { + if ( !mysql_select_db( $db,$this->connection ) ) + die( "cannot select database '$db'. Check rights." ); + } + + return true; + } + + + + function disconnect() + { + $ret = mysql_close( $this->connection ); + $this->connection = null; + return $ret; + } + + + + function simpleQuery($query) + { + $result = mysql_query($query, $this->connection); + + if ( ! $result ) + die( '<pre>'.$query."\n".'<span style="color:red;">'.mysql_error().'</span></pre>' ); + + return $result;; + } + + + function fetchRow( $result, $rownum ) + { + return mysql_fetch_array( $result,MYSQL_ASSOC ); + } + + + function freeResult($result) + { + if ( is_resource($result) ) + return mysql_free_result($result); + return true; + } + + + function numCols($result) + { + return mysql_num_fields( $result ); + } + + + + function numRows( $result ) + { + return mysql_num_rows($result); + } +} + ?> \ No newline at end of file diff --git a/db/postgresql.class.php b/db/postgresql.class.php @@ -1,144 +1,103 @@ -<?php - -// -// +----------------------------------------------------------------------+ -// | PHP version 4.0 | -// +----------------------------------------------------------------------+ -// | Copyright (c) 1997-2001 The PHP Group | -// +----------------------------------------------------------------------+ -// | This source file is subject to version 2.02 of the PHP license, | -// | that is bundled with this package in the file LICENSE, and is | -// | available at through the world-wide-web at | -// | http://www.php.net/license/2_02.txt. | -// | If you did not receive a copy of the PHP license and are unable to | -// | obtain it through the world-wide-web, please send a note to | -// | license@php.net so we can mail you a copy immediately. | -// +----------------------------------------------------------------------+ -// | Authors: Stig Bakken <ssb@fast.no> | -// | Jan Dankert <phpdb@jandankert.de> | -// +----------------------------------------------------------------------+ -// - -/** - * Datenbank-abhaengige Methoden fuer PostgreSQL - * @author $Author$ - * @version $Revision$ - * @package openrat.database - */ -class DB_postgresql -{ - var $connection; - var $autocommit = true; - var $fetchmode = DB_FETCHMODE_ORDERED; /* Default fetch mode */ - var $isError = false; - - function connect( $conf ) - { - $host = $conf['host']; - $user = $conf['user']; - $pw = $conf['password']; - $db = $conf['database']; - - if ( $conf['persistent'] ) - $connect_function = 'pg_pconnect'; - else $connect_function = 'pg_connect'; - - if ( $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" ); - elseif ( $host != '' ) - $this->connection = $connect_function( "host=$host dbname=$db" ); - else - $this->connection = $connect_function( "dbname=$db"); - - return true; - } - - - - function disconnect() - { - $ret = pg_close( $this->connection ); - $this->connection = null; - return $ret; - } - - - - function simpleQuery($query) - { - $result = @pg_exec( $this->connection,$query ); - - if ( ! $result ) - { - die( '<pre>'.$query."\n".'<span style="color:red;">'.pg_errormessage().'</span></pre>' ); - } - - return $result;; - } - - - function affectedRows( $result=null ) - { - return pg_affected_rows( $result ); - } - - - function fetchRow( $result, $fetchmode, $rownum ) - { - if ( $rownum !== null ) - { - $arr = pg_fetch_array( $result,$rownum,PGSQL_ASSOC ); - } - else - { - $arr = pg_fetch_array( $result ); - } - - if ( ! $arr ) - { - $this->isError = true; - } - - return $arr; - } - - - function freeResult($result) - { - if (is_resource($result)) - { - return pg_freeresult($result); - } - return true; - } - - - function numCols($result) - { - $cols = pg_numfields( $result ); - - if ( ! $cols ) - { - return $this->postgresRaiseError(); - } - - return $cols; - } - - - - function numRows( $result ) - { - $rows = pg_numrows($result); - - if ($rows === null ) - { - $this->isError = true; - } - return $rows; - } -} - +<?php + +// +// +----------------------------------------------------------------------+ +// | PHP version 4.0 | +// +----------------------------------------------------------------------+ +// | Copyright (c) 1997-2001 The PHP Group | +// +----------------------------------------------------------------------+ +// | This source file is subject to version 2.02 of the PHP license, | +// | that is bundled with this package in the file LICENSE, and is | +// | available at through the world-wide-web at | +// | http://www.php.net/license/2_02.txt. | +// | If you did not receive a copy of the PHP license and are unable to | +// | obtain it through the world-wide-web, please send a note to | +// | license@php.net so we can mail you a copy immediately. | +// +----------------------------------------------------------------------+ +// | Authors: Stig Bakken <ssb@fast.no> | +// | Jan Dankert <phpdb@jandankert.de> | +// +----------------------------------------------------------------------+ +// + +/** + * Datenbank-abhaengige Methoden fuer PostgreSQL + * @author $Author$ + * @version $Revision$ + * @package openrat.database + */ +class DB_postgresql +{ + var $connection; + + + function connect( $conf ) + { + $host = $conf['host']; + $user = $conf['user']; + $pw = $conf['password']; + $db = $conf['database']; + + if ( $conf['persistent'] ) + $connect_function = 'pg_pconnect'; + else $connect_function = 'pg_connect'; + + if ( $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" ); + elseif ( $host != '' ) + $this->connection = $connect_function( "host=$host dbname=$db" ); + else + $this->connection = $connect_function( "dbname=$db"); + + return true; + } + + + + function disconnect() + { + $ret = pg_close( $this->connection ); + $this->connection = null; + return $ret; + } + + + + function simpleQuery($query) + { + $result = @pg_exec( $this->connection,$query ); + + if ( ! $result ) + die( '<pre>'.$query."\n".'<span style="color:red;">'.pg_errormessage().'</span></pre>' ); + + return $result;; + } + + + function fetchRow( $result, $rownum ) + { + return pg_fetch_array( $result,$rownum,PGSQL_ASSOC ); + } + + + function freeResult($result) + { + return pg_freeresult($result); + } + + + function numCols($result ) + { + return pg_numfields( $result ); + } + + + + function numRows( $result ) + { + return pg_numrows($result); + } +} + ?> \ No newline at end of file