Passed
Push — master ( aa56fb...89b72d )
by Joao
10:12 queued 12s
created

DblibDatabase::schema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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
    public static function schema()
12
    {
13
        return ['dblib', 'sqlsrv'];
14
    }
15
16
    public static function prepareEnvironment(UriInterface $uri)
17
    {
18
        $database = preg_replace('~^/~', '', $uri->getPath());
19
20
        $customUri = new Uri($uri->__toString());
21
22
        $dbDriver = Factory::getDbRelationalInstance($customUri->withPath('/')->__toString());
23
        $dbDriver->execute("IF NOT EXISTS(select * from sys.databases where name='$database') CREATE DATABASE $database");
24
    }
25
26
    public function createDatabase()
27
    {
28
        $database = preg_replace('~^/~', '', $this->getDbDriver()->getUri()->getPath());
29
30
        $this->getDbDriver()->execute("IF NOT EXISTS(select * from sys.databases where name='$database') CREATE DATABASE $database");
31
        $this->getDbDriver()->execute("USE $database");
32
    }
33
34
    public function dropDatabase()
35
    {
36
        $database = preg_replace('~^/~', '', $this->getDbDriver()->getUri()->getPath());
37
38
        $this->getDbDriver()->execute("use master");
39
        $this->getDbDriver()->execute("drop database $database");
40
    }
41
42
    protected function createTableIfNotExists($database, $createTable)
43
    {
44
        $this->getDbDriver()->execute("use $database");
45
46
        $sql = "IF (NOT EXISTS (SELECT *
47
                 FROM INFORMATION_SCHEMA.TABLES
48
                 WHERE TABLE_SCHEMA = 'dbo'
49
                 AND  TABLE_NAME = '" . $this->getMigrationTable() . "'))
50
            BEGIN
51
                $createTable
52
            END";
53
54
        $this->getDbDriver()->execute($sql);
55
    }
56
57
    /**
58
     * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException
59
     * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
60
     */
61
    public function createVersion()
62
    {
63
        $database = preg_replace('~^/~', '', $this->getDbDriver()->getUri()->getPath());
64
        $createTable = 'CREATE TABLE ' . $this->getMigrationTable() . ' (version int, status varchar(20), PRIMARY KEY (version))';
65
        $this->createTableIfNotExists($database, $createTable);
66
        $this->checkExistsVersion();
67
    }
68
69
    public function executeSql($sql)
70
    {
71
        $statements = preg_split("/;(\r\n|\r|\n)/", $sql);
72
73
        foreach ($statements as $sql) {
0 ignored issues
show
introduced by
$sql is overwriting one of the parameters of this function.
Loading history...
74
            $this->executeSqlInternal($sql);
75
        }
76
    }
77
78
    protected function executeSqlInternal($sql)
79
    {
80
        if (empty(trim($sql))) {
81
            return;
82
        }
83
        $this->getDbDriver()->execute($sql);
84
    }
85
86
    /**
87
     * @param $schema
88
     * @param $table
89
     * @return bool
90
     */
91
    protected function isTableExists($schema, $table)
92
    {
93
        $count = $this->getDbDriver()->getScalar(
94
            'SELECT count(*) FROM information_schema.tables ' .
95
            ' WHERE table_catalog = [[schema]] ' .
96
            '  AND table_name = [[table]] ',
97
            [
98
                "schema" => $schema,
99
                "table" => $table
100
            ]
101
        );
102
103
        return (intval($count) !== 0);
104
    }
105
}
106