mirror of
https://github.com/silverstripe/silverstripe-postgresql
synced 2024-10-22 17:05:45 +02:00
NEW: Allow 'null' limit in database queries
This commit is contained in:
parent
5a7ea699a0
commit
0a61b16caf
@ -7,10 +7,10 @@ Injector:
|
||||
properties:
|
||||
connector: %$PDOConnector
|
||||
schemaManager: %$PostgreSQLSchemaManager
|
||||
queryBuilder: %$DBQueryBuilder
|
||||
queryBuilder: %$PostgreSQLQueryBuilder
|
||||
PostgreSQLDatabase:
|
||||
class: 'PostgreSQLDatabase'
|
||||
properties:
|
||||
connector: %$PostgreSQLConnector
|
||||
schemaManager: %$PostgreSQLSchemaManager
|
||||
queryBuilder: %$DBQueryBuilder
|
||||
queryBuilder: %$PostgreSQLQueryBuilder
|
42
code/PostgreSQLQueryBuilder.php
Normal file
42
code/PostgreSQLQueryBuilder.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
class PostgreSQLQueryBuilder extends DBQueryBuilder {
|
||||
|
||||
/**
|
||||
* Return the LIMIT clause ready for inserting into a query.
|
||||
*
|
||||
* @param SQLSelect $query The expression object to build from
|
||||
* @param array $parameters Out parameter for the resulting query parameters
|
||||
* @return string The finalised limit SQL fragment
|
||||
*/
|
||||
public function buildLimitFragment(SQLSelect $query, array &$parameters) {
|
||||
$nl = $this->getSeparator();
|
||||
|
||||
// Ensure limit is given
|
||||
$limit = $query->getLimit();
|
||||
if(empty($limit)) return '';
|
||||
|
||||
// For literal values return this as the limit SQL
|
||||
if( ! is_array($limit)) {
|
||||
return "{$nl}LIMIT $limit";
|
||||
}
|
||||
|
||||
// Assert that the array version provides the 'limit' key
|
||||
if( ! array_key_exists('limit', $limit) || ($limit['limit'] !== null && ! is_numeric($limit['limit']))) {
|
||||
throw new InvalidArgumentException(
|
||||
'DBQueryBuilder::buildLimitSQL(): Wrong format for $limit: '. var_export($limit, true)
|
||||
);
|
||||
}
|
||||
|
||||
if($limit['limit'] === null) {
|
||||
$limit['limit'] = 'ALL';
|
||||
}
|
||||
|
||||
$clause = "{$nl}LIMIT {$limit['limit']}";
|
||||
if(isset($limit['start']) && is_numeric($limit['start']) && $limit['start'] !== 0) {
|
||||
$clause .= " OFFSET {$limit['start']}";
|
||||
}
|
||||
return $clause;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user