Permalink
Browse files

ensure buildJoinStatement() does not add schemaname when table value …

…is a subquery, fixes #2709
  • Loading branch information...
1 parent 4e67698 commit 34a730dfd8189482f163599f3c96ea17c33ecb5b @ceeram ceeram committed Apr 2, 2012
Showing with 40 additions and 1 deletion.
  1. +2 −1 lib/Cake/Model/Datasource/DboSource.php
  2. +38 −0 lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php
@@ -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);
}
@@ -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.