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 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 int |
||
66 | */ |
||
67 | protected $fieldsCount; |
||
68 | |||
69 | /** |
||
70 | * @var \Plasma\StatementInterface|null |
||
71 | */ |
||
72 | protected $resolveValue; |
||
73 | |||
74 | /** |
||
75 | * Constructor. |
||
76 | * @param \Plasma\ClientInterface $client |
||
77 | * @param \Plasma\DriverInterface $driver |
||
78 | * @param string $query |
||
79 | */ |
||
80 | function __construct(\Plasma\ClientInterface $client, \Plasma\DriverInterface $driver, string $query) { |
||
81 | parent::__construct($driver); |
||
82 | |||
83 | $this->client = $client; |
||
84 | $this->driver = $driver; |
||
85 | $this->query = $query; |
||
86 | |||
87 | [ 'query' => $this->rewrittenQuery, 'parameters' => $this->rewrittenParams ] = \Plasma\Utility::parseParameters($this->query, '?'); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Get the encoded message for writing to the database connection. |
||
92 | * @return string |
||
93 | */ |
||
94 | function getEncodedMessage(): string { |
||
95 | return \chr(static::COMMAND_ID).$this->rewrittenQuery; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Sends the next received value into the command. |
||
100 | * @param mixed $value |
||
101 | * @return void |
||
102 | * @throws \Plasma\Drivers\MySQL\Messages\ParseException |
||
103 | */ |
||
104 | function onNext($value): void { |
||
105 | if($value instanceof \Plasma\Drivers\MySQL\Messages\PrepareStatementOkMessage) { |
||
106 | $this->okResponse = $value; |
||
107 | $this->fieldsCount = $this->okResponse->numColumns; |
||
108 | } elseif($value instanceof \Plasma\Drivers\MySQL\ProtocolOnNextCaller) { |
||
109 | $buffer = $value->getBuffer(); |
||
110 | $parser = $value->getParser(); |
||
111 | |||
112 | $parsed = $this->handleQueryOnNextCallerColumns($buffer, $parser); |
||
113 | |||
114 | if($this->paramsDone) { |
||
115 | $this->fields[$parsed->getName()] = $parsed; |
||
116 | } else { |
||
117 | $this->params[] = $parsed; |
||
118 | } |
||
119 | } elseif( |
||
1 ignored issue
–
show
Coding Style
introduced
by
![]() |
|||
120 | $value instanceof \Plasma\Drivers\MySQL\Messages\EOFMessage || $value instanceof \Plasma\Drivers\MySQL\Messages\OkResponseMessage |
||
121 | ) { |
||
122 | if(!$this->paramsDone) { |
||
123 | $this->paramsDone = true; |
||
124 | return; |
||
125 | } |
||
126 | |||
127 | $this->finished = true; |
||
128 | |||
129 | $id = $this->okResponse->statementID; |
||
130 | $queryr = $this->rewrittenQuery; |
||
131 | $paramsr = $this->rewrittenParams; |
||
132 | |||
133 | $this->resolveValue = new \Plasma\Drivers\MySQL\Statement($this->client, $this->driver, $id, $this->query, $queryr, $paramsr, $this->params, $this->fields); |
||
134 | $this->deferred->resolve($this->resolveValue); |
||
135 | } else { |
||
136 | throw new \Plasma\Drivers\MySQL\Messages\ParseException('Command received value of type ' |
||
137 | .(\is_object($value) ? \get_class($value) : \gettype($value)).' it can not handle'); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Whether the sequence ID should be resetted. |
||
143 | * @return bool |
||
144 | */ |
||
145 | function resetSequence(): bool { |
||
146 | return true; |
||
147 | } |
||
148 | } |
||
149 |