Skip to content

Commit 2ea3c55

Browse files
committed
Enable extension of connection inspection methods
1 parent 82f9d3d commit 2ea3c55

File tree

7 files changed

+78
-21
lines changed

7 files changed

+78
-21
lines changed

src/Illuminate/Database/Connection.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,24 @@ public function getDriverName()
13471347
return $this->getConfig('driver');
13481348
}
13491349

1350+
/**
1351+
* Get a human-readable name for the given connection.
1352+
*/
1353+
public function getConnectionName(): string
1354+
{
1355+
return $this->getName() . '(' . $this->getDriverName() . ')';
1356+
}
1357+
1358+
/**
1359+
* Get the number of open connections for a database.
1360+
*
1361+
* @return int|null
1362+
*/
1363+
public function getConnectionCount(): ?int
1364+
{
1365+
return null;
1366+
}
1367+
13501368
/**
13511369
* Get the query grammar used by the connection.
13521370
*

src/Illuminate/Database/Console/DatabaseInspectionCommand.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,7 @@ abstract class DatabaseInspectionCommand extends Command
2222
*/
2323
protected function getConnectionName(ConnectionInterface $connection, $database)
2424
{
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-
};
25+
return method_exists($connection, 'getConnectionName') ? $connection->getConnectionName() : $database;
3426
}
3527

3628
/**
@@ -41,18 +33,7 @@ protected function getConnectionName(ConnectionInterface $connection, $database)
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 method_exists($connection, 'getConnectionCount') ? $connection->getConnectionCount() : null;
5637
}
5738

5839
/**

src/Illuminate/Database/MariaDbConnection.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public function getServerVersion(): string
3232
return Str::between(parent::getServerVersion(), '5.5.5-', '-MariaDB');
3333
}
3434

35+
public function getConnectionName(): string
36+
{
37+
return $this->getName() . ' (MariaDB)';
38+
}
39+
3540
/**
3641
* Get the default query grammar instance.
3742
*

src/Illuminate/Database/MySqlConnection.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@ public function isMaria()
4848
return str_contains($this->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION), 'MariaDB');
4949
}
5050

51+
public function getConnectionName(): string
52+
{
53+
return $this->getName() . ' (' . ($this->isMaria() ? 'MariaDB' : 'MySQL') . ')';
54+
}
55+
56+
public function getConnectionCount(): ?int
57+
{
58+
$result = $this->selectOne('show status where variable_name = "threads_connected"');
59+
60+
if (! $result) {
61+
return null;
62+
}
63+
64+
return (int) $result['Value'];
65+
}
66+
5167
/**
5268
* Get the server version for the connection.
5369
*

src/Illuminate/Database/PostgresConnection.php

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

1313
class PostgresConnection extends Connection
1414
{
15+
public function getConnectionName(): string
16+
{
17+
return $this->getName() . ' (PostgreSQL)';
18+
}
19+
20+
public function getConnectionCount(): ?int
21+
{
22+
$result = $this->selectOne('select count(*) as "Value" from pg_stat_activity');
23+
24+
if (! $result) {
25+
return null;
26+
}
27+
28+
return (int) $result['Value'];
29+
}
30+
1531
/**
1632
* Escape a binary value for safe SQL embedding.
1733
*

src/Illuminate/Database/SQLiteConnection.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public function __construct($pdo, $database = '', $tablePrefix = '', array $conf
3131
$this->configureSynchronous();
3232
}
3333

34+
public function getConnectionName(): string
35+
{
36+
return $this->getName() . ' (SQLite)';
37+
}
38+
3439
/**
3540
* Enable or disable foreign key constraints if configured.
3641
*

src/Illuminate/Database/SqlServerConnection.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,22 @@ protected function getDefaultQueryGrammar()
9090
return $this->withTablePrefix($grammar);
9191
}
9292

93+
public function getConnectionName(): string
94+
{
95+
return $this->getName() . ' (SQL Server)';
96+
}
97+
98+
public function getConnectionCount(): ?int
99+
{
100+
$result = $this->selectOne('select count(*) Value from sys.dm_exec_sessions where status = ?', ['running']);
101+
102+
if (! $result) {
103+
return null;
104+
}
105+
106+
return (int) $result['Value'];
107+
}
108+
93109
/**
94110
* Get a schema builder instance for the connection.
95111
*

0 commit comments

Comments
 (0)