7trait MultiplyInsertTrait
11 public static function multiplyInsertWithoutDuplicate(array $insertFields, array $params = []):
void
13 if (empty($insertFields))
18 if (($params[
'DEADLOCK_SAFE'] ??
false) && !empty($params[
'UNIQUE_FIELDS'] ?? []))
20 $insertFields = self::prepareFieldsToMinimizeDeadlocks($insertFields, $params[
'UNIQUE_FIELDS']);
23 [$fields, $insertStatement] = static::prepareInsertMultiple($insertFields);
24 $table = $sqlHelper->quote(static::getTableName());
26 $sql = $sqlHelper->getInsertIgnore($table,
"({$fields})",
"VALUES {$insertStatement}");
28 self::execute($sql, $params);
31 public static function multiplyMerge(array $insertFields, array $updateFields, array $uniqueFields =
null, array $params = []):
void
33 if (empty($insertFields))
39 $sqlHelper = $connection->getSqlHelper();
40 $table = static::getTableName();
41 if ($uniqueFields ===
null)
43 $entity = static::getEntity();
44 $uniqueFields = $entity->getPrimaryArray();
47 if ($params[
'DEADLOCK_SAFE'] ??
false)
49 $insertFields = self::prepareFieldsToMinimizeDeadlocks($insertFields, $uniqueFields);
52 [, $insert] = static::prepareInsertMultiple($insertFields);
53 $fields = array_keys(array_values($insertFields)[0]);
55 $sql = $sqlHelper->prepareMergeSelect($table, $uniqueFields, $fields,
" VALUES {$insert}", $updateFields);
57 self::execute($sql, $params);
60 private static function prepareInsertMultiple(array $insertFields): array
62 $tableName = static::getTableName();
66 foreach ($insertFields as $insertField)
68 $insert = $sqlHelper->prepareInsert($tableName, $insertField);
70 $inserts[] =
"({$insert[1]})";
72 $insertStatement = implode(
',', $inserts);
74 return [$fields, $insertStatement];
77 private static function execute(
string $sql, array $params):
void
79 if (isset($params[
'DEADLOCK_SAFE']) && $params[
'DEADLOCK_SAFE'])
81 self::executeDeadlockSafeQuery($sql, $params[
'MAX_RETRY_COUNT'] ??
null);
static getConnection($name="")