Skip to content

Commit

Permalink
[HOTFIX] - User correct OFFSET/LIMIT syntax for MSSQL (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmcrae authored Dec 4, 2023
1 parent cc7dedb commit 3ed1c0a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 18 deletions.
22 changes: 22 additions & 0 deletions lib/PicoDb/Driver/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
22 changes: 22 additions & 0 deletions lib/PicoDb/Driver/Mssql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
44 changes: 26 additions & 18 deletions lib/PicoDb/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -457,16 +461,18 @@ 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),
$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());
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -820,16 +826,18 @@ 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),
$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
)
));
}

Expand Down

0 comments on commit 3ed1c0a

Please sign in to comment.