Insert.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php defined('SYSPATH') OR die('No direct script access.');
  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) 2008-2009 Kohana Team
  9. * @license http://kohanaphp.com/license
  10. */
  11. class Kohana_Database_Query_Builder_Insert extends Database_Query_Builder {
  12. // INSERT INTO ...
  13. protected $_table;
  14. // (...)
  15. protected $_columns = array();
  16. // VALUES (...)
  17. protected $_values = array();
  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 mixed $table table name or array($table, $alias) or object
  44. * @return $this
  45. */
  46. public function table($table)
  47. {
  48. $this->_table = $table;
  49. return $this;
  50. }
  51. /**
  52. * Set the columns that will be inserted.
  53. *
  54. * @param array $columns column names
  55. * @return $this
  56. */
  57. public function columns(array $columns)
  58. {
  59. $this->_columns = $columns;
  60. return $this;
  61. }
  62. /**
  63. * Adds or overwrites values. Multiple value sets can be added.
  64. *
  65. * @param array $values values list
  66. * @param ...
  67. * @return $this
  68. */
  69. public function values(array $values)
  70. {
  71. if ( ! is_array($this->_values))
  72. {
  73. throw new Kohana_Exception('INSERT INTO ... SELECT statements cannot be combined with INSERT INTO ... VALUES');
  74. }
  75. // Get all of the passed values
  76. $values = func_get_args();
  77. $this->_values = array_merge($this->_values, $values);
  78. return $this;
  79. }
  80. /**
  81. * Use a sub-query to for the inserted values.
  82. *
  83. * @param object $query Database_Query of SELECT type
  84. * @return $this
  85. */
  86. public function select(Database_Query $query)
  87. {
  88. if ($query->type() !== Database::SELECT)
  89. {
  90. throw new Kohana_Exception('Only SELECT queries can be combined with INSERT queries');
  91. }
  92. $this->_values = $query;
  93. return $this;
  94. }
  95. /**
  96. * Compile the SQL query and return it.
  97. *
  98. * @param mixed $db Database instance or name of instance
  99. * @return string
  100. */
  101. public function compile($db = NULL)
  102. {
  103. if ( ! is_object($db))
  104. {
  105. // Get the database instance
  106. $db = Database::instance($db);
  107. }
  108. // Start an insertion query
  109. $query = 'INSERT INTO '.$db->quote_table($this->_table);
  110. // Add the column names
  111. $query .= ' ('.implode(', ', array_map(array($db, 'quote_column'), $this->_columns)).') ';
  112. if (is_array($this->_values))
  113. {
  114. // Callback for quoting values
  115. $quote = array($db, 'quote');
  116. $groups = array();
  117. foreach ($this->_values as $group)
  118. {
  119. foreach ($group as $offset => $value)
  120. {
  121. if ((is_string($value) AND array_key_exists($value, $this->_parameters)) === FALSE)
  122. {
  123. // Quote the value, it is not a parameter
  124. $group[$offset] = $db->quote($value);
  125. }
  126. }
  127. $groups[] = '('.implode(', ', $group).')';
  128. }
  129. // Add the values
  130. $query .= 'VALUES '.implode(', ', $groups);
  131. }
  132. else
  133. {
  134. // Add the sub-query
  135. $query .= (string) $this->_values;
  136. }
  137. $this->_sql = $query;
  138. return parent::compile($db);;
  139. }
  140. public function reset()
  141. {
  142. $this->_table = NULL;
  143. $this->_columns =
  144. $this->_values = array();
  145. $this->_parameters = array();
  146. $this->_sql = NULL;
  147. return $this;
  148. }
  149. } // End Database_Query_Builder_Insert