Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions res/sqlite-worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,21 @@
}

$rows = array();
while (($row = $result->fetchArray(\SQLITE3_ASSOC)) !== false) {
// base64-encode any string that is not valid UTF-8 without control characters (BLOB)
foreach ($row as &$value) {
if (\is_string($value) && \preg_match('/[\x00-\x08\x11\x12\x14-\x1f\x7f]/u', $value) !== 0) {
$value = ['base64' => \base64_encode($value)];
} elseif (\is_float($value)) {
$value = ['float' => $value];
if ($result->numColumns() !== 0) {
// Fetch all rows only if this result set has any columns.
// INSERT/UPDATE/DELETE etc. do not return any columns, trying
// to fetch the results here will issue the same query again.
while (($row = $result->fetchArray(\SQLITE3_ASSOC)) !== false) {
// base64-encode any string that is not valid UTF-8 without control characters (BLOB)
foreach ($row as &$value) {
if (\is_string($value) && \preg_match('/[\x00-\x08\x11\x12\x14-\x1f\x7f]/u', $value) !== 0) {
$value = ['base64' => \base64_encode($value)];
} elseif (\is_float($value)) {
$value = ['float' => $value];
}
}
$rows[] = $row;
}
$rows[] = $row;
}
$result->finalize();

Expand Down
30 changes: 30 additions & 0 deletions tests/FunctionalDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,36 @@ public function testExecRejectsWhenAlreadyClosed($flag)
$loop->run();
}

/**
* @dataProvider provideSocketFlags
* @param bool $flag
*/
public function testQueryInsertResolvesWithResultWithLastInsertIdAndRunsUntilQuit($flag)
{
$loop = React\EventLoop\Factory::create();
$factory = new Factory($loop);

$ref = new ReflectionProperty($factory, 'useSocket');
$ref->setAccessible(true);
$ref->setValue($factory, $flag);

$promise = $factory->open(':memory:');

$data = null;
$promise->then(function (DatabaseInterface $db) use (&$data){
$db->exec('CREATE TABLE foo (id INTEGER PRIMARY KEY AUTOINCREMENT, bar STRING)');
$db->query('INSERT INTO foo (bar) VALUES (?)', ['test'])->then(function (Result $result) use (&$data) {
$data = $result->insertId;
});

$db->quit();
});

$loop->run();

$this->assertSame(1, $data);
}

protected function expectCallableNever()
{
$mock = $this->createCallableMock();
Expand Down