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
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* DriverAbstract |
33
|
|
|
* |
34
|
|
|
* @package Phossa2\Db |
35
|
|
|
* @author Hong Zhang <[email protected]> |
36
|
|
|
* @see ObjectAbstract |
37
|
|
|
* @see DriverInterface |
38
|
|
|
* @see TagAwareInterface |
39
|
|
|
* @version 2.0.0 |
40
|
|
|
* @since 2.0.0 added |
41
|
|
|
*/ |
42
|
|
|
abstract class DriverAbstract extends ObjectAbstract implements DriverInterface, TagAwareInterface |
43
|
|
|
{ |
44
|
|
|
use ConnectTrait, TransactionTrait, ErrorAwareTrait, ProfilerAwareTrait, TagAwareTrait; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Statement prototype |
48
|
|
|
* |
49
|
|
|
* @var StatementInterface |
50
|
|
|
* @access protected |
51
|
|
|
*/ |
52
|
|
|
protected $statement_prototype; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* current statement |
56
|
|
|
* |
57
|
|
|
* @var StatementInterface |
58
|
|
|
* @access protected |
59
|
|
|
*/ |
60
|
|
|
protected $statement; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* constructor |
64
|
|
|
* |
65
|
|
|
* @param array $parameters |
66
|
|
|
* @throws LogicException driver specific extension not loaded |
67
|
|
|
* @access public |
68
|
|
|
*/ |
69
|
|
|
public function __construct(array $parameters) |
70
|
|
|
{ |
71
|
|
|
if (!$this->extensionLoaded()) { |
72
|
|
|
throw new LogicException( |
73
|
|
|
Message::get(Message::DB_EXTENSION_NOTLOAD, get_class($this)), |
74
|
|
|
Message::DB_EXTENSION_NOTLOAD |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
$this->connect_parameters = $parameters; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritDoc} |
82
|
|
|
*/ |
83
|
|
|
public function prepare(/*# string */ $sql)/*# : bool */ |
84
|
|
|
{ |
85
|
|
|
if ($this->statement) { |
86
|
|
|
$this->statement->close(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// new statement |
90
|
|
|
$this->statement = clone $this->statement_prototype; |
91
|
|
|
$this->statement->setDriver($this); |
92
|
|
|
|
93
|
|
|
try { |
94
|
|
|
return $this->statement->prepare($sql); |
95
|
|
|
} catch (\Exception $e) { |
96
|
|
|
return $this->setError($e->getMessage(), $e->getCode()); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritDoc} |
102
|
|
|
*/ |
103
|
|
|
public function getStatement()/*# : StatementInterface */ |
104
|
|
|
{ |
105
|
|
|
if (null === $this->statement) { |
106
|
|
|
throw new NotFoundException( |
107
|
|
|
Message::get(Message::DB_STMT_NOTPREPARED), |
108
|
|
|
Message::DB_STMT_NOTPREPARED |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
return $this->statement; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritDoc} |
116
|
|
|
*/ |
117
|
|
|
public function query( |
118
|
|
|
/*# string */ $sql, |
119
|
|
|
array $parameters = [] |
120
|
|
|
)/*# : bool */ { |
121
|
|
|
try { |
122
|
|
|
$this->prepare($sql) && $this->statement->execute($parameters); |
123
|
|
|
return true; |
124
|
|
|
} catch (\Exception $e) { |
125
|
|
|
return $this->setError($e->getMessage(), $e->getCode()); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritDoc} |
131
|
|
|
*/ |
132
|
|
|
public function getResult()/*# : ResultInterface */ |
133
|
|
|
{ |
134
|
|
|
return $this->getStatement()->getResult(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritDoc} |
139
|
|
|
*/ |
140
|
|
|
public function affectedRows()/*# : int */ |
141
|
|
|
{ |
142
|
|
|
try { |
143
|
|
|
return $this->getResult()->affectedRows(); |
144
|
|
|
} catch (\Exception $e) { |
145
|
|
|
return 0; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritDoc} |
151
|
|
|
*/ |
152
|
|
|
public function lastInsertId($name = null) |
153
|
|
|
{ |
154
|
|
|
if ($this->isConnected()) { |
155
|
|
|
return $this->realLastId($name); |
156
|
|
|
} |
157
|
|
|
return null; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritDoc} |
162
|
|
|
*/ |
163
|
|
|
public function quote( |
164
|
|
|
$string, |
165
|
|
|
/*# int */ $type = Types::PARAM_STR |
166
|
|
|
)/*# : string */ { |
167
|
|
|
if ($this->isConnected()) { |
168
|
|
|
return $this->readlQuote($string, Types::guessType($string, $type)); |
|
|
|
|
169
|
|
|
} |
170
|
|
|
// default |
171
|
|
|
return "'" . $string . "'"; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Check driver specific extension loaded or not |
176
|
|
|
* |
177
|
|
|
* @return bool |
178
|
|
|
* @access protected |
179
|
|
|
*/ |
180
|
|
|
abstract protected function extensionLoaded()/*# : bool */; |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Driver specific last inserted id |
184
|
|
|
* |
185
|
|
|
* @param string|null $name sequence name |
186
|
|
|
* @return string|null |
187
|
|
|
* @access protected |
188
|
|
|
*/ |
189
|
|
|
abstract protected function realLastId($name); |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* The real quote method |
193
|
|
|
* |
194
|
|
|
* @param mixed $string |
195
|
|
|
* @param int $type |
196
|
|
|
* @return string |
197
|
|
|
* @access protected |
198
|
|
|
*/ |
199
|
|
|
abstract protected function realQuote( |
200
|
|
|
$string, |
201
|
|
|
/*# int */ $type |
202
|
|
|
)/*# : string */; |
203
|
|
|
} |
204
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.