|
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
|
1 |
|
function __construct(\Plasma\Drivers\MySQL\Driver $driver, \Plasma\Drivers\MySQL\Statement $statement) { |
|
37
|
1 |
|
$this->driver = $driver; |
|
38
|
1 |
|
$this->statement = $statement; |
|
39
|
1 |
|
} |
|
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
|
1 |
|
function close(): \React\Promise\PromiseInterface { |
|
70
|
1 |
|
if($this->closed) { |
|
71
|
|
|
return \React\Promise\resolve(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
$this->closed = true; |
|
75
|
|
|
|
|
76
|
1 |
|
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
|
1 |
|
function fetch(int $amount = 1): \React\Promise\PromiseInterface { |
|
87
|
1 |
|
if($this->closed) { |
|
88
|
|
|
return \React\Promise\resolve(false); |
|
89
|
1 |
|
} elseif($this->statement->isClosed()) { |
|
90
|
|
|
throw new \Plasma\Exception('Underlying statement has been closed'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
$fetch = new \Plasma\Drivers\MySQL\Commands\FetchCommand( |
|
94
|
1 |
|
$this->driver, |
|
95
|
1 |
|
$this, |
|
96
|
1 |
|
$this->statement->getID(), |
|
97
|
1 |
|
$this->statement->getQuery(), |
|
98
|
1 |
|
$this->statement->getParams(), |
|
99
|
1 |
|
array(), |
|
100
|
1 |
|
$this->statement->getColumns(), |
|
101
|
1 |
|
$amount |
|
102
|
|
|
); |
|
103
|
1 |
|
$this->driver->executeCommand($fetch); |
|
104
|
|
|
|
|
105
|
1 |
|
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
|
1 |
|
function processOkMessage($message): void { |
|
115
|
1 |
|
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
|
1 |
|
}); |
|
119
|
|
|
} |
|
120
|
1 |
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|