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
|
|
|
* @codeCoverageIgnore |
46
|
|
|
*/ |
47
|
|
|
function __destruct() { |
48
|
|
|
if(!$this->closed) { |
49
|
|
|
$this->close()->then(null, function () { |
50
|
|
|
// Error during implicit close, close the session |
51
|
|
|
$this->driver->close(); |
52
|
|
|
}); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Whether the cursor has been closed. |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
function isClosed(): bool { |
61
|
|
|
return $this->closed; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Closes the cursor and frees the associated resources on the server. |
66
|
|
|
* Closing a cursor more than once has no effect. |
67
|
|
|
* @return \React\Promise\PromiseInterface |
68
|
|
|
*/ |
69
|
|
|
function close(): \React\Promise\PromiseInterface { |
70
|
|
|
if($this->closed) { |
71
|
|
|
return \React\Promise\resolve(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->closed = true; |
75
|
|
|
|
76
|
|
|
return $this->statement->close(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* 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. |
81
|
|
|
* @param int $amount |
82
|
|
|
* @return \React\Promise\PromiseInterface |
83
|
|
|
* @throws \LogicException Thrown if the driver or DBMS does not support cursors. |
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 $message |
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', array($e)); |
118
|
|
|
}); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|