Fixing calculated field parsing for DboSource::fields()

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4686 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2007-03-27 01:34:59 +00:00
parent 804e21e869
commit f2c7c5a677
3 changed files with 78 additions and 68 deletions

View file

@ -1278,6 +1278,9 @@ class DboSource extends DataSource {
$offset = $length; $offset = $length;
} }
} }
if (empty($results) && !empty($buffer)) {
$results[] = $buffer;
}
if (!empty($results)) { if (!empty($results)) {
$fields = array_map('trim', $results); $fields = array_map('trim', $results);
@ -1298,7 +1301,7 @@ class DboSource extends DataSource {
} }
$count = count($fields); $count = count($fields);
if ($count >= 1 && $fields[0] != '*') { if ($count >= 1 && !in_array($fields[0], array('*', 'COUNT(*)'))) {
for($i = 0; $i < $count; $i++) { for($i = 0; $i < $count; $i++) {
if (!preg_match('/^.+\\(.*\\)/', $fields[$i])) { if (!preg_match('/^.+\\(.*\\)/', $fields[$i])) {
$prepend = ''; $prepend = '';

View file

@ -243,8 +243,8 @@ class Level extends Model {
'Group'=> array( 'Group'=> array(
'className' => 'Group' 'className' => 'Group'
), ),
'User' => array( 'User2' => array(
'className' => 'User' 'className' => 'User2'
) )
); );
@ -271,11 +271,11 @@ class Group extends Model {
); );
var $hasMany = array( var $hasMany = array(
'Category'=> array( 'Category2'=> array(
'className' => 'Category' 'className' => 'Category2'
), ),
'User'=> array( 'User2'=> array(
'className' => 'User' 'className' => 'User2'
) )
); );
@ -291,8 +291,8 @@ class Group extends Model {
} }
} }
class User extends Model { class User2 extends Model {
var $name = 'User'; var $name = 'User2';
var $table = 'user'; var $table = 'user';
var $useTable = false; var $useTable = false;
@ -306,8 +306,8 @@ class User extends Model {
); );
var $hasMany = array( var $hasMany = array(
'Article' => array( 'Article2' => array(
'className' => 'Article' 'className' => 'Article2'
), ),
); );
@ -324,8 +324,8 @@ class User extends Model {
} }
} }
class Category extends Model { class Category2 extends Model {
var $name = 'Category'; var $name = 'Category2';
var $table = 'category'; var $table = 'category';
var $useTable = false; var $useTable = false;
@ -335,19 +335,19 @@ class Category extends Model {
'foreignKey' => 'group_id' 'foreignKey' => 'group_id'
), ),
'ParentCat' => array( 'ParentCat' => array(
'className' => 'Category', 'className' => 'Category2',
'foreignKey' => 'parent_id' 'foreignKey' => 'parent_id'
) )
); );
var $hasMany = array( var $hasMany = array(
'ChildCat' => array( 'ChildCat' => array(
'className' => 'Category', 'className' => 'Category2',
'foreignKey' => 'parent_id' 'foreignKey' => 'parent_id'
), ),
'Article' => array( 'Article2' => array(
'className' => 'Article', 'className' => 'Article2',
'order'=>'Article.published_date DESC', 'order'=>'Article2.published_date DESC',
'foreignKey' => 'category_id', 'foreignKey' => 'category_id',
'limit'=>'3') 'limit'=>'3')
); );
@ -367,25 +367,25 @@ class Category extends Model {
} }
} }
class Article extends Model { class Article2 extends Model {
var $name = 'Article'; var $name = 'Article2';
var $table = 'article'; var $table = 'article';
var $useTable = false; var $useTable = false;
var $belongsTo = array( var $belongsTo = array(
'Category' => array( 'Category2' => array(
'className' => 'Category' 'className' => 'Category2'
), ),
'User' => array( 'User2' => array(
'className' => 'User' 'className' => 'User2'
) )
); );
var $hasOne = array( var $hasOne = array(
'Featured' => array( 'Featured' => array(
'className' => 'Featured' 'className' => 'Featured'
) )
); );
function loadInfo() { function loadInfo() {
if (!isset($this->_tableInfo)) { if (!isset($this->_tableInfo)) {
@ -422,11 +422,11 @@ class Featured extends Model {
var $useTable = false; var $useTable = false;
var $belongsTo = array( var $belongsTo = array(
'Article' => array( 'Article2' => array(
'className' => 'Article' 'className' => 'Article2'
), ),
'Category' => array( 'Category2' => array(
'Artiucle' => 'Category' 'Artiucle' => 'Category2'
) )
); );
@ -486,25 +486,25 @@ class DboSourceTest extends UnitTestCase {
} }
function testGenerateAssociationQuerySelfJoin() { function testGenerateAssociationQuerySelfJoin() {
$this->model = new Article(); $this->model = new Article2();
$this->_buildRelatedModels($this->model); $this->_buildRelatedModels($this->model);
$this->_buildRelatedModels($this->model->Category); $this->_buildRelatedModels($this->model->Category2);
$this->model->Category->ChildCat = new Category(); $this->model->Category2->ChildCat = new Category2();
$this->model->Category->ParentCat = new Category(); $this->model->Category2->ParentCat = new Category2();
$queryData = array(); $queryData = array();
foreach($this->model->Category->__associations as $type) { foreach($this->model->Category2->__associations as $type) {
foreach($this->model->Category->{$type} as $assoc => $assocData) { foreach($this->model->Category2->{$type} as $assoc => $assocData) {
$linkModel =& $this->model->Category->{$assoc}; $linkModel =& $this->model->Category2->{$assoc};
$external = isset($assocData['external']); $external = isset($assocData['external']);
if ($this->model->Category->name == $linkModel->name && $type != 'hasAndBelongsToMany' && $type != 'hasMany') { if ($this->model->Category2->name == $linkModel->name && $type != 'hasAndBelongsToMany' && $type != 'hasMany') {
$result = $this->db->generateSelfAssociationQuery($this->model->Category, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null); $result = $this->db->generateSelfAssociationQuery($this->model->Category2, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null);
$this->assertTrue($result); $this->assertTrue($result);
} else { } else {
if ($this->model->Category->useDbConfig == $linkModel->useDbConfig) { if ($this->model->Category2->useDbConfig == $linkModel->useDbConfig) {
$result = $this->db->generateAssociationQuery($this->model->Category, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null); $result = $this->db->generateAssociationQuery($this->model->Category2, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null);
$this->assertTrue($result); $this->assertTrue($result);
} }
} }
@ -512,7 +512,7 @@ class DboSourceTest extends UnitTestCase {
} }
$query = $this->db->generateAssociationQuery($model, $null, null, null, null, $queryData, false, $null); $query = $this->db->generateAssociationQuery($model, $null, null, null, null, $queryData, false, $null);
$this->assertPattern('/^SELECT\s+(.+)FROM(.+)LEFT\s+JOIN\s+`category`\s+AS\s+`ParentCat`\s+ON\s+`Category`\.`parent_id`\s+=\s+`ParentCat`\.`id`\s+WHERE/', $query); $this->assertPattern('/^SELECT\s+(.+)FROM(.+)LEFT\s+JOIN\s+`category`\s+AS\s+`ParentCat`\s+ON\s+`Category2`\.`parent_id`\s+=\s+`ParentCat`\.`id`\s+WHERE/', $query);
$this->model = new TestModel4(); $this->model = new TestModel4();
$this->model->loadInfo(); $this->model->loadInfo();
@ -832,7 +832,7 @@ class DboSourceTest extends UnitTestCase {
$result = $this->db->conditions('Sportstaette.sportstaette LIKE "%ru%" AND Sportstaette.sportstaettenart_id = 2'); $result = $this->db->conditions('Sportstaette.sportstaette LIKE "%ru%" AND Sportstaette.sportstaettenart_id = 2');
$expected = ' WHERE `Sportstaette`.`sportstaette` LIKE "%ru%" AND `Sportstaette`.`sportstaettenart_id` = 2'; $expected = ' WHERE `Sportstaette`.`sportstaette` LIKE "%ru%" AND `Sportstaette`.`sportstaettenart_id` = 2';
//$this->assertPattern('/\s*WHERE\s+`Sportstaette`\.`sportstaette`\s+LIKE\s+"%ru%"\s+AND\s+`Sports/', $result); $this->assertPattern('/\s*WHERE\s+`Sportstaette`\.`sportstaette`\s+LIKE\s+"%ru%"\s+AND\s+`Sports/', $result);
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->conditions('Sportstaette.sportstaettenart_id = 2 AND Sportstaette.sportstaette LIKE "%ru%"'); $result = $this->db->conditions('Sportstaette.sportstaettenart_id = 2 AND Sportstaette.sportstaette LIKE "%ru%"');
@ -990,11 +990,15 @@ class DboSourceTest extends UnitTestCase {
$result = $this->db->fields($this->model, null, array(), false); $result = $this->db->fields($this->model, null, array(), false);
$expected = array('id', 'client_id', 'name', 'login', 'passwd', 'addr_1', 'addr_2', 'zip_code', 'city', 'country', 'phone', 'fax', 'url', 'email', 'comments', 'last_login', 'created', 'updated'); $expected = array('id', 'client_id', 'name', 'login', 'passwd', 'addr_1', 'addr_2', 'zip_code', 'city', 'country', 'phone', 'fax', 'url', 'email', 'comments', 'last_login', 'created', 'updated');
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->fields($this->model, null, 'COUNT(*)');
$expected = array('COUNT(*)');
$this->assertEqual($result, $expected);
} }
function testMergeAssociations() { function testMergeAssociations() {
$data = array( $data = array(
'Article' => array( 'Article2' => array(
'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31' 'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
) )
); );
@ -1006,7 +1010,7 @@ class DboSourceTest extends UnitTestCase {
) )
); );
$expected = array( $expected = array(
'Article' => array( 'Article2' => array(
'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31' 'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
), ),
'Topic' => array( 'Topic' => array(
@ -1017,30 +1021,30 @@ class DboSourceTest extends UnitTestCase {
$this->assertEqual($data, $expected); $this->assertEqual($data, $expected);
$data = array( $data = array(
'Article' => array( 'Article2' => array(
'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31' 'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
) )
); );
$merge = array( $merge = array(
'User' => array ( 'User2' => array (
array( array(
'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31' 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
) )
) )
); );
$expected = array( $expected = array(
'Article' => array( 'Article2' => array(
'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31' 'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
), ),
'User' => array( 'User2' => array(
'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31' 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
) )
); );
$this->db->__mergeAssociation($data, $merge, 'User', 'belongsTo'); $this->db->__mergeAssociation($data, $merge, 'User2', 'belongsTo');
$this->assertEqual($data, $expected); $this->assertEqual($data, $expected);
$data = array( $data = array(
'Article' => array( 'Article2' => array(
'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31' 'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
) )
); );
@ -1050,7 +1054,7 @@ class DboSourceTest extends UnitTestCase {
) )
); );
$expected = array( $expected = array(
'Article' => array( 'Article2' => array(
'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31' 'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
), ),
'Comment' => array() 'Comment' => array()
@ -1101,7 +1105,7 @@ class DboSourceTest extends UnitTestCase {
'Comment' => array( 'Comment' => array(
'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31' 'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
), ),
'User' => array( 'User2' => array(
'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31' 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
) )
), ),
@ -1109,7 +1113,7 @@ class DboSourceTest extends UnitTestCase {
'Comment' => array( 'Comment' => array(
'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31' 'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
), ),
'User' => array( 'User2' => array(
'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31' 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
) )
) )
@ -1121,13 +1125,13 @@ class DboSourceTest extends UnitTestCase {
'Comment' => array( 'Comment' => array(
array( array(
'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31', 'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31',
'User' => array( 'User2' => array(
'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31' 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
) )
), ),
array( array(
'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31', 'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31',
'User' => array( 'User2' => array(
'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31' 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
) )
) )
@ -1146,7 +1150,7 @@ class DboSourceTest extends UnitTestCase {
'Comment' => array( 'Comment' => array(
'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31' 'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
), ),
'User' => array( 'User2' => array(
'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31' 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
), ),
'Tag' => array( 'Tag' => array(
@ -1158,7 +1162,7 @@ class DboSourceTest extends UnitTestCase {
'Comment' => array( 'Comment' => array(
'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31' 'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
), ),
'User' => array( 'User2' => array(
'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31' 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
), ),
'Tag' => array() 'Tag' => array()
@ -1171,7 +1175,7 @@ class DboSourceTest extends UnitTestCase {
'Comment' => array( 'Comment' => array(
array( array(
'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31', 'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31',
'User' => array( 'User2' => array(
'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31' 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
), ),
'Tag' => array( 'Tag' => array(
@ -1181,7 +1185,7 @@ class DboSourceTest extends UnitTestCase {
), ),
array( array(
'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31', 'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31',
'User' => array( 'User2' => array(
'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31' 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
), ),
'Tag' => array() 'Tag' => array()

View file

@ -271,6 +271,9 @@ class ModelTest extends CakeTestCase {
$result = $this->model->field('COUNT(*) AS count', true); $result = $this->model->field('COUNT(*) AS count', true);
$this->assertEqual($result, 4); $this->assertEqual($result, 4);
$result = $this->model->field('COUNT(*)', true);
$this->assertEqual($result, 4);
} }
function testBindUnbind() { function testBindUnbind() {