Skip to content

Commit 2ccca8a

Browse files
authored
Merge pull request #46 from clue-labs/empty
Fix reading empty rows containing only empty string columns
2 parents e48ed96 + 2832812 commit 2ccca8a

File tree

3 files changed

+401
-26
lines changed

3 files changed

+401
-26
lines changed

src/Protocal/Parser.php

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public function parse($data)
184184
$fieldCount = ord($this->read(1));
185185
field:
186186
if ($fieldCount === 0xFF) {
187-
//error packet
187+
// error packet
188188
$u = unpack('v', $this->read(2));
189189
$this->errno = $u[1];
190190
$state = $this->read(6);
@@ -193,7 +193,8 @@ public function parse($data)
193193

194194
$this->nextRequest();
195195
$this->onError();
196-
} elseif ($fieldCount === 0x00) { //OK Packet Empty
196+
} elseif ($fieldCount === 0x00) {
197+
// Empty OK Packet
197198
$this->debug('Ok Packet');
198199

199200
$isAuthenticated = false;
@@ -213,26 +214,39 @@ public function parse($data)
213214

214215
$this->message = $this->read($this->pctSize - $len + $this->length());
215216

216-
if ($isAuthenticated) {
217-
$this->onAuthenticated();
217+
if ($this->rsState === self::RS_STATE_ROW) {
218+
// Empty OK packet during result set => row with only empty strings
219+
$row = array();
220+
foreach ($this->resultFields as $field) {
221+
$row[$field['name']] = '';
222+
}
223+
$this->onResultRow($row);
218224
} else {
219-
$this->onSuccess();
225+
// otherwise this terminates a query without a result set (UPDATE, INSERT etc.)
226+
if ($isAuthenticated) {
227+
$this->onAuthenticated();
228+
} else {
229+
$this->onSuccess();
230+
}
231+
$this->debug(sprintf("AffectedRows: %d, InsertId: %d, WarnCount:%d", $this->affectedRows, $this->insertId, $this->warnCount));
232+
$this->nextRequest();
220233
}
221-
$this->debug(sprintf("AffectedRows: %d, InsertId: %d, WarnCount:%d", $this->affectedRows, $this->insertId, $this->warnCount));
222-
$this->nextRequest();
223-
224-
} elseif ($fieldCount === 0xFE) { //EOF Packet
234+
} elseif ($fieldCount === 0xFE) {
235+
// EOF Packet
225236
$this->debug('EOF Packet');
226237
if ($this->rsState === self::RS_STATE_ROW) {
238+
// finalize this result set (all rows completed)
227239
$this->debug('result done');
228240

229241
$this->nextRequest();
230242
$this->onResultDone();
231243
} else {
232-
++ $this->rsState;
244+
// move to next part of result set (header->field->row)
245+
++$this->rsState;
233246
}
234247

235-
} else { //Data packet
248+
} else {
249+
// Data packet
236250
$this->debug('Data Packet');
237251
$this->prepend(chr($fieldCount));
238252

@@ -266,24 +280,29 @@ public function parse($data)
266280
$field['decimals'] = ord($this->read(1));
267281
//var_dump($field);
268282
$this->resultFields[] = $field;
269-
270283
} elseif ($this->rsState === self::RS_STATE_ROW) {
271284
$this->debug('Row packet of Data packet');
272285
$row = [];
273-
for ($i = 0, $nf = sizeof($this->resultFields); $i < $nf; ++$i) {
274-
$row[$this->resultFields[$i]['name']] = $this->parseEncodedString();
286+
foreach ($this->resultFields as $field) {
287+
$row[$field['name']] = $this->parseEncodedString();
275288
}
276-
$this->resultRows[] = $row;
277-
$command = $this->queue->dequeue();
278-
$command->emit('result', array($row, $command, $command->getConnection()));
279-
$this->queue->unshift($command);
289+
$this->onResultRow($row);
280290
}
281291
}
282292
}
283293
$this->restBuffer($this->pctSize - $len + $this->length());
284294
goto packet;
285295
}
286296

297+
private function onResultRow($row)
298+
{
299+
// $this->debug('row data: ' . json_encode($row));
300+
$this->resultRows[] = $row;
301+
$command = $this->queue->dequeue();
302+
$command->emit('result', array($row, $command, $command->getConnection()));
303+
$this->queue->unshift($command);
304+
}
305+
287306
protected function onError()
288307
{
289308
$command = $this->queue->dequeue();

tests/BaseTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class BaseTestCase extends TestCase
88
{
9-
protected function getConnectionOptions()
9+
protected function getConnectionOptions($debug = false)
1010
{
1111
// can be controlled through ENV or by changing defaults in phpunit.xml
1212
return [
@@ -15,7 +15,7 @@ protected function getConnectionOptions()
1515
'dbname' => getenv('DB_DBNAME'),
1616
'user' => getenv('DB_USER'),
1717
'passwd' => getenv('DB_PASSWD'),
18-
];
18+
] + ($debug ? ['debug' => true] : []);
1919
}
2020

2121
protected function getDataTable()

0 commit comments

Comments
 (0)