Skip to content

Commit

Permalink
Merge branch 'master' into profiler_cli_mode
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 24, 2021
2 parents eb95ce1 + d764c4d commit c602aad
Show file tree
Hide file tree
Showing 18 changed files with 39 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: ['8.0']
php: ['8.0', '8.1']

fail-fast: false

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
],
"require": {
"php": ">=8.0 <8.1"
"php": ">=8.0 <8.2"
},
"require-dev": {
"tracy/tracy": "^2.8",
Expand Down
4 changes: 2 additions & 2 deletions examples/query-language-and-conditions.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
$dibi->test('
SELECT *
FROM customers
%if', isset($name), 'WHERE name LIKE ?', $name, '%end'
%if', isset($name), 'WHERE name LIKE ?', $name, '%end',
);
// -> SELECT * FROM customers WHERE name LIKE 'K%'

Expand All @@ -54,7 +54,7 @@
WHERE
%if', isset($name), 'name LIKE ?', $name, '
%if', $cond2, 'AND admin=1 %end
%else 1 LIMIT 10 %end'
%else 1 LIMIT 10 %end',
);
// -> SELECT * FROM customers WHERE LIMIT 10

Expand Down
4 changes: 2 additions & 2 deletions examples/query-language-basic-examples.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
SELECT COUNT(*) as [count]
FROM [comments]
WHERE [ip] LIKE ?', $ipMask, '
AND [date] > ', new Dibi\DateTime($timestamp)
AND [date] > ', new Dibi\DateTime($timestamp),
);
// -> SELECT COUNT(*) as [count] FROM [comments] WHERE [ip] LIKE '192.168.%' AND [date] > 876693600

Expand Down Expand Up @@ -69,7 +69,7 @@
$dibi->test('
SELECT *
FROM people
WHERE id IN (?)', $array
WHERE id IN (?)', $array,
);
// -> SELECT * FROM people WHERE id IN ( 1, 2, 3 )

Expand Down
2 changes: 1 addition & 1 deletion examples/using-datetime.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
'id' => 123,
'date' => new DateTime('12.3.2007'),
'stamp' => new DateTime('23.1.2007 10:23'),
]
],
);
// -> INSERT INTO [mytable] ([id], [date], [stamp]) VALUES (123, '2007-03-12', '2007-01-23 10-23-00')
2 changes: 1 addition & 1 deletion examples/using-substitutions.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ function substFallBack($expr)
$dibi->test("
UPDATE :account:user
SET name='John Doe', status=:active:
WHERE id=", 7
WHERE id=", 7,
);
// -> UPDATE eshop_user SET name='John Doe', status=7 WHERE id= 7
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Install Dibi via Composer:
composer require dibi/dibi
```

The Dibi 4.2 requires PHP version 8.0.
The Dibi 5.0 requires PHP version 8.0 and supports PHP up to 8.1.


Usage
Expand Down
6 changes: 3 additions & 3 deletions src/Dibi/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,9 @@ public function getSubstitutes(): HashMap
*/
public function substitute(string $value): string
{
return strpos($value, ':') === false
? $value
: preg_replace_callback('#:([^:\s]*):#', fn(array $m) => $this->substitutes->{$m[1]}, $value);
return str_contains($value, ':')
? preg_replace_callback('#:([^:\s]*):#', fn(array $m) => $this->substitutes->{$m[1]}, $value)
: $value;
}


Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Drivers/PostgreDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public static function createException(string $message, $code = null, string $sq
$message = substr($message, strlen($m[0]));
}

if ($code === '0A000' && strpos($message, 'truncate') !== false) {
if ($code === '0A000' && str_contains($message, 'truncate')) {
return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql);

} elseif ($code === '23502') {
Expand Down
4 changes: 2 additions & 2 deletions src/Dibi/Drivers/PostgreReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function getColumns(string $table): array
a.atttypmod-4 AS character_maximum_length,
NOT a.attnotnull AS is_nullable,
a.attnum AS ordinal_position,
adef.adsrc AS column_default
pg_get_expr(adef.adbin, adef.adrelid) AS column_default
FROM
pg_attribute a
JOIN pg_type ON a.atttypid = pg_type.oid
Expand All @@ -125,7 +125,7 @@ public function getColumns(string $table): array
'size' => $size > 0 ? $size : null,
'nullable' => $row['is_nullable'] === 'YES' || $row['is_nullable'] === 't' || $row['is_nullable'] === true,
'default' => $row['column_default'],
'autoincrement' => (int) $row['ordinal_position'] === $primary && substr($row['column_default'] ?? '', 0, 7) === 'nextval',
'autoincrement' => (int) $row['ordinal_position'] === $primary && str_starts_with($row['column_default'] ?? '', 'nextval'),
'vendor' => $row,
];
}
Expand Down
16 changes: 8 additions & 8 deletions src/Dibi/Drivers/SqliteDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,19 @@ public static function createException(string $message, $code, string $sql): Dib
if ($code !== 19) {
return new Dibi\DriverException($message, $code, $sql);

} elseif (strpos($message, 'must be unique') !== false
|| strpos($message, 'is not unique') !== false
|| strpos($message, 'UNIQUE constraint failed') !== false
} elseif (str_contains($message, 'must be unique')
|| str_contains($message, 'is not unique')
|| str_contains($message, 'UNIQUE constraint failed')
) {
return new Dibi\UniqueConstraintViolationException($message, $code, $sql);

} elseif (strpos($message, 'may not be null') !== false
|| strpos($message, 'NOT NULL constraint failed') !== false
} elseif (str_contains($message, 'may not be null')
|| str_contains($message, 'NOT NULL constraint failed')
) {
return new Dibi\NotNullConstraintViolationException($message, $code, $sql);

} elseif (strpos($message, 'foreign key constraint failed') !== false
|| strpos($message, 'FOREIGN KEY constraint failed') !== false
} elseif (str_contains($message, 'foreign key constraint failed')
|| str_contains($message, 'FOREIGN KEY constraint failed')
) {
return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql);

Expand Down Expand Up @@ -287,7 +287,7 @@ public function registerAggregateFunction(
string $name,
callable $rowCallback,
callable $agrCallback,
int $numArgs = -1
int $numArgs = -1,
): void {
$this->connection->createAggregate($name, $rowCallback, $agrCallback, $numArgs);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function __construct(Connection $connection, int $type, string $sql = nul

$dibiDir = dirname((new \ReflectionClass('dibi'))->getFileName()) . DIRECTORY_SEPARATOR;
foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $row) {
if (isset($row['file']) && is_file($row['file']) && strpos($row['file'], $dibiDir) !== 0) {
if (isset($row['file']) && is_file($row['file']) && !str_starts_with($row['file'], $dibiDir)) {
$this->source = [$row['file'], (int) $row['line']];
break;
}
Expand Down
7 changes: 4 additions & 3 deletions src/Dibi/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static function dump(string|Result $sql = null, bool $return = false): ?s
{
ob_start();
if ($sql instanceof Result && PHP_SAPI === 'cli') {
$hasColors = (substr((string) getenv('TERM'), 0, 5) === 'xterm');
$hasColors = (str_starts_with((string) getenv('TERM'), 'xterm'));
$maxLen = 0;
foreach ($sql as $i => $row) {
if ($i === 0) {
Expand Down Expand Up @@ -86,7 +86,7 @@ public static function dump(string|Result $sql = null, bool $return = false): ?s
// syntax highlight
$highlighter = "#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#is";
if (PHP_SAPI === 'cli') {
if (substr((string) getenv('TERM'), 0, 5) === 'xterm') {
if (str_starts_with((string) getenv('TERM'), 'xterm')) {
$sql = preg_replace_callback($highlighter, function (array $m) {
if (!empty($m[1])) { // comment
return "\033[1;30m" . $m[1] . "\033[0m";
Expand Down Expand Up @@ -179,6 +179,7 @@ public static function detectType(string $type): ?string
{
static $patterns = [
'^_' => Type::TEXT, // PostgreSQL arrays
'RANGE$' => Type::TEXT, // PostgreSQL range types
'BYTEA|BLOB|BIN' => Type::BINARY,
'TEXT|CHAR|POINT|INTERVAL|STRING' => Type::TEXT,
'YEAR|BYTE|COUNTER|SERIAL|INT|LONG|SHORT|^TINY$' => Type::INTEGER,
Expand Down Expand Up @@ -249,7 +250,7 @@ public static function loadFromFile(Connection $connection, string $file, callab
if (strtoupper(substr($s, 0, 10)) === 'DELIMITER ') {
$delimiter = trim(substr($s, 10));

} elseif (substr($ts = rtrim($s), -strlen($delimiter)) === $delimiter) {
} elseif (str_ends_with($ts = rtrim($s), $delimiter)) {
$sql .= substr($ts, 0, -strlen($delimiter));
$driver->query($sql);
$sql = '';
Expand Down
4 changes: 2 additions & 2 deletions src/Dibi/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ final public function fetchAll(int $offset = null, int $limit = null): array
*/
final public function fetchAssoc(string $assoc): array
{
if (strpos($assoc, ',') !== false) {
if (str_contains($assoc, ',')) {
return $this->oldFetchAssoc($assoc);
}

Expand Down Expand Up @@ -485,7 +485,7 @@ private function normalize(array &$row): void
$row[$key] = ((bool) $value) && $value !== 'f' && $value !== 'F';

} elseif ($type === Type::DATETIME || $type === Type::DATE || $type === Type::TIME) {
if ($value && substr((string) $value, 0, 3) !== '000') { // '', null, false, '0000-00-00', ...
if ($value && !str_starts_with((string) $value, '000')) { // '', null, false, '0000-00-00', ...
$value = new DateTime($value);
$row[$key] = $format ? $value->format($format) : $value;
} else {
Expand Down
14 changes: 7 additions & 7 deletions src/Dibi/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function asDateTime(string $key, string $format = null): DateTime|string|
{
$time = $this[$key];
if (!$time instanceof DateTime) {
if (!$time || substr((string) $time, 0, 3) === '000') { // '', null, false, '0000-00-00', ...
if (!$time || str_starts_with((string) $time, '000')) { // '', null, false, '0000-00-00', ...
return null;
}
$time = new DateTime($time);
Expand All @@ -61,37 +61,37 @@ public function __isset(string $key): bool
/********************* interfaces ArrayAccess, Countable & IteratorAggregate ****************d*g**/


final public function count()
final public function count(): int
{
return count((array) $this);
}


final public function getIterator()
final public function getIterator(): \ArrayIterator
{
return new \ArrayIterator($this);
}


final public function offsetSet($nm, $val)
final public function offsetSet($nm, $val): void
{
$this->$nm = $val;
}


final public function offsetGet($nm)
final public function offsetGet($nm): mixed
{
return $this->$nm;
}


final public function offsetExists($nm)
final public function offsetExists($nm): bool
{
return isset($this->$nm);
}


final public function offsetUnset($nm)
final public function offsetUnset($nm): void
{
unset($this->$nm);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public function formatValue(mixed $value, ?string $modifier): string
$v = $this->formatValue($v, $pair[1]);
if ($pair[1] === 'l' || $pair[1] === 'in') {
$op = 'IN ';
} elseif (strpos($pair[1], 'like') !== false) {
} elseif (str_contains($pair[1], 'like')) {
$op = 'LIKE ';
} elseif ($v === 'NULL') {
$op = 'IS ';
Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/dibi.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class dibi

/** version */
public const
VERSION = '4.2.2';
VERSION = '4.2.3';

/** sorting order */
public const
Expand Down
2 changes: 0 additions & 2 deletions tests/dibi/DateTime.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ Assert::same('1978-01-23 11:40:00.000000', (string) new DateTime(254_400_000));
Assert::same('1978-01-23 11:40:00.000000', (string) (new DateTime)->setTimestamp(254_400_000));
Assert::same(254_400_000, (new DateTime(254_400_000))->getTimestamp());

Assert::same('2050-08-13 11:40:00.000000', (string) new DateTime(254_400_0000));
Assert::same('2050-08-13 11:40:00.000000', (string) (new DateTime)->setTimestamp(254_400_0000));
Assert::same(is_int(2_544_000_000) ? 2_544_000_000 : '2544000000', (new DateTime(2_544_000_000))->getTimestamp()); // 64 bit

Assert::same('1978-05-05 00:00:00.000000', (string) new DateTime('1978-05-05'));

0 comments on commit c602aad

Please sign in to comment.