Skip to content

Commit bee8cd5

Browse files
committed
Move Schema::getConnectionsCount to Connection::getThreadsCount
1 parent f1e2205 commit bee8cd5

18 files changed

+67
-58
lines changed

src/Illuminate/Database/Connection.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,18 @@ public function raw($value)
10841084
return new Expression($value);
10851085
}
10861086

1087+
/**
1088+
* Get the number of open connections for a database.
1089+
*
1090+
* @return int|null
1091+
*/
1092+
public function getThreadsCount()
1093+
{
1094+
$query = $this->getQueryGrammar()->compileThreadsCount();
1095+
1096+
return $query ? $this->scalar($query) : null;
1097+
}
1098+
10871099
/**
10881100
* Escape a value for safe SQL embedding.
10891101
*

src/Illuminate/Database/Console/DatabaseInspectionCommand.php

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

55
use Illuminate\Console\Command;
6+
use Illuminate\Database\Connection;
67
use Illuminate\Database\ConnectionInterface;
78
use Illuminate\Support\Arr;
89

@@ -15,7 +16,7 @@ abstract class DatabaseInspectionCommand extends Command
1516
* @param string $database
1617
* @return string
1718
*
18-
* @deprecated Use $connection->getDriverTitle() instead.
19+
* @deprecated
1920
*/
2021
protected function getConnectionName(ConnectionInterface $connection, $database)
2122
{
@@ -28,11 +29,11 @@ protected function getConnectionName(ConnectionInterface $connection, $database)
2829
* @param \Illuminate\Database\ConnectionInterface $connection
2930
* @return int|null
3031
*
31-
* @deprecated Use Schema\Builder::getConnectionsCount() instead.
32+
* @deprecated
3233
*/
3334
protected function getConnectionCount(ConnectionInterface $connection)
3435
{
35-
return $connection->getSchemaBuilder()->getConnectionsCount();
36+
return $connection->getThreadsCount();
3637
}
3738

3839
/**

src/Illuminate/Database/Console/MonitorCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ protected function parseDatabases($databases)
8484
}
8585

8686
$maxConnections = $this->option('max');
87-
$connections = $this->connection->connection($database)->getSchemaBuilder()->getConnectionsCount();
87+
88+
$connections = $this->connection->connection($database)->getThreadsCount();
8889

8990
return [
9091
'database' => $database,

src/Illuminate/Database/Console/ShowCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function handle(ConnectionResolverInterface $connections)
4848
'name' => $connection->getDriverTitle(),
4949
'connection' => $connection->getName(),
5050
'version' => $connection->getServerVersion(),
51-
'open_connections' => $schema->getConnectionsCount(),
51+
'open_connections' => $connection->getThreadsCount(),
5252
],
5353
'tables' => $this->tables($connection, $schema),
5454
];

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,4 +1564,14 @@ public function getBitwiseOperators()
15641564
{
15651565
return $this->bitwiseOperators;
15661566
}
1567+
1568+
/**
1569+
* Get the number of open connections for a database.
1570+
*
1571+
* @return string|null
1572+
*/
1573+
public function compileThreadsCount()
1574+
{
1575+
return null;
1576+
}
15671577
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,9 @@ public function useLegacyGroupLimit(Builder $query)
4343
{
4444
return false;
4545
}
46+
47+
public function compileThreadsCount()
48+
{
49+
return 'select variable_value as `Value` from information_schema.global_status where variable_name = \'THREADS_CONNECTED\'';
50+
}
4651
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,4 +514,9 @@ protected function wrapJsonBooleanSelector($value)
514514

515515
return 'json_extract('.$field.$path.')';
516516
}
517+
518+
public function compileThreadsCount()
519+
{
520+
return 'select variable_value as `Value` from performance_schema.session_status where variable_name = \'threads_connected\'';
521+
}
517522
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,4 +762,9 @@ public function substituteBindingsIntoRawSql($sql, $bindings)
762762

763763
return $query;
764764
}
765+
766+
public function compileThreadsCount()
767+
{
768+
return 'select count(*) as "Value" from pg_stat_activity';
769+
}
765770
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,11 @@ public function compileSavepointRollBack($name)
497497
return 'ROLLBACK TRANSACTION '.$name;
498498
}
499499

500+
public function compileThreadsCount()
501+
{
502+
return 'select count(*) Value from sys.dm_exec_sessions where status = N\'running\'';
503+
}
504+
500505
/**
501506
* Get the format for database stored dates.
502507
*

src/Illuminate/Database/Schema/Builder.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -607,18 +607,6 @@ public function setConnection(Connection $connection)
607607
return $this;
608608
}
609609

610-
/**
611-
* Get the number of open connections for a database.
612-
*
613-
* @return int|null
614-
*/
615-
public function getConnectionsCount()
616-
{
617-
$query = $this->grammar->compileConnectionsCount();
618-
619-
return $query ? $this->connection->scalar($query) : null;
620-
}
621-
622610
/**
623611
* Set the Schema Blueprint resolver callback.
624612
*

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -382,14 +382,4 @@ public function supportsSchemaTransactions()
382382
{
383383
return $this->transactions;
384384
}
385-
386-
/**
387-
* Get the number of open connections for a database.
388-
*
389-
* @return string|null
390-
*/
391-
public function compileConnectionsCount()
392-
{
393-
return null;
394-
}
395385
}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,4 @@ protected function typeGeometry(Fluent $column)
5555
$column->srid ? ' ref_system_id='.$column->srid : ''
5656
);
5757
}
58-
59-
public function compileConnectionsCount()
60-
{
61-
return 'select variable_value as `Value` from information_schema.global_status where variable_name = \'THREADS_CONNECTED\'';
62-
}
6358
}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,9 +1337,4 @@ protected function wrapJsonSelector($value)
13371337

13381338
return 'json_unquote(json_extract('.$field.$path.'))';
13391339
}
1340-
1341-
public function compileConnectionsCount()
1342-
{
1343-
return 'select variable_value as `Value` from performance_schema.session_status where variable_name = \'threads_connected\'';
1344-
}
13451340
}

src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,9 +1201,4 @@ protected function modifyGeneratedAs(Blueprint $blueprint, Fluent $column)
12011201

12021202
return $sql;
12031203
}
1204-
1205-
public function compileConnectionsCount()
1206-
{
1207-
return 'select count(*) as "Value" from pg_stat_activity';
1208-
}
12091204
}

src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,9 +1054,4 @@ public function quoteString($value)
10541054

10551055
return "N'$value'";
10561056
}
1057-
1058-
public function compileConnectionsCount()
1059-
{
1060-
return 'select count(*) Value from sys.dm_exec_sessions where status = N\'running\'';
1061-
}
10621057
}

src/Illuminate/Support/Facades/Schema.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
* @method static void mixin(object $mixin, bool $replace = true)
4646
* @method static bool hasMacro(string $name)
4747
* @method static void flushMacros()
48-
* @method static int|null getConnectionsCount()
4948
*
5049
* @see \Illuminate\Database\Schema\Builder
5150
*/
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Database;
4+
5+
use Illuminate\Support\Facades\DB;
6+
7+
class ConnectionThreadsCountTest extends DatabaseTestCase
8+
{
9+
public function testGetThreadsCount()
10+
{
11+
$count = DB::connection()->getThreadsCount();
12+
if ($this->driver === 'sqlite') {
13+
$this->assertNull($count, 'SQLite does not support connection count');
14+
} else {
15+
$this->assertGreaterThanOrEqual(1, $count);
16+
}
17+
}
18+
}

tests/Integration/Database/SchemaBuilderTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -874,14 +874,4 @@ public function testAddingMacros()
874874
$this->assertTrue(Schema::hasForeignKeyForColumn('question_id', 'answers', 'questions'));
875875
$this->assertFalse(Schema::hasForeignKeyForColumn('body', 'answers', 'questions'));
876876
}
877-
878-
public function testGetConnectionsCount()
879-
{
880-
$count = Schema::getConnectionsCount();
881-
if ($this->driver === 'sqlite') {
882-
$this->assertNull($count, 'SQLite does not support connection count');
883-
} else {
884-
$this->assertGreaterThanOrEqual(1, $count);
885-
}
886-
}
887877
}

0 commit comments

Comments
 (0)