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

Driver::tables()   A

Complexity

Conditions 1
Paths 1

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