| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 | 105 |  |     public function __construct(Table $table) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 105 |  |         $this->table = $table; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 105 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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(bool $withStructure = true): TableDumper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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(bool $withData = true): TableDumper | 
            
                                                                        
                            
            
                                    
            
            
                | 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(bool $withDrop = true): TableDumper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 | 48 |  |     public function hasStructure(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 48 |  |         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 | 42 |  |     public function hasData(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 42 |  |         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 | 42 |  |     public function hasDrop(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 42 |  |         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(string $where_string): TableDumper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 | 30 |  |     public function getWhere(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 30 |  |         return $this->where; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * Get the Table related to this dumper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * @return Table    Returns the Table instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 81 |  |     public function getTable(): Table | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 81 |  |         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 | 33 |  |     public function dumpCreateStatement(PDO $db, $stream): void  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 33 |  |         if(!$this->hasStructure()) return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 21 |  |         $stmt = $db->query('SHOW CREATE TABLE `'.$this->table->getName().'`'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 21 |  |         fwrite($stream, $stmt->fetchColumn(1).";\r\n"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 | 21 |  |         $stmt->closeCursor(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 | 21 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      * Writes the DROP statement to the dump stream | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      * @param  resource $stream Stream to write the dump to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 36 |  |     public function dumpDropStatement($stream): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 36 |  |         if(!$this->hasDrop()) return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 24 |  |         fwrite($stream, 'DROP TABLE IF EXISTS `'.$this->table->getName()."`;\r\n"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 | 24 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |      * Writes the INSERT statements to the dump stream | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |      * @param  PDO      $db     PDO instance to use for DB queries | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |      * @param  resource $stream Stream to write the dump to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 | 36 |  |     public function dumpInsertStatement(PDO $db, $stream): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 | 36 |  |         if(!$this->hasData()) return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 | 27 |  |         $dataDumper = new TableDataDumper($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 | 27 |  |         $dataDumper->dump($db, $stream); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 | 27 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |      * Writes all the SQL statements of this dumper to the dump stream | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |      * @param  PDO      $db     PDO instance to use for DB queries | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |      * @param  resource $stream Stream to write the dump to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 | 3 |  |     public function dump(PDO $db, $stream): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |         //Drop table statement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 | 3 |  |         $this->dumpDropStatement($stream); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |         //Create table statement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 | 3 |  |         $this->dumpCreateStatement($db, $stream); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |         //Data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 | 3 |  |         $this->dumpInsertStatement($db, $stream); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 218 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 219 |  |  | } |