PlasmaPHP /
driver-mysql
| 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 int |
||
| 61 | */ |
||
| 62 | protected $fieldsCount; |
||
| 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($driver); |
||
| 77 | |||
| 78 | $this->client = $client; |
||
| 79 | $this->driver = $driver; |
||
| 80 | $this->query = $query; |
||
| 81 | |||
| 82 | [ 'query' => $this->rewrittenQuery, 'parameters' => $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 | //var_dump($value); ob_flush(); |
||
| 101 | |||
| 102 | if($value instanceof \Plasma\Drivers\MySQL\Messages\PrepareStatementOkMessage) { |
||
| 103 | $this->okResponse = $value; |
||
| 104 | $this->fieldsCount = $this->okResponse->numColumns; |
||
| 105 | } elseif($value instanceof \Plasma\Drivers\MySQL\ProtocolOnNextCaller) { |
||
| 106 | $buffer = $value->getBuffer(); |
||
| 107 | $parser = $value->getParser(); |
||
| 108 | |||
| 109 | $parsed = $this->handleQueryOnNextCallerColumns($buffer, $parser); |
||
| 110 | |||
| 111 | if($this->okResponse->numParams >= \count($this->params)) { |
||
| 112 | $this->params[] = $parsed; |
||
| 113 | } elseif($this->okResponse->numColumns >= \count($this->fields)) { |
||
| 114 | $this->fields[$parsed->getName()] = $parsed; |
||
| 115 | } else { |
||
| 116 | throw new \Plasma\Drivers\MySQL\Messages\ParseException('Command received more column definition packets than defined'); |
||
| 117 | } |
||
| 118 | } elseif( |
||
|
1 ignored issue
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 119 | $value instanceof \Plasma\Drivers\MySQL\Messages\EOFMessage || $value instanceof \Plasma\Drivers\MySQL\Messages\OkResponseMessage |
||
| 120 | ) { |
||
| 121 | $this->finished = true; |
||
| 122 | |||
| 123 | $id = $this->okResponse->statementID; |
||
| 124 | $queryr = $this->rewrittenQuery; |
||
| 125 | $paramsr = $this->rewrittenParams; |
||
| 126 | |||
| 127 | $this->resolveValue = new \Plasma\Drivers\MySQL\Statement($this->client, $this->driver, $id, $this->query, $queryr, $paramsr, $this->params, $this->fields); |
||
| 128 | $this->deferred->resolve($this->resolveValue); |
||
| 129 | } else { |
||
| 130 | throw new \Plasma\Drivers\MySQL\Messages\ParseException('Command received value of type ' |
||
| 131 | .(\is_object($value) ? \get_class($value) : \gettype($value)).' it can not handle'); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Whether the sequence ID should be resetted. |
||
| 137 | * @return bool |
||
| 138 | */ |
||
| 139 | function resetSequence(): bool { |
||
| 140 | return true; |
||
| 141 | } |
||
| 142 | } |
||
| 143 |