Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HOTFIX] - User correct OFFSET/LIMIT syntax for MSSQL #25

Merged
merged 4 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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($limit)) {
$clause .= ' FETCH NEXT '.$limit.' ROWS ONLY';
}

if (! is_null($offset)) {
$clause = ' OFFSET '.$offset.' ROWS';
joshmcrae marked this conversation as resolved.
Show resolved Hide resolved
}

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
Loading