Fixing issue where calculated columns were getting quoted incorrectly. Test included. Reformatting code to be 100 characters or less.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8084 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2009-03-09 18:36:32 +00:00
parent ba78e04be9
commit eea46e7cc8
2 changed files with 65 additions and 29 deletions

View file

@ -411,12 +411,23 @@ class DboSource extends DataSource {
$data[$i] = str_replace($this->startQuote . '(', '(', $data[$i]);
$data[$i] = str_replace(')' . $this->startQuote, ')', $data[$i]);
if (strpos($data[$i], ' AS ')) {
$data[$i] = str_replace(' AS ', $this->endQuote . ' AS ' . $this->startQuote, $data[$i]);
if (preg_match('/\s+AS\s+/', $data[$i])) {
if (preg_match('/\w+\s+AS\s+/', $data[$i])) {
$quoted = $this->endQuote . ' AS ' . $this->startQuote;
$data[$i] = str_replace(' AS ', $quoted, $data[$i]);
} else {
$quoted = ' AS ' . $this->startQuote;
$data[$i] = str_replace(' AS ', $quoted, $data[$i]) . $this->endQuote;
}
}
if (!empty($this->endQuote) && $this->endQuote == $this->startQuote) {
if (substr_count($data[$i], $this->endQuote) % 2 == 1) {
$data[$i] = trim($data[$i], $this->endQuote);
if (substr($data[$i], -2) == $this->endQuote . $this->endQuote) {
$data[$i] = substr($data[$i], 0, -1);
} else {
$data[$i] = trim($data[$i], $this->endQuote);
}
}
}
if (strpos($data[$i], '*')) {
@ -1680,7 +1691,7 @@ class DboSource extends DataSource {
strpos($fields[$i], ' ') !== false ||
strpos($fields[$i], '(') !== false
);
$fields[$i] = $this->name(($prefix ? '' : '') . $alias . '.' . $fields[$i]);
$fields[$i] = $this->name(($prefix ? $alias . '.' : '') . $fields[$i]);
} else {
$value = array();
$comma = strpos($fields[$i], ',');

View file

@ -2717,8 +2717,13 @@ class DboSourceTest extends CakeTestCase {
$expected = array('count(*)', '`TestModel`.`name`');
$this->assertEqual($result, $expected);
$result = $this->testDb->fields($this->Model, null, 'field1, field2, field3, count(*), name');
$expected = array('`TestModel`.`field1`', '`TestModel`.`field2`', '`TestModel`.`field3`', 'count(*)', '`TestModel`.`name`');
$result = $this->testDb->fields(
$this->Model, null, 'field1, field2, field3, count(*), name'
);
$expected = array(
'`TestModel`.`field1`', '`TestModel`.`field2`',
'`TestModel`.`field3`', 'count(*)', '`TestModel`.`name`'
);
$this->assertEqual($result, $expected);
$result = $this->testDb->fields($this->Model, null, array('dayofyear(now())'));
@ -2734,8 +2739,24 @@ class DboSourceTest extends CakeTestCase {
$this->assertEqual($result, $expected);
$result = $this->testDb->fields($this->Model, null, array('field AS AnotherName'));
$expected = array('`field` AS `AnotherName`');
$this->assertEqual($result, $expected);
$result = $this->testDb->fields($this->Model, null, array(
'TestModel.field AS AnotherName'
));
$expected = array('`TestModel`.`field` AS `AnotherName`');
$this->assertEqual($result, $expected);
$result = $this->testDb->fields($this->Model, 'Foo', array(
'id', 'title', '(user_count + discussion_count + post_count) AS score'
));
$expected = array(
'`Foo`.`id`',
'`Foo`.`title`',
'(user_count + discussion_count + post_count) AS `score`'
);
$this->assertEqual($result, $expected);
}
/**
* testMergeAssociations method
@ -2744,40 +2765,44 @@ class DboSourceTest extends CakeTestCase {
* @return void
*/
function testMergeAssociations() {
$data = 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'
)
);
$merge = array(
'Topic' => array(
array('id' => '1', 'topic' => 'Topic', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31')
)
);
$data = 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'
));
$merge = array('Topic' => array(array(
'id' => '1', 'topic' => 'Topic', 'created' => '2007-03-17 01:16:23',
'updated' => '2007-03-17 01:18:31'
)));
$expected = 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(
'id' => '1', 'topic' => 'Topic', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
'id' => '1', 'topic' => 'Topic', 'created' => '2007-03-17 01:16:23',
'updated' => '2007-03-17 01:18:31'
)
);
$this->testDb->__mergeAssociation($data, $merge, 'Topic', 'hasOne');
$this->assertEqual($data, $expected);
$data = 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'
)
);
$merge = array(
'User2' => array(
array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31')
)
);
$data = 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'
));
$merge = array('User2' => array(array(
'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
)));
$expected = 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'
),
'User2' => array(
'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'