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

Remove importants from database panel css to allow overwrite it in custom css #313

Open
wants to merge 53 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
52a346c
ResultSet: added fetchList() as alias for fetchFields() & shortcuts
dg Aug 28, 2024
7c4be75
github actions updated
dg Sep 2, 2024
caaaa42
cs
dg Sep 4, 2024
3a1030c
opened 4.0-dev
dg Mar 1, 2021
e67b168
readme: added jumbo
dg May 16, 2024
35cf35a
removed old Driver::SUPPORT & MySqlDriver::Error constants (BC break)
dg May 10, 2024
49c05db
deprecated methods trigger notices
dg Jan 19, 2022
0e7f616
DatabaseExtension: removed obsolete option 'reflection'
dg Aug 29, 2024
4d4ae57
Driver::getColumns() nativetype -> nativeType, autoincrement -> autoI…
dg Sep 4, 2024
c2fe331
Reflection: added 'scale' field
dg Sep 4, 2024
c26d1e1
returns date-time as immutable Nette\Database\DateTime by default (BC…
dg Aug 27, 2024
5059ce6
MySQL: convertBoolean is true by default (BC break)
dg Aug 28, 2024
923bc75
removed aliases for ISupplementalDriver, IConventions (BC break)
dg Aug 10, 2024
1727e55
removed IRow & IRowContainer (BC break)
dg Aug 10, 2024
5c5b607
removed formatLike() (BC break)
dg Sep 4, 2024
6cffe42
ConnectionPanel: refactoring, initialize($addBarPanel) is true by def…
dg Aug 27, 2024
40ab5c6
connections are always lazy (BC break)
dg Aug 29, 2024
8c86228
renamed Nette\Database\ResultSet -> Result
dg Aug 28, 2024
96c2e8a
renamed Nette\Database\Driver -> Nette\Database\Drivers\Engine (BC br…
dg Aug 28, 2024
f3e3cd9
renamed drivers to *Engine
dg Sep 3, 2024
362e62c
added new drivers
dg Aug 16, 2024
b435456
Connection: the driver is created in the constructor
dg Sep 3, 2024
e511b44
connections & engines are created by Driver
dg Sep 4, 2024
9718880
PDO replaced by Connection
dg Sep 4, 2024
b775226
added Connection::getServerVersion()
dg Aug 15, 2024
b11c0bf
added Connection::execute()
dg Sep 4, 2024
be8936f
Connection::getPdo() deprecated
dg Aug 15, 2024
0cf2662
added TypeConverter
dg Aug 17, 2024
4415f62
uses TypeConverter to convert values to PHP
dg Sep 4, 2024
bb7f7fb
added option convertDateTime
dg Sep 3, 2024
0eb80cf
added option convertDecimal & converts to int/float [Closes #257]
dg Sep 3, 2024
68f45f8
SqlsrvDriver: converts BIT to boolean (BC break)
dg Aug 17, 2024
9075da3
removed custom rowNormalizer (BC break)
dg Sep 4, 2024
4bb25d2
PDOStatement replaced by Result
dg Aug 29, 2024
1e40d23
Result::getColumnMeta is cached (ref #212)
dg Aug 15, 2024
9bc920b
try/catch checking and converting of PDOException moved to drivers
dg Sep 4, 2024
955b992
DriverException::getCode() returns driver error code instead of SQLSt…
dg Aug 27, 2024
235def4
Connection: calling query() moved here from Result
dg Sep 3, 2024
2478ffb
added Connection::getLastQuery(), Result::getQuery(), DriverException…
dg Sep 3, 2024
3cadf6e
Connection::getInsertId() returns int|string or exception (BC break)
dg Aug 14, 2024
11f5d20
drivers: getForeignKeys() works with multi-column foreign keys
dg Sep 4, 2024
f981ccc
Explorer, Selection, Structure: change of constructor dependencies (B…
dg Aug 13, 2024
9b50bd2
Connection::getDsn() deprecated (BC break)
dg Aug 17, 2024
45c712f
auxiliary commit
dg Aug 29, 2024
b9d5ce0
merging Connection & Explorer classes into one (BC break)
dg Sep 6, 2024
33708a6
removed IStructure (BC break)
dg Sep 2, 2024
24167bc
Engine::applyLimit() returns string (BC break)
dg Sep 3, 2024
1ef0bd4
Engine::delimite() -> delimit() (BC break)
dg Sep 4, 2024
b7aeb45
Selection: uses yield for iteration
dg Sep 5, 2024
2e2aa3b
Result: uses yield for iteration
dg Sep 5, 2024
4b58125
added Explorer::createFromDsn() & createFromParameters()
dg Sep 3, 2024
a1798c0
wip php 8.4 test
dg Aug 14, 2024
1769e30
Remove importants from database panel css to allow overwrite it in cu…
website21cz Oct 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Tests
on: [push, pull_request]

env:
php-extensions: mbstring, intl, pdo_sqlsrv-5.10.0beta2
php-extensions: mbstring, intl, pdo_sqlsrv-5.12.0
php-tools: "composer:v2, pecl"

jobs:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "3.2-dev"
"dev-master": "4.0-dev"
}
}
}
24 changes: 15 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
Nette Database
==============
[![Nette Database](https://github.com/nette/database/assets/194960/97d8f31b-096c-466c-a76f-f5b9e511ea8d)](https://doc.nette.org/database)

[![Downloads this Month](https://img.shields.io/packagist/dm/nette/database.svg)](https://packagist.org/packages/nette/database)
[![Tests](https://github.com/nette/database/actions/workflows/tests.yml/badge.svg?branch=v3.2)](https://github.com/nette/database/actions)
[![Tests](https://github.com/nette/database/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/nette/database/actions)
[![Latest Stable Version](https://poser.pugx.org/nette/database/v/stable)](https://github.com/nette/database/releases)
[![License](https://img.shields.io/badge/license-New%20BSD-blue.svg)](https://github.com/nette/database/blob/master/license.md)

 <!---->

Introduction
------------

Nette provides a powerful layer for accessing your database easily.

- composes SQL queries with ease
- easily fetches data
- uses efficient queries and does not transmit unnecessary data
composes SQL queries with ease<br>
✅ significantly simplifies retrieving data without writing SQL queries<br>
uses efficient queries and does not transmit unnecessary data

The [Nette Database Core](https://doc.nette.org/database-core) is a wrapper around the PDO and provides core functionality.
The [Nette Database Core](https://doc.nette.org/en/database/core) is a wrapper around the PDO and provides core functionality.

The [Nette Database Explorer](https://doc.nette.org/database-explorer) layer helps you to fetch database data more easily and in a more optimized way.
The [Nette Database Explorer](https://doc.nette.org/en/database/explorer) layer helps you to fetch database data more easily and in a more optimized way.

 <!---->

[Support Me](https://github.com/sponsors/dg)
--------------------------------------------
Expand All @@ -30,6 +31,7 @@ Do you like Nette Database? Are you looking forward to the new features?

Thank you!

 <!---->

Installation
------------
Expand All @@ -42,20 +44,22 @@ composer require nette/database

It requires PHP version 8.1 and supports PHP up to 8.4.

 <!---->

Usage
-----

This is just a piece of documentation. [Please see our website](https://doc.nette.org/database).

 <!---->

Database Core
-------------

To create a new database connection just create a new instance of `Nette\Database\Connection` class:

```php
$database = new Nette\Database\Connection($dsn, $user, $password); // the same arguments as uses PDO
$database = new Nette\Database\Explorer($dsn, $user, $password); // the same arguments as uses PDO
```

Connection allows you to easily query your database by calling `query` method:
Expand All @@ -71,6 +75,8 @@ $database->query('UPDATE users SET ? WHERE id=?', $data, $id);
$database->query('SELECT * FROM categories WHERE id=?', 123)->dump();
```

 <!---->

Database Explorer
-----------------

Expand Down
44 changes: 17 additions & 27 deletions src/Bridges/DatabaseDI/DatabaseExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Nette\Bridges\DatabaseDI;

use Nette;
use Nette\DI\Definitions\Statement;
use Nette\Schema\Expect;
use Tracy;

Expand Down Expand Up @@ -77,6 +78,10 @@ public function beforeCompile(): void

private function setupDatabase(\stdClass $config, string $name): void
{
if (!empty($config->reflection)) {
throw new Nette\DeprecatedException('The "reflection" option is deprecated, use "conventions" instead.');
}

$builder = $this->getContainerBuilder();

foreach ($config->options as $key => $value) {
Expand All @@ -90,48 +95,33 @@ private function setupDatabase(\stdClass $config, string $name): void
}
}

$connection = $builder->addDefinition($this->prefix("$name.connection"))
->setFactory(Nette\Database\Connection::class, [$config->dsn, $config->user, $config->password, $config->options])
->setAutowired($config->autowired);
$cacheId = 'Nette.Database.' . hash('xxh128', $name . $config->dsn);
$cache = new Statement(Nette\Caching\Cache::class, [1 => $cacheId]);

$structure = $builder->addDefinition($this->prefix("$name.structure"))
->setFactory(Nette\Database\Structure::class)
->setArguments([$connection])
$explorer = $builder->addDefinition($this->prefix($name))
->setFactory(Nette\Database\Explorer::class, [$config->dsn, $config->user, $config->password, $config->options])
->addSetup('setCache', [$cache])
->setAutowired($config->autowired);

if (!empty($config->reflection)) {
$conventionsServiceName = 'reflection';
$config->conventions = $config->reflection;
if (is_string($config->conventions) && strtolower($config->conventions) === 'conventional') {
$config->conventions = 'Static';
}
} else {
$conventionsServiceName = 'conventions';
}

if (!$config->conventions) {
$conventions = null;
if (!$config->conventions || $config->conventions === 'discovered') {

} elseif (is_string($config->conventions)) {
$conventions = $builder->addDefinition($this->prefix("$name.$conventionsServiceName"))
$conventions = $builder->addDefinition($this->prefix("$name.conventions"))
->setFactory(preg_match('#^[a-z]+$#Di', $config->conventions)
? 'Nette\Database\Conventions\\' . ucfirst($config->conventions) . 'Conventions'
: $config->conventions)
->setArguments(strtolower($config->conventions) === 'discovered' ? [$structure] : [])
->setAutowired($config->autowired);
$explorer->addSetup('setConventions', [$conventions]);

} else {
$conventions = Nette\DI\Helpers::filterArguments([$config->conventions])[0];
$explorer->addSetup('setConventions', [Nette\DI\Helpers::filterArguments([$config->conventions])[0]]);
}

$builder->addDefinition($this->prefix("$name.explorer"))
->setFactory(Nette\Database\Explorer::class, [$connection, $structure, $conventions])
->setAutowired($config->autowired);

$builder->addAlias($this->prefix("$name.context"), $this->prefix("$name.explorer"));
$builder->addAlias($this->prefix("$name.connection"), $this->prefix($name));
$builder->addAlias($this->prefix("$name.context"), $this->prefix($name));
$builder->addAlias($this->prefix("$name.explorer"), $this->prefix($name));

if ($this->name === 'database') {
$builder->addAlias($this->prefix($name), $this->prefix("$name.connection"));
$builder->addAlias("nette.database.$name", $this->prefix($name));
$builder->addAlias("nette.database.$name.context", $this->prefix("$name.explorer"));
}
Expand Down
71 changes: 30 additions & 41 deletions src/Bridges/DatabaseTracy/ConnectionPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
namespace Nette\Bridges\DatabaseTracy;

use Nette;
use Nette\Database\Connection;
use Nette\Database\DriverException;
use Nette\Database\Explorer;
use Nette\Database\Helpers;
use Nette\Database\Result;
use Tracy;


Expand All @@ -21,27 +23,19 @@
class ConnectionPanel implements Tracy\IBarPanel
{
public int $maxQueries = 100;

public string $name;

public bool|string $explain = true;

public bool $disabled = false;

public float $performanceScale = 0.25;

private float $totalTime = 0;

private int $count = 0;

private array $queries = [];

private array $events = [];
private Tracy\BlueScreen $blueScreen;


public static function initialize(
Connection $connection,
bool $addBarPanel = false,
Explorer $explorer,
bool $addBarPanel = true,
string $name = '',
bool $explain = true,
?Tracy\Bar $bar = null,
Expand All @@ -52,7 +46,7 @@ public static function initialize(
$blueScreen->addPanel(self::renderException(...));

if ($addBarPanel) {
$panel = new self($connection, $blueScreen);
$panel = new self($explorer, $blueScreen);
$panel->explain = $explain;
$panel->name = $name;
$bar ??= Tracy\Debugger::getBar();
Expand All @@ -63,14 +57,14 @@ public static function initialize(
}


public function __construct(Connection $connection, Tracy\BlueScreen $blueScreen)
public function __construct(Explorer $explorer, Tracy\BlueScreen $blueScreen)
{
$connection->onQuery[] = $this->logQuery(...);
$explorer->onQuery[] = $this->logQuery(...);
$this->blueScreen = $blueScreen;
}


private function logQuery(Connection $connection, $result): void
private function logQuery(Explorer $connection, $result): void
{
if ($this->disabled) {
return;
Expand All @@ -79,7 +73,7 @@ private function logQuery(Connection $connection, $result): void
$this->count++;

$source = null;
$trace = $result instanceof \PDOException
$trace = $result instanceof DriverException
? $result->getTrace()
: debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
foreach ($trace as $row) {
Expand All @@ -88,40 +82,33 @@ private function logQuery(Connection $connection, $result): void
&& preg_match('~\.(php.?|phtml)$~', $row['file'])
&& !$this->blueScreen->isCollapsed($row['file']))
&& ($row['class'] ?? '') !== self::class
&& !is_a($row['class'] ?? '', Connection::class, allow_string: true)
&& !is_a($row['class'] ?? '', Explorer::class, allow_string: true)
) {
$source = [$row['file'], (int) $row['line']];
break;
}
}

if ($result instanceof Nette\Database\ResultSet) {
if ($result instanceof Result) {
$this->totalTime += $result->getTime();
if ($this->count < $this->maxQueries) {
$this->queries[] = [$connection, $result->getQueryString(), $result->getParameters(), $source, $result->getTime(), $result->getRowCount(), null];
$this->events[] = [$connection, $result->getQuery(), $source, $result->getTime(), $result->getRowCount(), null];
}
} elseif ($result instanceof \PDOException && $this->count < $this->maxQueries) {
$this->queries[] = [$connection, $result->queryString, null, $source, null, null, $result->getMessage()];
} elseif ($result instanceof DriverException && $this->count < $this->maxQueries) {
$this->events[] = [$connection, $result->getQuery(), $source, null, null, $result->getMessage()];
}
}


public static function renderException(?\Throwable $e): ?array
{
if (!$e instanceof \PDOException) {
if (!$e instanceof DriverException) {
return null;
}

if (isset($e->queryString)) {
$sql = $e->queryString;

} elseif ($item = Tracy\Helpers::findTrace($e->getTrace(), 'PDO::prepare')) {
$sql = $item['args'][0];
}

return isset($sql) ? [
return $e->getQuery() ? [
'tab' => 'SQL',
'panel' => Helpers::dumpSql($sql, $e->params ?? []),
'panel' => Helpers::dumpSql($e->getQuery()),
] : null;
}

Expand All @@ -143,10 +130,11 @@ public function getPanel(): ?string
return null;
}

$queries = [];
foreach ($this->queries as $query) {
[$connection, $sql, $params, , , , $error] = $query;
$events = [];
foreach ($this->events as $event) {
[$connection, $query, , , , $error] = $event;
$explain = null;
$sql = $query->getSql();
$command = preg_match('#\s*\(?\s*(SELECT|INSERT|UPDATE|DELETE)\s#iA', $sql, $m)
? strtolower($m[1])
: null;
Expand All @@ -155,17 +143,18 @@ public function getPanel(): ?string
$cmd = is_string($this->explain)
? $this->explain
: 'EXPLAIN';
$explain = (new Nette\Database\ResultSet($connection, "$cmd $sql", $params))->fetchAll();
} catch (\PDOException) {
$rows = $connection->getConnection()->query("$cmd $sql", $query->getParameters());
for ($explain = []; $row = $rows->fetch(); $explain[] = $row);
} catch (DriverException) {
}
}

$query[] = $command;
$query[] = $explain;
$queries[] = $query;
$event[] = $command;
$event[] = $explain;
$events[] = $event;
}

return Nette\Utils\Helpers::capture(function () use ($queries, $connection) {
return Nette\Utils\Helpers::capture(function () use ($events, $connection) {
$name = $this->name;
$count = $this->count;
$totalTime = $this->totalTime;
Expand Down
20 changes: 10 additions & 10 deletions src/Bridges/DatabaseTracy/templates/ConnectionPanel.panel.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ use Tracy\Helpers;

?>
<style class="tracy-debug">
#tracy-debug td.nette-DbConnectionPanel-sql { background: white !important }
#tracy-debug .nette-DbConnectionPanel-source { color: #BBB !important }
#tracy-debug td.nette-DbConnectionPanel-sql { background: white }
#tracy-debug .nette-DbConnectionPanel-source { color: #BBB }
#tracy-debug .nette-DbConnectionPanel-explain td { white-space: pre }
#tracy-debug td.nette-DbConnectionPanel-sql-insert { background: #E7ffE7 !important }
#tracy-debug td.nette-DbConnectionPanel-sql-delete { background: #FFE7E7 !important }
#tracy-debug td.nette-DbConnectionPanel-sql-update { background: #E7FBFF !important }
#tracy-debug td.nette-DbConnectionPanel-sql-insert { background: #E7ffE7 }
#tracy-debug td.nette-DbConnectionPanel-sql-delete { background: #FFE7E7 }
#tracy-debug td.nette-DbConnectionPanel-sql-update { background: #E7FBFF }
</style>

<h1 title="<?= Helpers::escapeHtml($connection->getDsn()) ?>">Queries: <?php
<h1>Queries: <?php
echo $count, ($totalTime ? sprintf(', time: %0.3f ms', $totalTime * 1000) : ''), ', ', Helpers::escapeHtml($name) ?></h1>

<div class="tracy-inner">
<div class="tracy-inner-container">
<table class="tracy-sortable">
<tr><th>Time&nbsp;ms</th><th>SQL Query</th><th>Rows</th></tr>
<?php
foreach ($queries as $query):
[$connection, $sql, $params, $source, $time, $rows, $error, $command, $explain] = $query;
foreach ($events as $event):
[$connection, $query, $source, $time, $rows, $error, $command, $explain] = $event;
?>
<tr>
<td style="background:rgba(255, 95, 23, <?= sprintf('%0.3f', log($time * 1000 + 1, 10) * $performanceScale) ?>)" data-order="<?= (float) $time ?>">
Expand All @@ -36,7 +36,7 @@ use Tracy\Helpers;
<br /><a class="tracy-toggle tracy-collapsed" data-tracy-ref="^tr .nette-DbConnectionPanel-explain">explain</a>
<?php endif ?>
</td>
<td class="nette-DbConnectionPanel-sql nette-DbConnectionPanel-sql-<?= Helpers::escapeHtml($command) ?>"><?= DbHelpers::dumpSql($sql, $params, $connection) ?>
<td class="nette-DbConnectionPanel-sql nette-DbConnectionPanel-sql-<?= Helpers::escapeHtml($command) ?>"><?= DbHelpers::dumpSql($query, $connection) ?>
<?php if ($explain): ?>
<table class="tracy-collapsed nette-DbConnectionPanel-explain">
<tr>
Expand All @@ -61,6 +61,6 @@ use Tracy\Helpers;
</tr>
<?php endforeach ?>
</table>
<?php if (count($queries) < $count): ?><p>...and more</p><?php endif ?>
<?php if (count($events) < $count): ?><p>...and more</p><?php endif ?>
</div>
</div>
Loading