Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace ServerLogbuch\Dao\Db\DataProvider;
- class Postgres extends \bF\Dao\Db\DataProvider\Postgres
- {
- public function decorateRestrictions(\bF\Dao\Restriction\Restriction $restriction = null)
- {
- $queryString = '';
- $itemsPerPage = 0;
- $start = 0;
- $groupBy = '';
- $having = '';
- $sortBy = '';
- $orderBy = '';
- $filter = '';
- if (null != $restriction) {
- $itemsPerPage = $restriction->getBatchSize();
- $start = $restriction->getBatchStart();
- $groupBy = $restriction->getGroupBy();
- $having = $restriction->getHaving();
- $sortBy = $restriction->getCurrSort();
- $orderBy = $restriction->getCurrSortDir();
- $filter = $this->decorateFilterString($restriction->getAllFilters()->toArray());
- /**
- * The Group By statement addtionally needs to set the Current Sort
- * since the DISTINCT ON operation requires this.
- *
- * @link http://www.postgresql.org/docs/8.1/static/sql-select.html#SQL-DISTINCT
- */
- if (!empty($sortBy) && !empty($groupBy)) {
- if (is_array($sortBy)) {
- $groupBy = is_array($groupBy) ? $groupBy : array(
- $groupBy
- );
- $groupBy = array(
- $sortBy
- ) + $groupBy;
- } elseif ('' != $groupBy) {
- $groupBy = array(
- $sortBy,
- $groupBy
- );
- }
- }
- }
- if ('' != $filter) {
- $queryString .= ' WHERE ' . $filter;
- }
- if ('' != $groupBy) {
- if (!is_array($groupBy)) {
- $groupBy = array(
- $groupBy
- );
- }
- $groupByFields = '';
- foreach ($groupBy as $field) {
- $groupByFields .= $this->quoteFieldName($field) . ',';
- }
- if (!empty($having)) {
- if ($having instanceof \bF\Dao\Restriction\Filter) {
- $having = $this->decorateFilterString($having->toArray());
- }
- }
- $groupByFields = rtrim($groupByFields, ',');
- $queryString .= sprintf(' GROUP BY %s', $groupByFields);
- $queryString .= ('' != $having) ? ' HAVING ' . $having : '';
- }
- // add sort order
- if(true === method_exists($restriction, 'getSortOrders') && true !== empty($restriction->getSortOrders())) {
- $sortOrders = $restriction->getSortOrders();
- foreach($sortOrders as $field => $direction) {
- $queryString .= sprintf(' ORDER BY %s %s', $field, $direction);
- $queryString .= ','
- }
- rtrim($queryString, ',');
- } else {
- if ('' != $sortBy) {
- if (!is_array($sortBy)) {
- $sortBy = array(
- $sortBy
- );
- }
- $sortByFields = '';
- foreach ($sortBy as $field) {
- $sortByFields .= $this->quoteFieldName($field) . ',';
- }
- $sortByFields = rtrim($sortByFields, ',');
- $queryString .= sprintf(' ORDER BY %s', $sortByFields);
- $queryString .= ('' != $orderBy) ? ' ' . $orderBy : '';
- }
- // add Limit expression
- if ($itemsPerPage > 0) {
- $queryString .= ' LIMIT ' . $itemsPerPage . ' OFFSET ' . $start;
- }
- // return the decorated string
- return $queryString;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement