Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. <?php
  2.  
  3. namespace ServerLogbuch\Dao\Db\DataProvider;
  4.  
  5. class Postgres extends \bF\Dao\Db\DataProvider\Postgres
  6. {
  7. public function decorateRestrictions(\bF\Dao\Restriction\Restriction $restriction = null)
  8. {
  9. $queryString = '';
  10. $itemsPerPage = 0;
  11. $start = 0;
  12. $groupBy = '';
  13. $having = '';
  14. $sortBy = '';
  15. $orderBy = '';
  16. $filter = '';
  17.  
  18. if (null != $restriction) {
  19. $itemsPerPage = $restriction->getBatchSize();
  20. $start = $restriction->getBatchStart();
  21. $groupBy = $restriction->getGroupBy();
  22. $having = $restriction->getHaving();
  23. $sortBy = $restriction->getCurrSort();
  24. $orderBy = $restriction->getCurrSortDir();
  25. $filter = $this->decorateFilterString($restriction->getAllFilters()->toArray());
  26.  
  27. /**
  28. * The Group By statement addtionally needs to set the Current Sort
  29. * since the DISTINCT ON operation requires this.
  30. *
  31. * @link http://www.postgresql.org/docs/8.1/static/sql-select.html#SQL-DISTINCT
  32. */
  33. if (!empty($sortBy) && !empty($groupBy)) {
  34. if (is_array($sortBy)) {
  35. $groupBy = is_array($groupBy) ? $groupBy : array(
  36. $groupBy
  37. );
  38. $groupBy = array(
  39. $sortBy
  40. ) + $groupBy;
  41. } elseif ('' != $groupBy) {
  42. $groupBy = array(
  43. $sortBy,
  44. $groupBy
  45. );
  46. }
  47. }
  48. }
  49.  
  50. if ('' != $filter) {
  51. $queryString .= ' WHERE ' . $filter;
  52. }
  53.  
  54. if ('' != $groupBy) {
  55. if (!is_array($groupBy)) {
  56. $groupBy = array(
  57. $groupBy
  58. );
  59. }
  60.  
  61. $groupByFields = '';
  62. foreach ($groupBy as $field) {
  63. $groupByFields .= $this->quoteFieldName($field) . ',';
  64. }
  65.  
  66. if (!empty($having)) {
  67. if ($having instanceof \bF\Dao\Restriction\Filter) {
  68. $having = $this->decorateFilterString($having->toArray());
  69. }
  70. }
  71.  
  72. $groupByFields = rtrim($groupByFields, ',');
  73. $queryString .= sprintf(' GROUP BY %s', $groupByFields);
  74. $queryString .= ('' != $having) ? ' HAVING ' . $having : '';
  75. }
  76.  
  77. // add sort order
  78. if(true === method_exists($restriction, 'getSortOrders') && true !== empty($restriction->getSortOrders())) {
  79.  
  80. $sortOrders = $restriction->getSortOrders();
  81. foreach($sortOrders as $field => $direction) {
  82. $queryString .= sprintf(' ORDER BY %s %s', $field, $direction);
  83. $queryString .= ','
  84. }
  85. rtrim($queryString, ',');
  86.  
  87. } else {
  88.  
  89. if ('' != $sortBy) {
  90. if (!is_array($sortBy)) {
  91. $sortBy = array(
  92. $sortBy
  93. );
  94. }
  95. $sortByFields = '';
  96. foreach ($sortBy as $field) {
  97. $sortByFields .= $this->quoteFieldName($field) . ',';
  98. }
  99. $sortByFields = rtrim($sortByFields, ',');
  100. $queryString .= sprintf(' ORDER BY %s', $sortByFields);
  101. $queryString .= ('' != $orderBy) ? ' ' . $orderBy : '';
  102. }
  103. // add Limit expression
  104. if ($itemsPerPage > 0) {
  105. $queryString .= ' LIMIT ' . $itemsPerPage . ' OFFSET ' . $start;
  106. }
  107. // return the decorated string
  108. return $queryString;
  109. }
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement