Added Mongo layer by piggy backing the PHP/Pecl Mongo class.
This commit is contained in:
parent
630a1276aa
commit
83e8447d8a
4 changed files with 231 additions and 107 deletions
|
@ -70,23 +70,95 @@ class Database extends Object
|
|||
if (isset($config->datasources[$name]))
|
||||
{
|
||||
$datasource = $config->datasources[$name];
|
||||
$datasource['type'] = strtolower($datasource['type']);
|
||||
|
||||
switch ($datasource['type'])
|
||||
{
|
||||
case 'mysql':
|
||||
if (!isset(self::$instances['Database'][$name]))
|
||||
case 'Mongo':
|
||||
// Assembles the server string
|
||||
$server = 'mongodb://';
|
||||
|
||||
if (isset($datasource['username']))
|
||||
{
|
||||
self::$instances['Database'][$name] = new Database_MySQL($datasource['hostname'], $datasource['username'], $datasource['password'], $datasource['database']);
|
||||
$server .= $datasource['username'];
|
||||
|
||||
if (isset($datasource['password']))
|
||||
{
|
||||
$server .= ':' . $datasource['password'];
|
||||
}
|
||||
|
||||
$server .= '@';
|
||||
}
|
||||
|
||||
return self::$instances['Database'][$name];
|
||||
$server .= $datasource['hostname'] . ':' . $datasource['port'] . '/' . $datasource['database'];
|
||||
|
||||
// Attempts to connect
|
||||
try
|
||||
{
|
||||
$instance = new Mongo($server, array('persist' => 'pickles'));
|
||||
|
||||
// If we have database and collection, attempt to assign them
|
||||
if (isset($datasource['database']))
|
||||
{
|
||||
$instance = $instance->$datasource['database'];
|
||||
|
||||
if (isset($datasource['collection']))
|
||||
{
|
||||
$instance = $instance->$datasource['collection'];
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception $exception)
|
||||
{
|
||||
throw new Exception('Unable to connect to Mongo database');
|
||||
}
|
||||
break;
|
||||
|
||||
// @todo This is going to end up being all PDO driven
|
||||
case 'MySQL':
|
||||
if (!isset(self::$instances['Database'][$name]))
|
||||
{
|
||||
$class = 'Database_' . $datasource['type'];
|
||||
|
||||
$instance = new $class();
|
||||
|
||||
if (isset($datasource['hostname']))
|
||||
{
|
||||
$instance->setHostname($datasource['hostname']);
|
||||
}
|
||||
|
||||
if (isset($datasource['port']))
|
||||
{
|
||||
$instance->setPort($datasource['port']);
|
||||
}
|
||||
|
||||
if (isset($datasource['username']))
|
||||
{
|
||||
$instance->setUsername($datasource['username']);
|
||||
}
|
||||
|
||||
if (isset($datasource['password']))
|
||||
{
|
||||
$instance->setPassword($datasource['password']);
|
||||
}
|
||||
|
||||
if (isset($datasource['database']))
|
||||
{
|
||||
$instance->setDatabase($datasource['database']);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new Exception('Datasource type "' . $datasource['type'] . '" is invalid');
|
||||
break;
|
||||
}
|
||||
|
||||
if (isset($instance))
|
||||
{
|
||||
self::$instances['Database'][$name] = $instance;
|
||||
}
|
||||
|
||||
return self::$instances['Database'][$name];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
153
classes/Database/Common.php
Normal file
153
classes/Database/Common.php
Normal file
|
@ -0,0 +1,153 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Common Database Class File for PICKLES
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* Licensed under the GNU General Public License Version 3
|
||||
* Redistribution of these files must retain the above copyright notice.
|
||||
*
|
||||
* @package PICKLES
|
||||
* @author Josh Sherman <josh@phpwithpickles.org>
|
||||
* @copyright Copyright 2007-2010, Gravity Boulevard, LLC
|
||||
* @license http://www.gnu.org/licenses/gpl.html GPL v3
|
||||
* @link http://phpwithpickles.org
|
||||
*/
|
||||
|
||||
/**
|
||||
* Common Database Abstraction Layer
|
||||
*
|
||||
* Parent class that our database driver classes should be extending. Contains
|
||||
* basic functionality for instantiation and interfacing.
|
||||
*/
|
||||
abstract class Database_Common extends Object
|
||||
{
|
||||
/**
|
||||
* Hostname for the server
|
||||
*
|
||||
* @access protected
|
||||
* @var string
|
||||
*/
|
||||
protected $hostname = 'localhost';
|
||||
|
||||
/**
|
||||
* Port number for the server
|
||||
*
|
||||
* @access protected
|
||||
* @var integer
|
||||
*/
|
||||
protected $port = null;
|
||||
|
||||
/**
|
||||
* Username for the server
|
||||
*
|
||||
* @access protected
|
||||
* @var string
|
||||
*/
|
||||
protected $username = null;
|
||||
|
||||
/**
|
||||
* Password for the server
|
||||
*
|
||||
* @access protected
|
||||
* @var string
|
||||
*/
|
||||
protected $password = null;
|
||||
|
||||
/**
|
||||
* Database name for the server
|
||||
*
|
||||
* @access protected
|
||||
* @var string
|
||||
*/
|
||||
protected $database = null;
|
||||
|
||||
/**
|
||||
* Connection resource
|
||||
*
|
||||
* @access protected
|
||||
* @var object
|
||||
*/
|
||||
protected $connection = null;
|
||||
|
||||
/**
|
||||
* Results object for the executed statement
|
||||
*
|
||||
* @access protected
|
||||
* @var object
|
||||
*/
|
||||
protected $results = null;
|
||||
|
||||
/**
|
||||
* Set Hostname
|
||||
*
|
||||
* @param string $hostname hostname for the database
|
||||
*/
|
||||
public function setHostname($hostname)
|
||||
{
|
||||
return $this->hostname = $hostname;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Port
|
||||
*
|
||||
* @param integer $port port for the database
|
||||
*/
|
||||
public function setPort($port)
|
||||
{
|
||||
return $this->port = $port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Username
|
||||
*
|
||||
* @param string $username username for the database
|
||||
*/
|
||||
public function setUsername($username)
|
||||
{
|
||||
return $this->username = $username;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Password
|
||||
*
|
||||
* @param string $password password for the database
|
||||
*/
|
||||
public function setPassword($password)
|
||||
{
|
||||
return $this->password = $password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Database
|
||||
*
|
||||
* @param string $database database for the database
|
||||
*/
|
||||
public function setDatabase($database)
|
||||
{
|
||||
return $this->database = $database;
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens database connection
|
||||
*
|
||||
* Establishes a connection to the MySQL database based on the
|
||||
* configuration options that are available in the Config object.
|
||||
*
|
||||
* @abstract
|
||||
* @return boolean true on success, throws an exception overwise
|
||||
*/
|
||||
abstract public function open();
|
||||
|
||||
/**
|
||||
* Closes database connection
|
||||
*
|
||||
* Sets the connection to null regardless of state.
|
||||
*
|
||||
* @return boolean always true
|
||||
*/
|
||||
abstract public function close();
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,31 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Mongo Interface Class File for PICKLES
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* Licensed under the GNU General Public License Version 3
|
||||
* Redistribution of these files must retain the above copyright notice.
|
||||
*
|
||||
* @package PICKLES
|
||||
* @author Josh Sherman <josh@phpwithpickles.org>
|
||||
* @copyright Copyright 2007-2010, Gravity Boulevard, LLC
|
||||
* @license http://www.gnu.org/licenses/gpl.html GPL v3
|
||||
* @link http://phpwithpickles.org
|
||||
*/
|
||||
|
||||
/**
|
||||
* Mongo Database Abstraction Layer
|
||||
*
|
||||
* Requires PECL mongo >= 1.0.10
|
||||
* Note: PECL mongo 1.0.10 is included /vendors/pecl
|
||||
*
|
||||
* @link http://pecl.php.net/package/mongo
|
||||
* @link http://us2.php.net/manual/en/book.mongo.php
|
||||
*/
|
||||
class Database_Mongo extends Object
|
||||
{
|
||||
}
|
||||
|
||||
?>
|
|
@ -23,78 +23,8 @@
|
|||
*
|
||||
* @todo Add in a check if the class is being called directly and except.
|
||||
*/
|
||||
class Database_MySQL extends Object
|
||||
class Database_MySQL extends Database_Common
|
||||
{
|
||||
/**
|
||||
* Hostname for the MySQL Server
|
||||
*
|
||||
* @access private
|
||||
* @var string
|
||||
*/
|
||||
private $hostname = 'localhost';
|
||||
|
||||
/**
|
||||
* Username for the MySQL Server
|
||||
*
|
||||
* @access private
|
||||
* @var string
|
||||
*/
|
||||
private $username = null;
|
||||
|
||||
/**
|
||||
* Password for the MySQL Server
|
||||
*
|
||||
* @access private
|
||||
* @var string
|
||||
*/
|
||||
private $password = null;
|
||||
|
||||
/**
|
||||
* Database name for the MySQL Server
|
||||
*
|
||||
* @access private
|
||||
* @var string
|
||||
*/
|
||||
private $database = null;
|
||||
|
||||
/**
|
||||
* Connection resource to MySQL
|
||||
*
|
||||
* @access private
|
||||
* @var object
|
||||
*/
|
||||
private $connection = null;
|
||||
|
||||
/**
|
||||
* Results object for the executed statement
|
||||
*
|
||||
* @access private
|
||||
* @var object
|
||||
*/
|
||||
private $results = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* Sets up our connection variables
|
||||
*
|
||||
* @param string $hostname hostname to connect to
|
||||
* @param string $username username to use
|
||||
* @param string $password password to use
|
||||
* @param string $database database to connect to
|
||||
*/
|
||||
public function __construct($hostname, $username, $password, $database)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->hostname = $hostname;
|
||||
$this->username = $username;
|
||||
$this->password = $password;
|
||||
$this->database = $database;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens database connection
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue