• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/lib/db/pgsqlsqlhelper.php
  • Класс: BitrixMainDBPgsqlSqlHelper
  • Вызов: PgsqlSqlHelper::prepareMergeMultiple
public function prepareMergeMultiple($tableName, array $primaryFields, array $insertRows)
{
	$result = [];
	$head = '';
	$tail = '';
	$maxBodySize = 1024*1024; //1 Mb
	$body = [];
	$bodySize = 0;
	foreach ($insertRows as $insertFields)
	{
		$insert = $this->prepareInsert($tableName, $insertFields, true);
		if (!$head && $insert && $insert[0])
		{
			$head = 'INSERT INTO ' . $this->quote($tableName) . ' (' . implode(', ', $insert[0]) . ') VALUES ';
			$tail = ' ON CONFLICT (' . implode(',', array_map([$this, 'quote'], $primaryFields)) . ') DO UPDATE SET (' . implode(', ', $insert[0]) . ') = (' . implode(', ', array_map(function($f){return 'EXCLUDED.'.$f;}, $insert[0])) . ')';
		}
		if ($insert && $insert[1])
		{
			$values = '(' . implode(', ', $insert[1]) . ')';
			$bodySize += mb_strlen($values) + 4;
			$body[] = $values;
			if ($bodySize > $maxBodySize)
			{
				$result[] = $head.implode(', ', $body).$tail;
				$body = [];
				$bodySize = 0;
			}
		}
	}
	if ($body)
	{
		$result[] = $head.implode(', ', $body).$tail;
	}

	return $result;
}