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

Driver::rollback()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 8
nc 3
nop 0
1
<?php
2
3
namespace vakata\database\driver\ibase;
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 = null;
16
17
    public function __construct(array $connection)
18
    {
19
        $this->connection = $connection;
20
        if (!is_file($connection['name']) && is_file('/'.$connection['name'])) {
21
            $this->connection['name'] = '/'.$connection['name'];
22
        }
23
        $this->connection['host'] = ($connection['host'] === 'localhost' || $connection['host'] === '') ?
24
                '' : $connection['host'].':';
25
    }
26
    public function __destruct()
27
    {
28
        $this->disconnect();
29
    }
30
    protected function connect()
31
    {
32
        $this->lnk = call_user_func(
33
            $this->option('persist') ? '\ibase_pconnect' : '\ibase_connect',
34
            $this->connection['host'].$this->connection['name'],
35
            $this->connection['user'],
36
            $this->connection['pass'],
37
            strtoupper($this->option('charset', 'utf8'))
38
        );
39
        if ($this->lnk === false) {
40
            throw new DBException('Connect error: '.\ibase_errmsg());
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
            \ibase_close($this->lnk);
59
        }
60
    }
61
    public function prepare(string $sql) : StatementInterface
62
    {
63
        $this->connect();
64
        $statement = \ibase_prepare($this->transaction !== null ? $this->transaction : $this->lnk, $sql);
65
        if ($statement === false) {
66
            throw new DBException('Prepare error: ' . \ibase_errmsg());
67
        }
68
        return new Statement(
69
            $statement,
70
            $this->lnk
71
        );
72
    }
73
74
    public function begin() : bool
75
    {
76
        $this->connect();
77
        $this->transaction = \ibase_trans($this->lnk);
78
        if ($this->transaction === false) {
79
            $this->transaction === null;
80
        }
81
        return ($this->transaction !== null);
82
    }
83
    public function commit() : bool
84
    {
85
        $this->connect();
86
        if ($this->transaction === null) {
87
            return false;
88
        }
89
        if (!\ibase_commit($this->transaction)) {
90
            return false;
91
        }
92
        $this->transaction = null;
93
94
        return true;
95
    }
96
    public function rollback() : bool
97
    {
98
        $this->connect();
99
        if ($this->transaction === null) {
100
            return false;
101
        }
102
        if (!\ibase_rollback($this->transaction)) {
103
            return false;
104
        }
105
        $this->transaction = null;
106
107
        return true;
108
    }
109
110
    public function isTransaction()
111
    {
112
        return $this->transaction !== null;
113
    }
114
}
115