<?php
/*
Database.php, creates database connections and provides some basic
functions
Copyright (C) 2004 Arend van Beelen, Auton Rijnsburg
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, or (at your option)
any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
For any questions, comments or whatever, you may mail me at: hide@address.com
*/
require_once('String.php');
/**
* @brief Creates database connections and provides some basic functions
*
* This class can be used to create new database connections and to manage
* existing connections.
*/
class Database
{
/**
* Constructor.
*
* You do not have to instantiate this class yourself, instead you can
* use the static functions provided by this class.
*/
public function __construct()
{
$this->connections = array();
}
/**
* Destructor.
*
* Closes all managed database connections.
*/
public function __destruct()
{
foreach($this->connections as $connection)
{
$connection->disconnect();
}
}
/**
* Selects a database connection.
*
* Returns the specified database connection. The function will check whether
* this connection already exists and if so, returns the connection. If the
* connection does not yet exist, it will try to establish it.
*
* @param type The type of database to connect to, for example 'MySQL'. An
* appropriate DatabaseHandler needs to be installed to handle
* this type of database.
* @param server The server on which the database is running. This can be an
* IP address or a hostname.
* @param database The name of the actual database to select on the server
* when connected.
* @param username Username used to authenticate to the database server.
* @param password The user's password to authenticate to the database server.
* @return A DatabaseHandler object with an established connection to the
* specified database or @p false if no connection could be
* established.
*/
public static function connection($type, $server, $database, $username, $password)
{
global $Database;
foreach($Database->connections as $connection)
{
if($connection->type() == $type &&
$connection->server() == $server &&
$connection->database() == $database &&
$connection->username() == $username)
{
return $connection;
}
}
$className = String::stripSpecialChars($type).'_DatabaseHandler';
$include = include_once("DatabaseHandlers/$className.php");
if($include == false ||
class_exists($className) == false)
{
return false;
}
$connection = new $className;
if($connection->connect($server, $database, $username, $password) == false)
{
return false;
}
$Database->connections[] = $connection;
return $connection;
}
private $connections;
}
// create one global instance of the class
global $Database;
$Database = new Database();
?>