Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
sqlbatch.php
1<?php
9
14
15Loc::loadMessages(__FILE__);
16
22{
30 public static function divide(array $list, $limit = 300)
31 {
32 $length = count($list);
33 if ($length < $limit)
34 {
35 return array($list);
36 }
37
38 $result = array();
39 $partsCount = ceil($length / $limit);
40 for ($index = 0; $index < $partsCount; $index++)
41 {
42 $result[$index] = array_slice($list, $limit * $index, $limit);
43 }
44
45 return $result;
46 }
47
54 public static function getInString(array $values)
55 {
56 $conHelper = Application::getConnection()->getSqlHelper();
57 foreach($values as $index => $value)
58 {
59 $values[$index] = $conHelper->forSql($value);
60 }
61
62 return "'" . implode("', '", $values) . "'";
63 }
64
72 public static function update($tableName, array $fields)
73 {
74 $ids = []; $sets = [];
75 foreach ($fields as $item)
76 {
77 if (!isset($item['ID']) || !$item['ID'])
78 {
79 continue;
80 }
81
82 $id = (int) $item['ID'];
83 if ($id <= 0)
84 {
85 continue;
86 }
87 $ids[] = $id;
88 unset($item['ID']);
89
90 foreach ($item as $key => $value)
91 {
92 if (!isset($sets[$key]))
93 {
94 $sets[$key] = [];
95 }
96
97 $sets[$key][$id] = $value;
98 }
99 }
100
101 if (count($ids) <= 0 || count($sets) <= 0)
102 {
103 return;
104 }
105
106 $conHelper = Application::getConnection()->getSqlHelper();
107 $ids = implode(',', $ids);
108 $stringSets = [];
109 foreach ($sets as $key => $values)
110 {
111 $stringSet = "";
112 foreach ($values as $id => $value)
113 {
114 $value = $conHelper->forSql($value);
115 $stringSet .= "\nWHEN ID = $id THEN '$value'";
116 }
117 $stringSet = "\n$key = CASE $stringSet ELSE $key END";
118 $stringSets[] = $stringSet;
119 }
120 $stringSets = implode(', ', $stringSets) . "\n";
121
122
123 $sql = "UPDATE $tableName SET $stringSets WHERE ID in ($ids)";
124 Application::getConnection()->query($sql);
125 }
126
137 public static function insert(
138 string $tableName,
139 array $fields,
140 array $onDuplicateUpdateFields = [],
141 array $primaryFields = []
142 ): void
143 {
144 $columnNames = self::getFieldNames($fields);
145 if (count($columnNames) == 0)
146 {
147 return;
148 }
149
150 $sqlHelper = Application::getConnection()->getSqlHelper();
151
152 if (!empty($onDuplicateUpdateFields))
153 {
154 $sqlUpdateFields = [];
155 foreach ($onDuplicateUpdateFields as $field)
156 {
157 if (is_array($field))
158 {
159 $sqlUpdateFields[$field['NAME']] = $field['VALUE'];
160 }
161 else
162 {
163 $sqlUpdateFields[$field] = new SqlExpression(
164 'case when ?v is null then ?#.?# else ?v end',
165 $field,
166 $tableName,
167 $field,
168 $field,
169 );
170 }
171 }
172
173 $sql = self::prepareMergeValues($tableName, $primaryFields, $fields, $sqlUpdateFields);
174 }
175 else
176 {
177 $columnNamesString = implode(", ", $columnNames);
178 $valuesStrings = [];
179 foreach ($fields as $row)
180 {
181 [$columnNamesString, $valuesString] = $sqlHelper->prepareInsert($tableName, $row);
182 $valuesStrings[] = $valuesString;
183 }
184 $dataListString = implode('),(', $valuesStrings);
185 $sql = $sqlHelper->getInsertIgnore($tableName, "($columnNamesString)", " VALUES($dataListString)");
186 }
187
188 Application::getConnection()->query($sql);
189 }
190
191 private static function getFieldNames(array &$fields)
192 {
193 foreach ($fields as $items)
194 {
195 return array_keys($items);
196 }
197
198 return array();
199 }
200
213 private static function prepareMergeValues(string $tableName, array $primaryFields, array $insertRows, array $updateFields = []): string
214 {
215 $sqlHelper = Application::getConnection()->getSqlHelper();
216 $insertColumns = array_keys($insertRows[array_key_first($insertRows)] ?? []);
217 $insertValuesStrings = [];
218 foreach ($insertRows as $row)
219 {
220 [, $rowValues] = $sqlHelper->prepareInsert($tableName, $row);
221 $insertValuesStrings[] = $rowValues;
222 }
223
224 if (empty($updateFields))
225 {
226 $notPrimaryFields = array_diff($insertColumns, $primaryFields);
227 if (empty($notPrimaryFields))
228 {
229 trigger_error("Only primary fields to update, use getInsertIgnore() or specify fields", E_USER_WARNING);
230 }
231 $updateFields = $notPrimaryFields;
232 }
233
234 $compatibleUpdateFields = [];
235
236 foreach ($updateFields as $key => $value)
237 {
238 if (is_numeric($key) && is_string($value))
239 {
240 $compatibleUpdateFields[$value] = new SqlExpression('?v', $value);
241 }
242 else
243 {
244 $compatibleUpdateFields[$key] = $value;
245 }
246 }
247
248 $insertValueString = 'values (' . implode('),(', $insertValuesStrings) . ')';
249
250 return $sqlHelper->prepareMergeSelect($tableName, $primaryFields, $insertColumns, $insertValueString, $compatibleUpdateFields);
251 }
252
253}
static getConnection($name="")
static loadMessages($file)
Definition loc.php:64
static getInString(array $values)
Definition sqlbatch.php:54
static insert(string $tableName, array $fields, array $onDuplicateUpdateFields=[], array $primaryFields=[])
Definition sqlbatch.php:137
static update($tableName, array $fields)
Definition sqlbatch.php:72
static divide(array $list, $limit=300)
Definition sqlbatch.php:30