Completed
Push — master ( 407844...9daecf )
by Ivan
02:25
created

Driver::table()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 4
nc 4
nop 2
1
<?php
2
3
namespace vakata\database\driver\pdo;
4
5
use \vakata\database\DBException;
6
use \vakata\database\DriverInterface;
7
use \vakata\database\DriverAbstract;
8
use \vakata\database\StatementInterface;
9
use \vakata\database\schema\Table;
10
use \vakata\database\schema\TableRelation;
11
12
class Driver extends DriverAbstract implements DriverInterface
13
{
14
    use \vakata\database\driver\mysql\Schema,
15
        \vakata\database\driver\oracle\Schema,
16
        \vakata\database\driver\postgre\Schema
17
    {
18
        \vakata\database\driver\mysql\Schema::table as mtable;
19
        \vakata\database\driver\mysql\Schema::tables as mtables;
20
        \vakata\database\driver\oracle\Schema::table as otable;
21
        \vakata\database\driver\oracle\Schema::tables as otables;
22
        \vakata\database\driver\postgre\Schema::table as ptable;
23
        \vakata\database\driver\postgre\Schema::tables as ptables;
24
    }
25
26
    protected $lnk = null;
27
    protected $drv = null;
28
29
    public function __construct(array $connection)
30
    {
31
        $temp = explode('://', $connection['orig'], 2)[1];
32
        $temp = array_pad(explode('?', $temp, 2), 2, '');
33
        $connection = [];
34
        parse_str($temp[1], $connection['opts']);
35
        $temp = $temp[0];
36
        if (strpos($temp, '@') !== false) {
37
            $temp = array_pad(explode('@', $temp, 2), 2, '');
38
            list($connection['user'], $connection['pass']) = array_pad(explode(':', $temp[0], 2), 2, '');
39
            $temp = $temp[1];
40
        }
41
        $connection['dsn'] = $temp;
42
        $connection['name'] = '';
43
        $this->drv = explode(':', $temp)[0];
44
        $this->connection = $connection;
45
    }
46
    public function __destruct()
47
    {
48
        $this->disconnect();
49
    }
50
    protected function connect()
51
    {
52
        if ($this->lnk === null) {
53
            try {
54
                $this->lnk = new \PDO(
55
                    $this->connection['dsn'],
56
                    isset($this->connection['user']) ? $this->connection['user'] : '',
57
                    isset($this->connection['pass']) ? $this->connection['pass'] : '',
58
                    isset($this->connection['opts']) ? $this->connection['opts'] : []
59
                );
60
            } catch (\PDOException $e) {
61
                throw new DBException('Connect error: ' . $e->getMessage());
62
            }
63
        }
64
    }
65
    public function test() : bool
66
    {
67
        if ($this->lnk) {
68
            return true;
69
        }
70
        try {
71
            @$this->connect();
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
72
            return true;
73
        } catch (\Exception $e) {
74
            $this->lnk = null;
75
            return false;
76
        }
77
    }
78
    protected function disconnect()
79
    {
80
        $this->lnk = null;
81
    }
82
    public function prepare(string $sql) : StatementInterface
83
    {
84
        $this->connect();
85
        try {
86
            return new Statement($this->lnk->prepare($sql), $this->lnk);
87
        } catch (\PDOException $e) {
88
            throw new DBException($e->getMessage());
89
        }
90
    }
91
92
    public function begin() : bool
93
    {
94
        $this->connect();
95
        return $this->lnk->beginTransaction();
96
    }
97
    public function commit() : bool
98
    {
99
        $this->connect();
100
        return $this->lnk->commit();
101
    }
102
    public function rollback() : bool
103
    {
104
        $this->connect();
105
        return $this->lnk->rollback();
106
    }
107
108
    public function table(string $table, bool $detectRelations = true) : Table
109
    {
110
        switch ($this->drv) {
111
            case 'mysql':
112
                return $this->mtable($table, $detectRelations);
113
            case 'oci':
114
                return $this->otable($table, $detectRelations);
115
            case 'pgsql':
116
                return $this->ptable($table, $detectRelations);
117
            default:
118
                return parent::table($table, $detectRelations);
119
        }
120
    }
121
    public function tables() : array
122
    {
123
        switch ($this->drv) {
124
            case 'mysql':
125
                return $this->mtables();
126
            case 'oci':
127
                return $this->otables();
128
            case 'pgsql':
129
                return $this->ptables();
130
            default:
131
                return parent::tables();
132
        }
133
    }
134
}