/** * Transforms the query from the parent to provide LIMIT functionality. */ public function getQuery() { $query = parent::getQuery(); if ($this->hasLimit) { $max = $this->offset + $this->limit; if ($this->offset > 0) { $min = $this->offset + 1; $query = "SELECT * FROM (SELECT a.*, ROWNUM rn FROM ( {$query} ) a WHERE rownum <= {$max} ) WHERE rn >= {$min}"; } else { $query = "SELECT a.* FROM ( {$query} ) a WHERE ROWNUM <= {$max}"; } } return $query; }
/** * Returns the SQL string for the subselect. * * Example: * <code> * <?php * $subSelect = $q->subSelect(); * $subSelect->select( name )->from( 'table2' ); * $q->select( '*' ) * ->from( 'table1' ) * ->where ( $q->expr->eq( 'name', $subSelect ) ); * $stmt = $q->prepare(); * $stmt->execute(); * ?> * </code> * * @return string */ public function getQuery() { return '( ' . parent::getQuery() . ' )'; }
/** * Transforms the query from the parent to provide LIMIT functionality. * * Note: doesn't work exactly like the MySQL equivalent; it will always return * $limit rows even if $offset + $limit exceeds the total number of rows. * * @throws ezcQueryInvalidException if offset is used and orderBy is not. * @return string */ public function getQuery() { $query = parent::getQuery(); if ($this->hasLimit) { if ($this->offset) { if (!$this->orderString) { // Uh ow. We need some columns to sort in the oposite order to make this work throw new ezcQueryInvalidException("LIMIT workaround for MS SQL", "orderBy() was not called before getQuery()."); } return 'SELECT * FROM ( SELECT TOP ' . $this->limit . ' * FROM ( ' . self::top($this->offset + $this->limit, $query) . ' ) AS ezcDummyTable1 ' . $this->invertedOrderString . ' ) AS ezcDummyTable2 ' . $this->orderString; } return self::top($this->limit, $query); } return $query; }