1
|
|
|
<?php |
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) |
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 |
|
return $this->offsetSet($value->getTable()->getName(), $value); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritDoc} |
32
|
|
|
*/ |
33
|
72 |
|
public function offsetSet($index, $newval) |
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 |
|
return parent::offsetSet($newval->getTable()->getName(), $newval); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Table|string Adds a table, either by name, or by Table instance, to the collection |
47
|
|
|
* |
48
|
|
|
* @return TableDumper Retruns a TableDumper of the table that was just added |
49
|
|
|
*/ |
50
|
57 |
|
public function addTable($table): TableDumper |
51
|
|
|
{ |
52
|
57 |
|
if ($table instanceof Table) { |
53
|
6 |
|
$tableName = $table->getName(); |
54
|
51 |
|
} elseif (is_string($table)) { |
55
|
48 |
|
$tableName = $table; |
56
|
48 |
|
$table = new Table($tableName); |
57
|
|
|
} else { |
58
|
3 |
|
throw new InvalidArgumentException("Invalid value supplied for argument 'table'", 1); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
//First check if a dumper already exists for this table |
62
|
54 |
|
if (!$this->offsetExists($tableName)) { |
63
|
|
|
//Create new one |
64
|
54 |
|
$this->offsetSet($tableName, new TableDumper($table)); |
65
|
|
|
} |
66
|
|
|
|
67
|
54 |
|
return $this->offsetGet($tableName); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param TableDumperCollection|array<TableDumper|Table|string> Adds a list of tables, either by passing TableDumperCollection, or an array containing either TableDumper objects, Table objects or table naes |
73
|
|
|
* |
74
|
|
|
* @return TableDumperCollection Returns a TableDumperCollection of the list of tables that was just added |
75
|
|
|
*/ |
76
|
33 |
|
public function addListTables($listTables): TableDumperCollection |
77
|
|
|
{ |
78
|
|
|
//If arg is a TableDumperCollection, merge into this one |
79
|
33 |
|
if ($listTables instanceof TableDumperCollection) { |
80
|
3 |
|
foreach ($listTables as $table) { |
81
|
3 |
|
$this->append($table); |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
return $listTables; |
85
|
30 |
|
} elseif (is_array($listTables)) { |
86
|
|
|
//Create TableDumperCollection |
87
|
27 |
|
$listDumpers = new TableDumperCollection; |
88
|
|
|
|
89
|
27 |
|
foreach ($listTables as $table) { |
90
|
|
|
//If table is already a Dumper, simply append to this |
91
|
27 |
|
if ($table instanceof TableDumper) { |
92
|
6 |
|
$listDumpers[] = $table; |
93
|
6 |
|
$this->append($table); |
94
|
|
|
} else { |
95
|
21 |
|
$listDumpers[] = $this->addTable($table); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
27 |
|
return $listDumpers; |
100
|
|
|
} |
101
|
|
|
|
102
|
3 |
|
throw new \InvalidArgumentException("Invalid value supplied for argument 'listTables'", 1); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Writes all DROP statements to the dump stream |
107
|
|
|
* |
108
|
|
|
* @param resource $stream Stream to write the dump to |
109
|
|
|
* |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
9 |
|
public function dumpDropStatements($stream): void |
113
|
|
|
{ |
114
|
9 |
|
foreach ($this as $dumper) { |
115
|
9 |
|
if ($dumper->hasDrop()) { |
116
|
9 |
|
$dumper->dumpDropStatement($stream); |
117
|
|
|
} |
118
|
|
|
} |
119
|
9 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Writes all INSERT statements to the dump stream |
123
|
|
|
* |
124
|
|
|
* @param PDO $db PDO instance to use for DB queries |
125
|
|
|
* @param resource $stream Stream to write the dump to |
126
|
|
|
* |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
9 |
|
public function dumpInsertStatements(PDO $db, $stream): void |
130
|
|
|
{ |
131
|
9 |
|
foreach ($this as $dumper) { |
132
|
9 |
|
if ($dumper->hasData()) { |
133
|
9 |
|
$dumper->dumpInsertStatement($db, $stream); |
134
|
|
|
} |
135
|
|
|
} |
136
|
9 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Writes all the SQL statements of this dumper to the dump stream |
140
|
|
|
* |
141
|
|
|
* @param PDO $db PDO instance to use for DB queries |
142
|
|
|
* @param resource $stream Stream to write the dump to |
143
|
|
|
* @param boolean $groupDrops Determines if DROP statements will be grouped |
144
|
|
|
* @param boolean $groupInserts Determines if INSERT statements will be grouped |
145
|
|
|
* |
146
|
|
|
* @return void |
147
|
|
|
*/ |
148
|
30 |
|
public function dump(PDO $db, $stream, bool $groupDrops = false, bool $groupInserts = false): void |
149
|
|
|
{ |
150
|
30 |
|
if ($groupDrops) { |
151
|
6 |
|
$this->dumpDropStatements($stream); |
152
|
|
|
} |
153
|
|
|
|
154
|
30 |
|
foreach ($this as $dumper) { |
155
|
30 |
|
if ($dumper->hasDrop() && !$groupDrops) { |
156
|
12 |
|
$dumper->dumpDropStatement($stream); |
157
|
|
|
} |
158
|
|
|
|
159
|
30 |
|
if ($dumper->hasStructure()) { |
160
|
18 |
|
$dumper->dumpCreateStatement($db, $stream); |
161
|
|
|
} |
162
|
|
|
|
163
|
30 |
|
if ($dumper->hasData() && !$groupInserts) { |
164
|
15 |
|
$dumper->dumpInsertStatement($db, $stream); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
30 |
|
if ($groupInserts) { |
169
|
6 |
|
$this->dumpInsertStatements($db, $stream); |
170
|
|
|
} |
171
|
30 |
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
6 |
|
public function __call(string $name, array $arguments) |
175
|
|
|
{ |
176
|
|
|
//Call methods on TableDumper values |
177
|
6 |
|
foreach ($this as $value) { |
178
|
6 |
|
call_user_func_array([$value, $name], $arguments); |
179
|
|
|
} |
180
|
|
|
|
181
|
6 |
|
return $this; |
182
|
|
|
} |
183
|
|
|
} |