1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package CLI |
5
|
|
|
* @author Iurii Makukh <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2018, Iurii Makukh <[email protected]> |
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace gplcart\modules\cli\controllers; |
11
|
|
|
|
12
|
|
|
use gplcart\modules\cli\controllers\Base; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Handles commands related to database operations |
16
|
|
|
*/ |
17
|
|
|
class Database extends Base |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Database class instance |
22
|
|
|
* @var \gplcart\core\Database $db |
23
|
|
|
*/ |
24
|
|
|
protected $db; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructor |
28
|
|
|
*/ |
29
|
|
|
public function __construct() |
30
|
|
|
{ |
31
|
|
|
parent::__construct(); |
32
|
|
|
|
33
|
|
|
$this->db = $this->config->getDb(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Callback for "database-truncate" command |
38
|
|
|
*/ |
39
|
|
|
public function cmdTruncateDatabase() |
40
|
|
|
{ |
41
|
|
|
$all = $this->getParam('all'); |
42
|
|
|
$tables = $this->getArguments(); |
43
|
|
|
|
44
|
|
|
if (empty($tables) && empty($all)) { |
45
|
|
|
$this->errorExit($this->text('Invalid command')); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$confirm = null; |
49
|
|
|
|
50
|
|
|
if (!empty($tables)) { |
51
|
|
|
$confirm = $this->choose($this->text('Are you sure you want to empty the tables? It cannot be undone!')); |
52
|
|
|
} else if (!empty($all)) { |
53
|
|
|
$confirm = $this->choose($this->text('Are you sure you want to empty ALL tables in the database? It cannot be undone!')); |
54
|
|
|
$tables = $this->db->fetchColumnAll('SHOW TABLES'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ($confirm === 'y') { |
58
|
|
|
foreach ($tables as $table) { |
59
|
|
|
$this->db->query("TRUNCATE TABLE `$table`")->execute(); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->output(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Callback for "database-drop" command |
68
|
|
|
*/ |
69
|
|
|
public function cmdDropDatabase() |
70
|
|
|
{ |
71
|
|
|
$all = $this->getParam('all'); |
72
|
|
|
$tables = $this->getArguments(); |
73
|
|
|
|
74
|
|
|
if (empty($tables) && empty($all)) { |
75
|
|
|
$this->errorExit($this->text('Invalid command')); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$confirm = null; |
79
|
|
|
|
80
|
|
|
if (!empty($tables)) { |
81
|
|
|
$confirm = $this->choose($this->text('Are you sure you want to DELETE the tables? It cannot be undone!')); |
82
|
|
|
} else if (!empty($all)) { |
83
|
|
|
$confirm = $this->choose($this->text('Are you sure you want to DELETE ALL tables in the database? It cannot be undone!')); |
84
|
|
|
$tables = $this->db->fetchColumnAll('SHOW TABLES'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if ($confirm === 'y') { |
88
|
|
|
foreach ($tables as $table) { |
89
|
|
|
$this->db->query("DROP TABLE IF EXISTS `$table`")->execute(); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->output(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Callback for "database-add" command |
98
|
|
|
*/ |
99
|
|
|
public function cmdAddDatabase() |
100
|
|
|
{ |
101
|
|
|
|
102
|
|
|
$table = $this->getParam(0); |
103
|
|
|
$data = $this->getOptions(); |
104
|
|
|
|
105
|
|
|
if (empty($table) || empty($data)) { |
106
|
|
|
$this->errorExit($this->text('Invalid command')); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->line($this->db->insert($table, $data)); |
110
|
|
|
$this->output(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Callback for "database-delete" command |
115
|
|
|
*/ |
116
|
|
|
public function cmdDeleteDatabase() |
117
|
|
|
{ |
118
|
|
|
$table = $this->getParam(0); |
119
|
|
|
$conditions = $this->getOptions(); |
120
|
|
|
|
121
|
|
|
if (empty($table) || empty($conditions)) { |
122
|
|
|
$this->errorExit($this->text('Invalid command')); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if (!$this->db->delete($table, $conditions)) { |
126
|
|
|
$this->errorExit($this->text('An error occurred')); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$this->output(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Callback for "database-sql" command |
134
|
|
|
*/ |
135
|
|
|
public function cmdSqlDatabase() |
136
|
|
|
{ |
137
|
|
|
$sql = $this->getParam(0); |
138
|
|
|
|
139
|
|
|
if (empty($sql)) { |
140
|
|
|
$this->errorExit($this->text('Invalid command')); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$result = $this->db->query($sql)->fetchAll(\PDO::FETCH_ASSOC); |
144
|
|
|
|
145
|
|
|
$this->outputFormat($result); |
146
|
|
|
$this->outputFormatTableDatabase($result); |
147
|
|
|
$this->output(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Output table format |
152
|
|
|
* @param array $items |
153
|
|
|
*/ |
154
|
|
|
protected function outputFormatTableDatabase(array $items) |
155
|
|
|
{ |
156
|
|
|
$header = $rows = array(); |
157
|
|
|
|
158
|
|
|
if (!empty($items)) { |
159
|
|
|
|
160
|
|
|
$first = reset($items); |
161
|
|
|
$header = array_keys($first); |
162
|
|
|
|
163
|
|
|
foreach ($items as $item) { |
164
|
|
|
$rows[] = array_values($item); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$this->outputFormatTable($rows, $header); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
} |
172
|
|
|
|