| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace ByJG\DbMigration\Commands; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use ByJG\AnyDataset\Factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use ByJG\Util\Uri; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class DblibCommand extends AbstractCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 11 |  | View Code Duplication |     public static function prepareEnvironment(Uri $uri) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |         $database = preg_replace('~^/~', '', $uri->getPath()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         $customUri = new Uri($uri->__toString()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         $dbDriver = Factory::getDbRelationalInstance($customUri->withPath('/')->__toString()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         $dbDriver->execute("IF NOT EXISTS(select * from sys.databases where name='$database') CREATE DATABASE $database"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 21 |  | View Code Duplication |     public function createDatabase() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $database = preg_replace('~^/~', '', $this->getDbDriver()->getUri()->getPath()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $this->getDbDriver()->execute("IF NOT EXISTS(select * from sys.databases where name='$database') CREATE DATABASE $database"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $this->getDbDriver()->execute("USE $database"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 29 |  | View Code Duplication |     public function dropDatabase() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $database = preg_replace('~^/~', '', $this->getDbDriver()->getUri()->getPath()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->getDbDriver()->execute("use master"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->getDbDriver()->execute("drop database $database"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     protected function createTableIfNotExists($database, $table, $createTable) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->getDbDriver()->execute("use $database"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $sql = "IF (NOT EXISTS (SELECT *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                  FROM INFORMATION_SCHEMA.TABLES  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                  WHERE TABLE_SCHEMA = 'dbo'  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                  AND  TABLE_NAME = '$table')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             BEGIN | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 $createTable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             END"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->getDbDriver()->execute($sql); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     public function createVersion() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $database = preg_replace('~^/~', '', $this->getDbDriver()->getUri()->getPath()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $table = 'migration_version'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $createTable = 'CREATE TABLE migration_version (version int)'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->createTableIfNotExists($database, $table, $createTable); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->checkExistsVersion(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     public function executeSql($sql) | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         $statements = explode(";", $sql); | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         foreach ($statements as $sql) { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |             $this->executeSqlInternal($sql); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     protected function executeSqlInternal($sql) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $this->getDbDriver()->execute($sql); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.