Completed
Push — master ( 85d2a2...f0e67d )
by Ivan
04:12
created

Driver   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 27
lcom 2
cbo 6
dl 0
loc 128
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 2
A __destruct() 0 4 1
A connect() 0 15 6
A test() 0 13 3
A disconnect() 0 4 1
A raw() 0 4 1
A prepare() 0 9 2
A begin() 0 5 1
A commit() 0 5 1
A rollback() 0 5 1
A table() 0 13 4
A tables() 0 13 4
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
        $connection['opts'] = [];
35
        parse_str($temp[1], $connection['opts']);
36
        $temp = $temp[0];
37
        if (strpos($temp, '@') !== false) {
38
            $temp = array_pad(explode('@', $temp, 2), 2, '');
39
            list($connection['user'], $connection['pass']) = array_pad(explode(':', $temp[0], 2), 2, '');
40
            $temp = $temp[1];
41
        }
42
        $connection['dsn'] = $temp;
43
        $connection['name'] = '';
44
        $this->drv = explode(':', $temp)[0];
45
        $this->connection = $connection;
46
    }
47
    public function __destruct()
48
    {
49
        $this->disconnect();
50
    }
51
    public function connect()
52
    {
53
        if ($this->lnk === null) {
54
            try {
55
                $this->lnk = new \PDO(
56
                    $this->connection['dsn'],
57
                    isset($this->connection['user']) ? $this->connection['user'] : '',
58
                    isset($this->connection['pass']) ? $this->connection['pass'] : '',
59
                    isset($this->connection['opts']) ? $this->connection['opts'] : []
60
                );
61
            } catch (\PDOException $e) {
62
                throw new DBException('Connect error: ' . $e->getMessage());
63
            }
64
        }
65
    }
66
    public function test() : bool
67
    {
68
        if ($this->lnk) {
69
            return true;
70
        }
71
        try {
72
            $this->connect();
73
            return true;
74
        } catch (\Exception $e) {
75
            $this->lnk = null;
76
            return false;
77
        }
78
    }
79
    public function disconnect()
80
    {
81
        $this->lnk = null;
82
    }
83
    public function raw(string $sql)
84
    {
85
        return $this->lnk->query($sql);
86
    }
87
    public function prepare(string $sql) : StatementInterface
88
    {
89
        $this->connect();
90
        try {
91
            return new Statement($this->lnk->prepare($sql), $this->lnk);
92
        } catch (\PDOException $e) {
93
            throw new DBException($e->getMessage());
94
        }
95
    }
96
97
    public function begin() : bool
98
    {
99
        $this->connect();
100
        return $this->lnk->beginTransaction();
101
    }
102
    public function commit() : bool
103
    {
104
        $this->connect();
105
        return $this->lnk->commit();
106
    }
107
    public function rollback() : bool
108
    {
109
        $this->connect();
110
        return $this->lnk->rollback();
111
    }
112
113
    public function table(string $table, bool $detectRelations = true) : Table
114
    {
115
        switch ($this->drv) {
116
            case 'mysql':
117
                return $this->mtable($table, $detectRelations);
118
            case 'oci':
119
                return $this->otable($table, $detectRelations);
120
            case 'pgsql':
121
                return $this->ptable($table, $detectRelations);
122
            default:
123
                return parent::table($table, $detectRelations);
124
        }
125
    }
126
    public function tables() : array
127
    {
128
        switch ($this->drv) {
129
            case 'mysql':
130
                return $this->mtables();
131
            case 'oci':
132
                return $this->otables();
133
            case 'pgsql':
134
                return $this->ptables();
135
            default:
136
                return parent::tables();
137
        }
138
    }
139
}
140