Skip to content

Commit

Permalink
Fixing issue where calculated columns were getting quoted incorrectly…
Browse files Browse the repository at this point in the history
…. 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
  • Loading branch information
nateabele committed Mar 9, 2009
1 parent ba78e04 commit eea46e7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 29 deletions.
19 changes: 15 additions & 4 deletions cake/libs/model/datasources/dbo_source.php
Expand Up @@ -411,12 +411,23 @@ function name($data) {
$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], '*')) {
Expand Down Expand Up @@ -1680,7 +1691,7 @@ function fields(&$model, $alias = null, $fields = array(), $quote = true) {
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], ',');
Expand Down
75 changes: 50 additions & 25 deletions cake/tests/cases/libs/model/datasources/dbo_source.test.php
Expand Up @@ -2717,8 +2717,13 @@ function testFieldParsing() {
$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())'));
Expand All @@ -2734,8 +2739,24 @@ function testFieldParsing() {
$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
Expand All @@ -2744,40 +2765,44 @@ function testFieldParsing() {
* @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'
Expand Down

0 comments on commit eea46e7

Please sign in to comment.