Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
IndexTableTrait.php
1<?php
2
3namespace Bitrix\Im\V2\Common;
4
10
11trait IndexTableTrait
12{
14
15 private static bool $isAlreadyPlanned = false;
16
17 public static function indexInBackground(): void
18 {
19 if (self::$isAlreadyPlanned)
20 {
21 return;
22 }
23
24 self::$isAlreadyPlanned = true;
25
26 Application::getInstance()->addBackgroundJob(fn () => self::runIndex());
27 }
28
29 public static function prepareSearchString(string $searchString): string
30 {
31 $searchString = Content::prepareStringToken($searchString);
32 $searchString = Helper::matchAgainstWildcard($searchString);
33
34 return $searchString;
35 }
36
37 public static function deleteByFilter(array $filter): void
38 {
39 $connection = Application::getConnection();
40 $sqlHelper = $connection->getSqlHelper();
41 $indexTable = $sqlHelper->quote(static::getTableName());
42 $baseTable = $sqlHelper->quote(static::getBaseDataClass()::getTableName());
43 $indexTablePrimary = $sqlHelper->quote(static::getEntity()->getPrimary());
44 $baseTablePrimary = $sqlHelper->quote(static::getBaseDataClass()::getEntity()->getPrimary());
45 $whereStatement = Query::buildFilterSql(static::getBaseDataClass()::getEntity(), $filter);
46
47 if ($connection->getType() == 'mysql')
48 {
49 $sql = "
50 DELETE {$indexTable}
51 FROM {$indexTable}
52 INNER JOIN {$baseTable}
53 ON {$indexTable}.{$indexTablePrimary} = {$baseTable}.{$baseTablePrimary}
54 WHERE {$whereStatement};
55 ";
56 }
57 elseif ($connection->getType() == 'pgsql')
58 {
59 $sql = "
60 DELETE FROM {$indexTable}
61 USING {$baseTable}
62 WHERE
63 {$indexTable}.{$indexTablePrimary} = {$baseTable}.{$baseTablePrimary}
64 AND {$whereStatement}
65 ";
66 }
67
68 $connection->queryExecute($sql);
69 }
70
71 public static function updateIndexStatus(array $ids, bool $status = true): void
72 {
73 if (empty($ids))
74 {
75 return;
76 }
77
78 $connection = Application::getConnection();
79 $sqlHelper = $connection->getSqlHelper();
80 $baseTable = $sqlHelper->quote(static::getBaseDataClass()::getTableName());
81 $baseTablePrimary = $sqlHelper->quote(static::getBaseDataClass()::getEntity()->getPrimary());
82 $implodeIds = implode(',', $ids);
83 $statusString = $status ? 'Y' : 'N';
84
85 $sql = "
86 UPDATE {$baseTable}
87 SET IS_INDEXED = '{$statusString}'
88 WHERE {$baseTablePrimary} IN ({$implodeIds});
89 ";
90
91 $connection->queryExecute($sql);
92 }
93
94 private static function runIndex(): void
95 {
96 self::index();
97 self::$isAlreadyPlanned = false;
98 }
99
103 abstract protected static function getBaseDataClass(): string;
104
105 abstract public static function index(): void;
106}
static getConnection($name="")