MySqlDatabase   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 14
c 0
b 0
f 0
dl 0
loc 45
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A schema() 0 3 1
A executeSql() 0 3 1
A prepareEnvironment() 0 8 1
A createDatabase() 0 6 1
A dropDatabase() 0 5 1
A createVersion() 0 4 1
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 MySqlDatabase extends AbstractDatabase
10
{
11
    public static function schema()
12
    {
13
        return ['mysql', 'mariadb'];
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("CREATE SCHEMA IF NOT EXISTS `$database` DEFAULT CHARACTER SET utf8 ;");
24
    }
25
26
    public function createDatabase()
27
    {
28
        $database = preg_replace('~^/~', '', $this->getDbDriver()->getUri()->getPath());
29
30
        $this->getDbDriver()->execute("CREATE SCHEMA IF NOT EXISTS `$database` DEFAULT CHARACTER SET utf8 ;");
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("drop database `$database`");
39
    }
40
41
    /**
42
     * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException
43
     * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
44
     */
45
    public function createVersion()
46
    {
47
        $this->getDbDriver()->execute('CREATE TABLE IF NOT EXISTS ' . $this->getMigrationTable() . ' (version int, status varchar(20), PRIMARY KEY (version))');
48
        $this->checkExistsVersion();
49
    }
50
51
    public function executeSql($sql)
52
    {
53
        $this->getDbDriver()->execute($sql);
54
    }
55
}
56