|
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 $connection; |
|
15
|
|
|
protected $lnk = null; |
|
16
|
|
|
protected $transaction = null; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(array $connection) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->connection = $connection; |
|
21
|
|
|
if (!is_file($connection['name']) && is_file('/'.$connection['name'])) { |
|
22
|
|
|
$this->connection['name'] = '/'.$connection['name']; |
|
23
|
|
|
} |
|
24
|
|
|
$this->connection['host'] = ($connection['host'] === 'localhost' || $connection['host'] === '') ? |
|
25
|
|
|
'' : $connection['host'].':'; |
|
26
|
|
|
} |
|
27
|
|
|
public function __destruct() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->disconnect(); |
|
30
|
|
|
} |
|
31
|
|
|
protected function connect() |
|
32
|
|
|
{ |
|
33
|
|
|
$this->lnk = call_user_func( |
|
34
|
|
|
$this->option('persist') ? '\ibase_pconnect' : '\ibase_connect', |
|
35
|
|
|
$this->connection['host'].$this->connection['name'], |
|
36
|
|
|
$this->connection['user'], |
|
37
|
|
|
$this->connection['pass'], |
|
38
|
|
|
strtoupper($this->option('charset', 'utf8')) |
|
39
|
|
|
); |
|
40
|
|
|
if ($this->lnk === false) { |
|
41
|
|
|
throw new DBException('Connect error: '.\ibase_errmsg()); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
protected function disconnect() |
|
45
|
|
|
{ |
|
46
|
|
|
if (is_resource($this->lnk)) { |
|
47
|
|
|
\ibase_close($this->lnk); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
public function prepare(string $sql) : StatementInterface |
|
51
|
|
|
{ |
|
52
|
|
|
$this->connect(); |
|
53
|
|
|
$statement = \ibase_prepare($this->transaction !== null ? $this->transaction : $this->lnk, $sql); |
|
54
|
|
|
if ($statement === false) { |
|
55
|
|
|
throw new DBException('Prepare error: ' . \ibase_errmsg()); |
|
56
|
|
|
} |
|
57
|
|
|
return new Statement( |
|
58
|
|
|
$statement, |
|
59
|
|
|
$this->lnk |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function begin() : bool |
|
64
|
|
|
{ |
|
65
|
|
|
$this->connect(); |
|
66
|
|
|
$this->transaction = \ibase_trans($this->lnk); |
|
67
|
|
|
if ($this->transaction === false) { |
|
68
|
|
|
$this->transaction === null; |
|
69
|
|
|
} |
|
70
|
|
|
return ($this->transaction !== null); |
|
71
|
|
|
} |
|
72
|
|
|
public function commit() : bool |
|
73
|
|
|
{ |
|
74
|
|
|
$this->connect(); |
|
75
|
|
|
if ($this->transaction === null) { |
|
76
|
|
|
return false; |
|
77
|
|
|
} |
|
78
|
|
|
if (!\ibase_commit($this->transaction)) { |
|
79
|
|
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
$this->transaction = null; |
|
82
|
|
|
|
|
83
|
|
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
public function rollback() : bool |
|
86
|
|
|
{ |
|
87
|
|
|
$this->connect(); |
|
88
|
|
|
if ($this->transaction === null) { |
|
89
|
|
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
if (!\ibase_rollback($this->transaction)) { |
|
92
|
|
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
$this->transaction = null; |
|
95
|
|
|
|
|
96
|
|
|
return true; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function isTransaction() |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->transaction !== null; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|