|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Plasma Driver MySQL component |
|
4
|
|
|
* Copyright 2018-2019 PlasmaPHP, All Rights Reserved |
|
5
|
|
|
* |
|
6
|
|
|
* Website: https://github.com/PlasmaPHP |
|
7
|
|
|
* License: https://github.com/PlasmaPHP/driver-mysql/blob/master/LICENSE |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Plasma\Drivers\MySQL; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Represents a Prepared Statement. |
|
14
|
|
|
*/ |
|
15
|
|
|
class Statement implements \Plasma\StatementInterface { |
|
16
|
|
|
/** |
|
17
|
|
|
* @var \Plasma\ClientInterface |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $client; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var \Plasma\Drivers\MySQL\Driver |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $driver; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var mixed |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $id; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $query; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $rewrittenQuery; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $rewrittenParams; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var \Plasma\ColumnDefinitionInterface[] |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $params; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var \Plasma\ColumnDefinitionInterface[] |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $columns; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var bool |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $closed = false; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Constructor. |
|
63
|
|
|
* @param \Plasma\ClientInterface $client |
|
64
|
|
|
* @param \Plasma\Drivers\MySQL\Driver $driver |
|
65
|
|
|
* @param mixed $id |
|
66
|
|
|
* @param string $query |
|
67
|
|
|
* @param string $rQuery |
|
68
|
|
|
* @param array $rParams |
|
69
|
|
|
* @param \Plasma\ColumnDefinitionInterface[] $params |
|
70
|
|
|
* @param \Plasma\ColumnDefinitionInterface[] $columns |
|
71
|
|
|
*/ |
|
72
|
|
|
function __construct( |
|
73
|
|
|
\Plasma\ClientInterface $client, \Plasma\Drivers\MySQL\Driver $driver, $id, string $query, string $rQuery, array $rParams, array $params, array $columns |
|
74
|
|
|
) { |
|
75
|
|
|
$this->client = $client; |
|
76
|
|
|
$this->driver = $driver; |
|
77
|
|
|
|
|
78
|
|
|
$this->id = $id; |
|
79
|
|
|
$this->query = $query; |
|
80
|
|
|
$this->rewrittenQuery = $rQuery; |
|
81
|
|
|
$this->rewrittenParams = $rParams; |
|
82
|
|
|
$this->params = $params; |
|
83
|
|
|
$this->columns = $columns; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Destructor. Runs once the instance goes out of scope. |
|
88
|
|
|
* Please do not rely on the destructor to properly close your statement. |
|
89
|
|
|
* ALWAYS explicitely close the statement once you're done. |
|
90
|
|
|
* @codeCoverageIgnore |
|
91
|
|
|
*/ |
|
92
|
|
|
function __destruct() { |
|
93
|
|
|
if(!$this->closed) { |
|
94
|
|
|
$this->close()->then(null, function () { |
|
95
|
|
|
// Error during implicit close, close the session |
|
96
|
|
|
$this->driver->close(); |
|
97
|
|
|
}); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get the driver-dependent ID of this statement. |
|
103
|
|
|
* The return type can be of ANY type, as the ID depends on the driver and DBMS. |
|
104
|
|
|
* @return mixed |
|
105
|
|
|
*/ |
|
106
|
|
|
function getID() { |
|
107
|
|
|
return $this->id; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get the prepared query. |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
|
|
function getQuery(): string { |
|
115
|
|
|
return $this->query; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Whether the statement has been closed. |
|
120
|
|
|
* @return bool |
|
121
|
|
|
*/ |
|
122
|
|
|
function isClosed(): bool { |
|
123
|
|
|
return $this->closed; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Closes the prepared statement and frees the associated resources on the server. |
|
128
|
|
|
* Closing a statement more than once has no effect. |
|
129
|
|
|
* @return \React\Promise\PromiseInterface |
|
130
|
|
|
*/ |
|
131
|
|
|
function close(): \React\Promise\PromiseInterface { |
|
132
|
|
|
if($this->closed) { |
|
133
|
|
|
return \React\Promise\resolve(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$this->closed = true; |
|
137
|
|
|
|
|
138
|
|
|
$close = new \Plasma\Drivers\MySQL\Commands\StatementCloseCommand($this->driver, $this->id); |
|
139
|
|
|
$this->driver->executeCommand($close); |
|
140
|
|
|
|
|
141
|
|
|
return $close->getPromise()->then(function () { |
|
142
|
|
|
$this->client->checkinConnection($this->driver); |
|
143
|
|
|
}); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Executes the prepared statement. Resolves with a `QueryResult` instance. |
|
148
|
|
|
* @param array $params |
|
149
|
|
|
* @return \React\Promise\PromiseInterface |
|
150
|
|
|
* @throws \Plasma\Exception Thrown if the statement is executed after it has been closed, or thrown for insufficent or missing parameters. |
|
151
|
|
|
* @see \Plasma\QueryResultInterface |
|
152
|
|
|
*/ |
|
153
|
|
|
function execute(array $params = array()): \React\Promise\PromiseInterface { |
|
154
|
|
|
if($this->closed) { |
|
155
|
|
|
throw new \Plasma\Exception('Statement has been closed'); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
$params = \Plasma\Utility::replaceParameters($this->rewrittenParams, $params); |
|
159
|
|
|
|
|
160
|
|
|
$execute = new \Plasma\Drivers\MySQL\Commands\StatementExecuteCommand($this->driver, $this->id, $this->query, $params, $this->params); |
|
161
|
|
|
$this->driver->executeCommand($execute); |
|
162
|
|
|
|
|
163
|
|
|
return $execute->getPromise(); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Runs the given querybuilder on the underlying driver instance. However the query will be ignored, only the parameters are used. |
|
168
|
|
|
* The driver CAN throw an exception if the given querybuilder is not supported. |
|
169
|
|
|
* An example would be a SQL querybuilder and a Cassandra driver. |
|
170
|
|
|
* @param \Plasma\QueryBuilderInterface $query |
|
171
|
|
|
* @return \React\Promise\PromiseInterface |
|
172
|
|
|
* @throws \Plasma\Exception |
|
173
|
|
|
*/ |
|
174
|
|
|
function runQuery(\Plasma\QueryBuilderInterface $query): \React\Promise\PromiseInterface { |
|
175
|
|
|
return $this->execute($query->getParameters()); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Creates a cursor. |
|
180
|
|
|
* @param array $params |
|
181
|
|
|
* @return \React\Promise\PromiseInterface |
|
182
|
|
|
* @throws \LogicException Thrown if the driver or DBMS does not support cursors. |
|
183
|
|
|
* @throws \Plasma\Exception Thrown if the statement is executed after it has been closed, or if it's not a SELECT query, or for insufficent or missing parameters. |
|
184
|
|
|
* @internal |
|
185
|
|
|
*/ |
|
186
|
|
|
function createReadCursor(array $params = array()): \React\Promise\PromiseInterface { |
|
187
|
|
|
if($this->closed) { |
|
188
|
|
|
throw new \Plasma\Exception('Statement has been closed'); |
|
189
|
|
|
} elseif(empty($this->columns)) { |
|
190
|
|
|
throw new \Plasma\Exception('Query is not a SELECT query'); |
|
191
|
|
|
} elseif(!$this->driver->supportsCursors()) { |
|
192
|
|
|
throw new \LogicException('Used DBMS version does not support cursors'); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
$params = \Plasma\Utility::replaceParameters($this->rewrittenParams, $params); |
|
196
|
|
|
|
|
197
|
|
|
$execute = new \Plasma\Drivers\MySQL\Commands\StatementExecuteCommand($this->driver, $this->id, $this->query, $params, $this->params, 0x05); |
|
198
|
|
|
$this->driver->executeCommand($execute); |
|
199
|
|
|
|
|
200
|
|
|
return $execute->getPromise()->then(function () { |
|
201
|
|
|
return (new \Plasma\Drivers\MySQL\StatementCursor($this->driver, $this)); |
|
202
|
|
|
}); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Get the parsed parameters. |
|
207
|
|
|
* @return \Plasma\ColumnDefinitionInterface[] |
|
208
|
|
|
*/ |
|
209
|
|
|
function getParams(): array { |
|
210
|
|
|
return $this->params; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Get the columns. |
|
215
|
|
|
* @return \Plasma\ColumnDefinitionInterface[] |
|
216
|
|
|
*/ |
|
217
|
|
|
function getColumns(): array { |
|
218
|
|
|
return $this->columns; |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
|