Passed
Push — master ( 86c63e...a506e2 )
by Joao
01:10 queued 10s
created

DblibDatabase   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 47.31 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 44
loc 93
rs 10
c 0
b 0
f 0

8 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() 7 7 1
A executeSql() 0 11 3
A executeSqlInternal() 0 4 1
A isTableExists() 14 14 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\Database;
4
5
use ByJG\AnyDataset\Db\Factory;
6
use ByJG\Util\Uri;
7
use Psr\Http\Message\UriInterface;
8
9
class DblibDatabase extends AbstractDatabase
10
{
11
12 View Code Duplication
    public static function prepareEnvironment(UriInterface $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...
13
    {
14
        $database = preg_replace('~^/~', '', $uri->getPath());
15
16
        $customUri = new Uri($uri->__toString());
17
18
        $dbDriver = Factory::getDbRelationalInstance($customUri->withPath('/')->__toString());
19
        $dbDriver->execute("IF NOT EXISTS(select * from sys.databases where name='$database') CREATE DATABASE $database");
20
    }
21
22 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...
23
    {
24
        $database = preg_replace('~^/~', '', $this->getDbDriver()->getUri()->getPath());
25
26
        $this->getDbDriver()->execute("IF NOT EXISTS(select * from sys.databases where name='$database') CREATE DATABASE $database");
27
        $this->getDbDriver()->execute("USE $database");
28
    }
29
30 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...
31
    {
32
        $database = preg_replace('~^/~', '', $this->getDbDriver()->getUri()->getPath());
33
34
        $this->getDbDriver()->execute("use master");
35
        $this->getDbDriver()->execute("drop database $database");
36
    }
37
38
    protected function createTableIfNotExists($database, $createTable)
39
    {
40
        $this->getDbDriver()->execute("use $database");
41
42
        $sql = "IF (NOT EXISTS (SELECT * 
43
                 FROM INFORMATION_SCHEMA.TABLES 
44
                 WHERE TABLE_SCHEMA = 'dbo' 
45
                 AND  TABLE_NAME = '" . $this->getMigrationTable() . "'))
46
            BEGIN
47
                $createTable
48
            END";
49
50
        $this->getDbDriver()->execute($sql);
51
    }
52
53
    /**
54
     * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException
55
     * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
56
     */
57 View Code Duplication
    public function createVersion()
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...
58
    {
59
        $database = preg_replace('~^/~', '', $this->getDbDriver()->getUri()->getPath());
60
        $createTable = 'CREATE TABLE ' . $this->getMigrationTable() . ' (version int, status varchar(20))';
61
        $this->createTableIfNotExists($database, $createTable);
62
        $this->checkExistsVersion();
63
    }
64
65
    public function executeSql($sql)
66
    {
67
        $statements = preg_split("/;(\r\n|\r|\n)/", $sql);
68
69
        foreach ($statements as $sql) {
70
            if (empty(trim($sql))) {
71
                continue;
72
            }
73
            $this->executeSqlInternal($sql);
74
        }
75
    }
76
77
    protected function executeSqlInternal($sql)
78
    {
79
        $this->getDbDriver()->execute($sql);
80
    }
81
82
    /**
83
     * @param $schema
84
     * @param $table
85
     * @return bool
86
     */
87 View Code Duplication
    protected function isTableExists($schema, $table)
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...
88
    {
89
        $count = $this->getDbDriver()->getScalar(
90
            'SELECT count(*) FROM information_schema.tables ' .
91
            ' WHERE table_catalog = [[schema]] ' .
92
            '  AND table_name = [[table]] ',
93
            [
94
                "schema" => $schema,
95
                "table" => $table
96
            ]
97
        );
98
99
        return (intval($count) !== 0);
100
    }
101
}
102