1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
sqlbatch.php
См. документацию.
1<?php
8namespace Bitrix\Sender\Internals;
9
10use Bitrix\Main\DB\SqlExpression;
11use Bitrix\Main\Localization\Loc;
12use Bitrix\Main\Application;
13use Bitrix\Main\Type\DateTime;
14
15Loc::loadMessages(__FILE__);
16
22{
23 private const KEY_FIELDS_SEPARATOR = '#kvA3[56U?OWz16l#';
24
32 public static function divide(array $list, $limit = 300)
33 {
34 $length = count($list);
35 if ($length < $limit)
36 {
37 return array($list);
38 }
39
40 $result = array();
41 $partsCount = ceil($length / $limit);
42 for ($index = 0; $index < $partsCount; $index++)
43 {
44 $result[$index] = array_slice($list, $limit * $index, $limit);
45 }
46
47 return $result;
48 }
49
56 public static function getInString(array $values)
57 {
58 $conHelper = Application::getConnection()->getSqlHelper();
59 foreach($values as $index => $value)
60 {
61 $values[$index] = $conHelper->forSql($value);
62 }
63
64 return "'" . implode("', '", $values) . "'";
65 }
66
74 public static function update($tableName, array $fields)
75 {
76 $ids = []; $sets = [];
77 foreach ($fields as $item)
78 {
79 if (!isset($item['ID']) || !$item['ID'])
80 {
81 continue;
82 }
83
84 $id = (int) $item['ID'];
85 if ($id <= 0)
86 {
87 continue;
88 }
89 $ids[] = $id;
90 unset($item['ID']);
91
92 foreach ($item as $key => $value)
93 {
94 if (!isset($sets[$key]))
95 {
96 $sets[$key] = [];
97 }
98
99 $sets[$key][$id] = $value;
100 }
101 }
102
103 if (count($ids) <= 0 || count($sets) <= 0)
104 {
105 return;
106 }
107
108 $conHelper = Application::getConnection()->getSqlHelper();
109 $ids = implode(',', $ids);
110 $stringSets = [];
111 foreach ($sets as $key => $values)
112 {
113 $stringSet = "";
114 foreach ($values as $id => $value)
115 {
116 $value = $conHelper->forSql($value);
117 $stringSet .= "\nWHEN ID = $id THEN '$value'";
118 }
119 $stringSet = "\n$key = CASE $stringSet ELSE $key END";
120 $stringSets[] = $stringSet;
121 }
122 $stringSets = implode(', ', $stringSets) . "\n";
123
124
125 $sql = "UPDATE $tableName SET $stringSets WHERE ID in ($ids)";
126 Application::getConnection()->query($sql);
127 }
128
139 public static function insert(
140 string $tableName,
142 array $onDuplicateUpdateFields = [],
143 array $primaryFields = []
144 ): void
145 {
146 $columnNames = self::getFieldNames($fields);
147 if (count($columnNames) == 0)
148 {
149 return;
150 }
151
152 $sqlHelper = Application::getConnection()->getSqlHelper();
153
154 if (!empty($onDuplicateUpdateFields))
155 {
156 $sqlUpdateFields = [];
157 foreach ($onDuplicateUpdateFields as $field)
158 {
159 if (is_array($field))
160 {
161 $sqlUpdateFields[$field['NAME']] = $field['VALUE'];
162 }
163 else
164 {
165 $sqlUpdateFields[$field] = new SqlExpression(
166 'case when ?v is null then ?#.?# else ?v end',
167 $field,
168 $tableName,
169 $field,
170 $field,
171 );
172 }
173 }
174 $fields = self::getUniqueRowsByPrimaryFields($fields, $primaryFields);
175
176 $sql = self::prepareMergeValues($tableName, $primaryFields, $fields, $sqlUpdateFields);
177 }
178 else
179 {
180 $columnNamesString = implode(", ", $columnNames);
181 $valuesStrings = [];
182 foreach ($fields as $row)
183 {
184 [$columnNamesString, $valuesString] = $sqlHelper->prepareInsert($tableName, $row);
185 $valuesStrings[] = $valuesString;
186 }
187 $dataListString = implode('),(', $valuesStrings);
188 $sql = $sqlHelper->getInsertIgnore($tableName, "($columnNamesString)", " VALUES($dataListString)");
189 }
190
191 Application::getConnection()->query($sql);
192 }
193
194 private static function getUniqueRowsByPrimaryFields(array $rows, array $primaryFields): array
195 {
196 $unique = [];
197
198 foreach($rows as $row)
199 {
200 $primaryValues = array_intersect_key($row, array_flip($primaryFields));
201 $key = implode(self::KEY_FIELDS_SEPARATOR, $primaryValues);
202 $unique[$key] = $row;
203 }
204
205 return array_values($unique);
206 }
207
208 private static function getFieldNames(array &$fields)
209 {
210 foreach ($fields as $items)
211 {
212 return array_keys($items);
213 }
214
215 return array();
216 }
217
230 private static function prepareMergeValues(string $tableName, array $primaryFields, array $insertRows, array $updateFields = []): string
231 {
232 $sqlHelper = Application::getConnection()->getSqlHelper();
233 $insertColumns = array_keys($insertRows[array_key_first($insertRows)] ?? []);
234 $insertValuesStrings = [];
235 foreach ($insertRows as $row)
236 {
237 [, $rowValues] = $sqlHelper->prepareInsert($tableName, $row);
238 $insertValuesStrings[] = $rowValues;
239 }
240
241 if (empty($updateFields))
242 {
243 $notPrimaryFields = array_diff($insertColumns, $primaryFields);
244 if (empty($notPrimaryFields))
245 {
246 trigger_error("Only primary fields to update, use getInsertIgnore() or specify fields", E_USER_WARNING);
247 }
248 $updateFields = $notPrimaryFields;
249 }
250
251 $compatibleUpdateFields = [];
252
253 foreach ($updateFields as $key => $value)
254 {
255 if (is_numeric($key) && is_string($value))
256 {
257 $compatibleUpdateFields[$value] = new SqlExpression('?v', $value);
258 }
259 else
260 {
261 $compatibleUpdateFields[$key] = $value;
262 }
263 }
264
265 $insertValueString = 'values (' . implode('),(', $insertValuesStrings) . ')';
266
267 return $sqlHelper->prepareMergeSelect($tableName, $primaryFields, $insertColumns, $insertValueString, $compatibleUpdateFields);
268 }
269
270}
static getInString(array $values)
Определения sqlbatch.php:56
static insert(string $tableName, array $fields, array $onDuplicateUpdateFields=[], array $primaryFields=[])
Определения sqlbatch.php:139
static update($tableName, array $fields)
Определения sqlbatch.php:74
static divide(array $list, $limit=300)
Определения sqlbatch.php:32
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$value
Определения Param.php:39
if(empty($signedUserToken)) $key
Определения quickway.php:257
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$items
Определения template.php:224
$rows
Определения options.php:264
$fields
Определения yandex_run.php:501