11 private static $tableFields;
21 public static function bulkAdd(array $rows, $primary =
null):
void
27 $tableName = static::getTableName();
28 $connection = Main\Application::getConnection();
29 $sqlHelper = $connection->getSqlHelper();
31 if (empty(static::$tableFields))
33 static::$tableFields = $connection->getTableFields($tableName);
36 $columns0 = \array_keys($rows[0]);
38 foreach ($columns0 as $c)
40 $columns[$c] = $sqlHelper->quote(\mb_strtoupper($c));
44 foreach ($rows as $data)
46 foreach ($data as $columnName => $value)
48 $data[$columnName] = $sqlHelper->convertToDb($value, static::$tableFields[$columnName]);
50 $sqlValues[] =
'('.\implode(
', ', $data).
')';
54 $tableName = $sqlHelper->quote($tableName);
55 $sql =
"INSERT INTO {$tableName} (".\implode(
', ', $columns).
") VALUES ".\implode(
', ', $sqlValues);
57 $checkPrimary =
false;
60 if (!\is_array($primary))
62 $primary = array($primary);
64 if (\count(\array_intersect($primary, $columns0)) > 0)
72 foreach (\array_diff($columns0, $primary) as $columnName)
74 $sqlUpdate[] =
"{$columns[$columnName]} = VALUES({$columns[$columnName]})";
76 $sql .=
" ON DUPLICATE KEY UPDATE ".\implode(
', ', $sqlUpdate);
79 $connection->queryExecute($sql);
90 public static function bulkUpdate(array $fields, array $filter = []):
void
96 $tableName = static::getTableName();
97 $connection = Main\Application::getConnection();
98 $helper = $connection->getSqlHelper();
100 $update = $helper->prepareUpdate($tableName, $fields);
101 $valuesSql = $update[0];
103 if (!empty($valuesSql))
105 $tableName = $helper->quote($tableName);
108 $hasSubQuery =
false;
109 foreach ($filter as $field => $value)
111 if (\mb_strpos($field,
'.') !==
false)
119 $whereSql = (static::query())
120 ->setSelect([
'ID' =>
'ID'])
124 $querySql =
"UPDATE {$tableName} target INNER JOIN ({$whereSql}) source ON target.ID = source.ID SET {$valuesSql} ";
128 $whereSql = Main\ORM\Query\Query::buildFilterSql(static::getEntity(), $filter);
129 $querySql =
"UPDATE {$tableName} SET {$valuesSql} WHERE {$whereSql}";
134 $querySql =
"UPDATE {$tableName} SET {$valuesSql}";
137 $connection->queryExecute($querySql);
148 public static function bulkDelete(array $filter = []):
void
150 $connection = Main\Application::getConnection();
151 $tableName = $connection->getSqlHelper()->quote(static::getTableName());
155 $hasSubQuery =
false;
156 foreach ($filter as $field => $value)
158 if (\mb_strpos($field,
'.') !==
false)
166 $whereSql = (static::query())
167 ->setSelect([
'ID' =>
'ID'])
171 $querySql =
"DELETE target FROM {$tableName} target INNER JOIN ({$whereSql}) source ON target.ID = source.ID";
175 $whereSql = Main\ORM\Query\Query::buildFilterSql(static::getEntity(), $filter);
176 $querySql =
"DELETE FROM {$tableName} WHERE {$whereSql}";
181 $querySql =
"TRUNCATE TABLE {$tableName}";
184 $connection->queryExecute($querySql);
196 private static function prepareWhere(array $filterFields, array $filterAlias = []):
string
198 $sqlHelper = Main\Application::getConnection()->getSqlHelper();
202 foreach ($filterFields as $key => $val)
204 if ($key ===
'LOGIC')
210 if (!\is_numeric($key))
212 if (\preg_match(
"/^([=<>!@%]+)([^=<>!@%]+)$/", $key, $parts))
214 [, $operator, $key] = $parts;
216 if (\is_array($val) && !isset($val[
'LOGIC']))
218 if ($operator ===
'=')
222 elseif ($operator ===
'!')
227 if (isset($filterAlias[$key]))
229 $key = $filterAlias[$key];
235 $where[] =
"$key != '". $sqlHelper->forSql($val).
"'";
239 $where[] =
"$key LIKE '%". $sqlHelper->forSql($val).
"%'";
243 $where[] =
"$key NOT LIKE '%". $sqlHelper->forSql($val).
"%'";
248 if (\is_array($val) && \count($val) > 0)
250 $val = \array_map(array($sqlHelper,
'forSql'), $val);
251 $where[] =
"$key IN('".\implode(
"', '", $val).
"')";
253 elseif (\is_string($val) && $val <>
'')
255 $where[] =
"$key IN(".$val.
')';
262 if (\is_array($val) && \count($val) > 0)
264 $val = \array_map(array($sqlHelper,
'forSql'), $val);
265 $where[] =
"$key NOT IN('".\implode(
"', '", $val).
"')";
267 elseif (\is_string($val) && $val <>
'')
269 $where[] =
"$key NOT IN(".$val.
')';
279 if (isset($val[
'LOGIC']) && $val[
'LOGIC'] ===
'OR')
282 unset($val[
'LOGIC']);
285 $condition = array();
286 foreach ($val as $k => $v)
289 if (\preg_match(
"/^([=<>!@%]+)([^=<>!@%]+)$/", $k, $parts))
291 [, $subOperator, $k] = $parts;
293 if (isset($filterAlias[$k]))
295 $k = $filterAlias[$k];
297 switch ($subOperator)
300 $condition[] =
"$k != '".$sqlHelper->forSql($v).
"'";
303 $condition[] =
"$k LIKE '%".$sqlHelper->forSql($v).
"%'";
306 $condition[] =
"$k NOT LIKE '%".$sqlHelper->forSql($v).
"%'";
309 $condition[] =
"$k $subOperator '".$sqlHelper->forSql($v).
"'";
312 $where[] =
'('.\implode(
" $subLogic ", $condition).
')';
316 $where[] =
"$key $operator '".$sqlHelper->forSql($val).
"'";
326 $whereSql =
' AND '. \implode(
" $logic ", $where);