2013-02-18 03:48:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PicoDb;
|
|
|
|
|
|
|
|
class Database
|
|
|
|
{
|
2014-02-08 20:13:14 +01:00
|
|
|
private static $instances = array();
|
2013-02-18 03:48:21 +01:00
|
|
|
private $logs = array();
|
|
|
|
private $pdo;
|
|
|
|
|
|
|
|
|
|
|
|
public function __construct(array $settings)
|
|
|
|
{
|
|
|
|
if (! isset($settings['driver'])) {
|
|
|
|
throw new \LogicException('You must define a database driver.');
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($settings['driver']) {
|
|
|
|
|
|
|
|
case 'sqlite':
|
|
|
|
require_once __DIR__.'/Drivers/Sqlite.php';
|
2014-09-15 13:23:55 +02:00
|
|
|
$this->pdo = new Sqlite($settings);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'mysql':
|
|
|
|
require_once __DIR__.'/Drivers/Mysql.php';
|
|
|
|
$this->pdo = new Mysql($settings);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'postgres':
|
|
|
|
require_once __DIR__.'/Drivers/Postgres.php';
|
|
|
|
$this->pdo = new Postgres($settings);
|
2013-02-18 03:48:21 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new \LogicException('This database driver is not supported.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-09 21:13:36 +01:00
|
|
|
public static function bootstrap($name, \Closure $callback)
|
2014-02-08 20:13:14 +01:00
|
|
|
{
|
|
|
|
self::$instances[$name] = $callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static function get($name)
|
|
|
|
{
|
|
|
|
if (! isset(self::$instances[$name])) {
|
|
|
|
throw new \LogicException('No database instance created with that name.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_callable(self::$instances[$name])) {
|
|
|
|
self::$instances[$name] = call_user_func(self::$instances[$name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$instances[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-18 03:48:21 +01:00
|
|
|
public function setLogMessage($message)
|
|
|
|
{
|
|
|
|
$this->logs[] = $message;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getLogMessages()
|
|
|
|
{
|
2013-07-13 02:26:47 +02:00
|
|
|
return $this->logs;
|
2013-02-18 03:48:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getConnection()
|
|
|
|
{
|
|
|
|
return $this->pdo;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-16 11:50:39 +02:00
|
|
|
public function closeConnection()
|
|
|
|
{
|
|
|
|
$this->pdo = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-18 03:48:21 +01:00
|
|
|
public function escapeIdentifier($value)
|
|
|
|
{
|
|
|
|
return $this->pdo->escapeIdentifier($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function execute($sql, array $values = array())
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
|
|
|
|
$this->setLogMessage($sql);
|
|
|
|
$this->setLogMessage(implode(', ', $values));
|
|
|
|
|
|
|
|
$rq = $this->pdo->prepare($sql);
|
|
|
|
$rq->execute($values);
|
|
|
|
|
|
|
|
return $rq;
|
|
|
|
}
|
|
|
|
catch (\PDOException $e) {
|
|
|
|
|
2014-02-08 20:13:14 +01:00
|
|
|
if ($this->pdo->inTransaction()) $this->pdo->rollback();
|
2013-02-18 03:48:21 +01:00
|
|
|
$this->setLogMessage($e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function startTransaction()
|
|
|
|
{
|
2014-02-09 18:34:38 +01:00
|
|
|
if (! $this->pdo->inTransaction()) {
|
|
|
|
$this->pdo->beginTransaction();
|
|
|
|
}
|
2013-02-18 03:48:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function closeTransaction()
|
|
|
|
{
|
2014-02-09 18:34:38 +01:00
|
|
|
if ($this->pdo->inTransaction()) {
|
|
|
|
$this->pdo->commit();
|
|
|
|
}
|
2013-02-18 03:48:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function cancelTransaction()
|
|
|
|
{
|
2014-02-09 18:34:38 +01:00
|
|
|
if ($this->pdo->inTransaction()) {
|
|
|
|
$this->pdo->rollback();
|
|
|
|
}
|
2013-02-18 03:48:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function table($table_name)
|
|
|
|
{
|
2014-02-08 20:13:14 +01:00
|
|
|
require_once __DIR__.'/Table.php';
|
2013-02-18 03:48:21 +01:00
|
|
|
return new Table($this, $table_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function schema()
|
|
|
|
{
|
|
|
|
require_once __DIR__.'/Schema.php';
|
|
|
|
return new Schema($this);
|
|
|
|
}
|
|
|
|
}
|