diff --git a/lib/PicoDb/Driver/Base.php b/lib/PicoDb/Driver/Base.php index 790cd62..4449616 100644 --- a/lib/PicoDb/Driver/Base.php +++ b/lib/PicoDb/Driver/Base.php @@ -151,6 +151,28 @@ public function closeConnection() $this->pdo = null; } + /** + * Get offset limit clause + * + * @param int $limit + * @param int $offset + * @return string + */ + public function getLimitClause($limit, $offset) + { + $clause = ''; + + if (! is_null($limit)) { + $clause .= ' LIMIT ' . $limit; + } + + if (! is_null($offset)) { + $clause .= ' OFFSET ' . $offset; + } + + return $clause; + } + /** * Upsert for a key/value variable * diff --git a/lib/PicoDb/Driver/Mssql.php b/lib/PicoDb/Driver/Mssql.php index 83e75af..2598cf8 100644 --- a/lib/PicoDb/Driver/Mssql.php +++ b/lib/PicoDb/Driver/Mssql.php @@ -175,4 +175,26 @@ public function explain($sql, array $values) $this->getConnection()->exec('SET SHOWPLAN_ALL ON'); return $this->getConnection()->query($this->getSqlFromPreparedStatement($sql, $values))->fetchAll(PDO::FETCH_ASSOC); } + + /** + * Get offset limit clause + * + * @param int $limit + * @param int $offset + * @return string + */ + public function getLimitClause($limit, $offset) + { + $clause = ''; + + if (! is_null($offset)) { + $clause = ' OFFSET '.$offset.' ROWS'; + } + + if (! is_null($limit)) { + $clause .= ' FETCH NEXT '.$limit.' ROWS ONLY'; + } + + return $clause; + } } diff --git a/lib/PicoDb/Table.php b/lib/PicoDb/Table.php index 153e04b..503cd80 100644 --- a/lib/PicoDb/Table.php +++ b/lib/PicoDb/Table.php @@ -105,17 +105,17 @@ class Table * SQL limit * * @access private - * @var string + * @var int */ - private $sqlLimit = ''; + private $sqlLimit = null; /** * SQL offset * * @access private - * @var string + * @var int */ - private $sqlOffset = ''; + private $sqlOffset = null; /** * SQL order @@ -398,15 +398,17 @@ public function subquery($sql, $alias) public function exists() { $sql = sprintf( - 'SELECT 1 FROM %s %s %s %s %s %s %s %s', + 'SELECT 1 FROM %s %s %s %s %s %s %s', $this->db->escapeIdentifier($this->name), implode(' ', $this->joins), $this->conditionBuilder->build(), empty($this->groupBy) ? '' : 'GROUP BY '.implode(', ', $this->groupBy), $this->aggregatedConditionBuilder->build(), $this->sqlOrder, - $this->sqlLimit, - $this->sqlOffset + $this->db->getDriver()->getLimitClause( + $this->sqlLimit, + $this->sqlOffset + ) ); $rq = $this->db->execute($sql, $this->getValues()); @@ -430,15 +432,17 @@ public function count(string $column = '*') $sql = sprintf( - 'SELECT COUNT(' . $column . ') FROM %s %s %s %s %s %s %s %s', + 'SELECT COUNT(' . $column . ') FROM %s %s %s %s %s %s %s', $this->db->escapeIdentifier($this->name), implode(' ', $this->joins), $this->conditionBuilder->build(), empty($this->groupBy) ? '' : 'GROUP BY '.implode(', ', $this->groupBy), $this->aggregatedConditionBuilder->build(), $this->sqlOrder, - $this->sqlLimit, - $this->sqlOffset + $this->db->getDriver()->getLimitClause( + $this->sqlLimit, + $this->sqlOffset + ) ); $rq = $this->db->execute($sql, $this->getValues()); @@ -457,7 +461,7 @@ public function count(string $column = '*') public function sum(string $column) { $sql = sprintf( - 'SELECT SUM(%s) FROM %s %s %s %s %s %s %s %s', + 'SELECT SUM(%s) FROM %s %s %s %s %s %s %s', $column, $this->db->escapeIdentifier($this->name), implode(' ', $this->joins), @@ -465,8 +469,10 @@ public function sum(string $column) empty($this->groupBy) ? '' : 'GROUP BY '.implode(', ', $this->groupBy), $this->aggregatedConditionBuilder->build(), $this->sqlOrder, - $this->sqlLimit, - $this->sqlOffset + $this->db->getDriver()->getLimitClause( + $this->sqlLimit, + $this->sqlOffset + ) ); $rq = $this->db->execute($sql, $this->getValues()); @@ -705,7 +711,7 @@ public function desc($column) public function limit($value) { if (! is_null($value)) { - $this->sqlLimit = ' LIMIT '.(int) $value; + $this->sqlLimit = $value; } return $this; @@ -721,7 +727,7 @@ public function limit($value) public function offset($value) { if (! is_null($value)) { - $this->sqlOffset = ' OFFSET '.(int) $value; + $this->sqlOffset = $value; } return $this; @@ -820,7 +826,7 @@ public function buildSelectQuery() $this->groupBy = $this->db->escapeIdentifierList($this->groupBy); return trim(sprintf( - 'SELECT %s FROM %s %s %s %s %s %s %s %s', + 'SELECT %s FROM %s %s %s %s %s %s %s', $this->sqlSelect, $this->db->escapeIdentifier($this->name), implode(' ', $this->joins), @@ -828,8 +834,10 @@ public function buildSelectQuery() empty($this->groupBy) ? '' : 'GROUP BY '.implode(', ', $this->groupBy), $this->aggregatedConditionBuilder->build(), $this->sqlOrder, - $this->sqlLimit, - $this->sqlOffset + $this->db->getDriver()->getLimitClause( + $this->sqlLimit, + $this->sqlOffset + ) )); }