Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
MultiplyInsertTrait.php
1<?php
2
3namespace Bitrix\Im\V2\Common;
4
6
7trait MultiplyInsertTrait
8{
10
11 public static function multiplyInsertWithoutDuplicate(array $insertFields, array $params = []): void
12 {
13 if (empty($insertFields))
14 {
15 return;
16 }
17
18 if (($params['DEADLOCK_SAFE'] ?? false) && !empty($params['UNIQUE_FIELDS'] ?? []))
19 {
20 $insertFields = self::prepareFieldsToMinimizeDeadlocks($insertFields, $params['UNIQUE_FIELDS']);
21 }
22 $sqlHelper = Application::getConnection()->getSqlHelper();
23 [$fields, $insertStatement] = static::prepareInsertMultiple($insertFields);
24 $table = $sqlHelper->quote(static::getTableName());
25
26 $sql = $sqlHelper->getInsertIgnore($table, "({$fields})", "VALUES {$insertStatement}");
27
28 self::execute($sql, $params);
29 }
30
31 public static function multiplyMerge(array $insertFields, array $updateFields, array $uniqueFields = null, array $params = []): void
32 {
33 if (empty($insertFields))
34 {
35 return;
36 }
37
38 $connection = Application::getConnection();
39 $sqlHelper = $connection->getSqlHelper();
40 $table = static::getTableName();
41 if ($uniqueFields === null)
42 {
43 $entity = static::getEntity();
44 $uniqueFields = $entity->getPrimaryArray();
45 }
46
47 if ($params['DEADLOCK_SAFE'] ?? false)
48 {
49 $insertFields = self::prepareFieldsToMinimizeDeadlocks($insertFields, $uniqueFields);
50 }
51
52 [, $insert] = static::prepareInsertMultiple($insertFields);
53 $fields = array_keys(array_values($insertFields)[0]);
54
55 $sql = $sqlHelper->prepareMergeSelect($table, $uniqueFields, $fields, " VALUES {$insert}", $updateFields);
56
57 self::execute($sql, $params);
58 }
59
60 private static function prepareInsertMultiple(array $insertFields): array
61 {
62 $tableName = static::getTableName();
63 $sqlHelper = Application::getConnection()->getSqlHelper();
64 $inserts = [];
65 $fields = '';
66 foreach ($insertFields as $insertField)
67 {
68 $insert = $sqlHelper->prepareInsert($tableName, $insertField);
69 $fields = $insert[0];
70 $inserts[] = "({$insert[1]})";
71 }
72 $insertStatement = implode(',', $inserts);
73
74 return [$fields, $insertStatement];
75 }
76
77 private static function execute(string $sql, array $params): void
78 {
79 if (isset($params['DEADLOCK_SAFE']) && $params['DEADLOCK_SAFE'])
80 {
81 self::executeDeadlockSafeQuery($sql, $params['MAX_RETRY_COUNT'] ?? null);
82 }
83 else
84 {
85 Application::getConnection()->queryExecute($sql);
86 }
87 }
88}
static getConnection($name="")