DatabaseInspectionCommand.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace Illuminate\Database\Console;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Database\ConnectionInterface;
  5. use Illuminate\Database\MariaDbConnection;
  6. use Illuminate\Database\MySqlConnection;
  7. use Illuminate\Database\PostgresConnection;
  8. use Illuminate\Database\SQLiteConnection;
  9. use Illuminate\Database\SqlServerConnection;
  10. use Illuminate\Support\Arr;
  11. abstract class DatabaseInspectionCommand extends Command
  12. {
  13. /**
  14. * Get a human-readable name for the given connection.
  15. *
  16. * @param \Illuminate\Database\ConnectionInterface $connection
  17. * @param string $database
  18. * @return string
  19. */
  20. protected function getConnectionName(ConnectionInterface $connection, $database)
  21. {
  22. return match (true) {
  23. $connection instanceof MySqlConnection && $connection->isMaria() => 'MariaDB',
  24. $connection instanceof MySqlConnection => 'MySQL',
  25. $connection instanceof MariaDbConnection => 'MariaDB',
  26. $connection instanceof PostgresConnection => 'PostgreSQL',
  27. $connection instanceof SQLiteConnection => 'SQLite',
  28. $connection instanceof SqlServerConnection => 'SQL Server',
  29. default => $database,
  30. };
  31. }
  32. /**
  33. * Get the number of open connections for a database.
  34. *
  35. * @param \Illuminate\Database\ConnectionInterface $connection
  36. * @return int|null
  37. */
  38. protected function getConnectionCount(ConnectionInterface $connection)
  39. {
  40. $result = match (true) {
  41. $connection instanceof MySqlConnection => $connection->selectOne('show status where variable_name = "threads_connected"'),
  42. $connection instanceof PostgresConnection => $connection->selectOne('select count(*) as "Value" from pg_stat_activity'),
  43. $connection instanceof SqlServerConnection => $connection->selectOne('select count(*) Value from sys.dm_exec_sessions where status = ?', ['running']),
  44. default => null,
  45. };
  46. if (! $result) {
  47. return null;
  48. }
  49. return Arr::wrap((array) $result)['Value'];
  50. }
  51. /**
  52. * Get the connection configuration details for the given connection.
  53. *
  54. * @param string $database
  55. * @return array
  56. */
  57. protected function getConfigFromDatabase($database)
  58. {
  59. $database ??= config('database.default');
  60. return Arr::except(config('database.connections.'.$database), ['password']);
  61. }
  62. /**
  63. * Remove the table prefix from a table name, if it exists.
  64. *
  65. * @param \Illuminate\Database\ConnectionInterface $connection
  66. * @param string $table
  67. * @return string
  68. */
  69. protected function withoutTablePrefix(ConnectionInterface $connection, string $table)
  70. {
  71. $prefix = $connection->getTablePrefix();
  72. return str_starts_with($table, $prefix)
  73. ? substr($table, strlen($prefix))
  74. : $table;
  75. }
  76. }