Skip to content

Commit

Permalink
ensure buildJoinStatement() does not add schemaname when table value …
Browse files Browse the repository at this point in the history
…is a subquery, fixes #2709
  • Loading branch information
ceeram committed Apr 2, 2012
1 parent 4e67698 commit 34a730d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/Cake/Model/Datasource/DboSource.php
Expand Up @@ -1651,7 +1651,8 @@ public function buildJoinStatement($join) {
$data['conditions'] = trim($this->conditions($data['conditions'], true, false));
}
if (!empty($data['table'])) {
$data['table'] = $this->fullTableName($data['table']);
$schema = !(is_string($data['table']) && strpos($data['table'], '(') === 0);
$data['table'] = $this->fullTableName($data['table'], true, $schema);
}
return $this->renderJoinStatement($data);
}
Expand Down
38 changes: 38 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php
Expand Up @@ -836,4 +836,42 @@ public function testTransactionLogging() {
$log = $db->getLog();
$this->assertEquals($expected, $log['log'][0]);
}

/**
* data provider for testBuildJoinStatement
*
* @return array
*/
public static function joinStatements($schema) {
return array(
array(array(
'type' => 'LEFT',
'alias' => 'PostsTag',
'table' => 'posts_tags',
'conditions' => array('PostsTag.post_id = Post.id')
), 'LEFT JOIN cakephp.posts_tags AS PostsTag ON (PostsTag.post_id = Post.id)'),
array(array(
'type' => 'LEFT',
'alias' => 'Stock',
'table' => '(SELECT Stock.article_id, sum(quantite) quantite FROM stocks AS Stock GROUP BY Stock.article_id)',
'conditions' => 'Stock.article_id = Article.id'
), 'LEFT JOIN (SELECT Stock.article_id, sum(quantite) quantite FROM stocks AS Stock GROUP BY Stock.article_id) AS Stock ON (Stock.article_id = Article.id)')
);
}

/**
* Test buildJoinStatement()
* ensure that schemaName is not added when table value is a subquery
*
* @dataProvider joinStatements
* @return void
*/
public function testBuildJoinStatement($join, $expected) {
$db = $this->getMock('DboTestSource', array('getSchemaName'));
$db->expects($this->any())
->method('getSchemaName')
->will($this->returnValue('cakephp'));
$result = $db->buildJoinStatement($join);
$this->assertEquals($expected, $result);
}
}

0 comments on commit 34a730d

Please sign in to comment.