Completed
Push — master ( 424df6...6d7ac3 )
by Joao
9s
created

src/Database/DblibDatabase.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace ByJG\DbMigration\Database;
4
5
use ByJG\AnyDataset\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
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()
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()
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, $table, $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 = '$table'))
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
    public function createVersion()
58
    {
59
        $database = preg_replace('~^/~', '', $this->getDbDriver()->getUri()->getPath());
60
        $table = 'migration_version';
61
        $createTable = 'CREATE TABLE migration_version (version int, status varchar(20))';
62
        $this->createTableIfNotExists($database, $table, $createTable);
63
        $this->checkExistsVersion();
64
    }
65
66
    public function executeSql($sql)
67
    {
68
        $statements = explode(";", $sql);
69
70
        foreach ($statements as $sql) {
71
            $this->executeSqlInternal($sql);
72
        }
73
    }
74
75
    protected function executeSqlInternal($sql)
76
    {
77
        $this->getDbDriver()->execute($sql);
78
    }
79
}
80