Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
87.18% |
34 / 39 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
ShowSchema | |
87.18% |
34 / 39 |
|
50.00% |
1 / 2 |
6.08 | |
0.00% |
0 / 1 |
run | |
76.19% |
16 / 21 |
|
0.00% |
0 / 1 |
4.22 | |||
getTableList | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
2 |
1 | <?php declare(strict_types=1); |
2 | /* |
3 | * This file is part of Aplus Framework Dev Commands Library. |
4 | * |
5 | * (c) Natan Felles <natanfelles@gmail.com> |
6 | * |
7 | * For the full copyright and license information, please view the LICENSE |
8 | * file that was distributed with this source code. |
9 | */ |
10 | namespace Framework\CLI\Commands; |
11 | |
12 | use Framework\CLI\CLI; |
13 | use Framework\Debug\Debugger; |
14 | |
15 | /** |
16 | * Class ShowSchema. |
17 | * |
18 | * @package dev-commands |
19 | */ |
20 | class ShowSchema extends DatabaseCommand |
21 | { |
22 | protected string $description = 'Shows database schema information.'; |
23 | |
24 | public function run() : void |
25 | { |
26 | $this->setDatabase(); |
27 | $schema = $this->console->getArgument(0); |
28 | if (empty($schema)) { |
29 | $schema = CLI::prompt('Enter a schema name'); |
30 | CLI::newLine(); |
31 | } |
32 | $show = $this->getDatabase()->query( |
33 | 'SHOW DATABASES LIKE ' . $this->getDatabase()->quote($schema) |
34 | )->fetchArray(); |
35 | if (empty($show)) { |
36 | CLI::beep(); |
37 | CLI::error('Schema not found: ' . $schema); |
38 | return; |
39 | } |
40 | $list = $this->getTableList($schema); |
41 | if ($list) { |
42 | CLI::write( |
43 | CLI::style('Schema: ', 'white') . CLI::style($schema, 'yellow') |
44 | ); |
45 | CLI::table($list, \array_keys($list[0])); |
46 | CLI::write('Total: ' . \count($list)); |
47 | return; |
48 | } |
49 | CLI::write('No tables.'); |
50 | } |
51 | |
52 | /** |
53 | * @param string $schema |
54 | * |
55 | * @return array<int,array<string,string>> |
56 | */ |
57 | public function getTableList(string $schema) : array |
58 | { |
59 | $sql = 'SELECT TABLE_NAME, ENGINE, TABLE_COLLATION, DATA_LENGTH, INDEX_LENGTH, DATA_FREE, AUTO_INCREMENT, TABLE_ROWS, TABLE_COMMENT |
60 | FROM information_schema.TABLES WHERE TABLE_SCHEMA = ' . $this->getDatabase() |
61 | ->quote($schema) . ' ORDER BY TABLE_NAME'; |
62 | $tables = $this->getDatabase()->query($sql)->fetchArrayAll(); |
63 | $list = []; |
64 | foreach ($tables as $table) { |
65 | $list[] = [ |
66 | 'Table' => $table['TABLE_NAME'], |
67 | 'Engine' => $table['ENGINE'], |
68 | 'Collation' => $table['TABLE_COLLATION'], |
69 | 'Data Length' => Debugger::convertSize($table['DATA_LENGTH'] ?? 0), |
70 | 'Index Length' => Debugger::convertSize($table['INDEX_LENGTH'] ?? 0), |
71 | 'Data Free' => Debugger::convertSize($table['DATA_FREE'] ?? 0), |
72 | 'Auto Increment' => $table['AUTO_INCREMENT'], |
73 | 'Rows' => $table['TABLE_ROWS'], |
74 | 'Comment' => $table['TABLE_COMMENT'], |
75 | ]; |
76 | } |
77 | return $list; |
78 | } |
79 | } |