|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Recca0120\Terminal\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\DatabaseManager; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use stdClass; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
9
|
|
|
|
|
10
|
|
|
class Mysql extends Command |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* The console command name. |
|
14
|
|
|
* |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $name = 'mysql'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The console command description. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $description = 'mysql console'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* $connection. |
|
28
|
|
|
* |
|
29
|
|
|
* @var DatabaseManager |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $databaseManager; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* __construct. |
|
35
|
|
|
* |
|
36
|
|
|
* @param DatabaseManager $databaseManager |
|
37
|
1 |
|
*/ |
|
38
|
|
|
public function __construct(DatabaseManager $databaseManager) |
|
39
|
1 |
|
{ |
|
40
|
|
|
parent::__construct(); |
|
41
|
1 |
|
|
|
42
|
1 |
|
$this->databaseManager = $databaseManager; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Handle the command. |
|
47
|
|
|
* |
|
48
|
|
|
* @throws \InvalidArgumentException |
|
49
|
1 |
|
*/ |
|
50
|
|
|
public function handle() |
|
51
|
1 |
|
{ |
|
52
|
1 |
|
$sql = $this->option('command'); |
|
53
|
1 |
|
$connection = $this->databaseManager->connection($this->option('connection')); |
|
54
|
1 |
|
$rows = $this->castArray($connection->select($sql, [], true)); |
|
55
|
1 |
|
$headers = array_keys(Arr::get($rows, 0, [])); |
|
56
|
1 |
|
$this->table($headers, $rows); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* castArray. |
|
61
|
|
|
* |
|
62
|
|
|
* @param stdClass[] $rows |
|
63
|
|
|
* @return array[] |
|
64
|
1 |
|
*/ |
|
65
|
|
|
protected function castArray($rows) |
|
66
|
1 |
|
{ |
|
67
|
1 |
|
return array_map(static function ($row) { |
|
68
|
1 |
|
return (array) $row; |
|
69
|
|
|
}, $rows); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get the console command options. |
|
74
|
|
|
* |
|
75
|
|
|
* @return array |
|
76
|
1 |
|
*/ |
|
77
|
|
|
protected function getOptions() |
|
78
|
|
|
{ |
|
79
|
1 |
|
return [ |
|
80
|
1 |
|
['command', null, InputOption::VALUE_REQUIRED], |
|
81
|
|
|
['connection', null, InputOption::VALUE_OPTIONAL], |
|
82
|
|
|
]; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|