Example #1
0
 protected function _getAggregate($method, ISelectExpression $expression)
 {
     if ($this->isEmpty() && !$this->_isLoaded) {
         $orderBy = $this->_query->getClause('ORDER BY');
         //Remove order by for improved query performance
         if ($orderBy !== null) {
             $this->_query->removeClause('ORDER BY');
         }
         //Run a sub query if a limit is specified
         if ($this->_query->hasClause('LIMIT')) {
             $builder = $this->_getQueryBuilder();
             $aggregateQuery = $builder::select($expression)->from(SubQuerySelectExpression::create($this->_query, '_'));
             $result = Arrays::first(Arrays::first($this->_getDataStore()->getData($aggregateQuery)));
         } else {
             $originalClause = $this->_query->getClause('SELECT');
             $this->_query->addClause((new SelectClause())->addExpression($expression));
             $result = Arrays::first(Arrays::first($this->_getDataStore()->getData($this->_query)));
             $this->_query->addClause($originalClause);
         }
         if ($orderBy !== null) {
             $this->_query->addClause($orderBy);
         }
         return $result;
     }
     return parent::$method();
 }
 public function assembleSubQueryExpression(SubQuerySelectExpression $expr)
 {
     return '(' . $this->assembleSegment($expr->getQuery()) . ')' . ($expr->hasAlias() ? ' AS ' . $this->escapeField($expr->getAlias()) : '');
 }
 public function testStatic()
 {
     $statement = $this->_basicQuery();
     $this->assertEquals('(SELECT * FROM tbl) AS query', QueryAssembler::stringify(SubQuerySelectExpression::create($statement, 'query')));
     $this->assertEquals('(SELECT * FROM tbl) AS ' . substr(md5(QueryAssembler::stringify($statement)), 0, 6), QueryAssembler::stringify(SubQuerySelectExpression::create($statement)));
 }
Example #4
0
 public function testSubquery()
 {
     $subQuery = SubQuerySelectExpression::create(QueryBuilder::select('myField')->from('myTable')->limitWithOffset(5, 10), '_');
     $stmt = QueryBuilder::select(CountSelectExpression::create())->from($subQuery);
     $this->assertEquals('SELECT COUNT(*) FROM (SELECT myField FROM myTable LIMIT 5,10) AS _', QueryAssembler::stringify($stmt));
 }