|
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
|
|
|
public 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
|
|
|
$this->lnk = null; |
|
53
|
|
|
return false; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
public function disconnect() |
|
57
|
|
|
{ |
|
58
|
|
|
if (is_resource($this->lnk)) { |
|
59
|
|
|
\ibase_close($this->lnk); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
public function raw(string $sql) |
|
63
|
|
|
{ |
|
64
|
|
|
return \ibase_query($this->lnk, $sql); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
public function prepare(string $sql) : StatementInterface |
|
67
|
|
|
{ |
|
68
|
|
|
$this->connect(); |
|
69
|
|
|
$statement = \ibase_prepare($this->transaction !== null ? $this->transaction : $this->lnk, $sql); |
|
70
|
|
|
if ($statement === false) { |
|
71
|
|
|
throw new DBException('Prepare error: ' . \ibase_errmsg()); |
|
72
|
|
|
} |
|
73
|
|
|
return new Statement( |
|
74
|
|
|
$statement, |
|
75
|
|
|
$this->lnk |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function begin() : bool |
|
80
|
|
|
{ |
|
81
|
|
|
$this->connect(); |
|
82
|
|
|
$this->transaction = \ibase_trans($this->lnk); |
|
83
|
|
|
if ($this->transaction === false) { |
|
84
|
|
|
$this->transaction === null; |
|
85
|
|
|
} |
|
86
|
|
|
return ($this->transaction !== null); |
|
87
|
|
|
} |
|
88
|
|
|
public function commit() : bool |
|
89
|
|
|
{ |
|
90
|
|
|
$this->connect(); |
|
91
|
|
|
if ($this->transaction === null) { |
|
92
|
|
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
if (!\ibase_commit($this->transaction)) { |
|
95
|
|
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
$this->transaction = null; |
|
98
|
|
|
|
|
99
|
|
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
public function rollback() : bool |
|
102
|
|
|
{ |
|
103
|
|
|
$this->connect(); |
|
104
|
|
|
if ($this->transaction === null) { |
|
105
|
|
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
if (!\ibase_rollback($this->transaction)) { |
|
108
|
|
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
$this->transaction = null; |
|
111
|
|
|
|
|
112
|
|
|
return true; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function isTransaction() |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->transaction !== null; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.