Completed
Pull Request — master (#1)
by Joao
04:35
created

DblibCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 34.33 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 4
dl 23
loc 67
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A prepareEnvironment() 9 9 1
A createDatabase() 7 7 1
A dropDatabase() 7 7 1
A createTableIfNotExists() 0 14 1
A createVersion() 0 8 1
A executeSql() 0 8 2
A executeSqlInternal() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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