1 | <?php |
||
2 | /** |
||
3 | * Plasma Driver MySQL component |
||
4 | * Copyright 2018 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 | * Query command. |
||
14 | * @internal |
||
15 | */ |
||
16 | class QueryCommand extends PromiseCommand { |
||
17 | /** |
||
18 | * The identifier for this command. |
||
19 | * @var int |
||
20 | * @source |
||
21 | */ |
||
22 | const COMMAND_ID = 0x03; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $query; |
||
28 | |||
29 | /** |
||
30 | * @var \Plasma\ColumnDefinitionInterface[] |
||
31 | */ |
||
32 | protected $fields = array(); |
||
33 | |||
34 | /** |
||
35 | * @var int|null |
||
36 | */ |
||
37 | protected $fieldsCount; |
||
38 | |||
39 | /** |
||
40 | * @var \Plasma\StreamQueryResult|\Plasma\QueryResult|null |
||
41 | */ |
||
42 | protected $resolveValue; |
||
43 | |||
44 | /** |
||
45 | * Constructor. |
||
46 | * @param \Plasma\DriverInterface $driver |
||
47 | * @param string $query |
||
48 | */ |
||
49 | function __construct(\Plasma\DriverInterface $driver, string $query) { |
||
50 | parent::__construct($driver); |
||
51 | |||
52 | $this->driver = $driver; |
||
53 | $this->query = $query; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Get the encoded message for writing to the database connection. |
||
58 | * @return string |
||
59 | */ |
||
60 | function getEncodedMessage(): string { |
||
61 | return \chr(static::COMMAND_ID).$this->query; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Sends the next received value into the command. |
||
66 | * @param mixed $value |
||
67 | * @return void |
||
68 | */ |
||
69 | function onNext($value): void { |
||
70 | if($value instanceof \Plasma\Drivers\MySQL\ProtocolOnNextCaller) { |
||
71 | $this->handleQueryOnNextCaller($value); |
||
72 | } elseif($value instanceof \Plasma\Drivers\MySQL\Messages\OkResponseMessage || $value instanceof \Plasma\Drivers\MySQL\Messages\EOFMessage) { |
||
73 | if($this->resolveValue !== null) { |
||
74 | $value->getParser()->markCommandAsFinished($this); |
||
75 | } elseif($this->fieldsCount == 0 && $value instanceof \Plasma\Drivers\MySQL\Messages\OkResponseMessage) { // Matching 0 and null |
||
76 | $this->resolveValue = new \Plasma\QueryResult($value->affectedRows, $value->warningsCount, $value->lastInsertedID); |
||
77 | $value->getParser()->markCommandAsFinished($this); |
||
78 | } else { |
||
79 | $this->createResolve(); |
||
80 | } |
||
81 | } |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Handles query commands on next caller. |
||
86 | * @param \Plasma\Drivers\MySQL\ProtocolOnNextCaller $value |
||
87 | * @return void |
||
88 | */ |
||
89 | function handleQueryOnNextCaller(\Plasma\Drivers\MySQL\ProtocolOnNextCaller $value): void { |
||
90 | $buffer = $value->getBuffer(); |
||
91 | $parser = $value->getParser(); |
||
92 | |||
93 | if($this->resolveValue !== null) { |
||
94 | $row = $this->parseResultsetRow($buffer); |
||
95 | var_dump($row); |
||
0 ignored issues
–
show
Security
Debugging Code
introduced
by
![]() |
|||
96 | $this->emit('data', array($row)); |
||
97 | } else { |
||
98 | $field = $this->handleQueryOnNextCallerColumns($buffer, $parser); |
||
99 | if($field) { |
||
100 | $this->fields[$field->getName()] = $field; |
||
101 | } |
||
102 | } |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Creates the resolve value and resolves the promise. |
||
107 | * @return void |
||
108 | */ |
||
109 | function createResolve(): void { |
||
110 | $this->resolveValue = new \Plasma\StreamQueryResult($this->driver, $this, 0, 0, $this->fields, null); |
||
111 | $this->deferred->resolve($this->resolveValue); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Whether the sequence ID should be resetted. |
||
116 | * @return bool |
||
117 | */ |
||
118 | function resetSequence(): bool { |
||
119 | return true; |
||
120 | } |
||
121 | } |
||
122 |