Completed
Push — master ( a98725...347d9e )
by Ivan
03:41
created

Driver   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 78
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 2
A __destruct() 0 4 1
B connect() 0 14 6
A disconnect() 0 6 2
A prepare() 0 5 1
A begin() 0 7 1
A commit() 0 8 1
A rollback() 0 8 1
A isTransaction() 0 4 1
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
    protected function disconnect()
51
    {
52
        if (is_resource($this->lnk)) {
53
            \odbc_close($this->lnk);
54
        }
55
    }
56
    public function prepare(string $sql) : StatementInterface
57
    {
58
        $this->connect();
59
        return new Statement($sql, $this->lnk);
60
    }
61
62
    public function begin() : bool
63
    {
64
        $this->connect();
65
        $this->transaction = true;
66
        \odbc_autocommit($this->lnk, false);
67
        return true;
68
    }
69
    public function commit() : bool
70
    {
71
        $this->connect();
72
        $this->transaction = false;
73
        $res = \odbc_commit($this->lnk);
74
        \odbc_autocommit($this->lnk, false);
75
        return $res;
76
    }
77
    public function rollback() : bool
78
    {
79
        $this->connect();
80
        $this->transaction = false;
81
        $res = \odbc_rollback($this->lnk);
82
        \odbc_autocommit($this->lnk, false);
83
        return $res;
84
    }
85
    public function isTransaction()
86
    {
87
        return $this->transaction;
88
    }
89
}
90