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\Commands; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Fetch command. |
14
|
|
|
* @internal |
15
|
|
|
*/ |
16
|
|
|
class FetchCommand extends StatementExecuteCommand { |
17
|
|
|
/** |
18
|
|
|
* The identifier for this command. |
19
|
|
|
* @var int |
20
|
|
|
* @source |
21
|
|
|
*/ |
22
|
|
|
const COMMAND_ID = 0x1C; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \Plasma\Drivers\MySQL\StatementCursor |
26
|
|
|
*/ |
27
|
|
|
protected $cursor; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
protected $amount; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $rows = array(); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Constructor. |
41
|
|
|
* @param \Plasma\DriverInterface $driver |
42
|
|
|
* @param \Plasma\Drivers\MySQL\StatementCursor $cursor |
43
|
|
|
* @param mixed $id |
44
|
|
|
* @param string $query |
45
|
|
|
* @param array $params |
46
|
|
|
* @param \Plasma\ColumnDefinitionInterface[] $paramsDef |
47
|
|
|
* @param \Plasma\ColumnDefinitionInterface[] $fields |
48
|
|
|
* @param int $amount |
49
|
|
|
*/ |
50
|
|
|
function __construct( |
51
|
|
|
\Plasma\DriverInterface $driver, |
52
|
|
|
\Plasma\Drivers\MySQL\StatementCursor $cursor, |
53
|
|
|
$id, |
54
|
|
|
string $query, |
55
|
|
|
array $params, |
56
|
|
|
array $paramsDef, |
57
|
|
|
array $fields, |
58
|
|
|
int $amount |
59
|
|
|
) { |
60
|
|
|
parent::__construct($driver, $id, $query, $params, $paramsDef, 0); |
61
|
|
|
|
62
|
|
|
$this->cursor = $cursor; |
63
|
|
|
$this->fields = $fields; |
64
|
|
|
$this->amount = $amount; |
65
|
|
|
|
66
|
|
|
$this->on('data', function ($row) { |
67
|
|
|
$this->rows[] = $row; |
68
|
|
|
}); |
69
|
|
|
|
70
|
|
|
$this->removeAllListeners('end'); |
71
|
|
|
$this->once('end', function () { |
72
|
|
|
// Let the event loop read the stream buffer before resolving |
73
|
|
|
$this->driver->getLoop()->futureTick(function () { |
74
|
|
|
// Unwrap if we only have one row |
75
|
|
|
$crows = \count($this->rows); |
76
|
|
|
$rows = ($crows === 1 ? \reset($this->rows) : ($crows === 0 ? false : $this->rows)); |
77
|
|
|
|
78
|
|
|
$this->deferred->resolve($rows); |
79
|
|
|
$this->rows = null; |
80
|
|
|
}); |
81
|
|
|
}); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the encoded message for writing to the database connection. |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
function getEncodedMessage(): string { |
89
|
|
|
$packet = \chr(static::COMMAND_ID); |
90
|
|
|
$packet .= \Plasma\BinaryBuffer::writeInt4($this->id); |
91
|
|
|
$packet .= \Plasma\BinaryBuffer::writeInt4($this->amount); |
92
|
|
|
|
93
|
|
|
return $packet; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Sends the next received value into the command. |
98
|
|
|
* @param mixed $value |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
function onNext($value): void { |
102
|
|
|
if($value instanceof \Plasma\Drivers\MySQL\ProtocolOnNextCaller) { |
103
|
|
|
$this->handleQueryOnNextCaller($value); |
104
|
|
|
} elseif($value instanceof \Plasma\Drivers\MySQL\Messages\OkResponseMessage || $value instanceof \Plasma\Drivers\MySQL\Messages\EOFMessage) { |
105
|
|
|
$this->cursor->processOkMessage($value); |
106
|
|
|
$value->getParser()->markCommandAsFinished($this); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|