Skip to content

Commit

Permalink
Fix issues with sqlite field parsing.
Browse files Browse the repository at this point in the history
The previous attempt would still fail on unions or derived table join
queries. This new approach is a bit slower but more robust.

Refs #3972
  • Loading branch information
markstory committed Nov 13, 2014
1 parent 93a6fd5 commit 6092c16
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
12 changes: 9 additions & 3 deletions lib/Cake/Model/Datasource/Database/Sqlite.php
Expand Up @@ -302,9 +302,15 @@ public function resultSet($results) {
$querystring = $results->queryString;
if (stripos($querystring, 'SELECT') === 0 && stripos($querystring, 'FROM') > 0) {
$selectpart = substr($querystring, 7);
$selects = String::tokenize($selectpart, ',', '(', ')');
$last = count($selects) - 1;
$selects[$last] = trim(substr($selects[$last], 0, stripos($selects[$last], 'FROM')));
$selects = array();
foreach (String::tokenize($selectpart, ',', '(', ')') as $part) {
$fromPos = stripos($part, ' FROM ');
if ($fromPos !== false) {
$selects[] = trim(substr($part, 0, $fromPos));
break;
}
$selects[] = $part;
}
} elseif (strpos($querystring, 'PRAGMA table_info') === 0) {
$selects = array('cid', 'name', 'type', 'notnull', 'dflt_value', 'pk');
} elseif (strpos($querystring, 'PRAGMA index_list') === 0) {
Expand Down
40 changes: 29 additions & 11 deletions lib/Cake/Test/Case/Model/Datasource/Database/SqliteTest.php
Expand Up @@ -527,19 +527,37 @@ public function testFetchRowColumnParsing() {
$this->loadFixtures('User');
$sql = 'SELECT "User"."id", "User"."user", "User"."password", "User"."created", (1 + 1) AS "two" ' .
'FROM "users" AS "User" WHERE ' .
'"User"."id" IN (SELECT MAX("id") FROM "users")';
'"User"."id" IN (SELECT MAX("id") FROM "users") ' .
'OR "User.id" IN (5, 6, 7, 8)';
$result = $this->Dbo->fetchRow($sql);

$this->assertArrayHasKey('User', $result);
$this->assertArrayHasKey('0', $result);
$this->assertCount(2, $result, 'Too many top level keys');
$this->assertCount(4, $result['User'], 'Too many keys');
$this->assertCount(1, $result['0'], 'Too many keys');
$this->assertArrayHasKey('id', $result['User']);
$this->assertArrayHasKey('user', $result['User']);
$this->assertArrayHasKey('password', $result['User']);
$this->assertArrayHasKey('created', $result['User']);
$this->assertArrayHasKey('two', $result['0']);
$expected = array(
'User' => array(
'id' => 4,
'user' => 'garrett',
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
'created' => '2007-03-17 01:22:23'
),
0 => array(
'two' => 2
)
);
$this->assertEquals($expected, $result);

$sql = 'SELECT "User"."id", "User"."user" ' .
'FROM "users" AS "User" WHERE "User"."id" = 4 ' .
'UNION ' .
'SELECT "User"."id", "User"."user" ' .
'FROM "users" AS "User" WHERE "User"."id" = 3';
$result = $this->Dbo->fetchRow($sql);

$expected = array(
'User' => array(
'id' => 3,
'user' => 'larry',
),
);
$this->assertEquals($expected, $result);
}

}

0 comments on commit 6092c16

Please sign in to comment.