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