PgsqlDatabase   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 14
eloc 27
c 2
b 0
f 0
dl 0
loc 82
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A createDatabase() 0 4 1
A dropDatabase() 0 7 2
A createVersion() 0 4 1
A isDatabaseVersioned() 0 3 1
A createDatabaseIfNotExists() 0 9 2
A getDbDriverWithoutDatabase() 0 4 1
A prepareEnvironment() 0 5 1
A executeSql() 0 6 2
A executeSqlInternal() 0 6 2
A schema() 0 3 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 PgsqlDatabase extends AbstractDatabase
10
{
11
    public static function schema()
12
    {
13
        return 'pgsql';
14
    }
15
16
    public static function prepareEnvironment(UriInterface $uri)
17
    {
18
        $database = preg_replace('~^/~', '', $uri->getPath());
19
        $dbDriver = static::getDbDriverWithoutDatabase($uri);
20
        static::createDatabaseIfNotExists($dbDriver, $database);
21
    }
22
23
    protected static function getDbDriverWithoutDatabase(UriInterface $uri)
24
    {
25
        $customUri = new Uri($uri->__toString());
26
        return Factory::getDbRelationalInstance($customUri->withPath('/postgres')->__toString());
27
    }
28
29
    /**
30
     * @param \ByJG\AnyDataset\Db\DbDriverInterface $dbDriver
31
     * @param $database
32
     */
33
    protected static function createDatabaseIfNotExists($dbDriver, $database)
34
    {
35
        $currentDbName = $dbDriver->getScalar(
36
            "SELECT datname FROM pg_catalog.pg_database WHERE lower(datname) = lower(:dbname)",
37
            ['dbname' => $database]
38
        );
39
40
        if (empty($currentDbName)) {
41
            $dbDriver->execute("CREATE DATABASE $database WITH encoding=\"UTF8\";");
42
        }
43
    }
44
45
    public function createDatabase()
46
    {
47
        $database = preg_replace('~^/~', '', $this->getDbDriver()->getUri()->getPath());
48
        static::createDatabaseIfNotExists($this->getDbDriver(), $database);
49
    }
50
51
    public function dropDatabase()
52
    {
53
        $iterator = $this->getDbDriver()->getIterator(
54
            "select 'drop table if exists \"' || tablename || '\" cascade;' command from pg_tables where schemaname = 'public';"
55
        );
56
        foreach ($iterator as $singleRow) {
57
            $this->getDbDriver()->execute($singleRow->get('command'));
58
        }
59
    }
60
61
    /**
62
     * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException
63
     * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
64
     */
65
    public function createVersion()
66
    {
67
        $this->getDbDriver()->execute('CREATE TABLE IF NOT EXISTS ' . $this->getMigrationTable() . ' (version int, status varchar(20), PRIMARY KEY (version))');
68
        $this->checkExistsVersion();
69
    }
70
71
    public function executeSql($sql)
72
    {
73
        $statements = preg_split("/;(\r\n|\r|\n)/", $sql);
74
75
        foreach ($statements as $sql) {
0 ignored issues
show
introduced by
$sql is overwriting one of the parameters of this function.
Loading history...
76
            $this->executeSqlInternal($sql);
77
        }
78
    }
79
80
    protected function executeSqlInternal($sql)
81
    {
82
        if (empty(trim($sql))) {
83
            return;
84
        }
85
        $this->getDbDriver()->execute($sql);
86
    }
87
88
    public function isDatabaseVersioned()
89
    {
90
        return $this->isTableExists('public', $this->getMigrationTable());
91
    }
92
}
93