1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Db |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Db\Traits; |
16
|
|
|
|
17
|
|
|
use Phossa2\Db\Exception\LogicException; |
18
|
|
|
use Phossa2\Db\Interfaces\ConnectInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* ConnectTrait |
22
|
|
|
* |
23
|
|
|
* Implementation of ConnectInterface |
24
|
|
|
* |
25
|
|
|
* @package Phossa2\Db |
26
|
|
|
* @author Hong Zhang <[email protected]> |
27
|
|
|
* @see ConnectInterface |
28
|
|
|
* @version 2.0.0 |
29
|
|
|
* @since 2.0.0 added |
30
|
|
|
*/ |
31
|
|
|
trait ConnectTrait |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* the connection link |
35
|
|
|
* |
36
|
|
|
* @var mixed |
37
|
|
|
* @access protected |
38
|
|
|
*/ |
39
|
|
|
protected $link; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* connect parameters |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
* @access protected |
46
|
|
|
*/ |
47
|
|
|
protected $connect_parameters = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* connection attributes |
51
|
|
|
* |
52
|
|
|
* @var array |
53
|
|
|
* @access protected |
54
|
|
|
*/ |
55
|
|
|
protected $attributes = []; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritDoc} |
59
|
|
|
*/ |
60
|
|
|
public function connect() |
61
|
|
|
{ |
62
|
|
|
if ($this->isConnected()) { |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
try { |
67
|
|
|
$this->link = $this->realConnect($this->connect_parameters); |
68
|
|
|
$this->setDefaultAttributes(); |
69
|
|
|
} catch (\Exception $e) { |
70
|
|
|
throw new LogicException($e->getMessage(), $e->getCode()); |
71
|
|
|
} |
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritDoc} |
77
|
|
|
*/ |
78
|
|
|
public function disconnect() |
79
|
|
|
{ |
80
|
|
|
if ($this->isConnected()) { |
81
|
|
|
$this->realDisconnect(); |
82
|
|
|
$this->link = null; |
83
|
|
|
} |
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritDoc} |
89
|
|
|
*/ |
90
|
|
|
public function isConnected()/*# : bool */ |
91
|
|
|
{ |
92
|
|
|
return null !== $this->link; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritDoc} |
97
|
|
|
*/ |
98
|
|
|
public function ping()/*# : bool */ |
99
|
|
|
{ |
100
|
|
|
try { |
101
|
|
|
$this->connect(); |
102
|
|
|
} catch (\Exception $e) { |
103
|
|
|
return $this->setError($e->getMessage(), $e->getCode()); |
104
|
|
|
} |
105
|
|
|
return $this->realPing(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritDoc} |
110
|
|
|
*/ |
111
|
|
|
public function getLink() |
112
|
|
|
{ |
113
|
|
|
$this->connect(); |
114
|
|
|
return $this->link; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritDoc} |
119
|
|
|
*/ |
120
|
|
|
public function setAttribute(/*# string */ $attribute, $value) |
121
|
|
|
{ |
122
|
|
|
$this->attributes[$attribute] = $value; |
123
|
|
|
if ($this->isConnected()) { |
124
|
|
|
$this->realSetAttribute($attribute, $value); |
125
|
|
|
} |
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritDoc} |
131
|
|
|
*/ |
132
|
|
|
public function getAttribute(/*# string */ $attribute) |
133
|
|
|
{ |
134
|
|
|
// current attribute |
135
|
|
|
$curr = isset($this->attributes[$attribute]) ? |
136
|
|
|
$this->attributes[$attribute] : null; |
137
|
|
|
|
138
|
|
|
if ($this->isConnected()) { |
139
|
|
|
$real = $this->realGetAttribute($attribute); |
140
|
|
|
return $real ?: $curr; |
141
|
|
|
} else { |
142
|
|
|
return $curr; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Set default attributes |
148
|
|
|
* |
149
|
|
|
* @access protected |
150
|
|
|
*/ |
151
|
|
|
protected function setDefaultAttributes() |
152
|
|
|
{ |
153
|
|
|
if (!empty($this->attributes)) { |
154
|
|
|
foreach ($this->attributes as $attr => $val) { |
155
|
|
|
$this->realSetAttribute($attr, $val); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Driver specific connect |
162
|
|
|
* |
163
|
|
|
* @param array $parameters |
164
|
|
|
* @return mixed the link |
165
|
|
|
* @throws \Exception |
166
|
|
|
* @access protected |
167
|
|
|
*/ |
168
|
|
|
abstract protected function realConnect(array $parameters); |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Driver specific disconnect |
172
|
|
|
* |
173
|
|
|
* @access protected |
174
|
|
|
*/ |
175
|
|
|
abstract protected function realDisconnect(); |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Driver related ping |
179
|
|
|
* |
180
|
|
|
* @return bool |
181
|
|
|
* @access protected |
182
|
|
|
*/ |
183
|
|
|
abstract protected function realPing()/*# : bool */; |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Set connection specific attribute |
187
|
|
|
* |
188
|
|
|
* @param string attribute |
189
|
|
|
* @param mixed $value |
190
|
|
|
* @throws LogicException if attribute unknown |
191
|
|
|
* @access protected |
192
|
|
|
*/ |
193
|
|
|
abstract protected function realSetAttribute( |
194
|
|
|
/*# string */ $attribute, |
195
|
|
|
$value |
196
|
|
|
); |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Get connection specific attribute |
200
|
|
|
* |
201
|
|
|
* @param string attribute |
202
|
|
|
* @return mixed|null |
203
|
|
|
* @throws LogicException if attribute unknown |
204
|
|
|
* @access protected |
205
|
|
|
*/ |
206
|
|
|
abstract protected function realGetAttribute(/*# string */ $attribute); |
207
|
|
|
|
208
|
|
|
// from ErrorAwareInterface |
209
|
|
|
abstract public function setError( |
210
|
|
|
/*# string */ $message = '', |
211
|
|
|
/*# string */ $code = '' |
212
|
|
|
)/*# : bool */; |
213
|
|
|
} |
214
|
|
|
|