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
|
45 |
|
function __construct( |
73
|
|
|
\Plasma\ClientInterface $client, \Plasma\Drivers\MySQL\Driver $driver, $id, string $query, string $rQuery, array $rParams, array $params, array $columns |
74
|
|
|
) { |
75
|
45 |
|
$this->client = $client; |
76
|
45 |
|
$this->driver = $driver; |
77
|
|
|
|
78
|
45 |
|
$this->id = $id; |
79
|
45 |
|
$this->query = $query; |
80
|
45 |
|
$this->rewrittenQuery = $rQuery; |
81
|
45 |
|
$this->rewrittenParams = $rParams; |
82
|
45 |
|
$this->params = $params; |
83
|
45 |
|
$this->columns = $columns; |
84
|
45 |
|
} |
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
|
|
|
* @throws \Plasma\Exception |
91
|
|
|
* @codeCoverageIgnore |
92
|
|
|
*/ |
93
|
|
|
function __destruct() { |
94
|
|
|
if(!$this->closed) { |
95
|
|
|
$this->close()->then(null, function () { |
96
|
|
|
// Error during implicit close, close the session |
97
|
|
|
$this->driver->close(); |
98
|
|
|
}); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the driver-dependent ID of this statement. |
104
|
|
|
* The return type can be of ANY type, as the ID depends on the driver and DBMS. |
105
|
|
|
* @return mixed |
106
|
|
|
*/ |
107
|
1 |
|
function getID() { |
108
|
1 |
|
return $this->id; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the prepared query. |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
1 |
|
function getQuery(): string { |
116
|
1 |
|
return $this->query; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Whether the statement has been closed. |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
2 |
|
function isClosed(): bool { |
124
|
2 |
|
return $this->closed; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Closes the prepared statement and frees the associated resources on the server. |
129
|
|
|
* Closing a statement more than once has no effect. |
130
|
|
|
* @return \React\Promise\PromiseInterface |
131
|
|
|
*/ |
132
|
44 |
|
function close(): \React\Promise\PromiseInterface { |
133
|
44 |
|
if($this->closed) { |
134
|
1 |
|
return \React\Promise\resolve(); |
135
|
|
|
} |
136
|
|
|
|
137
|
44 |
|
$this->closed = true; |
138
|
|
|
|
139
|
44 |
|
$close = new \Plasma\Drivers\MySQL\Commands\StatementCloseCommand($this->driver, $this->id); |
140
|
44 |
|
$this->driver->executeCommand($close); |
141
|
|
|
|
142
|
|
|
return $close->getPromise()->then(function () { |
143
|
35 |
|
$this->client->checkinConnection($this->driver); |
144
|
44 |
|
}); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Executes the prepared statement. Resolves with a `QueryResult` instance. |
149
|
|
|
* @param array $params |
150
|
|
|
* @return \React\Promise\PromiseInterface |
151
|
|
|
* @throws \Plasma\Exception Thrown if the statement is executed after it has been closed, or thrown for insufficent or missing parameters. |
152
|
|
|
* @see \Plasma\QueryResultInterface |
153
|
|
|
*/ |
154
|
40 |
|
function execute(array $params = array()): \React\Promise\PromiseInterface { |
155
|
40 |
|
if($this->closed) { |
156
|
1 |
|
throw new \Plasma\Exception('Statement has been closed'); |
157
|
|
|
} |
158
|
|
|
|
159
|
39 |
|
$params = \Plasma\Utility::replaceParameters($this->rewrittenParams, $params); |
160
|
|
|
|
161
|
37 |
|
$execute = new \Plasma\Drivers\MySQL\Commands\StatementExecuteCommand($this->driver, $this->id, $this->query, $params, $this->params); |
162
|
37 |
|
$this->driver->executeCommand($execute); |
163
|
|
|
|
164
|
37 |
|
return $execute->getPromise(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Runs the given querybuilder on the underlying driver instance. However the query will be ignored, only the parameters are used. |
169
|
|
|
* The driver CAN throw an exception if the given querybuilder is not supported. |
170
|
|
|
* An example would be a SQL querybuilder and a Cassandra driver. |
171
|
|
|
* @param \Plasma\QueryBuilderInterface $query |
172
|
|
|
* @return \React\Promise\PromiseInterface |
173
|
|
|
* @throws \Plasma\Exception |
174
|
|
|
*/ |
175
|
|
|
function runQuery(\Plasma\QueryBuilderInterface $query): \React\Promise\PromiseInterface { |
176
|
|
|
return $this->execute($query->getParameters()); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get the parsed parameters. |
181
|
|
|
* @return \Plasma\ColumnDefinitionInterface[] |
182
|
|
|
*/ |
183
|
1 |
|
function getParams(): array { |
184
|
1 |
|
return $this->params; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get the columns. |
189
|
|
|
* @return \Plasma\ColumnDefinitionInterface[] |
190
|
|
|
*/ |
191
|
1 |
|
function getColumns(): array { |
192
|
1 |
|
return $this->columns; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|