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
|
|
|
* Statement Prepare command. |
14
|
|
|
* @internal |
15
|
|
|
*/ |
16
|
|
|
class StatementPrepareCommand extends PromiseCommand { |
17
|
|
|
/** |
18
|
|
|
* The identifier for this command. |
19
|
|
|
* @var int |
20
|
|
|
* @source |
21
|
|
|
*/ |
22
|
|
|
const COMMAND_ID = 0x16; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \Plasma\ClientInterface |
26
|
|
|
*/ |
27
|
|
|
protected $client; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $query; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $rewrittenQuery; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $rewrittenParams; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var \Plasma\Drivers\MySQL\Messages\PrepareStatementOkMessage |
46
|
|
|
*/ |
47
|
|
|
protected $okResponse; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var \Plasma\ColumnDefinitionInterface[] |
51
|
|
|
*/ |
52
|
|
|
protected $params = array(); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var \Plasma\ColumnDefinitionInterface[] |
56
|
|
|
*/ |
57
|
|
|
protected $fields = array(); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var bool |
61
|
|
|
*/ |
62
|
|
|
protected $paramsDone = false; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var \Plasma\StatementInterface|null |
66
|
|
|
*/ |
67
|
|
|
protected $resolveValue; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var bool |
71
|
|
|
*/ |
72
|
|
|
protected $deprecatedEOF; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Constructor. |
76
|
|
|
* @param \Plasma\ClientInterface $client |
77
|
|
|
* @param \Plasma\DriverInterface $driver |
78
|
|
|
* @param string $query |
79
|
|
|
*/ |
80
|
38 |
|
function __construct(\Plasma\ClientInterface $client, \Plasma\DriverInterface $driver, string $query) { |
81
|
38 |
|
parent::__construct($driver); |
82
|
|
|
|
83
|
38 |
|
$this->client = $client; |
84
|
38 |
|
$this->driver = $driver; |
85
|
38 |
|
$this->query = $query; |
86
|
38 |
|
$this->deprecatedEOF = (($driver->getHandshake()->capability & \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_DEPRECATE_EOF) !== 0); |
87
|
|
|
|
88
|
38 |
|
[ 'query' => $this->rewrittenQuery, 'parameters' => $this->rewrittenParams ] = \Plasma\Utility::parseParameters($this->query, '?'); |
89
|
38 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the encoded message for writing to the database connection. |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
38 |
|
function getEncodedMessage(): string { |
96
|
38 |
|
return \chr(static::COMMAND_ID).$this->rewrittenQuery; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Sends the next received value into the command. |
101
|
|
|
* @param mixed $value |
102
|
|
|
* @return void |
103
|
|
|
* @throws \Plasma\Drivers\MySQL\Messages\ParseException |
104
|
|
|
*/ |
105
|
38 |
|
function onNext($value): void { |
106
|
38 |
|
if($value instanceof \Plasma\Drivers\MySQL\Messages\PrepareStatementOkMessage) { |
107
|
38 |
|
$this->okResponse = $value; |
108
|
38 |
|
$this->fieldsCount = $this->okResponse->numColumns; |
109
|
38 |
|
$this->paramsDone = ($this->okResponse->numParams === 0); |
110
|
|
|
|
111
|
38 |
|
if($this->paramsDone && $this->fieldsCount === 0) { |
112
|
38 |
|
$this->createResolve(); |
113
|
|
|
} |
114
|
38 |
|
} elseif($value instanceof \Plasma\Drivers\MySQL\ProtocolOnNextCaller) { |
115
|
38 |
|
$buffer = $value->getBuffer(); |
116
|
38 |
|
$parser = $value->getParser(); |
117
|
|
|
|
118
|
38 |
|
$parsed = $this->handleQueryOnNextCallerColumns($buffer, $parser); |
119
|
|
|
|
120
|
|
|
if($this->paramsDone) { |
121
|
|
|
$this->fields[$parsed->getName()] = $parsed; |
122
|
|
|
} else { |
123
|
|
|
$this->params[] = $parsed; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if($this->deprecatedEOF) { |
127
|
|
|
if($this->paramsDone && $this->fieldsCount <= \count($this->fields)) { |
128
|
|
|
$this->createResolve(); |
129
|
|
|
} elseif($this->okResponse->numParams <= \count($this->params) && $this->resolveValue === null) { |
130
|
|
|
$this->paramsDone = true; |
131
|
|
|
|
132
|
|
|
if($this->fieldsCount === 0) { |
133
|
|
|
$this->createResolve(); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} elseif( |
138
|
|
|
$value instanceof \Plasma\Drivers\MySQL\Messages\EOFMessage || $value instanceof \Plasma\Drivers\MySQL\Messages\OkResponseMessage |
139
|
|
|
) { |
140
|
|
|
if(!$this->paramsDone) { |
141
|
|
|
$this->paramsDone = true; |
142
|
|
|
|
143
|
|
|
if($this->fieldsCount === 0) { |
144
|
|
|
$this->createResolve(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$this->createResolve(); |
151
|
|
|
} else { |
152
|
|
|
throw new \Plasma\Drivers\MySQL\Messages\ParseException('Command received value of type ' |
153
|
|
|
.(\is_object($value) ? \get_class($value) : \gettype($value)).' it can not handle'); |
154
|
|
|
} |
155
|
38 |
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Whether the sequence ID should be resetted. |
159
|
|
|
* @return bool |
160
|
|
|
*/ |
161
|
38 |
|
function resetSequence(): bool { |
162
|
38 |
|
return true; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Creates the resolve value. |
167
|
|
|
* @return void |
168
|
|
|
*/ |
169
|
|
|
protected function createResolve(): void { |
170
|
|
|
$this->finished = true; |
171
|
|
|
|
172
|
|
|
$id = $this->okResponse->statementID; |
173
|
|
|
$queryr = $this->rewrittenQuery; |
174
|
|
|
$paramsr = $this->rewrittenParams; |
175
|
|
|
|
176
|
|
|
$this->resolveValue = new \Plasma\Drivers\MySQL\Statement($this->client, $this->driver, $id, $this->query, $queryr, $paramsr, $this->params, $this->fields); |
177
|
|
|
$this->deferred->resolve($this->resolveValue); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|