mirror of
https://github.com/dg/dibi.git
synced 2025-08-30 17:29:53 +02:00
Compare commits
11 Commits
v4.1.2
...
revert-363
Author | SHA1 | Date | |
---|---|---|---|
|
dbbf0ca673 | ||
|
1bdf6e93d0 | ||
|
ed2a827419 | ||
|
e46be6cee6 | ||
|
0f69d5d32c | ||
|
e826e3a719 | ||
|
6eac117f5f | ||
|
2a2c814b0a | ||
|
dfab3d711c | ||
|
34e16031f7 | ||
|
73160e9418 |
@@ -32,6 +32,7 @@ after_failure:
|
||||
jobs:
|
||||
include:
|
||||
- name: Nette Code Checker
|
||||
php: 7.4
|
||||
install:
|
||||
- travis_retry composer create-project nette/code-checker temp/code-checker ^3 --no-progress
|
||||
script:
|
||||
@@ -39,6 +40,7 @@ jobs:
|
||||
|
||||
|
||||
- name: Nette Coding Standard
|
||||
php: 7.4
|
||||
install:
|
||||
- travis_retry composer create-project nette/coding-standard temp/coding-standard ^2 --no-progress
|
||||
script:
|
||||
@@ -46,11 +48,13 @@ jobs:
|
||||
|
||||
|
||||
- stage: Static Analysis (informative)
|
||||
php: 7.4
|
||||
script:
|
||||
- composer run-script phpstan
|
||||
|
||||
|
||||
- stage: Code Coverage
|
||||
php: 7.4
|
||||
script:
|
||||
- vendor/bin/tester -p phpdbg tests -s --coverage ./coverage.xml --coverage-src ./src
|
||||
after_script:
|
||||
|
@@ -25,7 +25,7 @@
|
||||
"classmap": ["src/"]
|
||||
},
|
||||
"scripts": {
|
||||
"phpstan": "phpstan analyse --autoload-file vendor/autoload.php --level 5 src",
|
||||
"phpstan": "phpstan analyse --autoload-file vendor/autoload.php --level 5 --configuration tests/phpstan.neon src",
|
||||
"tester": "tester tests -s"
|
||||
},
|
||||
"extra": {
|
||||
|
@@ -75,6 +75,8 @@ In the event of a connection error, it throws `Dibi\Exception`.
|
||||
|
||||
We query the database queries by the method `query()` which returns `Dibi\Result`. Rows are objects `Dibi\Row`.
|
||||
|
||||
You can try all the examples [online at the playground](https://repl.it/@DavidGrudl/dibi-playground).
|
||||
|
||||
```php
|
||||
$result = $database->query('SELECT * FROM users');
|
||||
|
||||
@@ -183,7 +185,7 @@ $result = $database->query('SELECT * FROM users WHERE id IN (%i)', $ids);
|
||||
// SELECT * FROM users WHERE id IN (10, 20, 30)
|
||||
```
|
||||
|
||||
The modifier '%n' is used if the table or column name is a variable. (Beware, do not allow the user to manipulate the content of such a variable):
|
||||
The modifier `%n` is used if the table or column name is a variable. (Beware, do not allow the user to manipulate the content of such a variable):
|
||||
|
||||
```php
|
||||
$table = 'blog.users';
|
||||
|
219
src/Dibi/Drivers/DummyDriver.php
Normal file
219
src/Dibi/Drivers/DummyDriver.php
Normal file
@@ -0,0 +1,219 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
|
||||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dibi\Drivers;
|
||||
|
||||
use Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* The dummy driver for testing purposes.
|
||||
*/
|
||||
class DummyDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
public function disconnect(): void
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public function query(string $sql): ?Dibi\ResultDriver
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public function getAffectedRows(): ?int
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public function getInsertId(?string $sequence): ?int
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public function begin(string $savepoint = null): void
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public function commit(string $savepoint = null): void
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public function rollback(string $savepoint = null): void
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public function getResource()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the connection reflector.
|
||||
*/
|
||||
public function getReflector(): Dibi\Reflector
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/********************* SQL ****************d*g**/
|
||||
|
||||
|
||||
/**
|
||||
* Encodes data for use in a SQL statement.
|
||||
*/
|
||||
public function escapeText(string $value): string
|
||||
{
|
||||
return "'" . str_replace("'", "''", $value) . "'";
|
||||
}
|
||||
|
||||
|
||||
public function escapeBinary(string $value): string
|
||||
{
|
||||
return "N'" . str_replace("'", "''", $value) . "'";
|
||||
}
|
||||
|
||||
|
||||
public function escapeIdentifier(string $value): string
|
||||
{
|
||||
return '[' . strtr($value, '[]', ' ') . ']';
|
||||
}
|
||||
|
||||
|
||||
public function escapeBool(bool $value): string
|
||||
{
|
||||
return $value ? '1' : '0';
|
||||
}
|
||||
|
||||
|
||||
public function escapeDate(\DateTimeInterface $value): string
|
||||
{
|
||||
return $value->format("'Y-m-d'");
|
||||
}
|
||||
|
||||
|
||||
public function escapeDateTime(\DateTimeInterface $value): string
|
||||
{
|
||||
return $value->format("'Y-m-d H:i:s.u'");
|
||||
}
|
||||
|
||||
|
||||
public function escapeDateInterval(\DateInterval $value): string
|
||||
{
|
||||
throw new Dibi\NotImplementedException;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Encodes string for use in a LIKE statement.
|
||||
*/
|
||||
public function escapeLike(string $value, int $pos): string
|
||||
{
|
||||
$value = strtr($value, ["'" => "''", '%' => '[%]', '_' => '[_]', '[' => '[[]']);
|
||||
return ($pos & 1 ? "'%" : "'") . $value . ($pos & 2 ? "%'" : "'");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Injects LIMIT/OFFSET to the SQL query.
|
||||
*/
|
||||
public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
|
||||
{
|
||||
if ($limit < 0 || $offset < 0) {
|
||||
throw new Dibi\NotSupportedException('Negative offset or limit.');
|
||||
|
||||
} elseif ($limit !== null || $offset) {
|
||||
$sql .= ' LIMIT ' . ($limit ?? '-1')
|
||||
. ($offset ? ' OFFSET ' . $offset : '');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/********************* Result ****************d*g**/
|
||||
|
||||
|
||||
public function getRowCount(): int
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
public function fetch(bool $assoc): ?array
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public function seek(int $row): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public function free(): void
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public function getResultResource()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public function getResultColumns(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decodes data from result set.
|
||||
*/
|
||||
public function unescapeBinary(string $value): string
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
|
||||
/********************* Reflector ****************d*g**/
|
||||
|
||||
|
||||
public function getTables(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
public function getColumns(string $table): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
public function getIndexes(string $table): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
public function getForeignKeys(string $table): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
@@ -91,7 +91,7 @@ class MySqliDriver implements Dibi\Driver
|
||||
@$this->connection->real_connect( // intentionally @
|
||||
(empty($config['persistent']) ? '' : 'p:') . $config['host'],
|
||||
$config['username'],
|
||||
$config['password'],
|
||||
$config['password'] ?? '',
|
||||
$config['database'] ?? '',
|
||||
$config['port'] ?? 0,
|
||||
$config['socket'],
|
||||
|
@@ -96,7 +96,7 @@ class SqliteResult implements Dibi\ResultDriver
|
||||
'name' => $this->resultSet->columnName($i),
|
||||
'table' => null,
|
||||
'fullname' => $this->resultSet->columnName($i),
|
||||
'nativetype' => $types[$this->resultSet->columnType($i)],
|
||||
'nativetype' => $types[$this->resultSet->columnType($i)] ?? null, // buggy in PHP 7.4.4 & 7.3.16, bug 79414
|
||||
];
|
||||
}
|
||||
return $columns;
|
||||
|
@@ -242,6 +242,9 @@ class Result implements IDataSource
|
||||
|
||||
$data = null;
|
||||
$assoc = preg_split('#(\[\]|->|=|\|)#', $assoc, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
|
||||
if (!$assoc) {
|
||||
throw new \InvalidArgumentException("Invalid descriptor '$assoc'.");
|
||||
}
|
||||
|
||||
// check columns
|
||||
foreach ($assoc as $as) {
|
||||
@@ -282,7 +285,7 @@ class Result implements IDataSource
|
||||
}
|
||||
|
||||
} elseif ($as !== '|') { // associative-array node
|
||||
$x = &$x[$row->$as];
|
||||
$x = &$x[(string) $row->$as];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -348,7 +351,7 @@ class Result implements IDataSource
|
||||
}
|
||||
|
||||
} else { // associative-array node
|
||||
$x = &$x[$row->$as];
|
||||
$x = &$x[(string) $row->$as];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -450,6 +453,7 @@ class Result implements IDataSource
|
||||
continue;
|
||||
}
|
||||
$value = $row[$key];
|
||||
|
||||
if ($type === Type::TEXT) {
|
||||
$row[$key] = (string) $value;
|
||||
|
||||
@@ -499,6 +503,12 @@ class Result implements IDataSource
|
||||
} else {
|
||||
$row[$key] = json_decode($value, $this->formats[$type] === 'array');
|
||||
}
|
||||
|
||||
} elseif ($type === null) {
|
||||
$row[$key] = $value;
|
||||
|
||||
} else {
|
||||
throw new \RuntimeException('Unexpected type ' . $type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -44,7 +44,7 @@ class dibi
|
||||
|
||||
/** version */
|
||||
public const
|
||||
VERSION = '4.1.2';
|
||||
VERSION = '4.1.3';
|
||||
|
||||
/** sorting order */
|
||||
public const
|
||||
|
4
tests/phpstan.neon
Normal file
4
tests/phpstan.neon
Normal file
@@ -0,0 +1,4 @@
|
||||
parameters:
|
||||
ignoreErrors:
|
||||
# The namespace is referenced, not the class.
|
||||
- '#Class dibi referenced with incorrect case: Dibi#'
|
Reference in New Issue
Block a user