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