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

Driver::test()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 8
nc 3
nop 0
1
<?php
2
3
namespace vakata\database\driver\odbc;
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
        $temp = explode('://', $connection['orig'], 2)[1];
20
        $temp = array_pad(explode('?', $temp, 2), 2, '');
21
        $connection = [];
22
        parse_str($temp[1], $connection['opts']);
23
        $temp = $temp[0];
24
        if (strpos($temp, '@') !== false) {
25
            $temp = array_pad(explode('@', $temp, 2), 2, '');
26
            list($connection['user'], $connection['pass']) = array_pad(explode(':', $temp[0], 2), 2, '');
27
            $temp = $temp[1];
28
        }
29
        $connection['dsn'] = $temp;
30
        $this->connection = $connection;
31
    }
32
    public function __destruct()
33
    {
34
        $this->disconnect();
35
    }
36
    protected function connect()
37
    {
38
        if ($this->lnk === null) {
39
            $this->lnk = call_user_func(
40
                $this->option('persist') ? '\odbc_pconnect' : '\odbc_connect',
41
                $this->connection['dsn'],
42
                isset($this->connection['user']) ? $this->connection['user'] : '',
43
                isset($this->connection['pass']) ? $this->connection['pass'] : ''
44
            );
45
            if ($this->lnk === false) {
46
                throw new DBException('Connect error');
47
            }
48
        }
49
    }
50
    public function test() : bool
51
    {
52
        if ($this->lnk) {
53
            return true;
54
        }
55
        try {
56
            $this->connect();
57
            return true;
58
        } catch (\Exception $e) {
59
            return false;
60
        }
61
    }
62
    protected function disconnect()
63
    {
64
        if (is_resource($this->lnk)) {
65
            \odbc_close($this->lnk);
66
        }
67
    }
68
    public function prepare(string $sql) : StatementInterface
69
    {
70
        $this->connect();
71
        return new Statement(
72
            $sql,
73
            $this->lnk,
74
            $this->connection['opts']['charset_in'] ?? null,
75
            $this->connection['opts']['charset_out'] ?? null
76
        );
77
    }
78
79
    public function begin() : bool
80
    {
81
        $this->connect();
82
        $this->transaction = true;
83
        \odbc_autocommit($this->lnk, false);
84
        return true;
85
    }
86
    public function commit() : bool
87
    {
88
        $this->connect();
89
        $this->transaction = false;
90
        $res = \odbc_commit($this->lnk);
91
        \odbc_autocommit($this->lnk, false);
92
        return $res;
93
    }
94
    public function rollback() : bool
95
    {
96
        $this->connect();
97
        $this->transaction = false;
98
        $res = \odbc_rollback($this->lnk);
99
        \odbc_autocommit($this->lnk, false);
100
        return $res;
101
    }
102
    public function isTransaction()
103
    {
104
        return $this->transaction;
105
    }
106
}
107