@@ -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 |