Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 public function select()
 {
     if ($this->query->getLimit() === null) {
         // No limit so we can just execute a normal query
         return parent::select();
     } else {
         // There is a limit so we need to emulate the LIMIT/OFFSET clause with ANSI-SQL
         $order = trim($this->orderings($this->query->getOrderings()));
         if (empty($order)) {
             $order = 'ORDER BY (SELECT 0)';
         }
         $sql = $this->query->isDistinct() ? 'SELECT DISTINCT ' : 'SELECT ';
         $sql .= $this->columns($this->query->getColumns());
         $sql .= ', ROW_NUMBER() OVER (' . $order . ') AS mako_rownum';
         $sql .= ' FROM ';
         $sql .= $this->wrap($this->query->getTable());
         $sql .= $this->joins($this->query->getJoins());
         $sql .= $this->wheres($this->query->getWheres());
         $sql .= $this->groupings($this->query->getGroupings());
         $sql .= $this->havings($this->query->getHavings());
         $offset = $this->query->getOffset() === null ? 0 : $this->query->getOffset();
         $limit = $offset + $this->query->getLimit();
         $offset = $offset + 1;
         $sql = 'SELECT * FROM (' . $sql . ') AS mako1 WHERE mako_rownum BETWEEN ' . $offset . ' AND ' . $limit;
         return ['sql' => $sql, 'params' => $this->params];
     }
 }
Esempio n. 2
0
 /**
  * {@inheritdoc}
  */
 public function select()
 {
     if ($this->query->getLimit() === null) {
         // No limit so we can just execute a normal query
         return parent::select();
     } else {
         $sql = $this->query->isDistinct() ? 'SELECT DISTINCT ' : 'SELECT ';
         $sql .= $this->columns($this->query->getColumns());
         $sql .= ' FROM ';
         $sql .= $this->wrap($this->query->getTable());
         $sql .= $this->joins($this->query->getJoins());
         $sql .= $this->wheres($this->query->getWheres());
         $sql .= $this->groupings($this->query->getGroupings());
         $sql .= $this->havings($this->query->getHavings());
         $sql .= $this->orderings($this->query->getOrderings());
         if ($this->query->getOffset() === null) {
             // No offset so we only need a simple subquery to emulate the LIMIT clause
             $sql = 'SELECT mako1.* FROM (' . $sql . ') mako1 WHERE rownum <= ' . $this->query->getLimit();
         } else {
             // There is an offset so we need to make a bunch of subqueries to emulate the LIMIT and OFFSET clauses
             $limit = $this->query->getLimit() + $this->query->getOffset();
             $offset = $this->query->getOffset() + 1;
             $sql = 'SELECT * FROM (SELECT mako1.*, rownum AS mako_rownum FROM (' . $sql . ') mako1 WHERE rownum <= ' . $limit . ') WHERE mako_rownum >= ' . $offset;
         }
         return ['sql' => $sql, 'params' => $this->params];
     }
 }