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

Driver   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 4
dl 0
loc 91
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 4
A __destruct() 0 4 1
B connect() 0 23 8
A test() 0 13 3
A disconnect() 0 6 3
A prepare() 0 9 2
A begin() 0 5 1
A commit() 0 5 1
A rollback() 0 5 1
1
<?php
2
3
namespace vakata\database\driver\mysql;
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
use \vakata\collection\Collection;
12
13
class Driver extends DriverAbstract implements DriverInterface
14
{
15
    use Schema;
16
    
17
    protected $lnk = null;
18
19
    public function __construct(array $connection)
20
    {
21
        $this->connection = $connection;
22
        if (!isset($this->connection['port'])) {
23
            $this->connection['port'] = 3306;
24
        }
25
        if (!isset($this->connection['opts'])) {
26
            $this->connection['opts'] = [];
27
        }
28
        if (!isset($this->connection['opts']['charset'])) {
29
            $this->connection['opts']['charset'] = 'UTF8';
30
        }
31
    }
32
    public function __destruct()
33
    {
34
        $this->disconnect();
35
    }
36
    protected function connect()
37
    {
38
        if ($this->lnk === null) {
39
            $this->lnk = new \mysqli(
40
                (isset($this->connection['opts']['persist']) && $this->connection['opts']['persist'] ? 'p:' : '') .
41
                    $this->connection['host'],
42
                $this->connection['user'],
43
                $this->connection['pass'],
44
                $this->connection['name'],
45
                isset($this->connection['opts']['socket']) ? null : $this->connection['port'],
46
                $this->connection['opts']['socket'] ?? null
47
            );
48
            if ($this->lnk->connect_errno) {
49
                throw new DBException('Connect error: '.$this->lnk->connect_errno);
50
            }
51
            if (!$this->lnk->set_charset($this->connection['opts']['charset'])) {
52
                throw new DBException('Charset error: '.$this->lnk->connect_errno);
53
            }
54
            if (isset($this->connection['opts']['timezone'])) {
55
                $this->lnk->query("SET time_zone = '".addslashes($this->connection['opts']['timezone'])."'");
56
            }
57
        }
58
    }
59
    public function test() : bool
60
    {
61
        if ($this->lnk) {
62
            return true;
63
        }
64
        try {
65
            @$this->connect();
66
            return true;
67
        } catch (\Exception $e) {
68
            $this->lnk = null;
69
            return false;
70
        }
71
    }
72
    protected function disconnect()
73
    {
74
        if ($this->lnk !== null && $this->lnk !== false) {
75
            $this->lnk->close();
76
        }
77
    }
78
    public function prepare(string $sql) : StatementInterface
79
    {
80
        $this->connect();
81
        $temp = $this->lnk->prepare($sql);
82
        if (!$temp) {
83
            throw new DBException('Could not prepare : '.$this->lnk->error.' <'.$sql.'>');
84
        }
85
        return new Statement($temp);
86
    }
87
88
    public function begin() : bool
89
    {
90
        $this->connect();
91
        return $this->lnk->begin_transaction();
92
    }
93
    public function commit() : bool
94
    {
95
        $this->connect();
96
        return $this->lnk->commit();
97
    }
98
    public function rollback() : bool
99
    {
100
        $this->connect();
101
        return $this->lnk->rollback();
102
    }
103
}