30 public static function divide(array $list, $limit = 300)
32 $length = count($list);
39 $partsCount = ceil($length / $limit);
40 for ($index = 0; $index < $partsCount; $index++)
42 $result[$index] = array_slice($list, $limit * $index, $limit);
57 foreach($values as $index => $value)
59 $values[$index] = $conHelper->forSql($value);
62 return "'" . implode(
"', '", $values) .
"'";
72 public static function update($tableName, array $fields)
74 $ids = []; $sets = [];
75 foreach ($fields as $item)
77 if (!isset($item[
'ID']) || !$item[
'ID'])
82 $id = (int) $item[
'ID'];
90 foreach ($item as $key => $value)
92 if (!isset($sets[$key]))
97 $sets[$key][$id] = $value;
101 if (count($ids) <= 0 || count($sets) <= 0)
107 $ids = implode(
',', $ids);
109 foreach ($sets as $key => $values)
112 foreach ($values as $id => $value)
114 $value = $conHelper->forSql($value);
115 $stringSet .=
"\nWHEN ID = $id THEN '$value'";
117 $stringSet =
"\n$key = CASE $stringSet ELSE $key END";
118 $stringSets[] = $stringSet;
120 $stringSets = implode(
', ', $stringSets) .
"\n";
123 $sql =
"UPDATE $tableName SET $stringSets WHERE ID in ($ids)";
140 array $onDuplicateUpdateFields = [],
141 array $primaryFields = []
144 $columnNames = self::getFieldNames($fields);
145 if (count($columnNames) == 0)
152 if (!empty($onDuplicateUpdateFields))
154 $sqlUpdateFields = [];
155 foreach ($onDuplicateUpdateFields as $field)
157 if (is_array($field))
159 $sqlUpdateFields[$field[
'NAME']] = $field[
'VALUE'];
164 'case when ?v is null then ?#.?# else ?v end',
173 $sql = self::prepareMergeValues($tableName, $primaryFields, $fields, $sqlUpdateFields);
177 $columnNamesString = implode(
", ", $columnNames);
179 foreach ($fields as $row)
181 [$columnNamesString, $valuesString] = $sqlHelper->prepareInsert($tableName, $row);
182 $valuesStrings[] = $valuesString;
184 $dataListString = implode(
'),(', $valuesStrings);
185 $sql = $sqlHelper->getInsertIgnore($tableName,
"($columnNamesString)",
" VALUES($dataListString)");
191 private static function getFieldNames(array &$fields)
193 foreach ($fields as $items)
195 return array_keys($items);
213 private static function prepareMergeValues(
string $tableName, array $primaryFields, array $insertRows, array $updateFields = []): string
216 $insertColumns = array_keys($insertRows[array_key_first($insertRows)] ?? []);
217 $insertValuesStrings = [];
218 foreach ($insertRows as $row)
220 [, $rowValues] = $sqlHelper->prepareInsert($tableName, $row);
221 $insertValuesStrings[] = $rowValues;
224 if (empty($updateFields))
226 $notPrimaryFields = array_diff($insertColumns, $primaryFields);
227 if (empty($notPrimaryFields))
229 trigger_error(
"Only primary fields to update, use getInsertIgnore() or specify fields", E_USER_WARNING);
231 $updateFields = $notPrimaryFields;
234 $compatibleUpdateFields = [];
236 foreach ($updateFields as $key => $value)
238 if (is_numeric($key) && is_string($value))
240 $compatibleUpdateFields[$value] =
new SqlExpression(
'?v', $value);
244 $compatibleUpdateFields[$key] = $value;
248 $insertValueString =
'values (' . implode(
'),(', $insertValuesStrings) .
')';
250 return $sqlHelper->prepareMergeSelect($tableName, $primaryFields, $insertColumns, $insertValueString, $compatibleUpdateFields);
static getConnection($name="")
static loadMessages($file)