commit ef25921b110891e8ef11536cb55be11f30005848
parent 9ce5d642d9f808e906ce31765dea02f90ba13dab
Author: dankert - Hamburg-Finkenwerder <devnull@localhost>
Date: Tue, 15 Sep 2009 00:00:20 +0200
Unterstützung für Datenbanken SQ-Lite, MySql-I und PDO. Vorbereitung für echte Prepared-Statements auf der Datenbank.
Diffstat:
8 files changed, 528 insertions(+), 11 deletions(-)
diff --git a/db/db.class.php b/db/db.class.php
@@ -26,8 +26,8 @@
/**
* Grundsaetzliche Darstellung einer Datenbank-Verbindung
- * @author $Author$
- * @version $Revision$
+ * @author $Author: dankert $
+ * @version $Revision: 1.9 $
* @package openrat.database
*/
class DB
@@ -322,6 +322,15 @@ class DB
return $results;
}
+
+
+ function prepare( $query, $param )
+ {
+ if ( method_exists( $this->client,'prepare' ) )
+ {
+ $this->client->prepare( $query, $param );
+ }
+ }
}
@@ -331,7 +340,7 @@ class DB
* Darstellung eines Datenbank-Ergebnisses.
*
* @author Jan Dankert
- * @version $Revision$
+ * @version $Revision: 1.9 $
* @package openrat.database
*/
diff --git a/db/mysql.class.php b/db/mysql.class.php
@@ -21,8 +21,8 @@
/**
* Datenbank-abhaengige Methoden fuer MySQL
- * @author $Author$
- * @version $Revision$
+ * @author $Author: dankert $
+ * @version $Revision: 1.5 $
* @package openrat.database
*/
class DB_mysql
@@ -58,13 +58,13 @@ class DB_mysql
$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();
+ $this->connection = $connect_function();
if ( !is_resource($this->connection) )
{
diff --git a/db/mysqli.class.php b/db/mysqli.class.php
@@ -0,0 +1,150 @@
+<?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: dankert $
+ * @version $Revision: 1.5 $
+ * @package openrat.database
+ */
+class DB_mysqli
+{
+ /**
+ * Die MySql-Verbindung.
+ *
+ * @var Resource
+ */
+ var $connection;
+
+ /**
+ * Datenbank-Fehler.
+ *
+ * @var String
+ */
+ var $error;
+
+
+ function connect( $conf )
+ {
+ $host = $conf['host'];
+ $user = $conf['user'];
+ $pw = $conf['password'];
+ $db = $conf['database'];
+ $host = '127.0.0.1';
+
+ if ( isset($conf['port']) )
+ $host .= ':'.$conf['port'];
+
+ // 5.3.0 - Added the ability of persistent connections.
+ if ( $conf['persistent'] && version_compare(PHP_VERSION, '5.3.0', '>') )
+ $host = 'p:'.$host; // Prepending host by p: opens a persistent connection.
+
+ $connect_function = 'mysqli_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();
+
+ #print_r($this->connection);
+
+ if ( false && !is_object($this->connection) )
+ {
+ $this->error = "Could not connect to mysqli-database on host $host.";
+ return false;
+ }
+
+ if ( $db != '' )
+ {
+ if ( !mysqli_select_db( $this->connection,$db ) )
+ {
+ $this->error = "Could not select database '$db' on host $host.";
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+
+
+ function disconnect()
+ {
+ $ret = mysqli_close( $this->connection );
+ $this->connection = null;
+ return $ret;
+ }
+
+
+
+ function query($query)
+ {
+ $result = mysqli_query($this->connection,$query);
+
+ if ( ! $result )
+ {
+ $this->error = 'Database error: '.mysql_error();
+ return FALSE;
+ }
+
+ return $result;
+ }
+
+
+ function fetchRow( $result, $rownum )
+ {
+ return mysqli_fetch_array( $result,MYSQL_ASSOC );
+ }
+
+
+ function freeResult($result)
+ {
+ if ( is_resource($result) )
+ return mysqli_free_result($result);
+ return true;
+ }
+
+
+ function numCols($result)
+ {
+ return mysqli_num_fields( $result );
+ }
+
+
+
+ function numRows( $result )
+ {
+ return mysqli_num_rows($result);
+ }
+
+
+ function prepare( $query,$param)
+ {
+ $stmt = mysqli_prepare($this->connection,$query);
+ // TODO: $stmt als Member merken und bei query() mit bind-vars f�llen.
+ }
+}
+
+?>+
\ No newline at end of file
diff --git a/db/pdo.class.php b/db/pdo.class.php
@@ -0,0 +1,125 @@
+<?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 PDO
+ * @author $Author: dankert $
+ * @version $Revision: 1.5 $
+ * @package openrat.database
+ */
+class DB_pdo
+{
+ /**
+ * Die PDO-Verbindung.
+ *
+ * @var Resource
+ */
+ var $connection;
+
+ /**
+ * Datenbank-Fehler.
+ *
+ * @var String
+ */
+ var $error;
+
+
+ function connect( $conf )
+ {
+ $url = $conf['dsn'];
+ $user = $conf['user'];
+ $pw = $conf['password'];
+ $db = $conf['database'];
+
+ 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_' )
+ $options[substr($c,8)] = $conf[$c];
+
+ $this->connection = new PDO($url, $user, $pw, $options);
+
+ if ( !is_object($this->connection) )
+ {
+ $this->error = "Could not connect to database on host $host. ".PDO::errorInfo();
+ return false;
+ }
+
+ return true;
+ }
+
+
+
+ function disconnect()
+ {
+ $this->connection = null;
+ return true;
+ }
+
+
+
+ function query($query)
+ {
+ $this->result = $this->connection->query($query);
+
+ if ( ! $this->result )
+ {
+ $this->error = 'Database error: '.PDO::errorInfo();
+ return FALSE;
+ }
+
+ return $this->result;
+ }
+
+
+ function fetchRow( $result, $rownum )
+ {
+ return $this->result->fetch( PDO::FETCH_ASSOC );
+ }
+
+
+ function freeResult($result)
+ {
+ return true;
+ }
+
+
+ function numCols($result)
+ {
+ die('called NumCols() in PDO');
+ }
+
+
+
+ function numRows( $result )
+ {
+ die('called NumRows in PDO()');
+ }
+}
+
+?>+
\ No newline at end of file
diff --git a/db/postgresql.class.php b/db/postgresql.class.php
@@ -21,8 +21,8 @@
/**
* Datenbank-abhaengige Methoden fuer PostgreSQL
- * @author $Author$
- * @version $Revision$
+ * @author $Author: dankert $
+ * @version $Revision: 1.5 $
* @package openrat.database
*/
class DB_postgresql
diff --git a/db/sqlite.class.php b/db/sqlite.class.php
@@ -0,0 +1,115 @@
+<?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 SQ-Lite-Datenbanken
+ * @author $Author: dankert $
+ * @version $Revision: 1.5 $
+ * @package openrat.database
+ */
+class DB_sqlite
+{
+ /**
+ * Die SQ-Lite-Verbindung.
+ *
+ * @var Resource
+ */
+ var $connection;
+
+ /**
+ * Datenbank-Fehler.
+ *
+ * @var String
+ */
+ var $error;
+
+
+ function connect( $conf )
+ {
+ $filename = $conf['filename'];
+
+ if ( $conf['persistent'] )
+ $connect_function = 'sqlite_popen';
+ else
+ $connect_function = 'sqlite_open';
+
+ $this->connection = @$connect_function($filename,0666,$error);
+
+ if ( !is_resource($this->connection) )
+ {
+ $this->error = 'Could not connect to sqlite-database: '.$error;
+ return false;
+ }
+
+ return true;
+ }
+
+
+
+ function disconnect()
+ {
+ $ret = sqlite_close( $this->connection );
+ $this->connection = null;
+ return $ret;
+ }
+
+
+
+ function query($query)
+ {
+ $result = sqlite_query($this->connection,$query );
+
+ if ( ! $result )
+ {
+ $this->error = 'Database error: '.sqlite_error_string(sqlite_last_error($this->connection));
+ return FALSE;
+ }
+
+ return $result;
+ }
+
+
+ function fetchRow( $result, $rownum )
+ {
+ return sqlite_fetch_array( $result,SQLITE_ASSOC );
+ }
+
+
+ function freeResult($result)
+ {
+ 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
@@ -0,0 +1,110 @@
+<?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 SQLITE3
+ * @author $Author: dankert $
+ * @version $Revision: 1.5 $
+ * @package openrat.database
+ */
+class DB_sqlite3
+{
+ /**
+ * Das SQLITE3-Verbindungsobjekt.
+ *
+ * @var Resource
+ */
+ var $connection;
+
+ /**
+ * Datenbank-Fehler.
+ *
+ * @var String
+ */
+ var $error;
+
+
+ function connect( $conf )
+ {
+ $filename = $conf['filename'];
+
+ $this->connection = new SQLite3( $filename );
+
+ if ( !is_object($this->connection) )
+ {
+ $this->error = "Could not connect to SQLITE3-database: ".SQLite3::lastErrorMsg();
+ return false;
+ }
+
+ return true;
+ }
+
+
+
+ function disconnect()
+ {
+ $this->connection->close();
+ $this->connection = null;
+ return true;
+ }
+
+
+
+ function query($query)
+ {
+ $this->result = $this->connection->query($query);
+
+ if ( !$result )
+ {
+ $this->error = 'Database error: '.SQLite3::lastErrorMsg();
+ return FALSE;
+ }
+
+ return $this->result;
+ }
+
+
+ function fetchRow( $result, $rownum )
+ {
+ return $this->result->fetchArray( SQLITE3_ASSOC );
+ }
+
+
+ function freeResult($result)
+ {
+ return true;
+ }
+
+
+ function numCols($result)
+ {
+ return $this->result->numColumns();
+ }
+
+
+
+ function numRows( $result )
+ {
+ return $this->result->numRows();
+ }
+}
+
+?>+
\ No newline at end of file
diff --git a/do.php b/do.php
@@ -81,6 +81,10 @@ require_once( OR_TEXTCLASSES_DIR ."include.inc.".PHP_EXT );
require_once( OR_DBCLASSES_DIR."db.class.php" );
require_once( OR_DBCLASSES_DIR."postgresql.class.php" );
require_once( OR_DBCLASSES_DIR."mysql.class.php" );
+require_once( OR_DBCLASSES_DIR."mysqli.class.php" );
+require_once( OR_DBCLASSES_DIR."sqlite.class.php" );
+require_once( OR_DBCLASSES_DIR."sqlite3.class.php" );
+require_once( OR_DBCLASSES_DIR."pdo.class.php" );
// Jetzt erst die Sitzung starten (nachdem alle Klassen zur Verfügung stehen).
session_start();