From 34a730dfd8189482f163599f3c96ea17c33ecb5b Mon Sep 17 00:00:00 2001 From: Ceeram Date: Mon, 2 Apr 2012 18:46:30 +0200 Subject: [PATCH] ensure buildJoinStatement() does not add schemaname when table value is a subquery, fixes #2709 --- lib/Cake/Model/Datasource/DboSource.php | 3 +- .../Case/Model/Datasource/DboSourceTest.php | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Model/Datasource/DboSource.php b/lib/Cake/Model/Datasource/DboSource.php index d6eef1632..992dd34a0 100644 --- a/lib/Cake/Model/Datasource/DboSource.php +++ b/lib/Cake/Model/Datasource/DboSource.php @@ -1651,7 +1651,8 @@ class DboSource extends DataSource { $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); } diff --git a/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php b/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php index 0df2ae367..2ce015461 100644 --- a/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php @@ -836,4 +836,42 @@ class DboSourceTest extends CakeTestCase { $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); + } }