Insert.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * Database query builder for INSERT 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_Insert extends Database_Query_Builder {
  12. // INSERT INTO ...
  13. protected $_table;
  14. // (...)
  15. protected $_columns = [];
  16. // VALUES (...)
  17. protected $_values = [];
  18. /**
  19. * Set the table and columns for an insert.
  20. *
  21. * @param mixed $table table name or array($table, $alias) or object
  22. * @param array $columns column names
  23. * @return void
  24. */
  25. public function __construct($table = NULL, array $columns = NULL)
  26. {
  27. if ($table)
  28. {
  29. // Set the inital table name
  30. $this->table($table);
  31. }
  32. if ($columns)
  33. {
  34. // Set the column names
  35. $this->_columns = $columns;
  36. }
  37. // Start the query with no SQL
  38. return parent::__construct(Database::INSERT, '');
  39. }
  40. /**
  41. * Sets the table to insert into.
  42. *
  43. * @param string $table table name
  44. * @return $this
  45. */
  46. public function table($table)
  47. {
  48. if ( ! is_string($table))
  49. throw new Kohana_Exception('INSERT INTO syntax does not allow table aliasing');
  50. $this->_table = $table;
  51. return $this;
  52. }
  53. /**
  54. * Set the columns that will be inserted.
  55. *
  56. * @param array $columns column names
  57. * @return $this
  58. */
  59. public function columns(array $columns)
  60. {
  61. $this->_columns = $columns;
  62. return $this;
  63. }
  64. /**
  65. * Adds or overwrites values. Multiple value sets can be added.
  66. *
  67. * @param array $values values list
  68. * @param ...
  69. * @return $this
  70. */
  71. public function values(array $values)
  72. {
  73. if ( ! is_array($this->_values))
  74. {
  75. throw new Kohana_Exception('INSERT INTO ... SELECT statements cannot be combined with INSERT INTO ... VALUES');
  76. }
  77. // Get all of the passed values
  78. $values = func_get_args();
  79. foreach ($values as $value)
  80. {
  81. $this->_values[] = $value;
  82. }
  83. return $this;
  84. }
  85. /**
  86. * Use a sub-query to for the inserted values.
  87. *
  88. * @param object $query Database_Query of SELECT type
  89. * @return $this
  90. */
  91. public function select(Database_Query $query)
  92. {
  93. if ($query->type() !== Database::SELECT)
  94. {
  95. throw new Kohana_Exception('Only SELECT queries can be combined with INSERT queries');
  96. }
  97. $this->_values = $query;
  98. return $this;
  99. }
  100. /**
  101. * Compile the SQL query and return it.
  102. *
  103. * @param mixed $db Database instance or name of instance
  104. * @return string
  105. */
  106. public function compile($db = NULL)
  107. {
  108. if ( ! is_object($db))
  109. {
  110. // Get the database instance
  111. $db = Database::instance($db);
  112. }
  113. // Start an insertion query
  114. $query = 'INSERT INTO '.$db->quote_table($this->_table);
  115. // Add the column names
  116. $query .= ' ('.implode(', ', array_map([$db, 'quote_column'], $this->_columns)).') ';
  117. if (is_array($this->_values))
  118. {
  119. // Callback for quoting values
  120. $groups = [];
  121. foreach ($this->_values as $group)
  122. {
  123. foreach ($group as $offset => $value)
  124. {
  125. if ((is_string($value) AND array_key_exists($value, $this->_parameters)) === FALSE)
  126. {
  127. // Quote the value, it is not a parameter
  128. $group[$offset] = $db->quote($value);
  129. }
  130. }
  131. $groups[] = '('.implode(', ', $group).')';
  132. }
  133. // Add the values
  134. $query .= 'VALUES '.implode(', ', $groups);
  135. }
  136. else
  137. {
  138. // Add the sub-query
  139. $query .= (string) $this->_values;
  140. }
  141. $this->_sql = $query;
  142. return parent::compile($db);
  143. }
  144. public function reset()
  145. {
  146. $this->_table = NULL;
  147. $this->_columns =
  148. $this->_values = [];
  149. $this->_parameters = [];
  150. $this->_sql = NULL;
  151. return $this;
  152. }
  153. } // End Database_Query_Builder_Insert