Delete.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Database query builder for DELETE statements. See [Query Builder](/database/query/builder) for usage and examples.
  4. *
  5. * @package Kohana/Database
  6. * @category Query
  7. * @author Kohana Team
  8. * @copyright (c) Kohana Team
  9. * @license https://koseven.ga/LICENSE.md
  10. */
  11. class Kohana_Database_Query_Builder_Delete extends Database_Query_Builder_Where {
  12. // DELETE FROM ...
  13. protected $_table;
  14. /**
  15. * Set the table for a delete.
  16. *
  17. * @param mixed $table table name or array($table, $alias) or object
  18. * @return void
  19. */
  20. public function __construct($table = NULL)
  21. {
  22. if ($table)
  23. {
  24. // Set the inital table name
  25. $this->_table = $table;
  26. }
  27. // Start the query with no SQL
  28. return parent::__construct(Database::DELETE, '');
  29. }
  30. /**
  31. * Sets the table to delete from.
  32. *
  33. * @param mixed $table table name or array($table, $alias) or object
  34. * @return $this
  35. */
  36. public function table($table)
  37. {
  38. $this->_table = $table;
  39. return $this;
  40. }
  41. /**
  42. * Compile the SQL query and return it.
  43. *
  44. * @param mixed $db Database instance or name of instance
  45. * @return string
  46. */
  47. public function compile($db = NULL)
  48. {
  49. if ( ! is_object($db))
  50. {
  51. // Get the database instance
  52. $db = Database::instance($db);
  53. }
  54. // Start a deletion query
  55. $query = 'DELETE FROM '.$db->quote_table($this->_table);
  56. if ( ! empty($this->_where))
  57. {
  58. // Add deletion conditions
  59. $query .= ' WHERE '.$this->_compile_conditions($db, $this->_where);
  60. }
  61. if ( ! empty($this->_order_by))
  62. {
  63. // Add sorting
  64. $query .= ' '.$this->_compile_order_by($db, $this->_order_by);
  65. }
  66. if ($this->_limit !== NULL)
  67. {
  68. // Add limiting
  69. $query .= ' LIMIT '.$this->_limit;
  70. }
  71. $this->_sql = $query;
  72. return parent::compile($db);
  73. }
  74. public function reset()
  75. {
  76. $this->_table = NULL;
  77. $this->_where = [];
  78. $this->_parameters = [];
  79. $this->_sql = NULL;
  80. return $this;
  81. }
  82. } // End Database_Query_Builder_Delete