mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
FIX: ensure limits to SQLQuery are passed as positive values
This commit is contained in:
parent
168663657b
commit
4ee709e88d
@ -423,17 +423,29 @@ class SQLQuery {
|
|||||||
*
|
*
|
||||||
* @param int|string|array $limit If passed as a string or array, assumes SQL escaped data.
|
* @param int|string|array $limit If passed as a string or array, assumes SQL escaped data.
|
||||||
* @param int $offset
|
* @param int $offset
|
||||||
|
*
|
||||||
|
* @throws InvalidArgumentException
|
||||||
|
*
|
||||||
* @return SQLQuery This instance
|
* @return SQLQuery This instance
|
||||||
*/
|
*/
|
||||||
public function setLimit($limit, $offset = 0) {
|
public function setLimit($limit, $offset = 0) {
|
||||||
|
if((is_numeric($limit) && $limit < 0) || (is_numeric($offset) && $offset < 0)) {
|
||||||
|
throw new InvalidArgumentException("SQLQuery::setLimit() only takes positive values");
|
||||||
|
}
|
||||||
|
|
||||||
if($limit && is_numeric($limit)) {
|
if($limit && is_numeric($limit)) {
|
||||||
$this->limit = array(
|
$this->limit = array(
|
||||||
'start' => $offset,
|
'start' => $offset,
|
||||||
'limit' => $limit,
|
'limit' => $limit,
|
||||||
);
|
);
|
||||||
} else if($limit && is_string($limit)) {
|
} else if($limit && is_string($limit)) {
|
||||||
if(strpos($limit, ',') !== false) list($start, $innerLimit) = explode(',', $limit, 2);
|
if(strpos($limit, ',') !== false) {
|
||||||
else list($innerLimit, $start) = explode(' OFFSET ', strtoupper($limit), 2);
|
list($start, $innerLimit) = explode(',', $limit, 2);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
list($innerLimit, $start) = explode(' OFFSET ', strtoupper($limit), 2);
|
||||||
|
}
|
||||||
|
|
||||||
$this->limit = array(
|
$this->limit = array(
|
||||||
'start' => trim($start),
|
'start' => trim($start),
|
||||||
'limit' => trim($innerLimit),
|
'limit' => trim($innerLimit),
|
||||||
|
@ -128,7 +128,31 @@ class SQLQueryTest extends SapphireTest {
|
|||||||
|
|
||||||
$this->assertEquals('SELECT *, RAND() AS "_SortColumn0" FROM MyTable ORDER BY "_SortColumn0" ASC', $query->sql());
|
$this->assertEquals('SELECT *, RAND() AS "_SortColumn0" FROM MyTable ORDER BY "_SortColumn0" ASC', $query->sql());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function testNegativeLimit() {
|
||||||
|
$query = new SQLQuery();
|
||||||
|
$query->setLimit(-10);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function testNegativeOffset() {
|
||||||
|
$query = new SQLQuery();
|
||||||
|
$query->setLimit(1, -10);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function testNegativeOffsetAndLimit() {
|
||||||
|
$query = new SQLQuery();
|
||||||
|
$query->setLimit(-10, -10);
|
||||||
|
}
|
||||||
|
|
||||||
public function testReverseOrderBy() {
|
public function testReverseOrderBy() {
|
||||||
$query = new SQLQuery();
|
$query = new SQLQuery();
|
||||||
$query->setFrom('MyTable');
|
$query->setFrom('MyTable');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user