Completed
Push — master ( 6749c3...175485 )
by Gabriel
01:38
created

TableDumper::dumpDropStatement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace Y0lk\SQLDumper;
3
4
use PDO;
5
6
/**
7
 * A TableDumper instance is used to dump a single Table, allowing you to specify certain dump options for this table only
8
 *
9
 * @author Gabriel Jean <[email protected]>
10
 */
11
class TableDumper {
12
13
    /**
14
     * @var Table   Table instance related to this dumper
15
     */
16
    protected $table;
17
18
    /**
19
     * @var boolean Specifies whether to include the table's structure (CREATE statement)
20
     */
21
    protected $withStructure = true;
22
23
    /**
24
     * @var boolean Specifies whether to include the table's data (INSERT statements)
25
     */
26
    protected $withData = true;
27
28
    /**
29
     * @var boolean Specifies whether to include a DROP TABLE statement
30
     */
31
    protected $withDrop = true;
32
33
    /**
34
     * @var string WHERE parameters written as a regular SQL string to select specific data from this table
35
     */
36
    protected $where = '';
37
38
39
    /**
40
     * @param Table Table this dumper will be used for
41
     */
42 87
    public function __construct(Table $table)
43
    {
44 87
        $this->table = $table;
45 87
    }
46
47
    /**
48
     * Specifies whether to include the table's structure (CREATE statement)
49
     * 
50
     * @param  boolean  $withStructure  TRUE to include structure, FALSE otherwise
51
     * 
52
     * @return TableDumper  Returns the TableDumper instance
53
     */
54 30
    public function withStructure($withStructure = true)
55
    {
56 30
        $this->withStructure = $withStructure;
57 30
        return $this;
58
    }
59
60
    /**
61
     * Specifies whether to include the table's data (INSERT statements)
62
     * 
63
     * @param  boolean  $withData   TRUE to include data, FALSE otherwise
64
     * 
65
     * @return TableDumper  Returns the TableDumper instance
66
     */
67 21
    public function withData($withData = true)
68
    {
69 21
        $this->withData = $withData;
70 21
        return $this;
71
    }
72
73
    /**
74
     * Specifies whether to include a DROP TABLE statement
75
     * 
76
     * @param  boolean  $withDrop   TRUE to include DROP statement, FALSE otherwise
77
     * 
78
     * @return TableDumper  Returns the TableDumper instance
79
     */
80 21
    public function withDrop($withDrop = true)
81
    {
82 21
        $this->withDrop = $withDrop;
83 21
        return $this;
84
    }
85
86
    /**
87
     * Returns whether or not this dumper will include the table's structure
88
     * 
89
     * @return boolean Returns TRUE if it includes it, FALSE otherwise
90
     */
91 15
    public function hasStructure()
92
    {
93 15
        return $this->withStructure;
94
    }
95
96
97
    /**
98
     * Returns whether or not this dumper will include the table's data
99
     * 
100
     * @return boolean Returns TRUE if it includes it, FALSE otherwise
101
     */
102 6
    public function hasData()
103
    {
104 6
        return $this->withData;
105
    }
106
107
108
    /**
109
     * Returns whether or not this dumper will include the table's DROP statement
110
     * 
111
     * @return boolean Returns TRUE if it includes it, FALSE otherwise
112
     */
113 6
    public function hasDrop()
114
    {
115 6
        return $this->withDrop;
116
    }
117
118
119
    /**
120
     * Add WHERE parameters to use when dumping the data 
121
     * 
122
     * @param  string   $where_string   SQL string that you would normally write after WHERE keyowrd
123
     * 
124
     * @return TableDumper  Returns the TableDumper instance
125
     */
126 6
    public function where($where_string)
127
    {
128 6
        $this->where = $where_string;
129 6
        return $this;
130
    }
131
132
    /**
133
     * Get the where param string
134
     * @return string Returns the WHERE param string
135
     */
136 3
    public function getWhere()
137
    {
138 3
        return $this->where;
139
    }
140
141
    /**
142
     * Get the Table related to this dumper
143
     * 
144
     * @return Table    Returns the Table instance
145
     */
146 66
    public function getTable()
147
    {
148 66
        return $this->table;
149
    }
150
151
    /**
152
     * Writes the CREATE statement to the dump stream
153
     * 
154
     * @param  PDO      $db     PDO instance to use for DB queries
155
     * @param  resource $stream Stream to write the dump to
156
     * 
157
     * @return void
158
     */
159 9
    protected function dumpCreateStatement(PDO $db, $stream) 
160
    {
161 9
        $stmt = $db->query('SHOW CREATE TABLE `'.$this->table->getName().'`');
162
163 9
        fwrite($stream, $stmt->fetchColumn(1).";\r\n");
164
165 9
        $stmt->closeCursor();
166 9
    }
167
168
    /**
169
     * Writes the DROP statement to the drump stream
170
     * 
171
     * @param  resource $stream Stream to write the dump to
172
     * 
173
     * @return void
174
     */
175 9
    protected function dumpDropStatement($stream)
176
    {
177 9
        fwrite($stream, 'DROP TABLE IF EXISTS `'.$this->table->getName()."`;\r\n");
178 9
    }
179
180
    /**
181
     * Writes the INSERT statements to the drump stream
182
     * 
183
     * @param  PDO      $db     PDO instance to use for DB queries
184
     * @param  resource $stream Stream to write the dump to
185
     * 
186
     * @return void
187
     */
188 12
    protected function dumpInsertStatement(PDO $db, $stream)
189
    {
190
        //Get data from table
191 12
        $select = 'SELECT * FROM '.$this->table->getName();
192
193 12
        if (!empty($this->where)) {
194 3
            $select .= ' WHERE '.$this->where;
195 2
        }
196
197
        //Add limit
198 12
        $limit = 1000;
199 12
        $select .= ' LIMIT :limit OFFSET :offset';
200
201 12
        $stmt = $db->prepare($select);
202 12
        $stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
203
204 12
        $i = 0;
205 12
        $j = 0;
206
207
        //Dump an INSERT of all rows with paging 
208
        do {    
209 12
            $stmt->bindValue(':offset', $i*$limit, PDO::PARAM_INT);
210 12
            $stmt->execute();
211
212 12
            while (($row = $stmt->fetch(PDO::FETCH_ASSOC)) !== false) {
213
                //Write start of INSERT statement
214 9
                if ($j === 0) {
215
                    //Gets keys from array indexes of first row
216 9
                    $fields = implode(',', array_keys($row));
217
                    
218 9
                    fwrite($stream, 'INSERT INTO `'.$this->table->getName().'` ('.$fields.') VALUES ');
219 6
                }
220
221
                //Write values of this row
222 9
                $valuesDump = '';
223
224 9
                if ($j > 0) {
225 9
                    $valuesDump .= ", \r\n";
226 6
                }
227
228 9
                $listValues = array_values($row);
229
230
                //Quote values or replace with NULL if null
231 9
                foreach ($listValues as $key => $value) {
232 9
                    $quotedValue = str_replace("'", "\'", str_replace('"', '\"', $value));
233 9
                    $listValues[$key] = (!isset($value) ? 'NULL' : "'".$quotedValue."'");
234 6
                }
235
236
                //Add values from this row to valuesDump
237 9
                $valuesDump .= '('.implode(',', $listValues).')';
238
239 9
                fwrite($stream, $valuesDump);
240 9
                $j++;
241 6
            }
242
243 12
            $stmt->closeCursor();
244 12
            $i++;
245
246 12
        } while ($j === $i*$limit);
247
248
        //If there was at least one row, write end of INSERT statement
249 12
        if ($j > 0) {
250 9
            fwrite($stream, ";\r\n");
251 6
        }
252 12
    }
253
254
    /**
255
     * Writes all the SQL statements of this dumper to the dump stream
256
     * 
257
     * @param  PDO      $db     PDO instance to use for DB queries
258
     * @param  resource $stream Stream to write the dump to
259
     * 
260
     * @return void
261
     */
262 21
    public function dump(PDO $db, $stream)
263
    {
264
        //Drop table statement
265 21
        if ($this->withDrop) {
266 9
            $this->dumpDropStatement($stream);
267 6
        }
268
269
        //Create table statement
270 21
        if ($this->withStructure) {
271 9
            $this->dumpCreateStatement($db, $stream);
272 6
        }
273
274
        //Data
275 21
        if ($this->withData) {
276 12
            $this->dumpInsertStatement($db, $stream);
277 8
        }
278
    }
279
}