implement drivers as Singleton
This commit is contained in:
parent
604e7c9bba
commit
961c869c30
5 changed files with 213 additions and 239 deletions
|
|
@ -63,7 +63,7 @@
|
||||||
|
|
||||||
|
|
||||||
// Construct and return Driver
|
// Construct and return Driver
|
||||||
return new $className($config);
|
return $className::singleton($config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,11 +20,12 @@
|
||||||
abstract class DatabaseDriver extends \nre\core\Driver
|
abstract class DatabaseDriver extends \nre\core\Driver
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Connection and login settings
|
* Driver class instance
|
||||||
*
|
*
|
||||||
* @var array
|
* @static
|
||||||
|
* @var DatabaseDriver
|
||||||
*/
|
*/
|
||||||
protected $config;
|
protected static $driver = null;
|
||||||
/**
|
/**
|
||||||
* Connection resource
|
* Connection resource
|
||||||
*
|
*
|
||||||
|
|
@ -36,31 +37,25 @@
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute a SQL-query.
|
* Singleton-pattern.
|
||||||
*
|
*
|
||||||
* @param string $query Query to run
|
* @param array $config Database driver configuration
|
||||||
* @return array Result
|
* @return DatabaseDriver Singleton-instance of database driver class
|
||||||
*/
|
*/
|
||||||
public abstract function query($query);
|
public static function singleton($config)
|
||||||
|
{
|
||||||
|
// Singleton
|
||||||
/**
|
if(self::$driver !== null) {
|
||||||
* Return the last insert id (of the last insert-query).
|
return self::$driver;
|
||||||
*
|
}
|
||||||
* @return int Last insert id
|
|
||||||
*/
|
// Construct
|
||||||
public abstract function getInsertId();
|
$className = get_called_class();
|
||||||
|
self::$driver = new $className($config);
|
||||||
|
|
||||||
/**
|
|
||||||
* Mask an input for using it in a SQL-query.
|
return self::$driver;
|
||||||
*
|
}
|
||||||
* @param mixed $input Input to mask
|
|
||||||
* @return mixed Masked input
|
|
||||||
*/
|
|
||||||
public abstract function mask($input);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -72,10 +67,21 @@
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
// Save values
|
// Establish connection
|
||||||
$this->config = $config;
|
$this->connect($config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Establish a connect to a MqSQL-database.
|
||||||
|
*
|
||||||
|
* @throws DatamodelException
|
||||||
|
* @param array $config Connection and login settings
|
||||||
|
*/
|
||||||
|
protected abstract function connect($config);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
|
||||||
|
|
@ -1,153 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
/**
|
|
||||||
* NRE
|
|
||||||
*
|
|
||||||
* @author coderkun <olli@coderkun.de>
|
|
||||||
* @copyright 2013 coderkun (http://www.coderkun.de)
|
|
||||||
* @license http://www.gnu.org/licenses/gpl.html
|
|
||||||
* @link http://www.coderkun.de/projects/nre
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace nre\drivers;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Implementation of a database driver for MySQL-databases.
|
|
||||||
*
|
|
||||||
* @author coderkun <olli@coderkun.de>
|
|
||||||
*/
|
|
||||||
class MysqlDriver extends \nre\drivers\DatabaseDriver
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct a MySQL-driver.
|
|
||||||
*
|
|
||||||
* @throws DatamodelException
|
|
||||||
* @param array $config Connection and login settings
|
|
||||||
*/
|
|
||||||
function __construct($config)
|
|
||||||
{
|
|
||||||
parent::__construct($config);
|
|
||||||
|
|
||||||
// Connect
|
|
||||||
$this->connect();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Execute a SQL-query.
|
|
||||||
*
|
|
||||||
* @param string $query Query to run
|
|
||||||
* @return array Result
|
|
||||||
*/
|
|
||||||
public function query($query)
|
|
||||||
{
|
|
||||||
// Return-array
|
|
||||||
$data = array();
|
|
||||||
|
|
||||||
|
|
||||||
// Execute query
|
|
||||||
$result = mysql_query($query, $this->connection);
|
|
||||||
|
|
||||||
// Check result
|
|
||||||
if(($errno = mysql_errno($this->connection)) > 0) {
|
|
||||||
throw new DatamodelException(mysql_error($this->connection), $errno);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Process result
|
|
||||||
if(is_resource($result))
|
|
||||||
{
|
|
||||||
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
|
|
||||||
$data[] = $row;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Return data
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the last insert id (of the last insert-query).
|
|
||||||
*
|
|
||||||
* @return int Last insert id
|
|
||||||
*/
|
|
||||||
public function getInsertId()
|
|
||||||
{
|
|
||||||
return mysql_insert_id($this->connection);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Mask an input for using it in a SQL-query.
|
|
||||||
*
|
|
||||||
* @param mixed $input Input to mask
|
|
||||||
* @return mixed Masked input
|
|
||||||
*/
|
|
||||||
public function mask($input)
|
|
||||||
{
|
|
||||||
return mysql_real_escape_string($input, $this->connection);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Establish a connect to a MqSQL-database.
|
|
||||||
*
|
|
||||||
* @throws DatamodelException
|
|
||||||
*/
|
|
||||||
private function connect()
|
|
||||||
{
|
|
||||||
// Connect
|
|
||||||
$con = @mysql_connect(
|
|
||||||
$this->config['host'],
|
|
||||||
$this->config['user'],
|
|
||||||
$this->config['password']
|
|
||||||
);
|
|
||||||
|
|
||||||
// Check connection
|
|
||||||
if($con === false) {
|
|
||||||
throw new \nre\exceptions\DatamodelException(mysql_error(), mysql_errno());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save connection
|
|
||||||
$this->connection = $con;
|
|
||||||
|
|
||||||
|
|
||||||
// Select database
|
|
||||||
$db = mysql_select_db(
|
|
||||||
$this->config['db'],
|
|
||||||
$this->connection
|
|
||||||
);
|
|
||||||
|
|
||||||
// Check database selection
|
|
||||||
if(!$db) {
|
|
||||||
throw new DatamodelException(mysql_error(), mysql_errno());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Configure connection
|
|
||||||
$this->configConnection();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Configure the current connection
|
|
||||||
*/
|
|
||||||
private function configConnection()
|
|
||||||
{
|
|
||||||
// Set character encoding
|
|
||||||
$this->query("SET NAMES 'utf8'", $this->connection);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
?>
|
|
||||||
167
drivers/MysqliDriver.inc
Normal file
167
drivers/MysqliDriver.inc
Normal file
|
|
@ -0,0 +1,167 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NRE
|
||||||
|
*
|
||||||
|
* @author coderkun <olli@coderkun.de>
|
||||||
|
* @copyright 2013 coderkun (http://www.coderkun.de)
|
||||||
|
* @license http://www.gnu.org/licenses/gpl.html
|
||||||
|
* @link http://www.coderkun.de/projects/nre
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace nre\drivers;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implementation of a database driver for MySQL-databases.
|
||||||
|
*
|
||||||
|
* @author coderkun <olli@coderkun.de>
|
||||||
|
*/
|
||||||
|
class MysqliDriver extends \nre\drivers\DatabaseDriver
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a MySQL-driver.
|
||||||
|
*
|
||||||
|
* @throws DatamodelException
|
||||||
|
* @param array $config Connection and login settings
|
||||||
|
*/
|
||||||
|
function __construct($config)
|
||||||
|
{
|
||||||
|
parent::__construct($config);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute a SQL-query.
|
||||||
|
*
|
||||||
|
* @throws DatamodelException
|
||||||
|
* @param string $query Query to run
|
||||||
|
* @param mixed … Params
|
||||||
|
* @return array Result
|
||||||
|
*/
|
||||||
|
public function query($query)
|
||||||
|
{
|
||||||
|
// Prepare statement
|
||||||
|
if(!($stmt = $this->connection->prepare($query))) {
|
||||||
|
throw new \nre\exceptions\DatamodelException($this->connection->error, $this->connection->errno);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Prepare data
|
||||||
|
$data = array();
|
||||||
|
|
||||||
|
// Bind parameters
|
||||||
|
$p = array_slice(func_get_args(), 1);
|
||||||
|
$params = array();
|
||||||
|
foreach($p as $key => $value) {
|
||||||
|
$params[$key] = &$p[$key];
|
||||||
|
}
|
||||||
|
if(count($params) > 0)
|
||||||
|
{
|
||||||
|
if(!(call_user_func_array(array($stmt, 'bind_param'), $params))) {
|
||||||
|
throw new \nre\exceptions\DatamodelException($this->connection->error, $this->connection->errno);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execute query
|
||||||
|
if(!$stmt->execute()) {
|
||||||
|
throw new \nre\exceptions\DatamodelException($this->connection->error, $this->connection->errno);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch result
|
||||||
|
if($result = $stmt->get_result()) {
|
||||||
|
while($row = $result->fetch_assoc()) {
|
||||||
|
$data[] = $row;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
$stmt->close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the last insert id (of the last insert-query).
|
||||||
|
*
|
||||||
|
* @return int Last insert id
|
||||||
|
*/
|
||||||
|
public function getInsertId()
|
||||||
|
{
|
||||||
|
return $this->connection->insert_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enable/disable autocommit feature.
|
||||||
|
*
|
||||||
|
* @param boolean $autocommit Enable/disable autocommit
|
||||||
|
*/
|
||||||
|
public function setAutocommit($autocommit)
|
||||||
|
{
|
||||||
|
$this->connection->autocommit($autocommit);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rollback the current transaction.
|
||||||
|
*/
|
||||||
|
public function rollback()
|
||||||
|
{
|
||||||
|
$this->connection->rollback();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Commit the current transaction.
|
||||||
|
*/
|
||||||
|
public function commit()
|
||||||
|
{
|
||||||
|
$this->connection->commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Establish a connect to a MqSQL-database.
|
||||||
|
*
|
||||||
|
* @throws DatamodelException
|
||||||
|
* @param array $config Connection and login settings
|
||||||
|
*/
|
||||||
|
protected function connect($config)
|
||||||
|
{
|
||||||
|
// Connect
|
||||||
|
$con = @new \mysqli(
|
||||||
|
$config['host'],
|
||||||
|
$config['user'],
|
||||||
|
$config['password'],
|
||||||
|
$config['db']
|
||||||
|
);
|
||||||
|
|
||||||
|
// Check connection
|
||||||
|
if($con->connect_error) {
|
||||||
|
throw new \nre\exceptions\DatamodelException($con->connect_error, $con->connect_errno);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set character encoding
|
||||||
|
if(!$con->set_charset('utf8')) {
|
||||||
|
throw new \nre\exceptions\DatamodelException($con->connect_error, $con->connect_errno);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save connection
|
||||||
|
$this->connection = $con;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
|
|
@ -19,14 +19,13 @@
|
||||||
*/
|
*/
|
||||||
class DatabaseModel extends \nre\core\Model
|
class DatabaseModel extends \nre\core\Model
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Database connection
|
* Database connection
|
||||||
*
|
*
|
||||||
* @static
|
* @static
|
||||||
* @var object
|
* @var DatabaseDriver
|
||||||
*/
|
*/
|
||||||
private static $db = NULL;
|
protected $db = NULL;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -34,64 +33,21 @@
|
||||||
/**
|
/**
|
||||||
* Construct a new datamase model.
|
* Construct a new datamase model.
|
||||||
*
|
*
|
||||||
|
* @throws DatamodelException
|
||||||
* @throws DriverNotFoundException
|
* @throws DriverNotFoundException
|
||||||
* @throws DriverNotValidException
|
* @throws DriverNotValidException
|
||||||
* @throws DatamodelException
|
* @param string $type Database type
|
||||||
* @param string $databaseType Database type
|
* @param array $config Connection settings
|
||||||
* @param array $config Connection settings
|
|
||||||
*/
|
*/
|
||||||
function __construct($databaseType, $config)
|
function __construct($type, $config)
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
// Load database driver
|
// Load database driver
|
||||||
$this->loadDriver($databaseType);
|
$this->loadDriver($type);
|
||||||
|
|
||||||
// Establish database connection
|
// Establish database connection
|
||||||
$this->connect($databaseType, $config);
|
$this->connect($type, $config);
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Execute a SQL-query.
|
|
||||||
*
|
|
||||||
* @param string $query Query to run
|
|
||||||
* @param mixed … Query parameters
|
|
||||||
* @return array Result
|
|
||||||
*/
|
|
||||||
protected function query($query)
|
|
||||||
{
|
|
||||||
// Mask parameters
|
|
||||||
$args = array($query);
|
|
||||||
foreach(array_slice(func_get_args(), 1) as $arg) {
|
|
||||||
$args[] = self::$db->mask($arg);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Format query
|
|
||||||
$query = call_user_func_array(
|
|
||||||
'sprintf',
|
|
||||||
$args
|
|
||||||
);
|
|
||||||
|
|
||||||
// Execute query
|
|
||||||
$data = self::$db->query($query);
|
|
||||||
|
|
||||||
|
|
||||||
// Return data
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the last insert id (of the last insert-query).
|
|
||||||
*
|
|
||||||
* @return int Last insert id
|
|
||||||
*/
|
|
||||||
protected function getInsertId()
|
|
||||||
{
|
|
||||||
return self::$db->getInsertId();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -104,10 +60,9 @@
|
||||||
* @throws DriverNotValidException
|
* @throws DriverNotValidException
|
||||||
* @param string $driverName Name of the database driver
|
* @param string $driverName Name of the database driver
|
||||||
*/
|
*/
|
||||||
private static function loadDriver($driverName)
|
private function loadDriver($driverName)
|
||||||
{
|
{
|
||||||
\nre\core\Driver::load($driverName);
|
\nre\core\Driver::load($driverName);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -116,12 +71,11 @@
|
||||||
*
|
*
|
||||||
* @throws DatamodelException
|
* @throws DatamodelException
|
||||||
* @param string $driverName Name of the database driver
|
* @param string $driverName Name of the database driver
|
||||||
|
* @param array $config Connection settings
|
||||||
*/
|
*/
|
||||||
private static function connect($driverName, $config)
|
private function connect($driverName, $config)
|
||||||
{
|
{
|
||||||
if(self::$db === NULL) {
|
$this->db = \nre\core\Driver::factory($driverName, $config);
|
||||||
self::$db = \nre\core\Driver::factory($driverName, $config);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue