public static function makeQuery($tableName, RedBeanModelSelectQueryAdapter $selectQueryAdapter, RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter, $offset = null, $count = null, $where = null, $orderBy = null, $groupBy = null) { assert('is_string($tableName) && $tableName != ""'); assert('$offset === null || is_integer($offset) && $offset >= 0'); assert('$count === null || is_integer($count) && $count >= 1'); assert('$where === null || is_string ($where) && $where != ""'); assert('$orderBy === null || is_string ($orderBy) && $orderBy != ""'); assert('$groupBy === null || is_string ($groupBy) && $groupBy != ""'); $quote = DatabaseCompatibilityUtil::getQuote(); $sql = $selectQueryAdapter->getSelect(); $sql .= "from "; //Added ( ) around from tables to ensure precedence over joins. $joinFromPart = $joinTablesAdapter->getJoinFromQueryPart(); if ($joinFromPart !== null) { $sql .= "("; $sql .= "{$quote}{$tableName}{$quote}"; $sql .= ", {$joinFromPart}) "; } else { $sql .= "{$quote}{$tableName}{$quote}"; $sql .= ' '; } $sql .= $joinTablesAdapter->getJoinQueryPart(); $joinWherePart = $joinTablesAdapter->getJoinWhereQueryPart(); if ($where !== null) { $sql .= "where {$where}"; if ($joinWherePart != null) { $sql .= " and {$joinWherePart}"; } } elseif ($joinWherePart != null) { $sql .= " where {$joinWherePart}"; } if ($groupBy !== null) { $sql .= " group by {$groupBy}"; } if ($orderBy !== null) { $sql .= " order by {$orderBy}"; } if ($count !== null) { $sql .= " limit {$count}"; } if ($offset !== null) { $sql .= " offset {$offset}"; } return $sql; }
public function testAddLeftTableAndGetAliasNameWithSpecifiedOnTableAliasName() { $quote = DatabaseCompatibilityUtil::getQuote(); $adapter = new RedBeanModelJoinTablesQueryAdapter('QueryFromModel'); $alias = $adapter->addLeftTableAndGetAliasName('zz', 'joinid'); $this->assertEquals('zz', $alias); $this->assertEquals(0, $adapter->getFromTableJoinCount()); $this->assertEquals(1, $adapter->getLeftTableJoinCount()); $fromPart = $adapter->getJoinFromQueryPart(); $joinPart = $adapter->getJoinQueryPart(); $wherePart = $adapter->getJoinWhereQueryPart(); $compareFromPart = null; $compareJoinPart = "left join {$quote}zz{$quote} "; $compareJoinPart .= "on {$quote}zz{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}joinid{$quote} "; $compareWherePart = null; $this->assertEquals($compareFromPart, $fromPart); $this->assertEquals($compareJoinPart, $joinPart); $this->assertEquals($compareWherePart, $wherePart); //Now add a specified onTableAliasName $alias = $adapter->addLeftTableAndGetAliasName('xyz', 'ajoinid', 'zz'); $this->assertEquals('xyz', $alias); $this->assertEquals(0, $adapter->getFromTableJoinCount()); $this->assertEquals(2, $adapter->getLeftTableJoinCount()); $fromPart = $adapter->getJoinFromQueryPart(); $joinPart = $adapter->getJoinQueryPart(); $wherePart = $adapter->getJoinWhereQueryPart(); $compareFromPart = null; $compareJoinPart = "left join {$quote}zz{$quote} "; $compareJoinPart .= "on {$quote}zz{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}joinid{$quote} "; $compareJoinPart .= "left join {$quote}xyz{$quote} "; $compareJoinPart .= "on {$quote}xyz{$quote}.{$quote}id{$quote} = {$quote}zz{$quote}.{$quote}ajoinid{$quote} "; $compareWherePart = null; $this->assertEquals($compareFromPart, $fromPart); $this->assertEquals($compareJoinPart, $joinPart); $this->assertEquals($compareWherePart, $wherePart); }