sqlquery.class.inc.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. // Copyright (C) 2010-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. * SQLQuery
  20. * build an mySQL compatible SQL query
  21. *
  22. * @copyright Copyright (C) 2010-2015 Combodo SARL
  23. * @license http://opensource.org/licenses/AGPL-3.0
  24. */
  25. /**
  26. * SQLQuery
  27. * build an mySQL compatible SQL query
  28. *
  29. * @package iTopORM
  30. */
  31. require_once('cmdbsource.class.inc.php');
  32. abstract class SQLQuery
  33. {
  34. private $m_SourceOQL = '';
  35. private $m_bBeautifulQuery = false;
  36. public function __construct()
  37. {
  38. }
  39. /**
  40. * Perform a deep clone (as opposed to "clone" which does copy a reference to the underlying objects
  41. **/
  42. public function DeepClone()
  43. {
  44. return unserialize(serialize($this));
  45. }
  46. public function SetSourceOQL($sOQL)
  47. {
  48. $this->m_SourceOQL = $sOQL;
  49. }
  50. public function GetSourceOQL()
  51. {
  52. return $this->m_SourceOQL;
  53. }
  54. abstract public function AddInnerJoin($oSQLQuery, $sLeftField, $sRightField, $sRightTable = '');
  55. abstract public function DisplayHtml();
  56. abstract public function RenderDelete($aArgs = array());
  57. abstract public function RenderUpdate($aArgs = array());
  58. abstract public function RenderSelect($aOrderBy = array(), $aArgs = array(), $iLimitCount = 0, $iLimitStart = 0, $bGetCount = false, $bBeautifulQuery = false);
  59. abstract public function RenderGroupBy($aArgs = array(), $bBeautifulQuery = false);
  60. abstract public function OptimizeJoins($aUsedTables, $bTopCall = true);
  61. protected static function ClauseSelect($aFields)
  62. {
  63. $aSelect = array();
  64. foreach ($aFields as $sFieldAlias => $sSQLExpr)
  65. {
  66. $aSelect[] = "$sSQLExpr AS $sFieldAlias";
  67. }
  68. $sSelect = implode(', ', $aSelect);
  69. return $sSelect;
  70. }
  71. protected static function ClauseGroupBy($aGroupBy)
  72. {
  73. $sRes = implode(', ', $aGroupBy);
  74. return $sRes;
  75. }
  76. protected static function ClauseDelete($aDelTableAliases)
  77. {
  78. $aDelTables = array();
  79. foreach ($aDelTableAliases as $sTableAlias)
  80. {
  81. $aDelTables[] = "$sTableAlias";
  82. }
  83. $sDelTables = implode(', ', $aDelTables);
  84. return $sDelTables;
  85. }
  86. protected static function ClauseFrom($aFrom, $sIndent = null, $iIndentLevel = 0)
  87. {
  88. $sLineBreakLong = $sIndent ? "\n".str_repeat($sIndent, $iIndentLevel + 1) : '';
  89. $sLineBreak = $sIndent ? "\n".str_repeat($sIndent, $iIndentLevel) : '';
  90. $sFrom = "";
  91. foreach ($aFrom as $sTableAlias => $aJoinInfo)
  92. {
  93. switch ($aJoinInfo["jointype"])
  94. {
  95. case "first":
  96. $sFrom .= $sLineBreakLong."`".$aJoinInfo["tablename"]."` AS `$sTableAlias`";
  97. $sFrom .= self::ClauseFrom($aJoinInfo["subfrom"], $sIndent, $iIndentLevel + 1);
  98. break;
  99. case "inner":
  100. case "inner_tree":
  101. if (count($aJoinInfo["subfrom"]) > 0)
  102. {
  103. $sFrom .= $sLineBreak."INNER JOIN ($sLineBreakLong`".$aJoinInfo["tablename"]."` AS `$sTableAlias`";
  104. $sFrom .= " ".self::ClauseFrom($aJoinInfo["subfrom"], $sIndent, $iIndentLevel + 1);
  105. $sFrom .= $sLineBreak.") ON ".$aJoinInfo["joincondition"];
  106. }
  107. else
  108. {
  109. // Unions do not suffer parenthesis around the "table AS alias"
  110. $sFrom .= $sLineBreak."INNER JOIN $sLineBreakLong`".$aJoinInfo["tablename"]."` AS `$sTableAlias`";
  111. $sFrom .= $sLineBreak." ON ".$aJoinInfo["joincondition"];
  112. }
  113. break;
  114. case "left":
  115. if (count($aJoinInfo["subfrom"]) > 0)
  116. {
  117. $sFrom .= $sLineBreak."LEFT JOIN ($sLineBreakLong`".$aJoinInfo["tablename"]."` AS `$sTableAlias`";
  118. $sFrom .= " ".self::ClauseFrom($aJoinInfo["subfrom"], $sIndent, $iIndentLevel + 1);
  119. $sFrom .= $sLineBreak.") ON ".$aJoinInfo["joincondition"];
  120. }
  121. else
  122. {
  123. // Unions do not suffer parenthesis around the "table AS alias"
  124. $sFrom .= $sLineBreak."LEFT JOIN $sLineBreakLong`".$aJoinInfo["tablename"]."` AS `$sTableAlias`";
  125. $sFrom .= $sLineBreak." ON ".$aJoinInfo["joincondition"];
  126. }
  127. break;
  128. default:
  129. throw new CoreException("Unknown jointype: '".$aJoinInfo["jointype"]."'");
  130. }
  131. }
  132. return $sFrom;
  133. }
  134. protected static function ClauseValues($aValues)
  135. {
  136. $aSetValues = array();
  137. foreach ($aValues as $sFieldSpec => $value)
  138. {
  139. $aSetValues[] = "$sFieldSpec = ".CMDBSource::Quote($value);
  140. }
  141. $sSetValues = implode(', ', $aSetValues);
  142. return $sSetValues;
  143. }
  144. protected static function ClauseWhere($oConditionExpr, $aArgs = array())
  145. {
  146. if (is_null($oConditionExpr))
  147. {
  148. return '1';
  149. }
  150. else
  151. {
  152. return $oConditionExpr->Render($aArgs);
  153. }
  154. }
  155. protected static function ClauseOrderBy($aOrderBy)
  156. {
  157. $aOrderBySpec = array();
  158. foreach($aOrderBy as $sFieldAlias => $bAscending)
  159. {
  160. // Note: sFieldAlias must have backticks around column aliases
  161. $aOrderBySpec[] = $sFieldAlias.($bAscending ? " ASC" : " DESC");
  162. }
  163. $sOrderBy = implode(", ", $aOrderBySpec);
  164. return $sOrderBy;
  165. }
  166. }