1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
helper.php
См. документацию.
1<?php
13
14namespace Bitrix\Sale\Location\DB;
15
16use Bitrix\Main;
17
18final class Helper extends CommonHelper
19{
20 public static function getSqlForAutoIncrement()
21 {
23 if ($dbConnection->getType() === 'pgsql')
24 {
25 return 'generated by default as identity';
26 }
27
28 return 'auto_increment';
29 }
30
31 public static function mergeTables($toTable, $fromTable, $fldMap, $fldCondition)
32 {
34 $dbHelper = $dbConnection->getSqlHelper();
35
36 $toTable = $dbHelper->forSql(trim($toTable));
37 $fromTable = $dbHelper->forSql(trim($fromTable));
38
39 if(!mb_strlen($toTable) || !mb_strlen($fromTable) || !is_array($fldMap) || empty($fldMap) || empty($fldCondition))
40 return false;
41
42 // update tab1, tab2 set tab1.aa = tab2.bb, tab1.cc = tab2.dd where tab1.ee = tab2.ff
43
44 if ($dbConnection->getType() === 'pgsql')
45 {
46 $toFields = [];
47 $fromFields = [];
48 $where = [];
49 foreach ($fldMap as $toFld => $fromFld)
50 {
51 $toFields[] = $dbHelper->forSql(trim($toFld));
52 $fromFields[] = $dbHelper->forSql(trim($fromFld));
53 }
54 foreach ($fldCondition as $left => $right)
55 {
56 $where[] = $toTable.'.'.$dbHelper->forSql(trim($left)).' = '.$fromTable.'.'.$dbHelper->forSql(trim($right));
57 }
58 $sql = 'update '.$toTable.' set ('.
59 implode(', ', $toFields).
60 ') = (select '.
61 implode(', ', $fromFields).
62 ' from '.$fromTable.' where '.implode(' and ', $where).')';
63 }
64 else
65 {
66 $sql = 'update '.$toTable.', '.$fromTable.' set ';
67
68 $fields = array();
69 foreach($fldMap as $toFld => $fromFld)
70 $fields[] = $toTable.'.'.$dbHelper->forSql(trim($toFld)).' = '.$fromTable.'.'.$dbHelper->forSql(trim($fromFld));
71
72 $sql .= implode(', ', $fields);
73
74 $where = array();
75 foreach($fldCondition as $left => $right)
76 $where[] = $toTable.'.'.$dbHelper->forSql(trim($left)).' = '.$fromTable.'.'.$dbHelper->forSql(trim($right));
77
78 $sql .= ' where '.implode(' and ', $where);
79 }
80
81 $dbConnection->query($sql);
82
83 return true;
84 }
85
86 public static function checkIndexNameExists($indexName, $tableName)
87 {
89 $dbHelper = $dbConnection->getSqlHelper();
90
91 $indexName = trim((string)$indexName);
92 $tableName = $dbHelper->forSql(trim((string)$tableName));
93
94 if ($indexName === '' || $tableName === '')
95 {
96 return false;
97 }
98
99 if ($dbConnection->getType() === 'pgsql')
100 {
101 $res = $dbConnection->query("SELECT INDEXNAME FROM PG_INDEXES WHERE TABLENAME = '$tableName'");
102
103 while($item = $res->fetch())
104 {
105 if (isset($item['INDEXNAME']) && $item['INDEXNAME'] === $indexName)
106 {
107 return true;
108 }
109 }
110 }
111 else
112 {
113 $res = $dbConnection->query("show index from ".$tableName);
114
115 while($item = $res->fetch())
116 {
117 if (isset($item['Key_name']) && $item['Key_name'] === $indexName)
118 {
119 return true;
120 }
121 if (isset($item['KEY_NAME']) && $item['KEY_NAME'] === $indexName)
122 {
123 return true;
124 }
125 }
126 }
127
128 return false;
129 }
130
131 public static function dropIndexByName($indexName, $tableName)
132 {
133 $dbConnection = Main\HttpApplication::getConnection();
134 $dbHelper = $dbConnection->getSqlHelper();
135
136 $indexName = $dbHelper->forSql(trim($indexName));
137 $tableName = $dbHelper->forSql(trim($tableName));
138
139 if(!mb_strlen($indexName) || !mb_strlen($tableName))
140 return false;
141
142 if(!static::checkIndexNameExists($indexName, $tableName))
143 return false;
144
145 $dbConnection->query("alter table {$tableName} drop index {$indexName}");
146
147 return true;
148 }
149
150 public static function getMaxTransferUnit()
151 {
152 $dbConnection = Main\HttpApplication::getConnection();
153 if ($dbConnection->getType() === 'pgsql')
154 {
155 return parent::getMaxTransferUnit();
156 }
157
158 $res = $dbConnection->query("SHOW VARIABLES LIKE 'max_allowed_packet'")->fetch();
159 if(!($res['Variable_name'] == 'max_allowed_packet' && $mtu = intval($res['Value'])))
160 return 0;
161
162 return $mtu;
163 }
164
165 // this function is used to adjust auto_increment value of a table to a certain position
166 public static function resetAutoIncrement($tableName, $startIndex = 1)
167 {
168 $startIndex = intval($startIndex);
169 if($startIndex <= 0 || !mb_strlen($tableName))
170 return false;
171
172 $dbConnection = Main\HttpApplication::getConnection();
173 $dbHelper = $dbConnection->getSqlHelper();
174
175 $tableName = $dbHelper->forSql(trim($tableName));
176
177 if ($dbConnection->getType() === 'pgsql')
178 {
179 $dbConnection->query("alter sequence {$tableName}_id_seq RESTART WITH $startIndex");
180 }
181 else
182 {
183 $dbConnection->query('alter table '.$tableName.' AUTO_INCREMENT = '.$startIndex);
184 }
185
186 return true;
187 }
188
189 public static function getQuerySeparatorSql()
190 {
191 return ";";
192 }
193}
static getConnection($name="")
Определения application.php:638
static resetAutoIncrement($tableName, $startIndex=1)
Определения helper.php:166
static dropIndexByName($indexName, $tableName)
Определения helper.php:131
static getSqlForAutoIncrement()
Определения helper.php:20
static getMaxTransferUnit()
Определения helper.php:150
static getQuerySeparatorSql()
Определения helper.php:189
static mergeTables($toTable, $fromTable, $fldMap, $fldCondition)
Определения helper.php:31
static checkIndexNameExists($indexName, $tableName)
Определения helper.php:86
$right
Определения options.php:8
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$fields
Определения yandex_run.php:501