PlasmaPHP /
driver-mysql
| 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 | 47 | function __construct( |
|
| 73 | \Plasma\ClientInterface $client, \Plasma\Drivers\MySQL\Driver $driver, $id, string $query, string $rQuery, array $rParams, array $params, array $columns |
||
| 74 | ) { |
||
| 75 | 47 | $this->client = $client; |
|
| 76 | 47 | $this->driver = $driver; |
|
| 77 | |||
| 78 | 47 | $this->id = $id; |
|
| 79 | 47 | $this->query = $query; |
|
| 80 | 47 | $this->rewrittenQuery = $rQuery; |
|
| 81 | 47 | $this->rewrittenParams = $rParams; |
|
| 82 | 47 | $this->params = $params; |
|
| 83 | 47 | $this->columns = $columns; |
|
| 84 | 47 | } |
|
| 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 | 2 | function getID() { |
|
| 107 | 2 | return $this->id; |
|
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Get the prepared query. |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | 2 | function getQuery(): string { |
|
| 115 | 2 | return $this->query; |
|
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Whether the statement has been closed. |
||
| 120 | * @return bool |
||
| 121 | */ |
||
| 122 | 3 | function isClosed(): bool { |
|
| 123 | 3 | 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 | 47 | function close(): \React\Promise\PromiseInterface { |
|
| 132 | 47 | if($this->closed) { |
|
| 133 | 1 | return \React\Promise\resolve(); |
|
| 134 | } |
||
| 135 | |||
| 136 | 47 | $this->closed = true; |
|
| 137 | |||
| 138 | 47 | $close = new \Plasma\Drivers\MySQL\Commands\StatementCloseCommand($this->driver, $this->id); |
|
| 139 | 47 | $this->driver->executeCommand($close); |
|
| 140 | |||
| 141 | return $close->getPromise()->then(function () { |
||
| 142 | 37 | $this->client->checkinConnection($this->driver); |
|
| 143 | 47 | }); |
|
| 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 | 41 | function execute(array $params = array()): \React\Promise\PromiseInterface { |
|
| 154 | 41 | if($this->closed) { |
|
| 155 | 1 | throw new \Plasma\Exception('Statement has been closed'); |
|
| 156 | } |
||
| 157 | |||
| 158 | 40 | $params = \Plasma\Utility::replaceParameters($this->rewrittenParams, $params); |
|
| 159 | |||
| 160 | 38 | $execute = new \Plasma\Drivers\MySQL\Commands\StatementExecuteCommand($this->driver, $this->id, $this->query, $params, $this->params); |
|
| 161 | 38 | $this->driver->executeCommand($execute); |
|
| 162 | |||
| 163 | 38 | 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 | 1 | function createCursor(array $params = array()): \React\Promise\PromiseInterface { |
|
| 187 | 1 | if($this->closed) { |
|
| 188 | throw new \Plasma\Exception('Statement has been closed'); |
||
| 189 | 1 | } elseif(empty($this->columns)) { |
|
| 190 | throw new \Plasma\Exception('Query is not a SELECT query'); |
||
| 191 | } elseif( |
||
| 192 | 1 | $this->driver->getHandshake() !== null && |
|
| 193 | ( |
||
| 194 | 1 | ($this->driver->getHandshake()->capability & \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_PS_MULTI_RESULTS) === 0 || |
|
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 195 | 1 | \version_compare($this->driver->getHandshake()->serverVersion, '5.7', '<') |
|
|
0 ignored issues
–
show
|
|||
| 196 | ) |
||
| 197 | ) { |
||
| 198 | throw new \LogicException('Used DBMS version does not support cursors'); |
||
| 199 | } |
||
| 200 | |||
| 201 | 1 | $params = \Plasma\Utility::replaceParameters($this->rewrittenParams, $params); |
|
| 202 | |||
| 203 | 1 | $execute = new \Plasma\Drivers\MySQL\Commands\StatementExecuteCommand($this->driver, $this->id, $this->query, $params, $this->params, 0x05); |
|
| 204 | 1 | $this->driver->executeCommand($execute); |
|
| 205 | |||
| 206 | return $execute->getPromise()->then(function () { |
||
| 207 | 1 | return (new \Plasma\Drivers\MySQL\StatementCursor($this->driver, $this)); |
|
| 208 | 1 | }); |
|
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Get the parsed parameters. |
||
| 213 | * @return \Plasma\ColumnDefinitionInterface[] |
||
| 214 | */ |
||
| 215 | 2 | function getParams(): array { |
|
| 216 | 2 | return $this->params; |
|
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get the columns. |
||
| 221 | * @return \Plasma\ColumnDefinitionInterface[] |
||
| 222 | */ |
||
| 223 | 2 | function getColumns(): array { |
|
| 224 | 2 | return $this->columns; |
|
| 225 | } |
||
| 226 | } |
||
| 227 |