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\Message\Message; |
18
|
|
|
use Phossa2\Db\Traits\DriverAwareTrait; |
19
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
20
|
|
|
use Phossa2\Db\Interfaces\ResultInterface; |
21
|
|
|
use Phossa2\Db\Exception\RuntimeException; |
22
|
|
|
use Phossa2\Db\Exception\NotFoundException; |
23
|
|
|
use Phossa2\Db\Interfaces\StatementInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* StatementAbstract |
27
|
|
|
* |
28
|
|
|
* @package Phossa2\Db |
29
|
|
|
* @author Hong Zhang <[email protected]> |
30
|
|
|
* @see ObjectAbstract |
31
|
|
|
* @see StatementInterface |
32
|
|
|
* @version 2.0.0 |
33
|
|
|
* @since 2.0.0 added |
34
|
|
|
*/ |
35
|
|
|
abstract class StatementAbstract extends ObjectAbstract implements StatementInterface |
36
|
|
|
{ |
37
|
|
|
use DriverAwareTrait; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* prepared statement |
41
|
|
|
* |
42
|
|
|
* @var mixed |
43
|
|
|
* @access protected |
44
|
|
|
*/ |
45
|
|
|
protected $prepared; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Result prototype |
49
|
|
|
* |
50
|
|
|
* @var ResultInterface |
51
|
|
|
* @access protected |
52
|
|
|
*/ |
53
|
|
|
protected $result_prototype; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var ResultInterface |
57
|
|
|
* @access protected |
58
|
|
|
*/ |
59
|
|
|
protected $result; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Desctructor |
63
|
|
|
* |
64
|
|
|
* @access public |
65
|
|
|
*/ |
66
|
|
|
public function __destruct() |
67
|
|
|
{ |
68
|
|
|
$this->close(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritDoc} |
73
|
|
|
*/ |
74
|
|
|
public function prepare(/*# string */ $sql)/*# : bool */ |
75
|
|
|
{ |
76
|
|
|
if (null === $this->prepared) { |
77
|
|
|
try { |
78
|
|
|
$res = $this->realPrepare($this->getDriver()->getLink(), $sql); |
79
|
|
|
if (false !== $res) { |
80
|
|
|
$this->prepared = $res; |
81
|
|
|
$this->getDriver()->getProfiler()->setSql($sql); |
82
|
|
|
return true; |
83
|
|
|
} |
84
|
|
|
} catch (\Exception $e) { |
85
|
|
|
throw new RuntimeException($e->getMessage(), $e->getCode(), $e); |
86
|
|
|
} |
87
|
|
|
throw new RuntimeException( |
88
|
|
|
Message::get(Message::DB_STMT_PREPARE_FAIL, $sql), |
89
|
|
|
Message::DB_STMT_PREPARE_FAIL |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritDoc} |
96
|
|
|
*/ |
97
|
|
|
public function execute(array $parameters = [])/*# : bool */ |
98
|
|
|
{ |
99
|
|
|
$this->checkPreparation(); // must be prepared |
100
|
|
|
$this->close(); // close previous result if any |
101
|
|
|
|
102
|
|
|
// int profiler |
103
|
|
|
$time = microtime(true); |
104
|
|
|
$this->getDriver()->getProfiler()->setParameters($parameters); |
105
|
|
|
|
106
|
|
|
if ($this->realExecute($parameters)) { |
107
|
|
|
$result = clone $this->result_prototype; |
108
|
|
|
$result($this->prepared); |
109
|
|
|
$this->result = $result; |
110
|
|
|
$this->getDriver()->getProfiler()->setExecutionTime(microtime(true) - $time); |
111
|
|
|
return true; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
throw new RuntimeException( |
115
|
|
|
Message::get( |
116
|
|
|
Message::DB_STMT_EXECUTE_FAIL, |
117
|
|
|
$this->getDriver()->getProfiler()->getSql() |
118
|
|
|
),Message::DB_STMT_EXECUTE_FAIL |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritDoc} |
124
|
|
|
*/ |
125
|
|
|
public function getResult()/*# : ResultInterface */ |
126
|
|
|
{ |
127
|
|
|
if (null === $this->result) { |
128
|
|
|
throw new NotFoundException( |
129
|
|
|
Message::get(Message::DB_STMT_NO_RESULT), |
130
|
|
|
Message::DB_STMT_NO_RESULT |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
return $this->result; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritDoc} |
138
|
|
|
*/ |
139
|
|
|
public function close() |
140
|
|
|
{ |
141
|
|
|
if ($this->prepared) { |
142
|
|
|
$this->realClose($this->prepared); |
143
|
|
|
$this->result = null; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Throw exception if not prepared |
149
|
|
|
* |
150
|
|
|
* @throws RuntimeException |
151
|
|
|
* @access protected |
152
|
|
|
*/ |
153
|
|
|
protected function checkPreparation() |
154
|
|
|
{ |
155
|
|
|
if (null === $this->prepared) { |
156
|
|
|
throw new RuntimeException( |
157
|
|
|
Message::get(Message::DB_STMT_NOTPREPARED), |
158
|
|
|
Message::DB_STMT_NOTPREPARED |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Driver specific prepare statement |
165
|
|
|
* |
166
|
|
|
* @param mixed $link db link resource |
167
|
|
|
* @param string $sql |
168
|
|
|
* @return mixed |
169
|
|
|
* @throws RuntimeException |
170
|
|
|
* @access protected |
171
|
|
|
*/ |
172
|
|
|
abstract protected function realPrepare($link, /*# string */ $sql); |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Driver specific statement execution |
176
|
|
|
* |
177
|
|
|
* @param array $parameters |
178
|
|
|
* @return bool |
179
|
|
|
* @throws RuntimeException |
180
|
|
|
* @access protected |
181
|
|
|
*/ |
182
|
|
|
abstract protected function realExecute(array $parameters)/*# : bool */; |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Close statement's result set |
186
|
|
|
* |
187
|
|
|
* @param mixed prepared low-level statement |
188
|
|
|
* @access protected |
189
|
|
|
*/ |
190
|
|
|
abstract protected function realClose($stmt); |
191
|
|
|
} |
192
|
|
|
|