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
|
|
|
$existing_tables = $this->db->fetchColumnAll('SHOW TABLES'); |
50
|
|
|
|
51
|
|
|
if (!empty($tables)) { |
52
|
|
|
$confirm = $this->choose($this->text('Are you sure you want to empty the tables? It cannot be undone!')); |
53
|
|
|
} else if (!empty($all)) { |
54
|
|
|
$confirm = $this->choose($this->text('Are you sure you want to empty ALL tables in the database? It cannot be undone!')); |
55
|
|
|
$tables = $existing_tables; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($confirm === 'y') { |
59
|
|
|
foreach (array_intersect($tables, $existing_tables) as $table) { |
60
|
|
|
$this->db->query("TRUNCATE TABLE `$table`")->execute(); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->output(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Callback for "database-drop" command |
69
|
|
|
*/ |
70
|
|
|
public function cmdDropDatabase() |
71
|
|
|
{ |
72
|
|
|
$all = $this->getParam('all'); |
73
|
|
|
$tables = $this->getArguments(); |
74
|
|
|
|
75
|
|
|
if (empty($tables) && empty($all)) { |
76
|
|
|
$this->errorExit($this->text('Invalid command')); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$confirm = null; |
80
|
|
|
$existing_tables = $this->db->fetchColumnAll('SHOW TABLES'); |
81
|
|
|
|
82
|
|
|
if (!empty($tables)) { |
83
|
|
|
$confirm = $this->choose($this->text('Are you sure you want to DELETE the tables? It cannot be undone!')); |
84
|
|
|
} else if (!empty($all)) { |
85
|
|
|
$confirm = $this->choose($this->text('Are you sure you want to DELETE ALL tables in the database? It cannot be undone!')); |
86
|
|
|
$tables = $existing_tables; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($confirm === 'y') { |
90
|
|
|
foreach (array_intersect($tables, $existing_tables) as $table) { |
91
|
|
|
$this->db->query("DROP TABLE IF EXISTS `$table`")->execute(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->output(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
} |
99
|
|
|
|