miniflux-legacy/vendor/fguillot/picodb/lib/PicoDb/Database.php

371 lines
7.4 KiB
PHP
Raw Normal View History

<?php
namespace PicoDb;
use Closure;
use PDOException;
use LogicException;
2016-07-31 00:41:42 +02:00
use PicoDb\Driver\Mssql;
2015-08-15 03:33:39 +02:00
use PicoDb\Driver\Sqlite;
use PicoDb\Driver\Mysql;
use PicoDb\Driver\Postgres;
/**
* Database
*
2016-07-31 00:41:42 +02:00
* @package PicoDb
* @author Frederic Guillot
2015-08-15 03:33:39 +02:00
*/
class Database
{
/**
* Database instances
*
* @static
2015-08-15 03:33:39 +02:00
* @access private
* @var array
*/
private static $instances = array();
2016-07-31 00:41:42 +02:00
/**
* Statement object
*
* @access protected
* @var StatementHandler
*/
protected $statementHandler;
/**
* Queries logs
*
* @access private
* @var array
*/
private $logs = array();
/**
2015-08-15 03:33:39 +02:00
* Driver instance
*
* @access private
*/
2015-08-15 03:33:39 +02:00
private $driver;
/**
2015-08-15 03:33:39 +02:00
* Initialize the driver
*
* @access public
2016-07-31 00:41:42 +02:00
* @param array $settings
*/
2016-07-31 00:41:42 +02:00
public function __construct(array $settings = array())
{
2016-07-31 00:41:42 +02:00
$this->driver = DriverFactory::getDriver($settings);
$this->statementHandler = new StatementHandler($this);
}
/**
* Destructor
*
* @access public
*/
public function __destruct()
{
$this->closeConnection();
}
/**
* Register a new database instance
*
* @static
* @access public
* @param string $name Instance name
* @param Closure $callback Callback
*/
2015-08-15 03:33:39 +02:00
public static function setInstance($name, Closure $callback)
{
self::$instances[$name] = $callback;
}
/**
* Get a database instance
*
* @static
* @access public
* @param string $name Instance name
* @return Database
*/
2015-08-15 03:33:39 +02:00
public static function getInstance($name)
{
if (! isset(self::$instances[$name])) {
2015-08-15 03:33:39 +02:00
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];
}
/**
* Add a log message
*
* @access public
2016-07-31 00:41:42 +02:00
* @param mixed $message
* @return Database
*/
public function setLogMessage($message)
{
2016-07-31 00:41:42 +02:00
$this->logs[] = is_array($message) ? var_export($message, true) : $message;
return $this;
}
/**
* Add many log messages
*
* @access public
* @param array $messages
* @return Database
*/
public function setLogMessages(array $messages)
{
foreach ($messages as $message) {
$this->setLogMessage($message);
}
return $this;
}
/**
* Get all queries logs
*
* @access public
* @return array
*/
public function getLogMessages()
{
return $this->logs;
}
/**
* Get the PDO connection
*
* @access public
2015-08-15 03:33:39 +02:00
* @return \PDO
*/
public function getConnection()
{
2015-08-15 03:33:39 +02:00
return $this->driver->getConnection();
}
/**
* Get the Driver instance
*
* @access public
2016-07-31 00:41:42 +02:00
* @return Mssql|Sqlite|Postgres|Mysql
2015-08-15 03:33:39 +02:00
*/
public function getDriver()
{
return $this->driver;
}
/**
* Get the last inserted id
*
* @access public
* @return integer
*/
public function getLastId()
{
2016-07-31 00:41:42 +02:00
return (int) $this->driver->getLastId();
}
/**
* Get statement object
*
* @access public
* @return StatementHandler
*/
public function getStatementHandler()
{
return $this->statementHandler;
}
/**
* Release the PDO connection
*
* @access public
*/
public function closeConnection()
{
2015-08-15 03:33:39 +02:00
$this->driver->closeConnection();
}
/**
* Escape an identifier (column, table name...)
*
* @access public
* @param string $value Value
2015-01-28 02:13:16 +01:00
* @param string $table Table name
* @return string
*/
2015-01-28 02:13:16 +01:00
public function escapeIdentifier($value, $table = '')
{
// Do not escape custom query
if (strpos($value, '.') !== false || strpos($value, ' ') !== false) {
return $value;
}
2015-01-28 02:13:16 +01:00
if (! empty($table)) {
2015-08-15 03:33:39 +02:00
return $this->driver->escape($table).'.'.$this->driver->escape($value);
}
return $this->driver->escape($value);
}
/**
* Escape an identifier list
*
* @access public
* @param array $identifiers List of identifiers
* @param string $table Table name
* @return string[]
*/
public function escapeIdentifierList(array $identifiers, $table = '')
{
foreach ($identifiers as $key => $value) {
$identifiers[$key] = $this->escapeIdentifier($value, $table);
2015-01-28 02:13:16 +01:00
}
2015-08-15 03:33:39 +02:00
return $identifiers;
}
/**
* Execute a prepared statement
*
2015-08-15 03:33:39 +02:00
* Note: returns false on duplicate keys instead of SQLException
*
* @access public
* @param string $sql SQL query
* @param array $values Values
2015-08-15 03:33:39 +02:00
* @return \PDOStatement|false
*/
public function execute($sql, array $values = array())
{
2016-07-31 00:41:42 +02:00
return $this->statementHandler
->withSql($sql)
->withPositionalParams($values)
->execute();
}
/**
* Run a transaction
*
* @access public
* @param Closure $callback Callback
* @return mixed
*/
public function transaction(Closure $callback)
{
2015-08-15 03:33:39 +02:00
try {
$this->startTransaction();
$result = $callback($this);
$this->closeTransaction();
return $result === null ? true : $result;
2016-07-31 00:41:42 +02:00
} catch (PDOException $e) {
return $this->statementHandler->handleSqlError($e);
2015-08-15 03:33:39 +02:00
}
}
/**
* Begin a transaction
*
* @access public
*/
public function startTransaction()
{
2015-08-15 03:33:39 +02:00
if (! $this->getConnection()->inTransaction()) {
$this->getConnection()->beginTransaction();
}
}
/**
* Commit a transaction
*
* @access public
*/
public function closeTransaction()
{
2015-08-15 03:33:39 +02:00
if ($this->getConnection()->inTransaction()) {
$this->getConnection()->commit();
}
}
/**
* Rollback a transaction
*
* @access public
*/
public function cancelTransaction()
{
2015-08-15 03:33:39 +02:00
if ($this->getConnection()->inTransaction()) {
2016-07-31 00:41:42 +02:00
$this->getConnection()->rollBack();
}
}
/**
2016-07-31 00:41:42 +02:00
* Get a table object
*
* @access public
2016-07-31 00:41:42 +02:00
* @param string $table
2015-08-15 03:33:39 +02:00
* @return Table
*/
2016-07-31 00:41:42 +02:00
public function table($table)
{
2016-07-31 00:41:42 +02:00
return new Table($this, $table);
}
2015-01-28 02:13:16 +01:00
/**
2016-07-31 00:41:42 +02:00
* Get a hashtable object
2015-01-28 02:13:16 +01:00
*
* @access public
2016-07-31 00:41:42 +02:00
* @param string $table
2015-08-15 03:33:39 +02:00
* @return Hashtable
2015-01-28 02:13:16 +01:00
*/
2016-07-31 00:41:42 +02:00
public function hashtable($table)
2015-01-28 02:13:16 +01:00
{
2016-07-31 00:41:42 +02:00
return new Hashtable($this, $table);
2015-01-28 02:13:16 +01:00
}
/**
2016-07-31 00:41:42 +02:00
* Get a LOB object
*
* @access public
2016-07-31 00:41:42 +02:00
* @param string $table
* @return LargeObject
*/
public function largeObject($table)
{
return new LargeObject($this, $table);
}
/**
* Get a schema object
*
* @access public
* @param string $namespace
2015-08-15 03:33:39 +02:00
* @return Schema
*/
2016-07-31 00:41:42 +02:00
public function schema($namespace = null)
{
2016-07-31 00:41:42 +02:00
$schema = new Schema($this);
if ($namespace !== null) {
$schema->setNamespace($namespace);
}
return $schema;
}
2016-07-31 00:41:42 +02:00
}