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 \Plasma\StreamQueryResult|\Plasma\QueryResult|null |
36
|
|
|
*/ |
37
|
|
|
protected $resolveValue; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
protected $deprecatedEOF; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructor. |
46
|
|
|
* @param \Plasma\DriverInterface $driver |
47
|
|
|
* @param string $query |
48
|
|
|
*/ |
49
|
6 |
|
function __construct(\Plasma\DriverInterface $driver, string $query) { |
50
|
6 |
|
parent::__construct($driver); |
51
|
|
|
|
52
|
6 |
|
$this->driver = $driver; |
53
|
6 |
|
$this->query = $query; |
54
|
6 |
|
$this->deprecatedEOF = (($driver->getHandshake()->capability & \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_DEPRECATE_EOF) !== 0); |
55
|
6 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the encoded message for writing to the database connection. |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
4 |
|
function getEncodedMessage(): string { |
62
|
4 |
|
return \chr(static::COMMAND_ID).$this->query; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Sends the next received value into the command. |
67
|
|
|
* @param mixed $value |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
6 |
|
function onNext($value): void { |
71
|
6 |
|
if($value instanceof \Plasma\Drivers\MySQL\ProtocolOnNextCaller) { |
72
|
5 |
|
$this->handleQueryOnNextCaller($value); |
73
|
6 |
|
} elseif($value instanceof \Plasma\Drivers\MySQL\Messages\OkResponseMessage || $value instanceof \Plasma\Drivers\MySQL\Messages\EOFMessage) { |
74
|
6 |
|
if($this->resolveValue !== null) { |
75
|
5 |
|
$value->getParser()->markCommandAsFinished($this); |
76
|
3 |
|
} elseif($this->fieldsCount == 0 && $value instanceof \Plasma\Drivers\MySQL\Messages\OkResponseMessage) { // Matching 0 and null |
77
|
3 |
|
$this->resolveValue = new \Plasma\QueryResult($value->affectedRows, $value->warningsCount, $value->lastInsertedID); |
78
|
3 |
|
$value->getParser()->markCommandAsFinished($this); |
79
|
|
|
} else { |
80
|
|
|
$this->createResolve(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
6 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Handles query commands on next caller. |
87
|
|
|
* @param \Plasma\Drivers\MySQL\ProtocolOnNextCaller $value |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
5 |
|
function handleQueryOnNextCaller(\Plasma\Drivers\MySQL\ProtocolOnNextCaller $value): void { |
91
|
5 |
|
$buffer = $value->getBuffer(); |
92
|
5 |
|
$parser = $value->getParser(); |
93
|
|
|
|
94
|
5 |
|
if($this->resolveValue !== null) { |
95
|
5 |
|
$row = $this->parseResultsetRow($buffer); |
96
|
5 |
|
$this->emit('data', array($row)); |
97
|
|
|
} else { |
98
|
5 |
|
$field = $this->handleQueryOnNextCallerColumns($buffer, $parser); |
99
|
5 |
|
if($field) { |
100
|
5 |
|
$this->fields[$field->getName()] = $field; |
101
|
|
|
} |
102
|
|
|
|
103
|
5 |
|
if($this->deprecatedEOF && $this->fieldsCount <= \count($this->fields)) { |
104
|
5 |
|
$this->createResolve(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
5 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Creates the resolve value and resolves the promise. |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
5 |
|
function createResolve(): void { |
114
|
5 |
|
$this->resolveValue = new \Plasma\StreamQueryResult($this->driver, $this, 0, 0, $this->fields, null); |
115
|
5 |
|
$this->deferred->resolve($this->resolveValue); |
116
|
5 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Whether the sequence ID should be resetted. |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
4 |
|
function resetSequence(): bool { |
123
|
4 |
|
return true; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|