Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
commonhelper.php
1<?php
15
16use Bitrix\Main;
17
18abstract class CommonHelper
19{
20 public static function getSqlForDataType($type, $len = 0)
21 {
22 if($type == 'int')
23 return 'int';
24
25 if($type == 'varchar')
26 return 'varchar('.(intval($len) ? intval($len) : '1').')';
27
28 if($type == 'char')
29 return 'char('.(intval($len) ? intval($len) : '1').')';
30
31 return '';
32 }
33
34 public static function getBatchInsertHead($tableName, $fields = array())
35 {
36 $map = array();
37
38 $dbHelper = Main\HttpApplication::getConnection()->getSqlHelper();
39
40 if(is_array($fields))
41 {
42 foreach($fields as $fld)
43 $map[] = $dbHelper->forSql($fld);
44 }
45
46 return 'INSERT IGNORE INTO '.$dbHelper->forSql($tableName).' ('.implode(',', $map).') values ';
47 }
48
49 public static function getBatchInsertTail()
50 {
51 return '';
52 }
53
54 public static function getBatchInsertSeparator()
55 {
56 return ', ';
57 }
58
59 public static function getBatchInsertValues($row, $tableName, $fields, $map)
60 {
61 return static::prepareSql($row, $fields, $map);
62 }
63
64 public static function getMaxTransferUnit()
65 {
66 return PHP_INT_MAX;
67 }
68
69 protected static function prepareSql($row, $fields, $map)
70 {
71 if (!is_array($row) || empty($row) || !is_array($fields) || empty($fields) || !is_array($map) || empty($map))
72 {
73 return '';
74 }
75
76 $connection = Main\HttpApplication::getConnection();
77 $sqlHelper = $connection->getSqlHelper();
78 unset($connection);
79
80 $sql = [];
81 foreach ($fields as $fld => $none)
82 {
83 $val = $row[$fld];
84
85 // only numeric and literal fields supported at the moment
86 if (
87 isset($map[$fld]['data_type'])
88 && $map[$fld]['data_type'] == 'integer'
89 )
90 {
91 $sql[] = (int)$val;
92 }
93 else
94 {
95 $sql[] = "'" . $sqlHelper->forSql($val) . "'";
96 }
97 }
98
99 return '(' . implode(',', $sql) . ')';
100 }
101
102 // makes sense only for mssql
103 public static function dropAutoIncrementRestrictions($tableName)
104 {
105 return false;
106 }
107
108 // same
109 public static function restoreAutoIncrementRestrictions($tableName)
110 {
111 return false;
112 }
113
114 // makes sense only for oracle
115 public static function incrementSequenceForTable($tableName)
116 {
117 return false;
118 }
119
120 // makes sense only for oracle
121 protected static function checkSequenceExistsForTable($tableName)
122 {
123 return false;
124 }
125
126 /*
127 public static function addPrimaryKey($tableName, $columns = array())
128 {
129 if(!strlen($tableName) || !is_array($columns) || empty($columns))
130 return false;
131
132 $dbConnection = Main\HttpApplication::getConnection();
133 $dbHelper = $dbConnection->getSqlHelper();
134
135 $tableName = $dbHelper->forSql($tableName);
136 $columns = static::escapeArray($columns);
137
138 $dbConnection->query("ALTER TABLE ".$tableName." ADD CONSTRAINT PK_".ToUpper($tableName)." PRIMARY KEY (".implode(', ', $columns).")");
139
140 return true;
141 }
142 */
143
144 // do nothing but for oracle
145 public static function addAutoIncrement()
146 {
147 return false;
148 }
149
150 public static function createIndex($tableName, $ixNamePostfix, $columns = array(), $unique = false)
151 {
152 if(!mb_strlen($tableName) || !mb_strlen($ixNamePostfix) || !is_array($columns) || empty($columns))
153 return false;
154
155 $dbConnection = Main\HttpApplication::getConnection();
156 $dbHelper = $dbConnection->getSqlHelper();
157
158 $tableName = $dbHelper->forSql($tableName);
159 $ixNamePostfix = $dbHelper->forSql($ixNamePostfix);
160 $columns = static::escapeArray($columns);
161
162 $ixName = static::getIndexName($tableName, $ixNamePostfix, $columns);
163
164 if(mb_strlen($ixName) > 30)
165 return false;
166
167 if(!static::checkIndexNameExists($ixName, $tableName))
168 {
169 $dbConnection->query("CREATE ".($unique ? "UNIQUE" : "")." INDEX ".$ixName." ON ".$tableName." (".implode(', ', $columns).")");
170 return true;
171 }
172
173 return false;
174 }
175
176 protected static function getIndexName($tableName, $ixNamePostfix, $columns = array())
177 {
178 return 'IX_'.preg_replace('#^B_#', '', ToUpper($tableName))."_".ToUpper($ixNamePostfix);
179 }
180
181 protected static function escapeArray($columns)
182 {
183 foreach($columns as &$col)
184 $col = Main\HttpApplication::getConnection()->getSqlHelper()->forSql($col);
185
186 return $columns;
187 }
188
189 public static function dropTable($tableName)
190 {
191 $dbConnection = Main\HttpApplication::getConnection();
192
193 $tableName = $dbConnection->getSqlHelper()->forSql($tableName);
194
195 if($dbConnection->isTableExists($tableName))
196 Main\HttpApplication::getConnection()->query('drop table '.$tableName);
197 }
198
199 public static function checkTableExists($tableName)
200 {
201 return Main\HttpApplication::getConnection()->isTableExists($tableName);
202 }
203
204 public static function truncateTable($tableName)
205 {
206 $dbConnection = Main\HttpApplication::getConnection();
207
208 $tableName = $dbConnection->getSqlHelper()->forSql($tableName);
209
210 if($dbConnection->isTableExists($tableName))
211 Main\HttpApplication::getConnection()->query('truncate table '.$tableName);
212 }
213
214 public static function getQuerySeparatorSql()
215 {
216 }
217
219 {
220 return true;
221 }
222}
static prepareSql($row, $fields, $map)
static getSqlForDataType($type, $len=0)
static getBatchInsertHead($tableName, $fields=array())
static incrementSequenceForTable($tableName)
static checkSequenceExistsForTable($tableName)
static getBatchInsertValues($row, $tableName, $fields, $map)
static restoreAutoIncrementRestrictions($tableName)
static createIndex($tableName, $ixNamePostfix, $columns=array(), $unique=false)
static dropAutoIncrementRestrictions($tableName)
static getIndexName($tableName, $ixNamePostfix, $columns=array())