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 int $amount |
||
48 | */ |
||
49 | 1 | function __construct(\Plasma\DriverInterface $driver, \Plasma\Drivers\MySQL\StatementCursor $cursor, $id, string $query, array $params, array $paramsDef, int $amount) { |
|
50 | 1 | parent::__construct($driver, $id, $query, $params, $paramsDef, 0); |
|
51 | |||
52 | 1 | $this->cursor = $cursor; |
|
53 | 1 | $this->amount = $amount; |
|
54 | |||
55 | $this->on('data', function ($row) { |
||
56 | $this->rows[] = $row; |
||
57 | 1 | }); |
|
58 | |||
59 | 1 | $this->removeAllListeners('end'); |
|
60 | $this->once('end', function () { |
||
61 | // Let the event loop read the stream buffer before resolving |
||
62 | $this->driver->getLoop()->futureTick(function () { |
||
63 | // Unwrap if we only have one row |
||
64 | $rows = (\count($this->rows) === 1 ? \reset($this->rows) : $this->rows); |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
65 | |||
66 | $this->deferred->resolve($this->rows); |
||
67 | $this->rows = null; |
||
68 | }); |
||
69 | 1 | }); |
|
70 | 1 | } |
|
71 | |||
72 | /** |
||
73 | * Get the encoded message for writing to the database connection. |
||
74 | * @return string |
||
75 | */ |
||
76 | function getEncodedMessage(): string { |
||
77 | $packet = \chr(static::COMMAND_ID); |
||
78 | $packet .= \Plasma\BinaryBuffer::writeInt4($this->id); |
||
79 | $packet .= \Plasma\BinaryBuffer::writeInt4($this->amount); |
||
80 | |||
81 | return $packet; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Sends the next received value into the command. |
||
86 | * @param mixed $value |
||
87 | * @return void |
||
88 | */ |
||
89 | function onNext($value): void { |
||
90 | if($value instanceof \Plasma\Drivers\MySQL\ProtocolOnNextCaller) { |
||
91 | $this->handleQueryOnNextCaller($value); |
||
92 | } elseif($value instanceof \Plasma\Drivers\MySQL\Messages\OkResponseMessage || $value instanceof \Plasma\Drivers\MySQL\Messages\EOFMessage) { |
||
93 | $this->cursor->processOkMessage($value); |
||
94 | $value->getParser()->markCommandAsFinished($this); |
||
95 | } |
||
96 | } |
||
97 | } |
||
98 |