2 declare(strict_types = 1);
18 use Doctrine\DBAL\Connection;
116 if (empty($this->columns)) {
117 foreach (
$values as $index => $value) {
118 $this->parameters[] = $value;
119 $this->types[] =
$types[$index] ??
null;
123 $this->values[] = $valueSet;
128 foreach ($this->columns as $index => $column) {
129 $namedValue = isset(
$values[$column]) || array_key_exists($column,
$values);
130 $positionalValue = isset(
$values[$index]) || array_key_exists($index,
$values);
132 if (!$namedValue && !$positionalValue) {
133 throw new \InvalidArgumentException(
134 sprintf(
'No value specified for column %s (index %d).', $column, $index),
139 if ($namedValue && $positionalValue &&
$values[$column] !==
$values[$index]) {
140 throw new \InvalidArgumentException(
141 sprintf(
'Multiple values specified for column %s (index %d).', $column, $index),
149 $namedType = isset(
$types[$column]);
150 $positionalType = isset(
$types[$index]);
152 if ($namedType && $positionalType &&
$types[$column] !==
$types[$index]) {
153 throw new \InvalidArgumentException(
154 sprintf(
'Multiple types specified for column %s (index %d).', $column, $index),
160 $this->types[] =
$types[$column];
165 if ($positionalType) {
166 $this->types[] =
$types[$index];
171 $this->types[] =
null;
174 $this->values[] = $valueSet;
187 $platform = $this->connection->getDatabasePlatform();
190 if ($insertMaxRows > 0 && count($this->values) > $insertMaxRows) {
191 throw new \LogicException(
193 'You can only insert %d rows in a single INSERT statement with platform "%s".',
201 return $this->connection->executeUpdate($this->
getSQL(), $this->parameters, $this->types);
211 $platform = $this->connection->getDatabasePlatform();
212 if ($platform->getName() ===
'mssql' && $platform->getReservedKeywordsList()->isKeyword(
'MERGE')) {
246 public function getSQL(): string
248 if (empty($this->values)) {
249 throw new \LogicException(
250 'You need to add at least one set of values before generating the SQL.',
258 if (!empty($this->columns)) {
259 $columnList = sprintf(
274 'INSERT INTO %s%s VALUES (%s)',
280 function (array $valueSet) {
281 return implode(
', ', $valueSet);