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\Driver; |
16
|
|
|
|
17
|
|
|
use Phossa2\Db\Types; |
18
|
|
|
use Phossa2\Db\Message\Message; |
19
|
|
|
use Phossa2\Db\Traits\ConnectTrait; |
20
|
|
|
use Phossa2\Shared\Aware\TagAwareTrait; |
21
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
22
|
|
|
use Phossa2\Db\Traits\TransactionTrait; |
23
|
|
|
use Phossa2\Db\Exception\LogicException; |
24
|
|
|
use Phossa2\Db\Traits\ProfilerAwareTrait; |
25
|
|
|
use Phossa2\Shared\Error\ErrorAwareTrait; |
26
|
|
|
use Phossa2\Db\Interfaces\DriverInterface; |
27
|
|
|
use Phossa2\Shared\Aware\TagAwareInterface; |
28
|
|
|
use Phossa2\Db\Exception\NotFoundException; |
29
|
|
|
use Phossa2\Db\Interfaces\StatementInterface; |
30
|
|
|
use Phossa2\Db\Exception\BadMethodCallException; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* DriverAbstract |
34
|
|
|
* |
35
|
|
|
* These methods are from ResultInterface |
36
|
|
|
* |
37
|
|
|
* @method bool isSelect() |
38
|
|
|
* @method int fieldCount() |
39
|
|
|
* @method int rowCount() |
40
|
|
|
* @method int affectedRows() |
41
|
|
|
* @method array fetchAll() |
42
|
|
|
* @method array fetchRow(int $rowCount) |
43
|
|
|
* @method array fetchCol(int $col, int $rowCount) |
44
|
|
|
* |
45
|
|
|
* @package Phossa2\Db |
46
|
|
|
* @author Hong Zhang <[email protected]> |
47
|
|
|
* @see ObjectAbstract |
48
|
|
|
* @see DriverInterface |
49
|
|
|
* @see TagAwareInterface |
50
|
|
|
* @version 2.0.0 |
51
|
|
|
* @since 2.0.0 added |
52
|
|
|
*/ |
53
|
|
|
abstract class DriverAbstract extends ObjectAbstract implements DriverInterface, TagAwareInterface |
54
|
|
|
{ |
55
|
|
|
use ConnectTrait, TransactionTrait, ErrorAwareTrait, ProfilerAwareTrait, TagAwareTrait; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Statement prototype |
59
|
|
|
* |
60
|
|
|
* @var StatementInterface |
61
|
|
|
* @access protected |
62
|
|
|
*/ |
63
|
|
|
protected $statement_prototype; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* current statement |
67
|
|
|
* |
68
|
|
|
* @var StatementInterface |
69
|
|
|
* @access protected |
70
|
|
|
*/ |
71
|
|
|
protected $statement; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* constructor |
75
|
|
|
* |
76
|
|
|
* @param array $parameters |
77
|
|
|
* @throws LogicException driver specific extension not loaded |
78
|
|
|
* @access public |
79
|
|
|
*/ |
80
|
|
|
public function __construct(array $parameters) |
81
|
|
|
{ |
82
|
|
|
if (!$this->extensionLoaded()) { |
83
|
|
|
throw new LogicException( |
84
|
|
|
Message::get(Message::DB_EXTENSION_NOTLOAD, get_class($this)), |
85
|
|
|
Message::DB_EXTENSION_NOTLOAD |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
$this->connect_parameters = $parameters; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* |
93
|
|
|
* @param string $method |
94
|
|
|
* @param array $args |
95
|
|
|
* @return mixed |
96
|
|
|
* @throws BadMethodCallException |
97
|
|
|
* @access public |
98
|
|
|
*/ |
99
|
|
|
public function __call(/*# string */ $method, array $args) |
100
|
|
|
{ |
101
|
|
|
$result = $this->getResult(); |
102
|
|
|
if (method_exists($result, $method)) { |
103
|
|
|
return call_user_func_array([$result, $method], $args); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
throw new BadMethodCallException( |
107
|
|
|
Message::get(Message::MSG_METHOD_NOTFOUND, $method), |
108
|
|
|
Message::MSG_METHOD_NOTFOUND |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritDoc} |
114
|
|
|
*/ |
115
|
|
|
public function prepare(/*# string */ $sql)/*# : bool */ |
116
|
|
|
{ |
117
|
|
|
if ($this->statement) { |
118
|
|
|
$this->statement->close(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// new statement |
122
|
|
|
$this->statement = clone $this->statement_prototype; |
123
|
|
|
$this->statement->setDriver($this); |
124
|
|
|
|
125
|
|
|
try { |
126
|
|
|
return $this->statement->prepare($sql); |
127
|
|
|
} catch (\Exception $e) { |
128
|
|
|
return $this->setError($e->getMessage(), $e->getCode()); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritDoc} |
134
|
|
|
*/ |
135
|
|
|
public function getStatement()/*# : StatementInterface */ |
136
|
|
|
{ |
137
|
|
|
if (null === $this->statement) { |
138
|
|
|
throw new NotFoundException( |
139
|
|
|
Message::get(Message::DB_STMT_NOTPREPARED), |
140
|
|
|
Message::DB_STMT_NOTPREPARED |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
return $this->statement; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritDoc} |
148
|
|
|
*/ |
149
|
|
|
public function query( |
150
|
|
|
/*# string */ $sql, |
151
|
|
|
array $parameters = [] |
152
|
|
|
)/*# : bool */ { |
153
|
|
|
try { |
154
|
|
|
$this->prepare($sql) && $this->statement->execute($parameters); |
155
|
|
|
return true; |
156
|
|
|
} catch (\Exception $e) { |
157
|
|
|
return $this->setError($e->getMessage(), $e->getCode()); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* {@inheritDoc} |
163
|
|
|
*/ |
164
|
|
|
public function getResult()/*# : ResultInterface */ |
165
|
|
|
{ |
166
|
|
|
return $this->getStatement()->getResult(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritDoc} |
171
|
|
|
*/ |
172
|
|
|
public function lastInsertId($name = null) |
173
|
|
|
{ |
174
|
|
|
if ($this->isConnected()) { |
175
|
|
|
return $this->realLastId($name); |
176
|
|
|
} |
177
|
|
|
return null; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritDoc} |
182
|
|
|
*/ |
183
|
|
|
public function quote( |
184
|
|
|
$string, |
185
|
|
|
/*# int */ $type = Types::PARAM_STR |
186
|
|
|
)/*# : string */ { |
187
|
|
|
if ($this->isConnected()) { |
188
|
|
|
return $this->realQuote($string, Types::guessType($string, $type)); |
189
|
|
|
} |
190
|
|
|
// default |
191
|
|
|
return "'" . $string . "'"; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Check driver specific extension loaded or not |
196
|
|
|
* |
197
|
|
|
* @return bool |
198
|
|
|
* @access protected |
199
|
|
|
*/ |
200
|
|
|
abstract protected function extensionLoaded()/*# : bool */; |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Driver specific last inserted id |
204
|
|
|
* |
205
|
|
|
* @param string|null $name sequence name |
206
|
|
|
* @return string|null |
207
|
|
|
* @access protected |
208
|
|
|
*/ |
209
|
|
|
abstract protected function realLastId($name); |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* The real quote method |
213
|
|
|
* |
214
|
|
|
* @param mixed $string |
215
|
|
|
* @param int $type |
216
|
|
|
* @return string |
217
|
|
|
* @access protected |
218
|
|
|
*/ |
219
|
|
|
abstract protected function realQuote( |
220
|
|
|
$string, |
221
|
|
|
/*# int */ $type |
222
|
|
|
)/*# : string */; |
223
|
|
|
} |
224
|
|
|
|