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 Statement Cursor. |
||
| 14 | */ |
||
| 15 | class StatementCursor implements \Plasma\CursorInterface { |
||
| 16 | /** |
||
| 17 | * @var \Plasma\Drivers\MySQL\Driver |
||
| 18 | */ |
||
| 19 | protected $driver; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var \Plasma\Drivers\MySQL\Statement |
||
| 23 | */ |
||
| 24 | protected $statement; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var bool |
||
| 28 | */ |
||
| 29 | protected $closed = false; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Constructor. |
||
| 33 | * @param \Plasma\Drivers\MySQL\Driver $driver |
||
| 34 | * @param \Plasma\Drivers\MySQL\Statement $statement |
||
| 35 | */ |
||
| 36 | function __construct(\Plasma\Drivers\MySQL\Driver $driver, \Plasma\Drivers\MySQL\Statement $statement) { |
||
| 37 | $this->driver = $driver; |
||
| 38 | $this->statement = $statement; |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Destructor. Runs once the instance goes out of scope. |
||
| 43 | * Please do not rely on the destructor to properly close your cursor. |
||
| 44 | * ALWAYS explicitely close the cursor once you're done. |
||
| 45 | * @throws \Plasma\Exception |
||
| 46 | * @codeCoverageIgnore |
||
| 47 | */ |
||
| 48 | function __destruct() { |
||
| 49 | if(!$this->closed) { |
||
| 50 | $this->close()->then(null, function () { |
||
| 51 | // Error during implicit close, close the session |
||
| 52 | $this->driver->close(); |
||
| 53 | }); |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Whether the cursor has been closed. |
||
| 59 | * @return bool |
||
| 60 | */ |
||
| 61 | function isClosed(): bool { |
||
| 62 | return $this->closed; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Closes the cursor and frees the associated resources on the server. |
||
| 67 | * Closing a cursor more than once has no effect. |
||
| 68 | * @return \React\Promise\PromiseInterface |
||
| 69 | */ |
||
| 70 | function close(): \React\Promise\PromiseInterface { |
||
| 71 | if($this->closed) { |
||
| 72 | return \React\Promise\resolve(); |
||
| 73 | } |
||
| 74 | |||
| 75 | $this->closed = true; |
||
| 76 | |||
| 77 | return $this->statement->close(); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Fetches the given amount of rows using the cursor. Resolves with the row, an array of rows (if amount > 1), or false if no more results exist. |
||
| 82 | * @param int $amount |
||
| 83 | * @return \React\Promise\PromiseInterface |
||
| 84 | * @throws \Plasma\Exception Thrown if the underlying statement has been closed. |
||
| 85 | */ |
||
| 86 | function fetch(int $amount = 1): \React\Promise\PromiseInterface { |
||
| 87 | if($this->closed) { |
||
| 88 | return \React\Promise\resolve(false); |
||
| 89 | } elseif($this->statement->isClosed()) { |
||
| 90 | throw new \Plasma\Exception('Underlying statement has been closed'); |
||
| 91 | } |
||
| 92 | |||
| 93 | $fetch = new \Plasma\Drivers\MySQL\Commands\FetchCommand( |
||
| 94 | $this->driver, |
||
| 95 | $this, |
||
| 96 | $this->statement->getID(), |
||
| 97 | $this->statement->getQuery(), |
||
| 98 | $this->statement->getParams(), |
||
| 99 | array(), |
||
| 100 | $this->statement->getColumns(), |
||
| 101 | $amount |
||
| 102 | ); |
||
| 103 | $this->driver->executeCommand($fetch); |
||
| 104 | |||
| 105 | return $fetch->getPromise(); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Processes the OK or EOF message. |
||
| 110 | * @param \Plasma\Drivers\MySQL\Messages\OkResponseMessage|\Plasma\Drivers\MySQL\Messages\EOFMessage |
||
| 111 | * @return void |
||
| 112 | * @internal |
||
| 113 | */ |
||
| 114 | function processOkMessage($message): void { |
||
| 115 | if(($message->statusFlags & \Plasma\Drivers\MySQL\StatusFlags::SERVER_STATUS_LAST_ROW_SENT) > 0) { |
||
| 116 | $this->close()->then(null, function (\Throwable $e) { |
||
| 117 | $this->driver->emit('error', $e); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 118 | }); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 |