|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
namespace Y0lk\SQLDumper; |
|
3
|
|
|
|
|
4
|
|
|
use ArrayObject; |
|
5
|
|
|
use PDO; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* A TableDumperCollection is used to group TableDumper objects together, allowing you to specify dump options on multiple table at once. |
|
10
|
|
|
* All TableDumper methods can be called directly on a TableDumperCollection, and will be executed on all the TableDumper instances in that collection. |
|
11
|
|
|
* |
|
12
|
|
|
* @author Gabriel Jean <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class TableDumperCollection extends ArrayObject |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* {@inheritDoc} |
|
18
|
|
|
*/ |
|
19
|
15 |
|
public function append($value): void |
|
20
|
|
|
{ |
|
21
|
|
|
//Make sure we're adding a TableDumper object |
|
22
|
15 |
|
if (!($value instanceof TableDumper)) { |
|
23
|
3 |
|
throw new InvalidArgumentException("TableDumperCollection only accepts TableDumper objects", 1); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
//Append with table_name as key |
|
27
|
12 |
|
$this->offsetSet($value->getTable()->getName(), $value); |
|
28
|
12 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritDoc} |
|
32
|
|
|
*/ |
|
33
|
72 |
|
public function offsetSet($index, $newval): void |
|
34
|
|
|
{ |
|
35
|
|
|
//Make sure we're adding a TableDumper object |
|
36
|
72 |
|
if (!($newval instanceof TableDumper)) { |
|
37
|
3 |
|
throw new InvalidArgumentException("TableDumperCollection only accepts TableDumper objects", 1); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
//Append with table_name as key |
|
41
|
69 |
|
parent::offsetSet($newval->getTable()->getName(), $newval); |
|
42
|
69 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param Table|string $table Adds a table, either by name, or by Table instance, to the collection |
|
47
|
|
|
* |
|
48
|
|
|
* @return TableDumper Returns a TableDumper of the table that was just added |
|
49
|
|
|
*/ |
|
50
|
54 |
|
public function addTable($table): TableDumper |
|
51
|
|
|
{ |
|
52
|
54 |
|
if ($table instanceof Table) { |
|
53
|
6 |
|
$tableName = $table->getName(); |
|
54
|
|
|
} else { |
|
55
|
48 |
|
$tableName = $table; |
|
56
|
48 |
|
$table = new Table($tableName); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
//First check if a dumper already exists for this table |
|
60
|
54 |
|
if (!$this->offsetExists($tableName)) { |
|
61
|
|
|
//Create new one |
|
62
|
54 |
|
$this->offsetSet($tableName, new TableDumper($table)); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
54 |
|
return $this->offsetGet($tableName); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param TableDumperCollection|array<TableDumper|Table|string> $listTables Adds a list of tables, either by passing TableDumperCollection, or an array containing either TableDumper objects, Table objects or table naes |
|
71
|
|
|
* |
|
72
|
|
|
* @return TableDumperCollection Returns a TableDumperCollection of the list of tables that was just added |
|
73
|
|
|
*/ |
|
74
|
30 |
|
public function addListTables($listTables): TableDumperCollection |
|
75
|
|
|
{ |
|
76
|
|
|
//If arg is a TableDumperCollection, merge into this one |
|
77
|
30 |
|
if ($listTables instanceof TableDumperCollection) { |
|
78
|
3 |
|
foreach ($listTables as $table) { |
|
79
|
3 |
|
$this->append($table); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
3 |
|
return $listTables; |
|
83
|
|
|
} else { |
|
84
|
27 |
|
return $this->addListTableArray($listTables); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Adds a list of tables passed as an array |
|
90
|
|
|
* @param array $listTables Array of tables to add |
|
91
|
|
|
* |
|
92
|
|
|
* @return TableDumperCollection Returns a TableDumperCollection of the list of tables that was just added |
|
93
|
|
|
*/ |
|
94
|
27 |
|
protected function addListTableArray(array $listTables): TableDumperCollection |
|
95
|
|
|
{ |
|
96
|
|
|
//Create TableDumperCollection |
|
97
|
27 |
|
$listDumpers = new TableDumperCollection; |
|
98
|
|
|
|
|
99
|
27 |
|
foreach ($listTables as $table) { |
|
100
|
|
|
//If table is already a Dumper, simply append to this |
|
101
|
27 |
|
if ($table instanceof TableDumper) { |
|
102
|
6 |
|
$listDumpers[] = $table; |
|
103
|
6 |
|
$this->append($table); |
|
104
|
|
|
} else { |
|
105
|
21 |
|
$listDumpers[] = $this->addTable($table); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
27 |
|
return $listDumpers; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Writes all DROP statements to the dump stream |
|
114
|
|
|
* |
|
115
|
|
|
* @param resource $stream Stream to write the dump to |
|
116
|
|
|
* |
|
117
|
|
|
* @return void |
|
118
|
|
|
*/ |
|
119
|
9 |
|
public function dumpDropStatements($stream): void |
|
120
|
|
|
{ |
|
121
|
9 |
|
foreach ($this as $dumper) { |
|
122
|
9 |
|
if ($dumper->hasDrop()) { |
|
123
|
9 |
|
$dumper->dumpDropStatement($stream); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
9 |
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Writes all INSERT statements to the dump stream |
|
130
|
|
|
* |
|
131
|
|
|
* @param PDO $db PDO instance to use for DB queries |
|
132
|
|
|
* @param resource $stream Stream to write the dump to |
|
133
|
|
|
* |
|
134
|
|
|
* @return void |
|
135
|
|
|
*/ |
|
136
|
9 |
|
public function dumpInsertStatements(PDO $db, $stream): void |
|
137
|
|
|
{ |
|
138
|
9 |
|
foreach ($this as $dumper) { |
|
139
|
9 |
|
if ($dumper->hasData()) { |
|
140
|
9 |
|
$dumper->dumpInsertStatement($db, $stream); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
9 |
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Writes all the SQL statements of this dumper to the dump stream |
|
147
|
|
|
* |
|
148
|
|
|
* @param PDO $db PDO instance to use for DB queries |
|
149
|
|
|
* @param resource $stream Stream to write the dump to |
|
150
|
|
|
* @param boolean $groupDrops Determines if DROP statements will be grouped |
|
151
|
|
|
* @param boolean $groupInserts Determines if INSERT statements will be grouped |
|
152
|
|
|
* |
|
153
|
|
|
* @return void |
|
154
|
|
|
*/ |
|
155
|
33 |
|
public function dump(PDO $db, $stream, bool $groupDrops = false, bool $groupInserts = false): void |
|
156
|
|
|
{ |
|
157
|
33 |
|
if ($groupDrops) { |
|
158
|
6 |
|
$this->dumpDropStatements($stream); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
33 |
|
foreach ($this as $dumper) { |
|
162
|
33 |
|
if (!$groupDrops) { |
|
163
|
27 |
|
$dumper->dumpDropStatement($stream); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
33 |
|
$dumper->dumpCreateStatement($db, $stream); |
|
167
|
|
|
|
|
168
|
33 |
|
if (!$groupInserts) { |
|
169
|
27 |
|
$dumper->dumpInsertStatement($db, $stream); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
33 |
|
if ($groupInserts) { |
|
174
|
6 |
|
$this->dumpInsertStatements($db, $stream); |
|
175
|
|
|
} |
|
176
|
33 |
|
} |
|
177
|
|
|
|
|
178
|
|
|
|
|
179
|
6 |
|
public function __call(string $name, array $arguments) |
|
180
|
|
|
{ |
|
181
|
|
|
//Call methods on TableDumper values |
|
182
|
6 |
|
foreach ($this as $value) { |
|
183
|
6 |
|
call_user_func_array([$value, $name], $arguments); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
6 |
|
return $this; |
|
187
|
|
|
} |
|
188
|
|
|
} |