diff --git a/DatabasePdoPgsql.php b/DatabasePdoPgsql.php index 5de80da..e9208bb 100644 --- a/DatabasePdoPgsql.php +++ b/DatabasePdoPgsql.php @@ -3,8 +3,8 @@ /** * PDO/PostgreSQL wrapper with (mostly) DatabaseMySQL interface :) * Select builder is inspired by MediaWiki's one. - * Version: 2019-12-28 - * (c) Vitaliy Filippov, 2015-2019 + * Version: 2020-02-18 + * (c) Vitaliy Filippov, 2015-2020 */ if (!defined('MS_HASH')) @@ -48,6 +48,7 @@ class DatabasePdoPgsql implements Database var $beginHook; var $ondestroy = 'commit'; + var $pgVersion; var $queryCount = 0; var $link; var $transactions = array(); @@ -67,7 +68,7 @@ class DatabasePdoPgsql implements Database * username Username * password Password * tableNames Table name mappings (virtual => real) - * queryLogFile Path to query log file + * queryLogFile Path to query log file or 'error_log' to log via error_log() * reconnect Whether to reconnect on idle timeout [true] * autoBegin Whether to automatically begin a transaction of first query [false] * beginHook Callback to call when starting a transaction @@ -124,6 +125,7 @@ class DatabasePdoPgsql implements Database } try { + $this->pgVersion = NULL; $this->link = new PDO($str, $this->username, $this->password, array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, @@ -139,6 +141,16 @@ class DatabasePdoPgsql implements Database $this->transactions = array(); } + function getPgVersion() + { + if (!$this->pgVersion) + { + // Версия в формате целого числа, типа 90600 + $this->pgVersion = $this->select('show server_version_num', MS_VALUE); + } + return $this->pgVersion; + } + function getDBName() { return $this->dbname; @@ -179,16 +191,22 @@ class DatabasePdoPgsql implements Database if ($this->autoBegin && !$this->transactions) $this->begin(); $this->queryCount++; - if ($this->queryLogFile) - { - if (strlen($sql) <= 100) - $sql = preg_replace("/\n\s*/", ' ', $sql); - $t = substr(floatval(microtime()), 1, 7); - file_put_contents($this->queryLogFile, date("[Y-m-d H:i:s$t] ").$sql."\n", FILE_APPEND); - } retry: try { + if ($this->queryLogFile) + $start = microtime(true); $r = $this->link->query($sql); + if ($this->queryLogFile) + { + $end = microtime(true); + if (strlen($sql) <= 100) + $sql = preg_replace("/\n\s*/", ' ', $sql); + $t = substr($start-intval($start), 1); + if ($this->queryLogFile === 'error_log') + error_log(sprintf("%.03f s ", $end-$start).$sql); + else + file_put_contents($this->queryLogFile, date("[Y-m-d H:i:s$t] ").sprintf("%.03f s ", $end-$start).$sql."\n", FILE_APPEND); + } } catch (Exception $e) { @@ -198,7 +216,12 @@ class DatabasePdoPgsql implements Database goto retry; } if ($this->queryLogFile) - file_put_contents($this->queryLogFile, date("[Y-m-d H:i:s] ")."Error: $e\n---- Query: $sql\n", FILE_APPEND); + { + if ($this->queryLogFile === 'error_log') + error_log("Error: $e\n---- Query: $sql"); + else + file_put_contents($this->queryLogFile, date("[Y-m-d H:i:s] ")."Error: $e\n---- Query: $sql\n", FILE_APPEND); + } throw $e; } return $r;