InsertQuery.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 4.5.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Database\Query;
  17. use Cake\Database\Exception\DatabaseException;
  18. use Cake\Database\Expression\ValuesExpression;
  19. use Cake\Database\Query;
  20. use InvalidArgumentException;
  21. /**
  22. * This class is used to generate INSERT queries for the relational database.
  23. */
  24. class InsertQuery extends Query
  25. {
  26. /**
  27. * Type of this query.
  28. *
  29. * @var string
  30. */
  31. protected string $_type = self::TYPE_INSERT;
  32. /**
  33. * List of SQL parts that will be used to build this query.
  34. *
  35. * @var array<string, mixed>
  36. */
  37. protected array $_parts = [
  38. 'comment' => null,
  39. 'with' => [],
  40. 'insert' => [],
  41. 'modifier' => [],
  42. 'values' => [],
  43. 'epilog' => null,
  44. ];
  45. /**
  46. * Create an insert query.
  47. *
  48. * Note calling this method will reset any data previously set
  49. * with Query::values().
  50. *
  51. * @param array $columns The columns to insert into.
  52. * @param array<int|string, string> $types A map between columns & their datatypes.
  53. * @return $this
  54. * @throws \InvalidArgumentException When there are 0 columns.
  55. */
  56. public function insert(array $columns, array $types = [])
  57. {
  58. if (empty($columns)) {
  59. throw new InvalidArgumentException('At least 1 column is required to perform an insert.');
  60. }
  61. $this->_dirty();
  62. $this->_parts['insert'][1] = $columns;
  63. if (!$this->_parts['values']) {
  64. $this->_parts['values'] = new ValuesExpression($columns, $this->getTypeMap()->setTypes($types));
  65. } else {
  66. $this->_parts['values']->setColumns($columns);
  67. }
  68. return $this;
  69. }
  70. /**
  71. * Set the table name for insert queries.
  72. *
  73. * @param string $table The table name to insert into.
  74. * @return $this
  75. */
  76. public function into(string $table)
  77. {
  78. $this->_dirty();
  79. $this->_parts['insert'][0] = $table;
  80. return $this;
  81. }
  82. /**
  83. * Set the values for an insert query.
  84. *
  85. * Multi inserts can be performed by calling values() more than one time,
  86. * or by providing an array of value sets. Additionally $data can be a Query
  87. * instance to insert data from another SELECT statement.
  88. *
  89. * @param \Cake\Database\Expression\ValuesExpression|\Cake\Database\Query|array $data The data to insert.
  90. * @return $this
  91. * @throws \Cake\Database\Exception\DatabaseException if you try to set values before declaring columns.
  92. * Or if you try to set values on non-insert queries.
  93. */
  94. public function values(ValuesExpression|Query|array $data)
  95. {
  96. if (empty($this->_parts['insert'])) {
  97. throw new DatabaseException(
  98. 'You cannot add values before defining columns to use.'
  99. );
  100. }
  101. $this->_dirty();
  102. if ($data instanceof ValuesExpression) {
  103. $this->_parts['values'] = $data;
  104. return $this;
  105. }
  106. $this->_parts['values']->add($data);
  107. return $this;
  108. }
  109. }