Delete.class.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * A class for creating SQL DELETE statements
  4. *
  5. * @author Ironpilot
  6. * @copyright Copywrite (c) 2011, STAPLE CODE
  7. *
  8. * This file is part of the STAPLE Framework.
  9. *
  10. * The STAPLE Framework is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by the
  12. * Free Software Foundation, either version 3 of the License, or (at your option)
  13. * any later version.
  14. *
  15. * The STAPLE Framework is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  18. * more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. class Staple_Query_Delete extends Staple_Query
  24. {
  25. const IGNORE = 'IGNORE';
  26. const LOW_PRIORITY = 'LOW_PRIORITY';
  27. const QUICK = 'QUICK';
  28. /**
  29. * Additional Query Flags
  30. * @var array[string]
  31. */
  32. protected $flags = array();
  33. /**
  34. * Array of Staple_Query_Join objects that represent table joins on the query
  35. * @var array[Staple_Query_Join]
  36. */
  37. protected $joins = array();
  38. public function __construct($table = NULL, mysqli $db = NULL)
  39. {
  40. parent::__construct($table, $db);
  41. }
  42. public function addFlag($flag)
  43. {
  44. switch($flag)
  45. {
  46. case self::ALL:
  47. case self::DISTINCT:
  48. case self::DISTINCTROW:
  49. case self::HIGH_PRIORITY:
  50. case self::STRAIGHT_JOIN:
  51. case self::SQL_SMALL_RESULT:
  52. case self::SQL_BIG_RESULT:
  53. case self::SQL_BUFFER_RESULT:
  54. case self::SQL_CACHE:
  55. case self::SQL_NO_CACHE:
  56. case self::SQL_CALC_FOUND_ROWS:
  57. $this->flags[] = $flag;
  58. break;
  59. }
  60. return $this;
  61. }
  62. public function clearFlags()
  63. {
  64. $this->flags = array();
  65. return $this;
  66. }
  67. /**
  68. * @param mixed $table
  69. * @param string $alias
  70. */
  71. public function setTable($table)
  72. {
  73. //@todo expand to include multiple tables
  74. $this->table = (string)$table;
  75. return $this;
  76. }
  77. /*-----------------------------------------------JOIN FUNCTIONS-----------------------------------------------*/
  78. public function addJoin(Staple_Query_Join $join)
  79. {
  80. $this->joins[] = $join;
  81. }
  82. public function removeJoin($table)
  83. {
  84. foreach($this->joins as $key=>$join)
  85. {
  86. if($join->getTable() == $table)
  87. {
  88. unset($this->joins[$key]);
  89. return true;
  90. }
  91. }
  92. return false;
  93. }
  94. public function leftJoin($table, $condition)
  95. {
  96. $this->addJoin(Staple_Query_Join::left($table, $condition));
  97. return $this;
  98. }
  99. public function innerJoin($table, $condition)
  100. {
  101. $this->addJoin(Staple_Query_Join::inner($table, $condition));
  102. return $this;
  103. }
  104. public function getJoins()
  105. {
  106. return $this->joins;
  107. }
  108. /*-----------------------------------------------BUILD FUNCTION-----------------------------------------------*/
  109. /**
  110. *
  111. * @see Staple_Query::build()
  112. */
  113. function build()
  114. {
  115. $stmt = 'DELETE ';
  116. //Flags
  117. if(count($this->flags) > 0)
  118. {
  119. $stmt .= ' '.implode(' ', $this->flags);
  120. }
  121. //FROM CLAUSE
  122. $stmt .= "FROM ".$this->table;
  123. //JOINS
  124. if(count($this->joins) > 0)
  125. {
  126. $stmt .= "\n".implode("\n", $this->joins);
  127. }
  128. //WHERE CLAUSE
  129. if(count($this->where) > 0)
  130. {
  131. $stmt .= "\nWHERE ".implode(' AND ', $this->where);
  132. }
  133. return $stmt;
  134. }
  135. }
  136. ?>