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
|
|
|
* Prepare command. |
14
|
|
|
* @internal |
15
|
|
|
*/ |
16
|
|
|
class PrepareCommand 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 \Plasma\DriverInterface |
31
|
|
|
*/ |
32
|
|
|
protected $driver; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $query; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $rewrittenQuery; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $rewrittenParams; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var \Plasma\Drivers\MySQL\Messages\PrepareStatementOkMessage |
51
|
|
|
*/ |
52
|
|
|
protected $okResponse; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var \Plasma\ColumnDefinitionInterface[] |
56
|
|
|
*/ |
57
|
|
|
protected $params = array(); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var \Plasma\ColumnDefinitionInterface[] |
61
|
|
|
*/ |
62
|
|
|
protected $fields = array(); |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var \Plasma\StatementInterface|null |
66
|
|
|
*/ |
67
|
|
|
protected $resolveValue; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Constructor. |
71
|
|
|
* @param \Plasma\ClientInterface $client |
72
|
|
|
* @param \Plasma\DriverInterface $driver |
73
|
|
|
* @param string $query |
74
|
|
|
*/ |
75
|
|
|
function __construct(\Plasma\ClientInterface $client, \Plasma\DriverInterface $driver, string $query) { |
76
|
|
|
parent::__construct(); |
77
|
|
|
|
78
|
|
|
$this->client = $client; |
79
|
|
|
$this->driver = $driver; |
80
|
|
|
$this->query = $query; |
81
|
|
|
|
82
|
|
|
[ $this->rewrittenQuery, $this->rewrittenParams ] = \Plasma\Utility::parseParameters($this->query, '?'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get the encoded message for writing to the database connection. |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
function getEncodedMessage(): string { |
90
|
|
|
return \chr(static::COMMAND_ID).$this->rewrittenQuery; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Sends the next received value into the command. |
95
|
|
|
* @param mixed $value |
96
|
|
|
* @return void |
97
|
|
|
* @throws \Plasma\Drivers\MySQL\Messages\ParseException |
98
|
|
|
*/ |
99
|
|
|
function onNext($value): void { |
100
|
|
|
if($value instanceof \Plasma\Drivers\MySQL\Messages\PrepareStatementOkMessage) { |
101
|
|
|
$this->okResponse = $value; |
102
|
|
|
} elseif($value instanceof \Plasma\Drivers\MySQL\ProtocolOnNextCaller) { |
103
|
|
|
$parsed = $this->handleQueryOnNextCallerColumns($value); |
104
|
|
|
|
105
|
|
|
if($this->okResponse->numParams >= \count($this->params)) { |
106
|
|
|
$this->params[] = $parsed; |
107
|
|
|
} elseif($this->okResponse->numColumns >= \count($this->fields)) { |
108
|
|
|
$this->fields[$parsed->getName()] = $parsed; |
109
|
|
|
} else { |
110
|
|
|
throw new \Plasma\Drivers\MySQL\Messages\ParseException('Command received more column definition packets than defined'); |
111
|
|
|
} |
112
|
|
|
} elseif( |
|
|
|
|
113
|
|
|
($value instanceof \Plasma\Drivers\MySQL\Messages\EOFMessage || $value instanceof \Plasma\Drivers\MySQL\Messages\OkResponseMessage) |
|
|
|
|
114
|
|
|
&& $this->okResponse->numParams <= \count($this->params) && $this->okResponse->numColumns <= \count($this->fields) |
115
|
|
|
) { |
116
|
|
|
$this->finished = true; |
117
|
|
|
|
118
|
|
|
$id = $this->okResponse->statementID; |
119
|
|
|
$queryr = $this->rewrittenQuery; |
120
|
|
|
$paramsr = $this->rewrittenParams; |
121
|
|
|
|
122
|
|
|
$this->resolveValue = new \Plasma\Drivers\MySQL\Statement($this->client, $this->driver, $id, $this->query, $queryr, $paramsr, $this->params, $this->columns); |
|
|
|
|
123
|
|
|
$this->deferred->resolve($this->resolveValue); |
124
|
|
|
} else { |
125
|
|
|
throw new \Plasma\Drivers\MySQL\Messages\ParseException('Command received value of type ' |
126
|
|
|
.(\is_object($value) ? \get_class($value) : \gettype($value)).' it can not handle'); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Whether the sequence ID should be resetted. |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
function resetSequence(): bool { |
135
|
|
|
return true; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|