Completed
Push — master ( b71ad6...2211ef )
by Ivan
08:51
created

Driver::__destruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace vakata\database\driver\postgre;
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
    protected $connection;
15
    protected $lnk = null;
16
    protected $transaction = false;
17
18
    public function __construct(array $connection)
19
    {
20
        $this->connection = $connection;
21
    }
22
    public function __destruct()
23
    {
24
        $this->disconnect();
25
    }
26
    protected function connect()
27
    {
28
        if ($this->lnk === null) {
29
            $this->lnk = call_user_func(
30
                $this->option('persist') ? '\pg_pconnect' : '\pg_connect',
31
                implode(" ", [
32
                    'user='.$this->connection['user'],
33
                    'password='.$this->connection['pass'],
34
                    'host='.$this->connection['host'],
35
                    'dbname='.$this->connection['name'],
36
                    "options='--client_encoding=".$this->option('charset', 'utf8')."'"
37
                ])
38
            );
39
            if ($this->lnk === false) {
40
                throw new DBException('Connect error');
41
            }
42
        }
43
    }
44
    protected function disconnect()
45
    {
46
        if (is_resource($this->lnk)) {
47
            \pg_close($this->lnk);
48
        }
49
    }
50
    public function prepare(string $sql) : StatementInterface
51
    {
52
        $this->connect();
53
        $binder = '?';
54
        if (strpos($sql, $binder) !== false) {
55
            $tmp = explode($binder, $sql);
56
            $sql = '';
57
            foreach ($tmp as $i => $v) {
58
                $sql .= $v;
59
                if (isset($tmp[($i + 1)])) {
60
                    $sql .= '$'.($i + 1);
61
                }
62
            }
63
        }
64
        return new Statement($sql, $this->lnk);
65
    }
66
67
    public function begin() : bool
68
    {
69
        $this->connect();
70
        try {
71
            $this->transaction = true;
72
            $this->query('BEGIN');
73
        } catch (DBException $e) {
74
            $this->transaction = false;
75
76
            return false;
77
        }
78
79
        return true;
80
    }
81
    public function commit() : bool
82
    {
83
        $this->connect();
84
        $this->transaction = false;
85
        try {
86
            $this->query('COMMIT');
87
        } catch (DBException $e) {
88
            return false;
89
        }
90
91
        return true;
92
    }
93
    public function rollback() : bool
94
    {
95
        $this->connect();
96
        $this->transaction = false;
97
        try {
98
            $this->query('ROLLBACK');
99
        } catch (DBException $e) {
100
            return false;
101
        }
102
103
        return true;
104
    }
105
    public function isTransaction()
106
    {
107
        return $this->transaction;
108
    }
109
}
110