Update.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Database query builder for UPDATE 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_Update extends Database_Query_Builder_Where {
  12. // UPDATE ...
  13. protected $_table;
  14. // SET ...
  15. protected $_set = [];
  16. /**
  17. * Set the table for a update.
  18. *
  19. * @param mixed $table table name or array($table, $alias) or object
  20. * @return void
  21. */
  22. public function __construct($table = NULL)
  23. {
  24. if ($table)
  25. {
  26. // Set the inital table name
  27. $this->_table = $table;
  28. }
  29. // Start the query with no SQL
  30. return parent::__construct(Database::UPDATE, '');
  31. }
  32. /**
  33. * Sets the table to update.
  34. *
  35. * @param mixed $table table name or array($table, $alias) or object
  36. * @return $this
  37. */
  38. public function table($table)
  39. {
  40. $this->_table = $table;
  41. return $this;
  42. }
  43. /**
  44. * Set the values to update with an associative array.
  45. *
  46. * @param array $pairs associative (column => value) list
  47. * @return $this
  48. */
  49. public function set(array $pairs)
  50. {
  51. foreach ($pairs as $column => $value)
  52. {
  53. $this->_set[] = [$column, $value];
  54. }
  55. return $this;
  56. }
  57. /**
  58. * Set the value of a single column.
  59. *
  60. * @param mixed $column table name or array($table, $alias) or object
  61. * @param mixed $value column value
  62. * @return $this
  63. */
  64. public function value($column, $value)
  65. {
  66. $this->_set[] = [$column, $value];
  67. return $this;
  68. }
  69. /**
  70. * Compile the SQL query and return it.
  71. *
  72. * @param mixed $db Database instance or name of instance
  73. * @return string
  74. */
  75. public function compile($db = NULL)
  76. {
  77. if ( ! is_object($db))
  78. {
  79. // Get the database instance
  80. $db = Database::instance($db);
  81. }
  82. // Start an update query
  83. $query = 'UPDATE '.$db->quote_table($this->_table);
  84. // Add the columns to update
  85. $query .= ' SET '.$this->_compile_set($db, $this->_set);
  86. if ( ! empty($this->_where))
  87. {
  88. // Add selection conditions
  89. $query .= ' WHERE '.$this->_compile_conditions($db, $this->_where);
  90. }
  91. if ( ! empty($this->_order_by))
  92. {
  93. // Add sorting
  94. $query .= ' '.$this->_compile_order_by($db, $this->_order_by);
  95. }
  96. if ($this->_limit !== NULL)
  97. {
  98. // Add limiting
  99. $query .= ' LIMIT '.$this->_limit;
  100. }
  101. $this->_sql = $query;
  102. return parent::compile($db);
  103. }
  104. public function reset()
  105. {
  106. $this->_table = NULL;
  107. $this->_set =
  108. $this->_where = [];
  109. $this->_limit = NULL;
  110. $this->_parameters = [];
  111. $this->_sql = NULL;
  112. return $this;
  113. }
  114. } // End Database_Query_Builder_Update