@@ -2,15 +2,15 @@ |
||
2 | 2 | namespace Y0lk\SQLDumper; |
3 | 3 | |
4 | 4 | class Table { |
5 | - protected $name; |
|
5 | + protected $name; |
|
6 | 6 | |
7 | - public function __construct($name) |
|
8 | - { |
|
9 | - $this->name = $name; |
|
10 | - } |
|
7 | + public function __construct($name) |
|
8 | + { |
|
9 | + $this->name = $name; |
|
10 | + } |
|
11 | 11 | |
12 | - public function getName() |
|
13 | - { |
|
14 | - return $this->name; |
|
15 | - } |
|
12 | + public function getName() |
|
13 | + { |
|
14 | + return $this->name; |
|
15 | + } |
|
16 | 16 | } |
17 | 17 | \ No newline at end of file |
@@ -4,144 +4,144 @@ |
||
4 | 4 | use PDO; |
5 | 5 | |
6 | 6 | class TableDumper { |
7 | - protected $db; |
|
8 | - |
|
9 | - protected $table; |
|
10 | - |
|
11 | - protected $withStructure = true; |
|
12 | - protected $withData = true; |
|
13 | - |
|
14 | - protected $withDrop = true; |
|
15 | - |
|
16 | - public function __construct(Table $table) |
|
17 | - { |
|
18 | - $this->table = $table; |
|
19 | - } |
|
20 | - |
|
21 | - public function withStructure($withStructure = true) |
|
22 | - { |
|
23 | - $this->withStructure = $withStructure; |
|
24 | - return $this; |
|
25 | - } |
|
26 | - |
|
27 | - public function withData($withData = true) |
|
28 | - { |
|
29 | - $this->withData = $withData; |
|
30 | - return $this; |
|
31 | - } |
|
32 | - |
|
33 | - public function withDrop($withDrop = true) |
|
34 | - { |
|
35 | - $this->withDrop = $withDrop; |
|
36 | - return $this; |
|
37 | - } |
|
38 | - |
|
39 | - public function where($where_string) |
|
40 | - { |
|
41 | - $this->where = $where_string; |
|
42 | - return $this; |
|
43 | - } |
|
44 | - |
|
45 | - protected function getTable() |
|
46 | - { |
|
47 | - return $this->table; |
|
48 | - } |
|
49 | - |
|
50 | - protected function dumpCreateStatement(PDO $db, $stream) |
|
51 | - { |
|
52 | - $stmt = $db->query('SHOW CREATE TABLE `'.$this->table->getName().'`'); |
|
53 | - |
|
54 | - fwrite($stream, $stmt->fetchColumn(1).";\r\n"); |
|
55 | - |
|
56 | - $stmt->closeCursor(); |
|
57 | - } |
|
58 | - |
|
59 | - protected function dumpDropStatement(PDO $db, $stream) |
|
60 | - { |
|
61 | - fwrite($stream, 'DROP TABLE IF EXISTS `'.$this->table->getName(). "`;\r\n"); |
|
62 | - } |
|
63 | - |
|
64 | - protected function dumpInsertStatement(PDO $db, $stream) |
|
65 | - { |
|
66 | - //Get data from table |
|
67 | - $select = 'SELECT * FROM '.$this->table->getName(); |
|
68 | - |
|
69 | - if(!empty($this->where)) { |
|
70 | - $select .= ' WHERE '.$this->where; |
|
71 | - } |
|
72 | - |
|
73 | - //Add limit |
|
74 | - $limit = 1000; |
|
75 | - $select .= ' LIMIT :limit OFFSET :offset'; |
|
76 | - |
|
77 | - $stmt = $db->prepare($select); |
|
78 | - $stmt->bindValue(':limit', $limit, PDO::PARAM_INT); |
|
79 | - |
|
80 | - $i = 0; |
|
81 | - $j = 0; |
|
82 | - |
|
83 | - //Dump an INSERT of all rows with paging |
|
84 | - do { |
|
85 | - $stmt->bindValue(':offset', $i*$limit, PDO::PARAM_INT); |
|
86 | - $stmt->execute(); |
|
87 | - |
|
88 | - while(($row = $stmt->fetch(PDO::FETCH_ASSOC)) !== false) { |
|
89 | - //Write start of INSERT statement |
|
90 | - if($j === 0) { |
|
91 | - //Gets keys from array indexes of first row |
|
92 | - $fields = implode(',', array_keys($row)); |
|
7 | + protected $db; |
|
8 | + |
|
9 | + protected $table; |
|
10 | + |
|
11 | + protected $withStructure = true; |
|
12 | + protected $withData = true; |
|
13 | + |
|
14 | + protected $withDrop = true; |
|
15 | + |
|
16 | + public function __construct(Table $table) |
|
17 | + { |
|
18 | + $this->table = $table; |
|
19 | + } |
|
20 | + |
|
21 | + public function withStructure($withStructure = true) |
|
22 | + { |
|
23 | + $this->withStructure = $withStructure; |
|
24 | + return $this; |
|
25 | + } |
|
26 | + |
|
27 | + public function withData($withData = true) |
|
28 | + { |
|
29 | + $this->withData = $withData; |
|
30 | + return $this; |
|
31 | + } |
|
32 | + |
|
33 | + public function withDrop($withDrop = true) |
|
34 | + { |
|
35 | + $this->withDrop = $withDrop; |
|
36 | + return $this; |
|
37 | + } |
|
38 | + |
|
39 | + public function where($where_string) |
|
40 | + { |
|
41 | + $this->where = $where_string; |
|
42 | + return $this; |
|
43 | + } |
|
44 | + |
|
45 | + protected function getTable() |
|
46 | + { |
|
47 | + return $this->table; |
|
48 | + } |
|
49 | + |
|
50 | + protected function dumpCreateStatement(PDO $db, $stream) |
|
51 | + { |
|
52 | + $stmt = $db->query('SHOW CREATE TABLE `'.$this->table->getName().'`'); |
|
53 | + |
|
54 | + fwrite($stream, $stmt->fetchColumn(1).";\r\n"); |
|
55 | + |
|
56 | + $stmt->closeCursor(); |
|
57 | + } |
|
58 | + |
|
59 | + protected function dumpDropStatement(PDO $db, $stream) |
|
60 | + { |
|
61 | + fwrite($stream, 'DROP TABLE IF EXISTS `'.$this->table->getName(). "`;\r\n"); |
|
62 | + } |
|
63 | + |
|
64 | + protected function dumpInsertStatement(PDO $db, $stream) |
|
65 | + { |
|
66 | + //Get data from table |
|
67 | + $select = 'SELECT * FROM '.$this->table->getName(); |
|
68 | + |
|
69 | + if(!empty($this->where)) { |
|
70 | + $select .= ' WHERE '.$this->where; |
|
71 | + } |
|
72 | + |
|
73 | + //Add limit |
|
74 | + $limit = 1000; |
|
75 | + $select .= ' LIMIT :limit OFFSET :offset'; |
|
76 | + |
|
77 | + $stmt = $db->prepare($select); |
|
78 | + $stmt->bindValue(':limit', $limit, PDO::PARAM_INT); |
|
79 | + |
|
80 | + $i = 0; |
|
81 | + $j = 0; |
|
82 | + |
|
83 | + //Dump an INSERT of all rows with paging |
|
84 | + do { |
|
85 | + $stmt->bindValue(':offset', $i*$limit, PDO::PARAM_INT); |
|
86 | + $stmt->execute(); |
|
87 | + |
|
88 | + while(($row = $stmt->fetch(PDO::FETCH_ASSOC)) !== false) { |
|
89 | + //Write start of INSERT statement |
|
90 | + if($j === 0) { |
|
91 | + //Gets keys from array indexes of first row |
|
92 | + $fields = implode(',', array_keys($row)); |
|
93 | 93 | |
94 | - fwrite($stream, 'INSERT INTO `'.$this->table->getName().'` ('.$fields.') VALUES '); |
|
95 | - } |
|
96 | - |
|
97 | - //Write values of this row |
|
98 | - $valuesDump = ''; |
|
99 | - |
|
100 | - if($j > 0) { |
|
101 | - $valuesDump .= ", \r\n"; |
|
102 | - } |
|
103 | - |
|
104 | - $listValues = array_values($row); |
|
105 | - |
|
106 | - //Quote values or replace with NULL if null |
|
107 | - foreach($listValues as $key => $value) { |
|
108 | - $quotedValue = str_replace("'", "\'", str_replace('"', '\"', $value)); |
|
109 | - $listValues[$key] = (!isset($value) ? 'NULL' : "'" . $quotedValue."'") ; |
|
110 | - } |
|
111 | - |
|
112 | - //Add values from this row to valuesDump |
|
113 | - $valuesDump .= '('.implode(',', $listValues).')'; |
|
114 | - |
|
115 | - fwrite($stream, $valuesDump); |
|
116 | - $j++; |
|
117 | - } |
|
118 | - |
|
119 | - $stmt->closeCursor(); |
|
120 | - $i++; |
|
121 | - |
|
122 | - } while($j === $i*$limit); |
|
123 | - |
|
124 | - //If there was at least one row, write end of INSERT statement |
|
125 | - if($j > 0) { |
|
126 | - fwrite($stream, ";\r\n"); |
|
127 | - } |
|
128 | - } |
|
129 | - |
|
130 | - public function dump(PDO $db, $stream) |
|
131 | - { |
|
132 | - //Drop table statement |
|
133 | - if($this->withDrop) { |
|
134 | - $this->dumpDropStatement($db, $stream); |
|
135 | - } |
|
136 | - |
|
137 | - //Create table statement |
|
138 | - if($this->withStructure) { |
|
139 | - $this->dumpCreateStatement($db, $stream); |
|
140 | - } |
|
141 | - |
|
142 | - //Data |
|
143 | - if($this->withData) { |
|
144 | - $this->dumpInsertStatement($db, $stream); |
|
145 | - } |
|
146 | - } |
|
94 | + fwrite($stream, 'INSERT INTO `'.$this->table->getName().'` ('.$fields.') VALUES '); |
|
95 | + } |
|
96 | + |
|
97 | + //Write values of this row |
|
98 | + $valuesDump = ''; |
|
99 | + |
|
100 | + if($j > 0) { |
|
101 | + $valuesDump .= ", \r\n"; |
|
102 | + } |
|
103 | + |
|
104 | + $listValues = array_values($row); |
|
105 | + |
|
106 | + //Quote values or replace with NULL if null |
|
107 | + foreach($listValues as $key => $value) { |
|
108 | + $quotedValue = str_replace("'", "\'", str_replace('"', '\"', $value)); |
|
109 | + $listValues[$key] = (!isset($value) ? 'NULL' : "'" . $quotedValue."'") ; |
|
110 | + } |
|
111 | + |
|
112 | + //Add values from this row to valuesDump |
|
113 | + $valuesDump .= '('.implode(',', $listValues).')'; |
|
114 | + |
|
115 | + fwrite($stream, $valuesDump); |
|
116 | + $j++; |
|
117 | + } |
|
118 | + |
|
119 | + $stmt->closeCursor(); |
|
120 | + $i++; |
|
121 | + |
|
122 | + } while($j === $i*$limit); |
|
123 | + |
|
124 | + //If there was at least one row, write end of INSERT statement |
|
125 | + if($j > 0) { |
|
126 | + fwrite($stream, ";\r\n"); |
|
127 | + } |
|
128 | + } |
|
129 | + |
|
130 | + public function dump(PDO $db, $stream) |
|
131 | + { |
|
132 | + //Drop table statement |
|
133 | + if($this->withDrop) { |
|
134 | + $this->dumpDropStatement($db, $stream); |
|
135 | + } |
|
136 | + |
|
137 | + //Create table statement |
|
138 | + if($this->withStructure) { |
|
139 | + $this->dumpCreateStatement($db, $stream); |
|
140 | + } |
|
141 | + |
|
142 | + //Data |
|
143 | + if($this->withData) { |
|
144 | + $this->dumpInsertStatement($db, $stream); |
|
145 | + } |
|
146 | + } |
|
147 | 147 | } |
148 | 148 | \ No newline at end of file |
@@ -58,7 +58,7 @@ discard block |
||
58 | 58 | |
59 | 59 | protected function dumpDropStatement(PDO $db, $stream) |
60 | 60 | { |
61 | - fwrite($stream, 'DROP TABLE IF EXISTS `'.$this->table->getName(). "`;\r\n"); |
|
61 | + fwrite($stream, 'DROP TABLE IF EXISTS `'.$this->table->getName()."`;\r\n"); |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | protected function dumpInsertStatement(PDO $db, $stream) |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | //Get data from table |
67 | 67 | $select = 'SELECT * FROM '.$this->table->getName(); |
68 | 68 | |
69 | - if(!empty($this->where)) { |
|
69 | + if (!empty($this->where)) { |
|
70 | 70 | $select .= ' WHERE '.$this->where; |
71 | 71 | } |
72 | 72 | |
@@ -85,9 +85,9 @@ discard block |
||
85 | 85 | $stmt->bindValue(':offset', $i*$limit, PDO::PARAM_INT); |
86 | 86 | $stmt->execute(); |
87 | 87 | |
88 | - while(($row = $stmt->fetch(PDO::FETCH_ASSOC)) !== false) { |
|
88 | + while (($row = $stmt->fetch(PDO::FETCH_ASSOC)) !== false) { |
|
89 | 89 | //Write start of INSERT statement |
90 | - if($j === 0) { |
|
90 | + if ($j === 0) { |
|
91 | 91 | //Gets keys from array indexes of first row |
92 | 92 | $fields = implode(',', array_keys($row)); |
93 | 93 | |
@@ -97,16 +97,16 @@ discard block |
||
97 | 97 | //Write values of this row |
98 | 98 | $valuesDump = ''; |
99 | 99 | |
100 | - if($j > 0) { |
|
100 | + if ($j > 0) { |
|
101 | 101 | $valuesDump .= ", \r\n"; |
102 | 102 | } |
103 | 103 | |
104 | 104 | $listValues = array_values($row); |
105 | 105 | |
106 | 106 | //Quote values or replace with NULL if null |
107 | - foreach($listValues as $key => $value) { |
|
107 | + foreach ($listValues as $key => $value) { |
|
108 | 108 | $quotedValue = str_replace("'", "\'", str_replace('"', '\"', $value)); |
109 | - $listValues[$key] = (!isset($value) ? 'NULL' : "'" . $quotedValue."'") ; |
|
109 | + $listValues[$key] = (!isset($value) ? 'NULL' : "'".$quotedValue."'"); |
|
110 | 110 | } |
111 | 111 | |
112 | 112 | //Add values from this row to valuesDump |
@@ -119,10 +119,10 @@ discard block |
||
119 | 119 | $stmt->closeCursor(); |
120 | 120 | $i++; |
121 | 121 | |
122 | - } while($j === $i*$limit); |
|
122 | + } while ($j === $i*$limit); |
|
123 | 123 | |
124 | 124 | //If there was at least one row, write end of INSERT statement |
125 | - if($j > 0) { |
|
125 | + if ($j > 0) { |
|
126 | 126 | fwrite($stream, ";\r\n"); |
127 | 127 | } |
128 | 128 | } |
@@ -130,17 +130,17 @@ discard block |
||
130 | 130 | public function dump(PDO $db, $stream) |
131 | 131 | { |
132 | 132 | //Drop table statement |
133 | - if($this->withDrop) { |
|
133 | + if ($this->withDrop) { |
|
134 | 134 | $this->dumpDropStatement($db, $stream); |
135 | 135 | } |
136 | 136 | |
137 | 137 | //Create table statement |
138 | - if($this->withStructure) { |
|
138 | + if ($this->withStructure) { |
|
139 | 139 | $this->dumpCreateStatement($db, $stream); |
140 | 140 | } |
141 | 141 | |
142 | 142 | //Data |
143 | - if($this->withData) { |
|
143 | + if ($this->withData) { |
|
144 | 144 | $this->dumpInsertStatement($db, $stream); |
145 | 145 | } |
146 | 146 | } |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | public function append($value) |
9 | 9 | { |
10 | 10 | //Make sure we're adding a TableDumper object |
11 | - if(!($value instanceof TableDumper)) { |
|
11 | + if (!($value instanceof TableDumper)) { |
|
12 | 12 | throw new \Exception("TableDumperCollection only accepts TableDumper objects", 1); |
13 | 13 | } |
14 | 14 | |
@@ -18,16 +18,16 @@ discard block |
||
18 | 18 | |
19 | 19 | public function addTable($table) |
20 | 20 | { |
21 | - if($table instanceof Table) { |
|
21 | + if ($table instanceof Table) { |
|
22 | 22 | $tableName = $table->getName(); |
23 | - } elseif(is_string($table)) { |
|
23 | + } elseif (is_string($table)) { |
|
24 | 24 | $tableName = $table; |
25 | 25 | } else { |
26 | 26 | throw new \Exception("Invalid value supplied for argument 'table'", 1); |
27 | 27 | } |
28 | 28 | |
29 | 29 | //First check if a dumper already exists for this table |
30 | - if(!$this->offsetExists($tableName)) { |
|
30 | + if (!$this->offsetExists($tableName)) { |
|
31 | 31 | //Create new one |
32 | 32 | $table = new Table($tableName); |
33 | 33 | $this->offsetSet($tableName, new TableDumper($table)); |
@@ -39,20 +39,20 @@ discard block |
||
39 | 39 | public function addListTables($listTables) |
40 | 40 | { |
41 | 41 | //If arg is a TableDumperCollection, merge into this one |
42 | - if($listTables instanceof TableDumperCollection) { |
|
42 | + if ($listTables instanceof TableDumperCollection) { |
|
43 | 43 | foreach ($listTables as $table) { |
44 | 44 | $this->append($table); |
45 | 45 | } |
46 | 46 | |
47 | 47 | return $listTables; |
48 | 48 | } |
49 | - elseif(is_array($listTables)) { |
|
49 | + elseif (is_array($listTables)) { |
|
50 | 50 | //Create TableDumperCollection |
51 | 51 | $listDumpers = new TableDumperCollection; |
52 | 52 | |
53 | 53 | foreach ($listTables as $table) { |
54 | 54 | //If table is already a Dumper, simply append to this |
55 | - if($table instanceof TableDumper) { |
|
55 | + if ($table instanceof TableDumper) { |
|
56 | 56 | $listDumpers[] = $table; |
57 | 57 | $this->append($table); |
58 | 58 | } else { |
@@ -45,8 +45,7 @@ discard block |
||
45 | 45 | } |
46 | 46 | |
47 | 47 | return $listTables; |
48 | - } |
|
49 | - elseif(is_array($listTables)) { |
|
48 | + } elseif(is_array($listTables)) { |
|
50 | 49 | //Create TableDumperCollection |
51 | 50 | $listDumpers = new TableDumperCollection; |
52 | 51 | |
@@ -61,8 +60,7 @@ discard block |
||
61 | 60 | } |
62 | 61 | |
63 | 62 | return $listDumpers; |
64 | - } |
|
65 | - else { |
|
63 | + } else { |
|
66 | 64 | throw new \Exception("Invalid value supplied for argument 'listTables'", 1); |
67 | 65 | return NULL; |
68 | 66 | } |