Skip to content

Commit

Permalink
Use String::tokenize() to split up fields.
Browse files Browse the repository at this point in the history
It is slightly more intelligent than explode()
Solves basic problems in Sqlite with virtualFields.

Fixes #2163
  • Loading branch information
markstory committed Oct 27, 2011
1 parent 010abd9 commit 43df8d3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/Cake/Model/Datasource/Database/Sqlite.php
Expand Up @@ -18,6 +18,7 @@
*/

App::uses('DboSource', 'Model/Datasource');
App::uses('String', 'Utility');

/**
* DBO implementation for the SQLite3 DBMS.
Expand Down Expand Up @@ -281,7 +282,7 @@ public function resultSet($results) {
$last = strripos($querystring, 'FROM');
if ($last !== false) {
$selectpart = substr($querystring, 7, $last - 8);
$selects = explode(',', $selectpart);
$selects = String::tokenize($selectpart, ',', '(', ')');
}
} elseif (strpos($querystring, 'PRAGMA table_info') === 0) {
$selects = array('cid', 'name', 'type', 'notnull', 'dflt_value', 'pk');
Expand Down
20 changes: 20 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/Database/SqliteTest.php
Expand Up @@ -20,6 +20,8 @@
App::uses('AppModel', 'Model');
App::uses('Sqlite', 'Model/Datasource/Database');

require_once dirname(dirname(dirname(__FILE__))) . DS . 'models.php';

/**
* DboSqliteTestDb class
*
Expand Down Expand Up @@ -88,6 +90,7 @@ class SqliteTest extends CakeTestCase {
*
*/
public function setUp() {
parent::setUp();
Configure::write('Cache.disable', true);
$this->Dbo = ConnectionManager::getDataSource('test');
if (!$this->Dbo instanceof Sqlite) {
Expand All @@ -100,6 +103,7 @@ public function setUp() {
*
*/
public function tearDown() {
parent::tearDown();
Configure::write('Cache.disable', false);
}

Expand Down Expand Up @@ -318,4 +322,20 @@ public function testDescribeWithUuidPrimaryKey() {
$this->assertEqual($result['id'], $expected);
$this->Dbo->query('DROP TABLE ' . $tableName);
}

/**
* Test virtualFields with functions.
*
* @return void
*/
public function testVirtualFieldWithFunction() {
$this->loadFixtures('User');
$User = ClassRegistry::init('User');
$User->virtualFields = array('name' => 'SUBSTR(User.user, 5)');

$result = $User->find('first', array(
'conditions' => array('User.user' => 'garrett')
));
$this->assertEquals('ett', $result['User']['name']);
}
}

0 comments on commit 43df8d3

Please sign in to comment.