From: Andrey Kutejko Date: Sat, 16 Mar 2019 23:05:35 +0000 (+0100) Subject: database provider X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=ca4c0111ff61ecfe4ab6db15422cb48e3eb3c150;p=ipf.git database provider --- diff --git a/ipf/bootstrap.php b/ipf/bootstrap.php index 09ae581..de5f6d0 100644 --- a/ipf/bootstrap.php +++ b/ipf/bootstrap.php @@ -2,6 +2,7 @@ namespace IPF; +use IPF\Database\DatabaseProvider; use IPF\Error\ErrorPageProvider; use IPF\Template\TemplateProvider; use Pimple\Container; @@ -42,12 +43,7 @@ class BootstrapProvider implements ServiceProviderInterface $container->register(new CoreServicesProvider()); $container->register(new TemplateProvider()); $container->register(new ErrorPageProvider()); - - $container['db'] = $container->factory(function ($c) { - $config = $c['settings']->get('database'); - return \IPF_Database::connectDBAL($config); - }); - + $container->register(new DatabaseProvider()); $container->register(new CliProvider()); $container['pipeline'] = function ($c) { diff --git a/ipf/database.php b/ipf/database.php index cdabcac..ce50932 100644 --- a/ipf/database.php +++ b/ipf/database.php @@ -1,27 +1,9 @@ '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); @@ -35,99 +17,3 @@ class IPF_Database 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); - } -} diff --git a/ipf/database/logger.php b/ipf/database/logger.php new file mode 100644 index 0000000..07494d3 --- /dev/null +++ b/ipf/database/logger.php @@ -0,0 +1,40 @@ +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); + } +} diff --git a/ipf/database/provider.php b/ipf/database/provider.php new file mode 100644 index 0000000..dabdf82 --- /dev/null +++ b/ipf/database/provider.php @@ -0,0 +1,40 @@ +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); + }); + } +}