<?php
-use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
-use Doctrine\DBAL\DriverManager;
class IPF_Database
{
- public static function connectDBAL($database)
- {
- $config = new Configuration();
-
- $connectionParams = array(
- 'driver' => 'pdo_' . \PFF\Arr::get($database, 'driver', 'mysql'),
- 'host' => \PFF\Arr::get($database, 'host', 'localhost'),
- 'dbname' => \PFF\Arr::get($database, 'database'),
- 'user' => \PFF\Arr::get($database, 'username'),
- 'password' => \PFF\Arr::get($database, 'password'),
- 'charset' => 'utf8',
- );
-
- return DriverManager::getConnection($connectionParams, $config);
- }
-
public static function queryOneObject(Connection $connection, $className, $sql, $params = [], $types = [])
{
$stmt = $connection->executeQuery($sql, $params, $types);
return $stmt->fetchAll(\PDO::FETCH_CLASS, $className);
}
}
-
-
-
-class PDOProfile extends PDO
-{
- public function __construct($dsn, $username=null, $password=null, $options=null)
- {
- parent::__construct($dsn, $username, $password, $options);
- $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('PDOProfileStatement', array($this)));
-
- $this->colors = array(
- "\033[1;35m", // magenta
- "\033[1;36m", // cyan
- );
- }
-
- public function exec($statement)
- {
- $start = microtime(true);
- $result = parent::exec($statement);
- $exec_time = microtime(true) - $start;
- $this->report($statement, null, $exec_time, $exec_time);
- return $result;
- }
-
- public function prepare($q, $driver_options=array())
- {
- $stmt = parent::prepare($q, $driver_options);
- $stmt->q = $q;
- return $stmt;
- }
-
- public function query($q)
- {
- $stmt = parent::query($q);
- $stmt->q = $q;
- return $stmt;
- }
-
- private $colors, $color = 0;
-
- private function getColor()
- {
- $color = $this->colors[$this->color];
- $this->color = ($this->color + 1) % count($this->colors);
- return $color;
- }
-
- public function report($query, $params, $total_time, $exec_time)
- {
- $color = $this->getColor();
- error_log($color.$query."\033[0m");
- if ($params)
- error_log($color.trim(print_r($params, 1))."\033[0m");
- error_log($color.sprintf('Query time: %0.3fms Total time: %0.3fms', $exec_time * 1000, $total_time * 1000)."\033[0m");
- }
-}
-
-class PDOProfileStatement extends PDOStatement
-{
- public $q = 'UNKNOWN QUERY', $params = null;
- private $start, $execute_time;
-
- protected function __construct($pdo)
- {
- $this->pdo = $pdo;
- $this->start = microtime(true);
- }
-
- function bindValue($parameter, $value, $data_type = null)
- {
- $success = parent::bindValue($parameter, $value, $data_type);
- if ($this->params === null) {
- $this->params = [];
- }
- $this->params[$parameter] = $value;
- return $success;
- }
-
- function execute($params=null)
- {
- if ($this->params === null) {
- $this->params = $params;
- }
- $start = microtime(true);
- $result = $params ? parent::execute($params) : parent::execute();
- $this->execute_time = microtime(true) - $start;
- return $result;
- }
-
- function __destruct()
- {
- $total = microtime(true) - $this->start;
- $this->pdo->report($this->q, $this->params, $total, $this->execute_time);
- }
-}
--- /dev/null
+<?php
+
+namespace IPF\Database;
+
+use Doctrine\DBAL\Logging\SQLLogger;
+
+final class DebugSQLLogger implements SQLLogger
+{
+ private $colors, $color = 0;
+ private $start;
+
+ function __construct()
+ {
+ $this->colors = array(
+ "\033[1;35m", // magenta
+ "\033[1;36m", // cyan
+ );
+ }
+
+ private function log($message)
+ {
+ $color = $this->colors[$this->color];
+ error_log($color . $message . "\033[0m");
+ }
+
+ public function startQuery($sql, array $params = null, array $types = null)
+ {
+ $this->start = microtime(true);
+ $this->log($sql);
+ if ($params)
+ $this->log(trim(print_r($params, 1)));
+ }
+
+ public function stopQuery()
+ {
+ $total = microtime(true) - $this->start;
+ $this->log(sprintf('Query time: %0.3fms', $total * 1000));
+ $this->color = ($this->color + 1) % count($this->colors);
+ }
+}
--- /dev/null
+<?php
+
+namespace IPF\Database;
+
+use Doctrine\DBAL\Configuration;
+use Doctrine\DBAL\DriverManager;
+use Pimple\Container;
+use Pimple\ServiceProviderInterface;
+
+class DatabaseProvider implements ServiceProviderInterface
+{
+ public function register(Container $container)
+ {
+ $container['db_logger'] = function ($c) {
+ if ($c['settings']->get('debug')) {
+ return new DebugSQLLogger();
+ } else {
+ return null;
+ }
+ };
+
+ $container['db_config'] = function ($c) {
+ $database = $c['settings']->get('database');
+ return array(
+ 'driver' => 'pdo_' . \PFF\Arr::get($database, 'driver', 'mysql'),
+ 'host' => \PFF\Arr::get($database, 'host', 'localhost'),
+ 'dbname' => \PFF\Arr::get($database, 'database'),
+ 'user' => \PFF\Arr::get($database, 'username'),
+ 'password' => \PFF\Arr::get($database, 'password'),
+ 'charset' => 'utf8',
+ );
+ };
+
+ $container['db'] = $container->factory(function ($c) {
+ $config = new Configuration();
+ $config->setSQLLogger($c['db_logger']);
+ return DriverManager::getConnection($c['db_config'], $config);
+ });
+ }
+}