Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for use tablename acquired as regclass at previous query in PostgreSQL (prevent to "delimite" two times) #263

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions .github/funding.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github: dg
custom: "https://nette.org/donate"
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: php
php:
- 7.1
- 7.2
- 7.3
- 7.4
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ install:
- IF EXIST c:\php (SET PHP=0) ELSE (SET PHP=1)
- IF %PHP%==1 mkdir c:\php
- IF %PHP%==1 cd c:\php
- IF %PHP%==1 curl https://windows.php.net/downloads/releases/archives/php-7.1.5-Win32-VC14-x64.zip --output php.zip
- IF %PHP%==1 curl https://windows.php.net/downloads/releases/archives/php-7.2.28-Win32-VC15-x64.zip --output php.zip
- IF %PHP%==1 7z x php.zip >nul
- IF %PHP%==1 echo extension_dir=ext >> php.ini
- IF %PHP%==1 echo extension=php_openssl.dll >> php.ini
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
],
"require": {
"php": ">=7.1",
"php": ">=7.2",
"ext-pdo": "*",
"nette/caching": "^3.0",
"nette/utils": "^3.1"
Expand All @@ -40,7 +40,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
"dev-master": "3.1-dev"
}
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ The recommended way to install is via Composer:
composer require nette/database
```

It requires PHP version 7.1 and supports PHP up to 7.4.
It requires PHP version 7.2 and supports PHP up to 7.4.


Usage
Expand Down
8 changes: 6 additions & 2 deletions src/Bridges/DatabaseTracy/ConnectionPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class ConnectionPanel implements Tracy\IBarPanel
/** @var bool */
public $disabled = false;

/** @var float */
public $performanceScale = 0.25;

/** @var float logged time */
private $totalTime = 0;

Expand All @@ -46,11 +49,11 @@ class ConnectionPanel implements Tracy\IBarPanel

public function __construct(Connection $connection)
{
$connection->onQuery[] = [$this, 'logQuery'];
$connection->onQuery[] = \Closure::fromCallable([$this, 'logQuery']);
}


public function logQuery(Connection $connection, $result): void
private function logQuery(Connection $connection, $result): void
{
if ($this->disabled) {
return;
Expand Down Expand Up @@ -137,6 +140,7 @@ public function getPanel(): ?string
$name = $this->name;
$count = $this->count;
$totalTime = $this->totalTime;
$performanceScale = $this->performanceScale;
require __DIR__ . '/templates/ConnectionPanel.panel.phtml';
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use Tracy\Helpers;
[$connection, $sql, $params, $source, $time, $rows, $error, $command, $explain] = $query;
?>
<tr>
<td>
<td style="background:rgba(255, 95, 23, <?= sprintf('%0.3f', log($time * 1000 + 1, 10) * $performanceScale) ?>)">
<?php if ($error): ?>
<span title="<?= Helpers::escapeHtml($error) ?>">ERROR</span>
<?php elseif ($time !== null): echo sprintf('%0.3f', $time * 1000); endif ?>
Expand Down
17 changes: 17 additions & 0 deletions src/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,23 @@ public function rollBack(): void
}


/**
* @return mixed
*/
public function transaction(callable $callback)
{
$this->beginTransaction();
try {
$res = $callback();
} catch (\Throwable $e) {
$this->rollBack();
throw $e;
}
$this->commit();
return $res;
}


/**
* Generates and executes SQL query.
*/
Expand Down
9 changes: 9 additions & 0 deletions src/Database/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ public function rollBack(): void
}


/**
* @return mixed
*/
public function transaction(callable $callback)
{
return $this->connection->transaction($callback);
}


public function getInsertId(string $sequence = null): string
{
return $this->connection->getInsertId($sequence);
Expand Down
7 changes: 7 additions & 0 deletions src/Database/Drivers/MsSqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ public function delimite(string $name): string
}


public function undelimite(string $name): string
{
$name = preg_replace('#(?<!\])\](?!\])#', '', preg_replace('#(?<!\[)\[(?!\[)#', '', $name));
return str_replace(['[[', ']]'], ['[', ']'], $name);
}


public function formatDateTime(\DateTimeInterface $value): string
{
return $value->format("'Y-m-d H:i:s'");
Expand Down
7 changes: 7 additions & 0 deletions src/Database/Drivers/MySqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ public function delimite(string $name): string
}


public function undelimite(string $name): string
{
$name = preg_replace('#(?<!`)`(?!`)#', '', $name);
return str_replace('``', '`', $name);
}


public function formatDateTime(\DateTimeInterface $value): string
{
return $value->format("'Y-m-d H:i:s'");
Expand Down
7 changes: 7 additions & 0 deletions src/Database/Drivers/OciDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ public function delimite(string $name): string
}


public function undelimite(string $name): string
{
$name = preg_replace('#(?<!")"(?!")#', '', $name);
return str_replace('""', '"', $name);
}


public function formatDateTime(\DateTimeInterface $value): string
{
return $value->format($this->fmtDateTime);
Expand Down
7 changes: 7 additions & 0 deletions src/Database/Drivers/OdbcDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ public function delimite(string $name): string
}


public function undelimite(string $name): string
{
$name = preg_replace('#(?<!\])\](?!\])#', '', preg_replace('#(?<!\[)\[(?!\[)#', '', $name));
return str_replace(['[[', ']]'], ['[', ']'], $name);
}


public function formatDateTime(\DateTimeInterface $value): string
{
return $value->format('#m/d/Y H:i:s#');
Expand Down
7 changes: 7 additions & 0 deletions src/Database/Drivers/PgSqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ public function delimite(string $name): string
}


public function undelimite(string $name): string
{
$name = preg_replace('#(?<!")"(?!")#', '', $name);
return str_replace('""', '"', $name);
}


public function formatDateTime(\DateTimeInterface $value): string
{
return $value->format("'Y-m-d H:i:s'");
Expand Down
8 changes: 8 additions & 0 deletions src/Database/Drivers/SqliteDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ public function delimite(string $name): string
}


public function undelimite(string $name): string
{
$name = preg_replace('#(?:^|\.)\[#', '', $name);
// Can not be fully undelimited due to ambigious delimite() - was delimited "[x x]" name originally "x x" or "x[]x"?
return $name;
}


public function formatDateTime(\DateTimeInterface $value): string
{
return $value->format($this->fmtDateTime);
Expand Down
7 changes: 7 additions & 0 deletions src/Database/Drivers/SqlsrvDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ public function delimite(string $name): string
}


public function undelimite(string $name): string
{
$name = preg_replace('#(?:^|\.)\[#', '', $name);
return str_replace(']]', ']', $name);
}


public function formatDateTime(\DateTimeInterface $value): string
{
/** @see https://msdn.microsoft.com/en-us/library/ms187819.aspx */
Expand Down
1 change: 1 addition & 0 deletions src/Database/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Helpers
'DATE' => IStructure::FIELD_DATE,
'(SMALL)?DATETIME(OFFSET)?\d*|TIME(STAMP.*)?' => IStructure::FIELD_DATETIME,
'BYTEA|(TINY|MEDIUM|LONG|)BLOB|(LONG )?(VAR)?BINARY|IMAGE' => IStructure::FIELD_BINARY,
'REGCLASS' => IStructure::FIELD_TABLENAME,
];


Expand Down
3 changes: 2 additions & 1 deletion src/Database/IStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ interface IStructure
FIELD_TIME = 'time',
FIELD_DATETIME = 'datetime',
FIELD_UNIX_TIMESTAMP = 'timestamp',
FIELD_TIME_INTERVAL = 'timeint';
FIELD_TIME_INTERVAL = 'timeint',
FIELD_TABLENAME = 'regclass';

/**
* Returns tables list.
Expand Down
5 changes: 5 additions & 0 deletions src/Database/ISupplementalDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ function convertException(\PDOException $e): DriverException;
*/
function delimite(string $name): string;

/**
* Undelimites identifier as reverse operation for delimite (for example to get not delimited table name).
*/
function undelimite(string $name): string;

/**
* Formats date-time for use in a SQL statement.
*/
Expand Down
3 changes: 3 additions & 0 deletions src/Database/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ public function normalizeRow(array $row): array

} elseif ($type === IStructure::FIELD_UNIX_TIMESTAMP) {
$row[$key] = Nette\Utils\DateTime::from($value);

} elseif ($type === IStructure::FIELD_TABLENAME) {
$row[$key] = $this->connection->getSupplementalDriver()->undelimite($value);
}
}

Expand Down
Loading