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 \Plasma\DriverInterface |
26
|
|
|
*/ |
27
|
|
|
protected $driver; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $query; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \Plasma\ColumnDefinitionInterface[] |
36
|
|
|
*/ |
37
|
|
|
protected $fields = array(); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var int|null |
41
|
|
|
*/ |
42
|
|
|
protected $fieldsCount; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var \Plasma\StreamQueryResult|\Plasma\QueryResult|null |
46
|
|
|
*/ |
47
|
|
|
protected $resolveValue; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Constructor. |
51
|
|
|
* @param \Plasma\DriverInterface $driver |
52
|
|
|
* @param string $query |
53
|
|
|
*/ |
54
|
2 |
|
function __construct(\Plasma\DriverInterface $driver, string $query) { |
55
|
2 |
|
parent::__construct(); |
56
|
|
|
|
57
|
2 |
|
$this->driver = $driver; |
58
|
2 |
|
$this->query = $query; |
59
|
2 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the encoded message for writing to the database connection. |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
2 |
|
function getEncodedMessage(): string { |
66
|
2 |
|
return \chr(static::COMMAND_ID).$this->query; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Sends the next received value into the command. |
71
|
|
|
* @param mixed $value |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
2 |
|
function onNext($value): void { |
75
|
2 |
|
if($value instanceof \Plasma\Drivers\MySQL\ProtocolOnNextCaller) { |
76
|
1 |
|
$this->handleQueryOnNextCaller($value); |
77
|
2 |
|
} elseif($value instanceof \Plasma\Drivers\MySQL\Messages\OkResponseMessage || $value instanceof \Plasma\Drivers\MySQL\Messages\EOFMessage) { |
78
|
2 |
|
if($this->resolveValue !== null) { |
79
|
|
|
$value->getParser()->markCommandAsFinished($this); |
80
|
2 |
|
} elseif(empty($this->fields) && $value instanceof \Plasma\Drivers\MySQL\Messages\OkResponseMessage) { |
81
|
2 |
|
$this->resolveValue = new \Plasma\QueryResult($value->affectedRows, $value->warningsCount, $value->lastInsertedID); |
82
|
2 |
|
$value->getParser()->markCommandAsFinished($this); |
83
|
|
|
} else { |
84
|
|
|
$this->createResolve(); |
85
|
|
|
} |
86
|
|
|
} |
87
|
2 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Creates the resolve value and resolves the promise. |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
|
function createResolve(): void { |
94
|
|
|
$this->resolveValue = new \Plasma\StreamQueryResult($this->driver, $this, 0, 0, $this->fields, null); |
95
|
|
|
$this->deferred->resolve($this->resolveValue); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Whether the sequence ID should be resetted. |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
2 |
|
function resetSequence(): bool { |
103
|
2 |
|
return true; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|