sqlunionquery.class.inc.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. // Copyright (C) 2015 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * SQLUnionQuery
  20. * build a mySQL compatible SQL query
  21. *
  22. * @copyright Copyright (C) 2015 Combodo SARL
  23. * @license http://opensource.org/licenses/AGPL-3.0
  24. */
  25. /**
  26. * SQLUnionQuery
  27. * build a mySQL compatible SQL query
  28. *
  29. * @package iTopORM
  30. */
  31. class SQLUnionQuery extends SQLQuery
  32. {
  33. protected $aQueries;
  34. protected $aGroupBy;
  35. public function __construct($aQueries, $aGroupBy)
  36. {
  37. parent::__construct();
  38. $this->aQueries = array();
  39. foreach ($aQueries as $oSQLQuery)
  40. {
  41. $this->aQueries[] = $oSQLQuery->DeepClone();
  42. }
  43. $this->aGroupBy = $aGroupBy;
  44. }
  45. public function DisplayHtml()
  46. {
  47. $aQueriesHtml = array();
  48. foreach ($this->aQueries as $oSQLQuery)
  49. {
  50. $aQueriesHtml[] = '<p>'.$oSQLQuery->DisplayHtml().'</p>';
  51. }
  52. echo implode('UNION', $aQueries);
  53. }
  54. public function AddInnerJoin($oSQLQuery, $sLeftField, $sRightField, $sRightTable = '')
  55. {
  56. foreach ($this->aQueries as $oSubSQLQuery)
  57. {
  58. $oSubSQLQuery->AddInnerJoin($oSQLQuery->DeepClone(), $sLeftField, $sRightField, $sRightTable = '');
  59. }
  60. }
  61. public function RenderDelete($aArgs = array())
  62. {
  63. throw new Exception(__class__.'::'.__function__.'Not implemented !');
  64. }
  65. // Interface, build the SQL query
  66. public function RenderUpdate($aArgs = array())
  67. {
  68. throw new Exception(__class__.'::'.__function__.'Not implemented !');
  69. }
  70. // Interface, build the SQL query
  71. public function RenderSelect($aOrderBy = array(), $aArgs = array(), $iLimitCount = 0, $iLimitStart = 0, $bGetCount = false, $bBeautifulQuery = false)
  72. {
  73. $this->m_bBeautifulQuery = $bBeautifulQuery;
  74. $sLineSep = $this->m_bBeautifulQuery ? "\n" : '';
  75. $sIndent = $this->m_bBeautifulQuery ? " " : null;
  76. $aSelects = array();
  77. foreach ($this->aQueries as $oSQLQuery)
  78. {
  79. // Render SELECTS without orderby/limit/count
  80. $aSelects[] = $oSQLQuery->RenderSelect(array(), $aArgs, 0, 0, false, $bBeautifulQuery);
  81. }
  82. $sSelects = '('.implode(")$sLineSep UNION$sLineSep(", $aSelects).')';
  83. if ($bGetCount)
  84. {
  85. $sFrom = "($sLineSep$sSelects$sLineSep) as __selects__";
  86. $sSQL = "SELECT$sLineSep COUNT(*) AS COUNT$sLineSep FROM $sFrom$sLineSep";
  87. }
  88. else
  89. {
  90. $aSelects = array();
  91. foreach ($this->aQueries as $oSQLQuery)
  92. {
  93. // Render SELECT without orderby/limit/count
  94. $aSelects[] = $oSQLQuery->RenderSelect(array(), $aArgs, 0, 0, false, $bBeautifulQuery);
  95. }
  96. $sSelect = $this->aQueries[0]->RenderSelectClause();
  97. $sOrderBy = $this->aQueries[0]->RenderOrderByClause($aOrderBy);
  98. if (!empty($sOrderBy))
  99. {
  100. $sOrderBy = "ORDER BY $sOrderBy$sLineSep";
  101. }
  102. if ($iLimitCount > 0)
  103. {
  104. $sLimit = 'LIMIT '.$iLimitStart.', '.$iLimitCount;
  105. }
  106. else
  107. {
  108. $sLimit = '';
  109. }
  110. $sSQL = $sSelects.$sLineSep.$sOrderBy.' '.$sLimit;
  111. }
  112. return $sSQL;
  113. }
  114. // Interface, build the SQL query
  115. public function RenderGroupBy($aArgs = array(), $bBeautifulQuery = false)
  116. {
  117. $this->m_bBeautifulQuery = $bBeautifulQuery;
  118. $sLineSep = $this->m_bBeautifulQuery ? "\n" : '';
  119. $sIndent = $this->m_bBeautifulQuery ? " " : null;
  120. $aSelects = array();
  121. foreach ($this->aQueries as $oSQLQuery)
  122. {
  123. // Render SELECTS without orderby/limit/count
  124. $aSelects[] = $oSQLQuery->RenderSelect(array(), $aArgs, 0, 0, false, $bBeautifulQuery);
  125. }
  126. $sSelects = '('.implode(")$sLineSep UNION$sLineSep(", $aSelects).')';
  127. $sFrom = "($sLineSep$sSelects$sLineSep) as __selects__";
  128. $aAliases = array();
  129. foreach ($this->aGroupBy as $sGroupAlias => $trash)
  130. {
  131. $aAliases[] = "`$sGroupAlias`";
  132. }
  133. $sSelect = implode(', ', $aAliases);
  134. $sGroupBy = implode(', ', $aAliases);
  135. $sSQL = "SELECT $sSelect,$sLineSep COUNT(*) AS _itop_count_$sLineSep FROM $sFrom$sLineSep GROUP BY $sGroupBy";
  136. return $sSQL;
  137. }
  138. public function OptimizeJoins($aUsedTables, $bTopCall = true)
  139. {
  140. foreach ($this->aQueries as $oSQLQuery)
  141. {
  142. $oSQLQuery->OptimizeJoins($aUsedTables);
  143. }
  144. }
  145. }