restructure application classes
This commit is contained in:
commit
a6d9bf653a
3471 changed files with 597952 additions and 0 deletions
87
drivers/DatabaseDriver.inc
Normal file
87
drivers/DatabaseDriver.inc
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
<?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;
|
||||
|
||||
|
||||
/**
|
||||
* Abstract class for implementing a database driver.
|
||||
*
|
||||
* @author coderkun <olli@coderkun.de>
|
||||
*/
|
||||
abstract class DatabaseDriver extends \nre\core\Driver
|
||||
{
|
||||
/**
|
||||
* Driver class instance
|
||||
*
|
||||
* @static
|
||||
* @var DatabaseDriver
|
||||
*/
|
||||
protected static $driver = null;
|
||||
/**
|
||||
* Connection resource
|
||||
*
|
||||
* @var resource
|
||||
*/
|
||||
protected $connection = null;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Singleton-pattern.
|
||||
*
|
||||
* @param array $config Database driver configuration
|
||||
* @return DatabaseDriver Singleton-instance of database driver class
|
||||
*/
|
||||
public static function singleton($config)
|
||||
{
|
||||
// Singleton
|
||||
if(self::$driver !== null) {
|
||||
return self::$driver;
|
||||
}
|
||||
|
||||
// Construct
|
||||
$className = get_called_class();
|
||||
self::$driver = new $className($config);
|
||||
|
||||
|
||||
return self::$driver;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new database driver.
|
||||
*
|
||||
* @param array $config Connection and login settings
|
||||
*/
|
||||
protected function __construct($config)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
// Establish connection
|
||||
$this->connect($config);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Establish a connect to a MqSQL-database.
|
||||
*
|
||||
* @throws DatamodelException
|
||||
* @param array $config Connection and login settings
|
||||
*/
|
||||
protected abstract function connect($config);
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
169
drivers/MysqliDriver.inc
Normal file
169
drivers/MysqliDriver.inc
Normal file
|
|
@ -0,0 +1,169 @@
|
|||
<?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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$stmt->close();
|
||||
return $data;
|
||||
}
|
||||
catch(Exception $e) {
|
||||
$stmt->close();
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
Loading…
Add table
Add a link
Reference in a new issue