mirror of
https://github.com/dg/dibi.git
synced 2025-09-04 19:55:26 +02:00
Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
f18056a066 | ||
|
0bd222b3f1 | ||
|
9f71f39470 | ||
|
0b0d805040 | ||
|
8c761eac5c | ||
|
2f857c28d6 | ||
|
efe1cbdc20 | ||
|
21dad1d846 | ||
|
7d55fd03b0 | ||
|
294787a26e |
@@ -46,12 +46,8 @@ jobs:
|
|||||||
|
|
||||||
|
|
||||||
- stage: Static Analysis (informative)
|
- stage: Static Analysis (informative)
|
||||||
install:
|
|
||||||
# Install PHPStan
|
|
||||||
- travis_retry composer create-project phpstan/phpstan-shim temp/phpstan --no-progress
|
|
||||||
- travis_retry composer install --no-progress --prefer-dist
|
|
||||||
script:
|
script:
|
||||||
- php temp/phpstan/phpstan.phar analyse --autoload-file vendor/autoload.php --level 5 src
|
- composer run-script phpstan
|
||||||
|
|
||||||
|
|
||||||
- stage: Code Coverage
|
- stage: Code Coverage
|
||||||
|
@@ -3,7 +3,7 @@
|
|||||||
"description": "Dibi is Database Abstraction Library for PHP",
|
"description": "Dibi is Database Abstraction Library for PHP",
|
||||||
"keywords": ["database", "dbal", "mysql", "postgresql", "sqlite", "mssql", "sqlsrv", "oracle", "access", "pdo", "odbc"],
|
"keywords": ["database", "dbal", "mysql", "postgresql", "sqlite", "mssql", "sqlsrv", "oracle", "access", "pdo", "odbc"],
|
||||||
"homepage": "https://dibiphp.com",
|
"homepage": "https://dibiphp.com",
|
||||||
"license": ["BSD-3-Clause", "GPL-2.0", "GPL-3.0"],
|
"license": ["BSD-3-Clause", "GPL-2.0-only", "GPL-3.0-only"],
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
"name": "David Grudl",
|
"name": "David Grudl",
|
||||||
@@ -15,7 +15,8 @@
|
|||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"tracy/tracy": "~2.2",
|
"tracy/tracy": "~2.2",
|
||||||
"nette/tester": "~2.0"
|
"nette/tester": "~2.0",
|
||||||
|
"phpstan/phpstan": "^0.12"
|
||||||
},
|
},
|
||||||
"replace": {
|
"replace": {
|
||||||
"dg/dibi": "*"
|
"dg/dibi": "*"
|
||||||
@@ -23,6 +24,10 @@
|
|||||||
"autoload": {
|
"autoload": {
|
||||||
"classmap": ["src/"]
|
"classmap": ["src/"]
|
||||||
},
|
},
|
||||||
|
"scripts": {
|
||||||
|
"phpstan": "phpstan analyse --autoload-file vendor/autoload.php --level 5 src",
|
||||||
|
"tester": "tester tests -s"
|
||||||
|
},
|
||||||
"extra": {
|
"extra": {
|
||||||
"branch-alias": {
|
"branch-alias": {
|
||||||
"dev-master": "4.1-dev"
|
"dev-master": "4.1-dev"
|
||||||
|
@@ -20,6 +20,7 @@ $dibi = new Dibi\Connection([
|
|||||||
// enable query logging to this file
|
// enable query logging to this file
|
||||||
'profiler' => [
|
'profiler' => [
|
||||||
'file' => 'log/log.sql',
|
'file' => 'log/log.sql',
|
||||||
|
'errorsOnly' => false,
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@@ -199,6 +199,7 @@ Three special modifiers are available for LIKE:
|
|||||||
| `%like~` | the expression starts with a string
|
| `%like~` | the expression starts with a string
|
||||||
| `%~like` | the expression ends with a string
|
| `%~like` | the expression ends with a string
|
||||||
| `%~like~` | the expression contains a string
|
| `%~like~` | the expression contains a string
|
||||||
|
| `%like` | the expression matches a string
|
||||||
|
|
||||||
Search for names beginning with a string:
|
Search for names beginning with a string:
|
||||||
|
|
||||||
|
@@ -51,6 +51,7 @@ class Connection implements IConnection
|
|||||||
* - profiler (array)
|
* - profiler (array)
|
||||||
* - run (bool) => enable profiler?
|
* - run (bool) => enable profiler?
|
||||||
* - file => file to log
|
* - file => file to log
|
||||||
|
* - errorsOnly (bool) => log only errors
|
||||||
* - substitutes (array) => map of driver specific substitutes (under development)
|
* - substitutes (array) => map of driver specific substitutes (under development)
|
||||||
* - onConnect (array) => list of SQL queries to execute (by Connection::query()) after connection is established
|
* - onConnect (array) => list of SQL queries to execute (by Connection::query()) after connection is established
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
@@ -70,7 +71,8 @@ class Connection implements IConnection
|
|||||||
// profiler
|
// profiler
|
||||||
if (isset($config['profiler']['file']) && (!isset($config['profiler']['run']) || $config['profiler']['run'])) {
|
if (isset($config['profiler']['file']) && (!isset($config['profiler']['run']) || $config['profiler']['run'])) {
|
||||||
$filter = $config['profiler']['filter'] ?? Event::QUERY;
|
$filter = $config['profiler']['filter'] ?? Event::QUERY;
|
||||||
$this->onEvent[] = [new Loggers\FileLogger($config['profiler']['file'], $filter), 'logEvent'];
|
$errorsOnly = $config['profiler']['errorsOnly'] ?? false;
|
||||||
|
$this->onEvent[] = [new Loggers\FileLogger($config['profiler']['file'], $filter, $errorsOnly), 'logEvent'];
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->substitutes = new HashMap(function (string $expr) { return ":$expr:"; });
|
$this->substitutes = new HashMap(function (string $expr) { return ":$expr:"; });
|
||||||
@@ -108,6 +110,7 @@ class Connection implements IConnection
|
|||||||
{
|
{
|
||||||
if ($this->config['driver'] instanceof Driver) {
|
if ($this->config['driver'] instanceof Driver) {
|
||||||
$this->driver = $this->config['driver'];
|
$this->driver = $this->config['driver'];
|
||||||
|
$this->translator = new Translator($this);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
} elseif (is_subclass_of($this->config['driver'], Driver::class)) {
|
} elseif (is_subclass_of($this->config['driver'], Driver::class)) {
|
||||||
@@ -124,6 +127,8 @@ class Connection implements IConnection
|
|||||||
$event = $this->onEvent ? new Event($this, Event::CONNECT) : null;
|
$event = $this->onEvent ? new Event($this, Event::CONNECT) : null;
|
||||||
try {
|
try {
|
||||||
$this->driver = new $class($this->config);
|
$this->driver = new $class($this->config);
|
||||||
|
$this->translator = new Translator($this);
|
||||||
|
|
||||||
if ($event) {
|
if ($event) {
|
||||||
$this->onEvent($event->done());
|
$this->onEvent($event->done());
|
||||||
}
|
}
|
||||||
@@ -149,7 +154,7 @@ class Connection implements IConnection
|
|||||||
{
|
{
|
||||||
if ($this->driver) {
|
if ($this->driver) {
|
||||||
$this->driver->disconnect();
|
$this->driver->disconnect();
|
||||||
$this->driver = null;
|
$this->driver = $this->translator = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -250,11 +255,7 @@ class Connection implements IConnection
|
|||||||
if (!$this->driver) {
|
if (!$this->driver) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
}
|
}
|
||||||
if (!$this->translator) {
|
return (clone $this->translator)->translate($args);
|
||||||
$this->translator = new Translator($this);
|
|
||||||
}
|
|
||||||
$translator = clone $this->translator;
|
|
||||||
return $translator->translate($args);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -40,9 +40,7 @@ class FirebirdDriver implements Dibi\Driver
|
|||||||
private $inTransaction = false;
|
private $inTransaction = false;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/** @throws Dibi\NotSupportedException */
|
||||||
* @throws Dibi\NotSupportedException
|
|
||||||
*/
|
|
||||||
public function __construct(array $config)
|
public function __construct(array $config)
|
||||||
{
|
{
|
||||||
if (!extension_loaded('interbase')) {
|
if (!extension_loaded('interbase')) {
|
||||||
@@ -271,7 +269,7 @@ class FirebirdDriver implements Dibi\Driver
|
|||||||
public function escapeLike(string $value, int $pos): string
|
public function escapeLike(string $value, int $pos): string
|
||||||
{
|
{
|
||||||
$value = addcslashes($this->escapeText($value), '%_\\');
|
$value = addcslashes($this->escapeText($value), '%_\\');
|
||||||
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'") . " ESCAPE '\\'";
|
return ($pos & 1 ? "'%" : "'") . $value . ($pos & 2 ? "%'" : "'") . " ESCAPE '\\'";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -47,9 +47,7 @@ class MySqliDriver implements Dibi\Driver
|
|||||||
private $buffered;
|
private $buffered;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/** @throws Dibi\NotSupportedException */
|
||||||
* @throws Dibi\NotSupportedException
|
|
||||||
*/
|
|
||||||
public function __construct(array $config)
|
public function __construct(array $config)
|
||||||
{
|
{
|
||||||
if (!extension_loaded('mysqli')) {
|
if (!extension_loaded('mysqli')) {
|
||||||
@@ -317,7 +315,7 @@ class MySqliDriver implements Dibi\Driver
|
|||||||
public function escapeLike(string $value, int $pos): string
|
public function escapeLike(string $value, int $pos): string
|
||||||
{
|
{
|
||||||
$value = addcslashes(str_replace('\\', '\\\\', $value), "\x00\n\r\\'%_");
|
$value = addcslashes(str_replace('\\', '\\\\', $value), "\x00\n\r\\'%_");
|
||||||
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
|
return ($pos & 1 ? "'%" : "'") . $value . ($pos & 2 ? "%'" : "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -37,9 +37,7 @@ class OdbcDriver implements Dibi\Driver
|
|||||||
private $microseconds = true;
|
private $microseconds = true;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/** @throws Dibi\NotSupportedException */
|
||||||
* @throws Dibi\NotSupportedException
|
|
||||||
*/
|
|
||||||
public function __construct(array $config)
|
public function __construct(array $config)
|
||||||
{
|
{
|
||||||
if (!extension_loaded('odbc')) {
|
if (!extension_loaded('odbc')) {
|
||||||
@@ -250,7 +248,7 @@ class OdbcDriver implements Dibi\Driver
|
|||||||
public function escapeLike(string $value, int $pos): string
|
public function escapeLike(string $value, int $pos): string
|
||||||
{
|
{
|
||||||
$value = strtr($value, ["'" => "''", '%' => '[%]', '_' => '[_]', '[' => '[[]']);
|
$value = strtr($value, ["'" => "''", '%' => '[%]', '_' => '[_]', '[' => '[[]']);
|
||||||
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
|
return ($pos & 1 ? "'%" : "'") . $value . ($pos & 2 ? "%'" : "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -42,9 +42,7 @@ class OracleDriver implements Dibi\Driver
|
|||||||
private $affectedRows;
|
private $affectedRows;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/** @throws Dibi\NotSupportedException */
|
||||||
* @throws Dibi\NotSupportedException
|
|
||||||
*/
|
|
||||||
public function __construct(array $config)
|
public function __construct(array $config)
|
||||||
{
|
{
|
||||||
if (!extension_loaded('oci8')) {
|
if (!extension_loaded('oci8')) {
|
||||||
@@ -270,7 +268,7 @@ class OracleDriver implements Dibi\Driver
|
|||||||
{
|
{
|
||||||
$value = addcslashes(str_replace('\\', '\\\\', $value), "\x00\\%_");
|
$value = addcslashes(str_replace('\\', '\\\\', $value), "\x00\\%_");
|
||||||
$value = str_replace("'", "''", $value);
|
$value = str_replace("'", "''", $value);
|
||||||
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
|
return ($pos & 1 ? "'%" : "'") . $value . ($pos & 2 ? "%'" : "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -42,9 +42,7 @@ class PdoDriver implements Dibi\Driver
|
|||||||
private $serverVersion = '';
|
private $serverVersion = '';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/** @throws Dibi\NotSupportedException */
|
||||||
* @throws Dibi\NotSupportedException
|
|
||||||
*/
|
|
||||||
public function __construct(array $config)
|
public function __construct(array $config)
|
||||||
{
|
{
|
||||||
if (!extension_loaded('pdo')) {
|
if (!extension_loaded('pdo')) {
|
||||||
@@ -324,29 +322,29 @@ class PdoDriver implements Dibi\Driver
|
|||||||
switch ($this->driverName) {
|
switch ($this->driverName) {
|
||||||
case 'mysql':
|
case 'mysql':
|
||||||
$value = addcslashes(str_replace('\\', '\\\\', $value), "\x00\n\r\\'%_");
|
$value = addcslashes(str_replace('\\', '\\\\', $value), "\x00\n\r\\'%_");
|
||||||
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
|
return ($pos & 1 ? "'%" : "'") . $value . ($pos & 2 ? "%'" : "'");
|
||||||
|
|
||||||
case 'oci':
|
case 'oci':
|
||||||
$value = addcslashes(str_replace('\\', '\\\\', $value), "\x00\\%_");
|
$value = addcslashes(str_replace('\\', '\\\\', $value), "\x00\\%_");
|
||||||
$value = str_replace("'", "''", $value);
|
$value = str_replace("'", "''", $value);
|
||||||
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
|
return ($pos & 1 ? "'%" : "'") . $value . ($pos & 2 ? "%'" : "'");
|
||||||
|
|
||||||
case 'pgsql':
|
case 'pgsql':
|
||||||
$bs = substr($this->connection->quote('\\', PDO::PARAM_STR), 1, -1); // standard_conforming_strings = on/off
|
$bs = substr($this->connection->quote('\\', PDO::PARAM_STR), 1, -1); // standard_conforming_strings = on/off
|
||||||
$value = substr($this->connection->quote($value, PDO::PARAM_STR), 1, -1);
|
$value = substr($this->connection->quote($value, PDO::PARAM_STR), 1, -1);
|
||||||
$value = strtr($value, ['%' => $bs . '%', '_' => $bs . '_', '\\' => '\\\\']);
|
$value = strtr($value, ['%' => $bs . '%', '_' => $bs . '_', '\\' => '\\\\']);
|
||||||
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
|
return ($pos & 1 ? "'%" : "'") . $value . ($pos & 2 ? "%'" : "'");
|
||||||
|
|
||||||
case 'sqlite':
|
case 'sqlite':
|
||||||
$value = addcslashes(substr($this->connection->quote($value, PDO::PARAM_STR), 1, -1), '%_\\');
|
$value = addcslashes(substr($this->connection->quote($value, PDO::PARAM_STR), 1, -1), '%_\\');
|
||||||
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'") . " ESCAPE '\\'";
|
return ($pos & 1 ? "'%" : "'") . $value . ($pos & 2 ? "%'" : "'") . " ESCAPE '\\'";
|
||||||
|
|
||||||
case 'odbc':
|
case 'odbc':
|
||||||
case 'mssql':
|
case 'mssql':
|
||||||
case 'dblib':
|
case 'dblib':
|
||||||
case 'sqlsrv':
|
case 'sqlsrv':
|
||||||
$value = strtr($value, ["'" => "''", '%' => '[%]', '_' => '[_]', '[' => '[[]']);
|
$value = strtr($value, ["'" => "''", '%' => '[%]', '_' => '[_]', '[' => '[[]']);
|
||||||
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
|
return ($pos & 1 ? "'%" : "'") . $value . ($pos & 2 ? "%'" : "'");
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new Dibi\NotImplementedException;
|
throw new Dibi\NotImplementedException;
|
||||||
|
@@ -35,9 +35,7 @@ class PostgreDriver implements Dibi\Driver
|
|||||||
private $affectedRows;
|
private $affectedRows;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/** @throws Dibi\NotSupportedException */
|
||||||
* @throws Dibi\NotSupportedException
|
|
||||||
*/
|
|
||||||
public function __construct(array $config)
|
public function __construct(array $config)
|
||||||
{
|
{
|
||||||
if (!extension_loaded('pgsql')) {
|
if (!extension_loaded('pgsql')) {
|
||||||
@@ -71,7 +69,7 @@ class PostgreDriver implements Dibi\Driver
|
|||||||
if (empty($config['persistent'])) {
|
if (empty($config['persistent'])) {
|
||||||
$this->connection = pg_connect($string, PGSQL_CONNECT_FORCE_NEW);
|
$this->connection = pg_connect($string, PGSQL_CONNECT_FORCE_NEW);
|
||||||
} else {
|
} else {
|
||||||
$this->connection = pg_pconnect($string, PGSQL_CONNECT_FORCE_NEW);
|
$this->connection = pg_pconnect($string);
|
||||||
}
|
}
|
||||||
restore_error_handler();
|
restore_error_handler();
|
||||||
}
|
}
|
||||||
@@ -318,7 +316,7 @@ class PostgreDriver implements Dibi\Driver
|
|||||||
$bs = pg_escape_string($this->connection, '\\'); // standard_conforming_strings = on/off
|
$bs = pg_escape_string($this->connection, '\\'); // standard_conforming_strings = on/off
|
||||||
$value = pg_escape_string($this->connection, $value);
|
$value = pg_escape_string($this->connection, $value);
|
||||||
$value = strtr($value, ['%' => $bs . '%', '_' => $bs . '_', '\\' => '\\\\']);
|
$value = strtr($value, ['%' => $bs . '%', '_' => $bs . '_', '\\' => '\\\\']);
|
||||||
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
|
return ($pos & 1 ? "'%" : "'") . $value . ($pos & 2 ? "%'" : "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -37,9 +37,7 @@ class SqliteDriver implements Dibi\Driver
|
|||||||
private $fmtDateTime;
|
private $fmtDateTime;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/** @throws Dibi\NotSupportedException */
|
||||||
* @throws Dibi\NotSupportedException
|
|
||||||
*/
|
|
||||||
public function __construct(array $config)
|
public function __construct(array $config)
|
||||||
{
|
{
|
||||||
if (!extension_loaded('sqlite3')) {
|
if (!extension_loaded('sqlite3')) {
|
||||||
@@ -254,7 +252,7 @@ class SqliteDriver implements Dibi\Driver
|
|||||||
public function escapeLike(string $value, int $pos): string
|
public function escapeLike(string $value, int $pos): string
|
||||||
{
|
{
|
||||||
$value = addcslashes($this->connection->escapeString($value), '%_\\');
|
$value = addcslashes($this->connection->escapeString($value), '%_\\');
|
||||||
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'") . " ESCAPE '\\'";
|
return ($pos & 1 ? "'%" : "'") . $value . ($pos & 2 ? "%'" : "'") . " ESCAPE '\\'";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -39,9 +39,7 @@ class SqlsrvDriver implements Dibi\Driver
|
|||||||
private $version = '';
|
private $version = '';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/** @throws Dibi\NotSupportedException */
|
||||||
* @throws Dibi\NotSupportedException
|
|
||||||
*/
|
|
||||||
public function __construct(array $config)
|
public function __construct(array $config)
|
||||||
{
|
{
|
||||||
if (!extension_loaded('sqlsrv')) {
|
if (!extension_loaded('sqlsrv')) {
|
||||||
@@ -243,7 +241,7 @@ class SqlsrvDriver implements Dibi\Driver
|
|||||||
public function escapeLike(string $value, int $pos): string
|
public function escapeLike(string $value, int $pos): string
|
||||||
{
|
{
|
||||||
$value = strtr($value, ["'" => "''", '%' => '[%]', '_' => '[_]', '[' => '[[]']);
|
$value = strtr($value, ["'" => "''", '%' => '[%]', '_' => '[_]', '[' => '[[]']);
|
||||||
return ($pos <= 0 ? "'%" : "'") . $value . ($pos >= 0 ? "%'" : "'");
|
return ($pos & 1 ? "'%" : "'") . $value . ($pos & 2 ? "%'" : "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -201,9 +201,7 @@ class Helpers
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/** @internal */
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
public static function getTypeCache(): HashMap
|
public static function getTypeCache(): HashMap
|
||||||
{
|
{
|
||||||
if (self::$types === null) {
|
if (self::$types === null) {
|
||||||
@@ -279,18 +277,14 @@ class Helpers
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/** @internal */
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
public static function false2Null($val)
|
public static function false2Null($val)
|
||||||
{
|
{
|
||||||
return $val === false ? null : $val;
|
return $val === false ? null : $val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/** @internal */
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
public static function intVal($value): int
|
public static function intVal($value): int
|
||||||
{
|
{
|
||||||
if (is_int($value)) {
|
if (is_int($value)) {
|
||||||
|
@@ -25,11 +25,15 @@ class FileLogger
|
|||||||
/** @var int */
|
/** @var int */
|
||||||
public $filter;
|
public $filter;
|
||||||
|
|
||||||
|
/** @var bool */
|
||||||
|
private $errorsOnly;
|
||||||
|
|
||||||
public function __construct(string $file, int $filter = null)
|
|
||||||
|
public function __construct(string $file, int $filter = null, bool $errorsOnly = false)
|
||||||
{
|
{
|
||||||
$this->file = $file;
|
$this->file = $file;
|
||||||
$this->filter = $filter ?: Dibi\Event::QUERY;
|
$this->filter = $filter ?: Dibi\Event::QUERY;
|
||||||
|
$this->errorsOnly = $errorsOnly;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -38,39 +42,41 @@ class FileLogger
|
|||||||
*/
|
*/
|
||||||
public function logEvent(Dibi\Event $event): void
|
public function logEvent(Dibi\Event $event): void
|
||||||
{
|
{
|
||||||
if (($event->type & $this->filter) === 0) {
|
if (
|
||||||
|
(($event->type & $this->filter) === 0)
|
||||||
|
|| ($this->errorsOnly === true && !$event->result instanceof \Exception)
|
||||||
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$handle = fopen($this->file, 'a');
|
|
||||||
if (!$handle) {
|
|
||||||
return; // or throw exception?
|
|
||||||
}
|
|
||||||
flock($handle, LOCK_EX);
|
|
||||||
|
|
||||||
if ($event->result instanceof \Exception) {
|
if ($event->result instanceof \Exception) {
|
||||||
$message = $event->result->getMessage();
|
$message = $event->result->getMessage();
|
||||||
if ($code = $event->result->getCode()) {
|
if ($code = $event->result->getCode()) {
|
||||||
$message = "[$code] $message";
|
$message = "[$code] $message";
|
||||||
}
|
}
|
||||||
fwrite($handle,
|
$this->writeToFile(
|
||||||
|
$event,
|
||||||
"ERROR: $message"
|
"ERROR: $message"
|
||||||
. "\n-- SQL: " . $event->sql
|
. "\n-- SQL: " . $event->sql
|
||||||
. "\n-- driver: " . $event->connection->getConfig('driver') . '/' . $event->connection->getConfig('name')
|
|
||||||
. ";\n-- " . date('Y-m-d H:i:s')
|
|
||||||
. "\n\n"
|
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
fwrite($handle,
|
$this->writeToFile(
|
||||||
|
$event,
|
||||||
'OK: ' . $event->sql
|
'OK: ' . $event->sql
|
||||||
. ($event->count ? ";\n-- rows: " . $event->count : '')
|
. ($event->count ? ";\n-- rows: " . $event->count : '')
|
||||||
. "\n-- takes: " . sprintf('%0.3f ms', $event->time * 1000)
|
. "\n-- takes: " . sprintf('%0.3f ms', $event->time * 1000)
|
||||||
. "\n-- source: " . implode(':', $event->source)
|
. "\n-- source: " . implode(':', $event->source)
|
||||||
. "\n-- driver: " . $event->connection->getConfig('driver') . '/' . $event->connection->getConfig('name')
|
|
||||||
. "\n-- " . date('Y-m-d H:i:s')
|
|
||||||
. "\n\n"
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
fclose($handle);
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private function writeToFile(Dibi\Event $event, string $message): void
|
||||||
|
{
|
||||||
|
$message .=
|
||||||
|
"\n-- driver: " . $event->connection->getConfig('driver') . '/' . $event->connection->getConfig('name')
|
||||||
|
. "\n-- " . date('Y-m-d H:i:s')
|
||||||
|
. "\n\n";
|
||||||
|
file_put_contents($this->file, $message, FILE_APPEND | LOCK_EX);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -106,18 +106,14 @@ class Column
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/** @return mixed */
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getDefault()
|
public function getDefault()
|
||||||
{
|
{
|
||||||
return $this->info['default'] ?? null;
|
return $this->info['default'] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/** @return mixed */
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getVendorInfo(string $key)
|
public function getVendorInfo(string $key)
|
||||||
{
|
{
|
||||||
return $this->info['vendor'][$key] ?? null;
|
return $this->info['vendor'][$key] ?? null;
|
||||||
|
@@ -46,9 +46,7 @@ class Database
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/** @return Table[] */
|
||||||
* @return Table[]
|
|
||||||
*/
|
|
||||||
public function getTables(): array
|
public function getTables(): array
|
||||||
{
|
{
|
||||||
$this->init();
|
$this->init();
|
||||||
@@ -56,9 +54,7 @@ class Database
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/** @return string[] */
|
||||||
* @return string[]
|
|
||||||
*/
|
|
||||||
public function getTableNames(): array
|
public function getTableNames(): array
|
||||||
{
|
{
|
||||||
$this->init();
|
$this->init();
|
||||||
|
@@ -38,9 +38,7 @@ class Result
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/** @return Column[] */
|
||||||
* @return Column[]
|
|
||||||
*/
|
|
||||||
public function getColumns(): array
|
public function getColumns(): array
|
||||||
{
|
{
|
||||||
$this->initColumns();
|
$this->initColumns();
|
||||||
@@ -48,9 +46,7 @@ class Result
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/** @return string[] */
|
||||||
* @return string[]
|
|
||||||
*/
|
|
||||||
public function getColumnNames(bool $fullNames = false): array
|
public function getColumnNames(bool $fullNames = false): array
|
||||||
{
|
{
|
||||||
$this->initColumns();
|
$this->initColumns();
|
||||||
|
@@ -69,9 +69,7 @@ class Table
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/** @return Column[] */
|
||||||
* @return Column[]
|
|
||||||
*/
|
|
||||||
public function getColumns(): array
|
public function getColumns(): array
|
||||||
{
|
{
|
||||||
$this->initColumns();
|
$this->initColumns();
|
||||||
@@ -79,9 +77,7 @@ class Table
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/** @return string[] */
|
||||||
* @return string[]
|
|
||||||
*/
|
|
||||||
public function getColumnNames(): array
|
public function getColumnNames(): array
|
||||||
{
|
{
|
||||||
$this->initColumns();
|
$this->initColumns();
|
||||||
@@ -113,9 +109,7 @@ class Table
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/** @return ForeignKey[] */
|
||||||
* @return ForeignKey[]
|
|
||||||
*/
|
|
||||||
public function getForeignKeys(): array
|
public function getForeignKeys(): array
|
||||||
{
|
{
|
||||||
$this->initForeignKeys();
|
$this->initForeignKeys();
|
||||||
@@ -123,9 +117,7 @@ class Table
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/** @return Index[] */
|
||||||
* @return Index[]
|
|
||||||
*/
|
|
||||||
public function getIndexes(): array
|
public function getIndexes(): array
|
||||||
{
|
{
|
||||||
$this->initIndexes();
|
$this->initIndexes();
|
||||||
|
@@ -296,9 +296,7 @@ class Result implements IDataSource
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/** @deprecated */
|
||||||
* @deprecated
|
|
||||||
*/
|
|
||||||
private function oldFetchAssoc(string $assoc)
|
private function oldFetchAssoc(string $assoc)
|
||||||
{
|
{
|
||||||
$this->seek(0);
|
$this->seek(0);
|
||||||
@@ -569,9 +567,7 @@ class Result implements IDataSource
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/** @return Reflection\Column[] */
|
||||||
* @return Reflection\Column[]
|
|
||||||
*/
|
|
||||||
final public function getColumns(): array
|
final public function getColumns(): array
|
||||||
{
|
{
|
||||||
return $this->getInfo()->getColumns();
|
return $this->getInfo()->getColumns();
|
||||||
|
@@ -415,12 +415,15 @@ final class Translator
|
|||||||
return (string) $value;
|
return (string) $value;
|
||||||
|
|
||||||
case 'like~': // LIKE string%
|
case 'like~': // LIKE string%
|
||||||
return $this->driver->escapeLike($value, 1);
|
return $this->driver->escapeLike($value, 2);
|
||||||
|
|
||||||
case '~like': // LIKE %string
|
case '~like': // LIKE %string
|
||||||
return $this->driver->escapeLike($value, -1);
|
return $this->driver->escapeLike($value, 1);
|
||||||
|
|
||||||
case '~like~': // LIKE %string%
|
case '~like~': // LIKE %string%
|
||||||
|
return $this->driver->escapeLike($value, 3);
|
||||||
|
|
||||||
|
case 'like': // LIKE string
|
||||||
return $this->driver->escapeLike($value, 0);
|
return $this->driver->escapeLike($value, 0);
|
||||||
|
|
||||||
case 'and':
|
case 'and':
|
||||||
|
@@ -44,7 +44,7 @@ class dibi
|
|||||||
|
|
||||||
/** version */
|
/** version */
|
||||||
public const
|
public const
|
||||||
VERSION = '4.1.0';
|
VERSION = '4.1.2';
|
||||||
|
|
||||||
/** sorting order */
|
/** sorting order */
|
||||||
public const
|
public const
|
||||||
|
@@ -52,6 +52,17 @@ test(function () use ($config) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test(function () use ($config) {
|
||||||
|
$conn = new Connection($config);
|
||||||
|
Assert::equal('hello', $conn->query('SELECT %s', 'hello')->fetchSingle());
|
||||||
|
|
||||||
|
$conn->disconnect();
|
||||||
|
|
||||||
|
$conn->connect();
|
||||||
|
Assert::equal('hello', $conn->query('SELECT %s', 'hello')->fetchSingle());
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
test(function () use ($config) {
|
test(function () use ($config) {
|
||||||
Assert::exception(function () use ($config) {
|
Assert::exception(function () use ($config) {
|
||||||
new Connection($config + ['onConnect' => '']);
|
new Connection($config + ['onConnect' => '']);
|
||||||
|
@@ -71,3 +71,9 @@ Assert::truthy($conn->fetchSingle('SELECT ? LIKE %~like~', 'baa', 'aa'));
|
|||||||
Assert::truthy($conn->fetchSingle('SELECT ? LIKE %~like~', 'aab', 'aa'));
|
Assert::truthy($conn->fetchSingle('SELECT ? LIKE %~like~', 'aab', 'aa'));
|
||||||
Assert::falsey($conn->fetchSingle('SELECT ? LIKE %~like~', 'bba', '%a'));
|
Assert::falsey($conn->fetchSingle('SELECT ? LIKE %~like~', 'bba', '%a'));
|
||||||
Assert::truthy($conn->fetchSingle('SELECT ? LIKE %~like~', 'b%a', '%a'));
|
Assert::truthy($conn->fetchSingle('SELECT ? LIKE %~like~', 'b%a', '%a'));
|
||||||
|
|
||||||
|
|
||||||
|
// matches
|
||||||
|
Assert::truthy($conn->fetchSingle('SELECT ? LIKE %like', 'a', 'a'));
|
||||||
|
Assert::falsey($conn->fetchSingle('SELECT ? LIKE %like', 'a', 'aa'));
|
||||||
|
Assert::falsey($conn->fetchSingle('SELECT ? LIKE %like', 'a', 'b'));
|
||||||
|
Reference in New Issue
Block a user