Skip to content

Commit 05421a0

Browse files
[11.x] Enable extension of connection inspection methods (#52231)
* Enable extension of connection inspection methods * Fix CS * Move the queries to the Grammar classes and show the driver name * Replace Grammar::getConnectionCount with compileConnectionCount * Add Connection::getDriverTitle and move compileConnectionCount to the Schema Grammar * Replace duplicate driver name by the connection name * New MySQL query * Move getConnectionCount to Schema\Builder * Add integration test for Schema::getConnectionCount() * Fix connection count query for mariadb * Fix Driver title * Pluralize ConnectionsCount * Move Schema::getConnectionsCount to Connection::getThreadsCount * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent eb1d94d commit 05421a0

15 files changed

+145
-29
lines changed

src/Illuminate/Database/Connection.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,18 @@ public function unprepared($query)
624624
});
625625
}
626626

627+
/**
628+
* Get the number of open connections for the database.
629+
*
630+
* @return int|null
631+
*/
632+
public function threadCount()
633+
{
634+
$query = $this->getQueryGrammar()->compileThreadCount();
635+
636+
return $query ? $this->scalar($query) : null;
637+
}
638+
627639
/**
628640
* Execute the given callback in "dry run" mode.
629641
*
@@ -1347,6 +1359,16 @@ public function getDriverName()
13471359
return $this->getConfig('driver');
13481360
}
13491361

1362+
/**
1363+
* Get a human-readable name for the given connection driver.
1364+
*
1365+
* @return string
1366+
*/
1367+
public function getDriverTitle()
1368+
{
1369+
return $this->getDriverName();
1370+
}
1371+
13501372
/**
13511373
* Get the query grammar used by the connection.
13521374
*

src/Illuminate/Database/Console/DatabaseInspectionCommand.php

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33
namespace Illuminate\Database\Console;
44

55
use Illuminate\Console\Command;
6+
use Illuminate\Database\Connection;
67
use Illuminate\Database\ConnectionInterface;
7-
use Illuminate\Database\MariaDbConnection;
8-
use Illuminate\Database\MySqlConnection;
9-
use Illuminate\Database\PostgresConnection;
10-
use Illuminate\Database\SQLiteConnection;
11-
use Illuminate\Database\SqlServerConnection;
128
use Illuminate\Support\Arr;
139

1410
abstract class DatabaseInspectionCommand extends Command
@@ -19,40 +15,25 @@ abstract class DatabaseInspectionCommand extends Command
1915
* @param \Illuminate\Database\ConnectionInterface $connection
2016
* @param string $database
2117
* @return string
18+
*
19+
* @deprecated
2220
*/
2321
protected function getConnectionName(ConnectionInterface $connection, $database)
2422
{
25-
return match (true) {
26-
$connection instanceof MySqlConnection && $connection->isMaria() => 'MariaDB',
27-
$connection instanceof MySqlConnection => 'MySQL',
28-
$connection instanceof MariaDbConnection => 'MariaDB',
29-
$connection instanceof PostgresConnection => 'PostgreSQL',
30-
$connection instanceof SQLiteConnection => 'SQLite',
31-
$connection instanceof SqlServerConnection => 'SQL Server',
32-
default => $database,
33-
};
23+
return $connection->getDriverTitle();
3424
}
3525

3626
/**
3727
* Get the number of open connections for a database.
3828
*
3929
* @param \Illuminate\Database\ConnectionInterface $connection
4030
* @return int|null
31+
*
32+
* @deprecated
4133
*/
4234
protected function getConnectionCount(ConnectionInterface $connection)
4335
{
44-
$result = match (true) {
45-
$connection instanceof MySqlConnection => $connection->selectOne('show status where variable_name = "threads_connected"'),
46-
$connection instanceof PostgresConnection => $connection->selectOne('select count(*) as "Value" from pg_stat_activity'),
47-
$connection instanceof SqlServerConnection => $connection->selectOne('select count(*) Value from sys.dm_exec_sessions where status = ?', ['running']),
48-
default => null,
49-
};
50-
51-
if (! $result) {
52-
return null;
53-
}
54-
55-
return Arr::wrap((array) $result)['Value'];
36+
return $connection->threadCount();
5637
}
5738

5839
/**

src/Illuminate/Database/Console/MonitorCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,11 @@ protected function parseDatabases($databases)
8585

8686
$maxConnections = $this->option('max');
8787

88+
$connections = $this->connection->connection($database)->threadCount();
89+
8890
return [
8991
'database' => $database,
90-
'connections' => $connections = $this->getConnectionCount($this->connection->connection($database)),
92+
'connections' => $connections,
9193
'status' => $maxConnections && $connections >= $maxConnections ? '<fg=yellow;options=bold>ALERT</>' : '<fg=green;options=bold>OK</>',
9294
];
9395
});

src/Illuminate/Database/Console/ShowCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ public function handle(ConnectionResolverInterface $connections)
4545
$data = [
4646
'platform' => [
4747
'config' => $this->getConfigFromDatabase($database),
48-
'name' => $this->getConnectionName($connection, $database),
48+
'name' => $connection->getDriverTitle(),
49+
'connection' => $connection->getName(),
4950
'version' => $connection->getServerVersion(),
50-
'open_connections' => $this->getConnectionCount($connection),
51+
'open_connections' => $connection->threadCount(),
5152
],
5253
'tables' => $this->tables($connection, $schema),
5354
];
@@ -159,6 +160,7 @@ protected function displayForCli(array $data)
159160
$this->newLine();
160161

161162
$this->components->twoColumnDetail('<fg=green;options=bold>'.$platform['name'].'</>', $platform['version']);
163+
$this->components->twoColumnDetail('Connection', Arr::get($platform['config'], 'connection'));
162164
$this->components->twoColumnDetail('Database', Arr::get($platform['config'], 'database'));
163165
$this->components->twoColumnDetail('Host', Arr::get($platform['config'], 'host'));
164166
$this->components->twoColumnDetail('Port', Arr::get($platform['config'], 'port'));

src/Illuminate/Database/MariaDbConnection.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212

1313
class MariaDbConnection extends MySqlConnection
1414
{
15+
/**
16+
* {@inheritdoc}
17+
*/
18+
public function getDriverTitle()
19+
{
20+
return 'MariaDB';
21+
}
22+
1523
/**
1624
* Determine if the connected database is a MariaDB database.
1725
*

src/Illuminate/Database/MySqlConnection.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414

1515
class MySqlConnection extends Connection
1616
{
17+
/**
18+
* {@inheritdoc}
19+
*/
20+
public function getDriverTitle()
21+
{
22+
return $this->isMaria() ? 'MariaDB' : 'MySQL';
23+
}
24+
1725
/**
1826
* Escape a binary value for safe SQL embedding.
1927
*

src/Illuminate/Database/PostgresConnection.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212

1313
class PostgresConnection extends Connection
1414
{
15+
/**
16+
* {@inheritdoc}
17+
*/
18+
public function getDriverTitle()
19+
{
20+
return 'PostgreSQL';
21+
}
22+
1523
/**
1624
* Escape a binary value for safe SQL embedding.
1725
*

src/Illuminate/Database/Query/Grammars/Grammar.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,6 +1429,16 @@ protected function compileLock(Builder $query, $value)
14291429
return is_string($value) ? $value : '';
14301430
}
14311431

1432+
/**
1433+
* Compile a query to get the number of open connections for a database.
1434+
*
1435+
* @return string|null
1436+
*/
1437+
public function compileThreadCount()
1438+
{
1439+
return null;
1440+
}
1441+
14321442
/**
14331443
* Determine if the grammar supports savepoints.
14341444
*

src/Illuminate/Database/Query/Grammars/MariaDbGrammar.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ public function compileJsonValueCast($value)
3333
return "json_query({$value}, '$')";
3434
}
3535

36+
/**
37+
* Compile a query to get the number of open connections for a database.
38+
*
39+
* @return string
40+
*/
41+
public function compileThreadCount()
42+
{
43+
return 'select variable_value as `Value` from information_schema.global_status where variable_name = \'THREADS_CONNECTED\'';
44+
}
45+
3646
/**
3747
* Determine whether to use a legacy group limit clause for MySQL < 8.0.
3848
*

src/Illuminate/Database/Query/Grammars/MySqlGrammar.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,16 @@ protected function compileDeleteWithoutJoins(Builder $query, $table, $where)
478478
return $sql;
479479
}
480480

481+
/**
482+
* Compile a query to get the number of open connections for a database.
483+
*
484+
* @return string
485+
*/
486+
public function compileThreadCount()
487+
{
488+
return 'select variable_value as `Value` from performance_schema.session_status where variable_name = \'threads_connected\'';
489+
}
490+
481491
/**
482492
* Wrap a single string in keyword identifiers.
483493
*

0 commit comments

Comments
 (0)