Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
helper.php
1<?php
15
16use Bitrix\Main;
17
18final class Helper extends CommonHelper
19{
20 public static function getSqlForAutoIncrement()
21 {
22 return 'auto_increment';
23 }
24
25 public static function mergeTables($toTable, $fromTable, $fldMap, $fldCondition)
26 {
27 $dbConnection = Main\HttpApplication::getConnection();
28 $dbHelper = $dbConnection->getSqlHelper();
29
30 $toTable = $dbHelper->forSql(trim($toTable));
31 $fromTable = $dbHelper->forSql(trim($fromTable));
32
33 if(!mb_strlen($toTable) || !mb_strlen($fromTable) || !is_array($fldMap) || empty($fldMap) || empty($fldCondition))
34 return false;
35
36 // update tab1, tab2 set tab1.aa = tab2.bb, tab1.cc = tab2.dd where tab1.ee = tab2.ff
37
38 $sql = 'update '.$toTable.', '.$fromTable.' set ';
39
40 $fields = array();
41 foreach($fldMap as $toFld => $fromFld)
42 $fields[] = $toTable.'.'.$dbHelper->forSql(trim($toFld)).' = '.$fromTable.'.'.$dbHelper->forSql(trim($fromFld));
43
44 $sql .= implode(', ', $fields);
45
46 $where = array();
47 foreach($fldCondition as $left => $right)
48 $where[] = $toTable.'.'.$dbHelper->forSql(trim($left)).' = '.$fromTable.'.'.$dbHelper->forSql(trim($right));
49
50 $sql .= ' where '.implode(' and ', $where);
51
52 $dbConnection->query($sql);
53
54 return true;
55 }
56
57 public static function checkIndexNameExists($indexName, $tableName)
58 {
59 $dbConnection = Main\HttpApplication::getConnection();
60 $dbHelper = $dbConnection->getSqlHelper();
61
62 $indexName = trim((string)$indexName);
63 $tableName = $dbHelper->forSql(trim((string)$tableName));
64
65 if ($indexName === '' || $tableName === '')
66 {
67 return false;
68 }
69
70 $res = $dbConnection->query("show index from ".$tableName);
71
72 while($item = $res->fetch())
73 {
74 if (isset($item['Key_name']) && $item['Key_name'] === $indexName)
75 {
76 return true;
77 }
78 if (isset($item['KEY_NAME']) && $item['KEY_NAME'] === $indexName)
79 {
80 return true;
81 }
82 }
83
84 return false;
85 }
86
87 public static function dropIndexByName($indexName, $tableName)
88 {
89 $dbConnection = Main\HttpApplication::getConnection();
90 $dbHelper = $dbConnection->getSqlHelper();
91
92 $indexName = $dbHelper->forSql(trim($indexName));
93 $tableName = $dbHelper->forSql(trim($tableName));
94
95 if(!mb_strlen($indexName) || !mb_strlen($tableName))
96 return false;
97
98 if(!static::checkIndexNameExists($indexName, $tableName))
99 return false;
100
101 $dbConnection->query("alter table {$tableName} drop index {$indexName}");
102
103 return true;
104 }
105
106 public static function getMaxTransferUnit()
107 {
108 $dbConnection = Main\HttpApplication::getConnection();
109
110 $res = $dbConnection->query("SHOW VARIABLES LIKE 'max_allowed_packet'")->fetch();
111 if(!($res['Variable_name'] == 'max_allowed_packet' && $mtu = intval($res['Value'])))
112 return 0;
113
114 return $mtu;
115 }
116
117 // this function is used to adjust auto_increment value of a table to a certain position
118 public static function resetAutoIncrement($tableName, $startIndex = 1)
119 {
120 $startIndex = intval($startIndex);
121 if($startIndex <= 0 || !mb_strlen($tableName))
122 return false;
123
124 $dbConnection = Main\HttpApplication::getConnection();
125 $dbHelper = $dbConnection->getSqlHelper();
126
127 $tableName = $dbHelper->forSql(trim($tableName));
128
129 $dbConnection->query('alter table '.$tableName.' AUTO_INCREMENT = '.$startIndex);
130
131 return true;
132 }
133
134 public static function getQuerySeparatorSql()
135 {
136 return ";";
137 }
138}
static resetAutoIncrement($tableName, $startIndex=1)
Definition helper.php:118
static dropIndexByName($indexName, $tableName)
Definition helper.php:87
static mergeTables($toTable, $fromTable, $fldMap, $fldCondition)
Definition helper.php:25
static checkIndexNameExists($indexName, $tableName)
Definition helper.php:57