Completed
Push — master ( 5924b0...d78793 )
by Ivan
02:02
created

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