mirror of
https://github.com/dg/dibi.git
synced 2025-09-02 10:32:33 +02:00
Compare commits
21 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
fc5e7db484 | ||
|
44b420f70d | ||
|
4689101b88 | ||
|
db16b9e87a | ||
|
803c9d539c | ||
|
e137dfa28b | ||
|
9651835f5b | ||
|
2cbebc02c4 | ||
|
4d647c2aed | ||
|
5c5838aee4 | ||
|
7df72fd6cf | ||
|
219882a962 | ||
|
7e127f5914 | ||
|
79f841ec90 | ||
|
69eaa71fde | ||
|
f895493016 | ||
|
19172c801e | ||
|
2b5683d0f2 | ||
|
76593b7da4 | ||
|
dd2fd654be | ||
|
12cbbb3140 |
29
.travis.yml
29
.travis.yml
@@ -2,6 +2,12 @@ language: php
|
||||
php:
|
||||
- 7.1
|
||||
- 7.2
|
||||
- 7.3
|
||||
- 7.4
|
||||
|
||||
services:
|
||||
- mysql
|
||||
- postgresql
|
||||
|
||||
before_install:
|
||||
# turn off XDebug
|
||||
@@ -25,18 +31,29 @@ after_failure:
|
||||
|
||||
jobs:
|
||||
include:
|
||||
- stage: Code Standard Checker
|
||||
php: 7.1
|
||||
- name: Nette Code Checker
|
||||
install:
|
||||
# Install Nette Code Checker
|
||||
- travis_retry composer create-project nette/code-checker temp/code-checker ^3 --no-progress
|
||||
# Install Nette Coding Standard
|
||||
- travis_retry composer create-project nette/coding-standard temp/coding-standard ^2 --no-progress
|
||||
script:
|
||||
- php temp/code-checker/code-checker --strict-types
|
||||
|
||||
|
||||
- name: Nette Coding Standard
|
||||
install:
|
||||
- travis_retry composer create-project nette/coding-standard temp/coding-standard ^2 --no-progress
|
||||
script:
|
||||
- php temp/coding-standard/ecs check src tests examples --config tests/coding-standard.yml
|
||||
|
||||
|
||||
- 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:
|
||||
- php temp/phpstan/phpstan.phar analyse --autoload-file vendor/autoload.php --level 5 src
|
||||
|
||||
|
||||
- stage: Code Coverage
|
||||
script:
|
||||
- vendor/bin/tester -p phpdbg tests -s --coverage ./coverage.xml --coverage-src ./src
|
||||
@@ -46,7 +63,7 @@ jobs:
|
||||
|
||||
|
||||
allow_failures:
|
||||
- php: 7.2
|
||||
- stage: Static Analysis (informative)
|
||||
- stage: Code Coverage
|
||||
|
||||
|
||||
|
@@ -26,7 +26,7 @@ Install Dibi via Composer:
|
||||
composer require dibi/dibi
|
||||
```
|
||||
|
||||
The Dibi 4.0 requires PHP version 7.1 and supports PHP up to 7.2. Older Dibi 3.x requires PHP 5.4 and supports PHP up to 7.2.
|
||||
The Dibi 4.0 requires PHP version 7.1 and supports PHP up to 7.4.
|
||||
|
||||
|
||||
Usage
|
||||
@@ -110,7 +110,7 @@ $ids = [10, 20, 30];
|
||||
$result = $database->query('SELECT * FROM users WHERE id IN (?)', $ids);
|
||||
```
|
||||
|
||||
**WARNING, never concencate parameters to SQL, the vulnerability would arise [SQL injection](https://en.wikipedia.org/wiki/SQL_injection)**
|
||||
**WARNING: Never concatenate parameters to SQL. It would create a [SQL injection](https://en.wikipedia.org/wiki/SQL_injection)** vulnerability.
|
||||
```
|
||||
$result = $database->query('SELECT * FROM users WHERE id = ' . $id); // BAD!!!
|
||||
```
|
||||
@@ -147,7 +147,7 @@ $name = $database->fetchSingle('SELECT name FROM users WHERE id = ?', $id);
|
||||
|
||||
### Modifiers
|
||||
|
||||
In addition to the `?` wild char, we can also use modifiers:
|
||||
In addition to the `?` wildcard char, we can also use modifiers:
|
||||
|
||||
| modifier | description
|
||||
|----------|-----
|
||||
@@ -161,6 +161,7 @@ In addition to the `?` wild char, we can also use modifiers:
|
||||
| %d | date (accepts DateTime, string or UNIX timestamp)
|
||||
| %dt | datetime (accepts DateTime, string or UNIX timestamp)
|
||||
| %n | identifier, ie the name of the table or column
|
||||
| %N | identifier, treats period as a common character, ie alias or a database name (`%n AS %N` or `DROP DATABASE %N`)
|
||||
| %SQL | SQL - directly inserts into SQL (the alternative is Dibi\Literal)
|
||||
| %ex | SQL expression or array of expressions
|
||||
| %lmt | special - adds LIMIT to the query
|
||||
|
@@ -119,6 +119,7 @@ class Connection implements IConnection
|
||||
{
|
||||
if ($this->config['driver'] instanceof Driver) {
|
||||
$this->driver = $this->config['driver'];
|
||||
$this->translator = new Translator($this);
|
||||
return;
|
||||
|
||||
} elseif (is_subclass_of($this->config['driver'], Driver::class)) {
|
||||
@@ -135,6 +136,8 @@ class Connection implements IConnection
|
||||
$event = $this->onEvent ? new Event($this, Event::CONNECT) : null;
|
||||
try {
|
||||
$this->driver = new $class($this->config);
|
||||
$this->translator = new Translator($this);
|
||||
|
||||
if ($event) {
|
||||
$this->onEvent($event->done());
|
||||
}
|
||||
@@ -160,7 +163,7 @@ class Connection implements IConnection
|
||||
{
|
||||
if ($this->driver) {
|
||||
$this->driver->disconnect();
|
||||
$this->driver = null;
|
||||
$this->driver = $this->translator = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -261,11 +264,7 @@ class Connection implements IConnection
|
||||
if (!$this->driver) {
|
||||
$this->connect();
|
||||
}
|
||||
if (!$this->translator) {
|
||||
$this->translator = new Translator($this);
|
||||
}
|
||||
$translator = clone $this->translator;
|
||||
return $translator->translate($args);
|
||||
return (clone $this->translator)->translate($args);
|
||||
}
|
||||
|
||||
|
||||
|
@@ -93,7 +93,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'],
|
||||
|
@@ -176,8 +176,11 @@ class PostgreReflector implements Dibi\Reflector
|
||||
$indexes[$row['relname']]['name'] = $row['relname'];
|
||||
$indexes[$row['relname']]['unique'] = $row['indisunique'] === 't' || $row['indisunique'] === true;
|
||||
$indexes[$row['relname']]['primary'] = $row['indisprimary'] === 't' || $row['indisprimary'] === true;
|
||||
$indexes[$row['relname']]['columns'] = [];
|
||||
foreach (explode(' ', $row['indkey']) as $index) {
|
||||
$indexes[$row['relname']]['columns'][] = $columns[$index];
|
||||
if (isset($columns[$index])) {
|
||||
$indexes[$row['relname']]['columns'][] = $columns[$index];
|
||||
}
|
||||
}
|
||||
}
|
||||
return array_values($indexes);
|
||||
|
@@ -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;
|
||||
|
@@ -65,11 +65,13 @@ class SqlsrvDriver implements Dibi\Driver
|
||||
$options['UID'] = (string) $options['UID'];
|
||||
$options['Database'] = (string) $options['Database'];
|
||||
|
||||
sqlsrv_configure('WarningsReturnAsErrors', 0);
|
||||
$this->connection = sqlsrv_connect($config['host'], $options);
|
||||
sqlsrv_configure('WarningsReturnAsErrors', 1);
|
||||
}
|
||||
|
||||
if (!is_resource($this->connection)) {
|
||||
$info = sqlsrv_errors();
|
||||
$info = sqlsrv_errors(SQLSRV_ERR_ERRORS);
|
||||
throw new Dibi\DriverException($info[0]['message'], $info[0]['code']);
|
||||
}
|
||||
$this->version = sqlsrv_server_info($this->connection)['SQLServerVersion'];
|
||||
|
@@ -30,6 +30,7 @@ namespace Dibi;
|
||||
* @method Fluent as(...$field)
|
||||
* @method Fluent on(...$cond)
|
||||
* @method Fluent and(...$cond)
|
||||
* @method Fluent or(...$cond)
|
||||
* @method Fluent using(...$cond)
|
||||
* @method Fluent asc()
|
||||
* @method Fluent desc()
|
||||
@@ -422,7 +423,7 @@ class Fluent implements IDataSource
|
||||
if ($clause === null) {
|
||||
$data = $this->clauses;
|
||||
if ($this->command === 'SELECT' && ($data['LIMIT'] || $data['OFFSET'])) {
|
||||
$args = array_merge(['%lmt %ofs', $data['LIMIT'][0], $data['OFFSET'][0]], $args);
|
||||
$args = array_merge(['%lmt %ofs', $data['LIMIT'][0] ?? null, $data['OFFSET'][0] ?? null], $args);
|
||||
unset($data['LIMIT'], $data['OFFSET']);
|
||||
}
|
||||
|
||||
|
@@ -282,7 +282,7 @@ class Result implements IDataSource
|
||||
}
|
||||
|
||||
} elseif ($as !== '|') { // associative-array node
|
||||
$x = &$x[$row->$as];
|
||||
$x = &$x[(string) $row->$as];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -350,7 +350,7 @@ class Result implements IDataSource
|
||||
}
|
||||
|
||||
} else { // associative-array node
|
||||
$x = &$x[$row->$as];
|
||||
$x = &$x[(string) $row->$as];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -452,6 +452,7 @@ class Result implements IDataSource
|
||||
continue;
|
||||
}
|
||||
$value = $row[$key];
|
||||
|
||||
if ($type === Type::TEXT) {
|
||||
$row[$key] = (string) $value;
|
||||
|
||||
@@ -463,8 +464,11 @@ class Result implements IDataSource
|
||||
} elseif ($type === Type::FLOAT) {
|
||||
$value = ltrim((string) $value, '0');
|
||||
$p = strpos($value, '.');
|
||||
if ($p !== false) {
|
||||
$e = strpos($value, 'e');
|
||||
if ($p !== false && $e === false) {
|
||||
$value = rtrim(rtrim($value, '0'), '.');
|
||||
} elseif ($p !== false && $e !== false) {
|
||||
$value = rtrim($value, '.');
|
||||
}
|
||||
if ($value === '' || $value[0] === '.') {
|
||||
$value = '0' . $value;
|
||||
@@ -494,6 +498,9 @@ class Result implements IDataSource
|
||||
|
||||
} elseif ($type === Type::JSON) {
|
||||
$row[$key] = json_decode($value, true);
|
||||
|
||||
} else {
|
||||
$row[$key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -519,6 +526,15 @@ class Result implements IDataSource
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns columns type.
|
||||
*/
|
||||
final public function getTypes(): array
|
||||
{
|
||||
return $this->types;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets date format.
|
||||
*/
|
||||
|
@@ -11,28 +11,28 @@ declare(strict_types=1);
|
||||
/**
|
||||
* Static container class for Dibi connections.
|
||||
*
|
||||
* @method void disconnect()
|
||||
* @method Dibi\Result query(...$args)
|
||||
* @method Dibi\Result nativeQuery(...$args)
|
||||
* @method bool test(...$args)
|
||||
* @method Dibi\DataSource dataSource(...$args)
|
||||
* @method Dibi\Row|null fetch(...$args)
|
||||
* @method array fetchAll(...$args)
|
||||
* @method mixed fetchSingle(...$args)
|
||||
* @method array fetchPairs(...$args)
|
||||
* @method int getAffectedRows()
|
||||
* @method int getInsertId(string $sequence = null)
|
||||
* @method void begin(string $savepoint = null)
|
||||
* @method void commit(string $savepoint = null)
|
||||
* @method void rollback(string $savepoint = null)
|
||||
* @method Dibi\Reflection\Database getDatabaseInfo()
|
||||
* @method Dibi\Fluent command()
|
||||
* @method Dibi\Fluent select(...$args)
|
||||
* @method Dibi\Fluent update(string|string[] $table, array $args)
|
||||
* @method Dibi\Fluent insert(string $table, array $args)
|
||||
* @method Dibi\Fluent delete(string $table)
|
||||
* @method Dibi\HashMap getSubstitutes()
|
||||
* @method int loadFile(string $file)
|
||||
* @method static void disconnect()
|
||||
* @method static Dibi\Result query(...$args)
|
||||
* @method static Dibi\Result nativeQuery(...$args)
|
||||
* @method static bool test(...$args)
|
||||
* @method static Dibi\DataSource dataSource(...$args)
|
||||
* @method static Dibi\Row|null fetch(...$args)
|
||||
* @method static array fetchAll(...$args)
|
||||
* @method static mixed fetchSingle(...$args)
|
||||
* @method static array fetchPairs(...$args)
|
||||
* @method static int getAffectedRows()
|
||||
* @method static int getInsertId(string $sequence = null)
|
||||
* @method static void begin(string $savepoint = null)
|
||||
* @method static void commit(string $savepoint = null)
|
||||
* @method static void rollback(string $savepoint = null)
|
||||
* @method static Dibi\Reflection\Database getDatabaseInfo()
|
||||
* @method static Dibi\Fluent command()
|
||||
* @method static Dibi\Fluent select(...$args)
|
||||
* @method static Dibi\Fluent update(string|string[] $table, array $args)
|
||||
* @method static Dibi\Fluent insert(string $table, array $args)
|
||||
* @method static Dibi\Fluent delete(string $table)
|
||||
* @method static Dibi\HashMap getSubstitutes()
|
||||
* @method static int loadFile(string $file)
|
||||
*/
|
||||
class dibi
|
||||
{
|
||||
@@ -44,7 +44,7 @@ class dibi
|
||||
|
||||
/** version */
|
||||
public const
|
||||
VERSION = '4.0.1';
|
||||
VERSION = '4.0.3';
|
||||
|
||||
/** sorting order */
|
||||
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) {
|
||||
Assert::exception(function () use ($config) {
|
||||
new Connection($config + ['onConnect' => '']);
|
||||
|
@@ -100,6 +100,11 @@ test(function () {
|
||||
Assert::same(['col' => 1.0], $result->test(['col' => 1]));
|
||||
Assert::same(['col' => 1.0], $result->test(['col' => 1.0]));
|
||||
|
||||
Assert::same(['col' => '1.1e+10'], $result->test(['col' => '1.1e+10']));
|
||||
Assert::same(['col' => '1.1e-10'], $result->test(['col' => '1.1e-10']));
|
||||
Assert::same(['col' => '1.1e+10'], $result->test(['col' => '001.1e+10']));
|
||||
Assert::notSame(['col' => '1.1e+1'], $result->test(['col' => '1.1e+10']));
|
||||
|
||||
setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'deu_deu');
|
||||
Assert::same(['col' => 0.0], $result->test(['col' => '']));
|
||||
Assert::same(['col' => 0.0], $result->test(['col' => '0']));
|
||||
|
Reference in New Issue
Block a user