1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
mssqlconnection.php
См. документацию.
1<?php
2
3namespace Bitrix\Main\DB;
4
5use Bitrix\Main\ArgumentException;
6use Bitrix\Main\ORM\Fields\ScalarField;
7
15{
16 /**********************************************************
17 * SqlHelper
18 **********************************************************/
19
20 protected function createSqlHelper()
21 {
22 return new MssqlSqlHelper($this);
23 }
24
25 /***********************************************************
26 * Connection and disconnection
27 ***********************************************************/
28
37 protected function connectInternal()
38 {
39 if ($this->isConnected)
40 {
41 return;
42 }
43
44 $connectionInfo = [
45 "UID" => $this->login,
46 "PWD" => $this->password,
47 "Database" => $this->database,
48 "ReturnDatesAsStrings" => true,
49 /*"CharacterSet" => "utf-8",*/
50 ];
51
52 if ($this->isPersistent())
53 {
54 $connectionInfo["ConnectionPooling"] = true;
55 }
56 else
57 {
58 $connectionInfo["ConnectionPooling"] = false;
59 }
60
61 $connection = sqlsrv_connect($this->host, $connectionInfo);
62
63 if (!$connection)
64 {
65 throw new ConnectionException('MS Sql connect error', $this->getErrorMessage());
66 }
67
68 $this->resource = $connection;
69 $this->isConnected = true;
70
71 // hide cautions
72 sqlsrv_configure("WarningsReturnAsErrors", 0);
73
74 $this->afterConnected();
75 }
76
83 protected function disconnectInternal()
84 {
85 if (!$this->isConnected)
86 {
87 return;
88 }
89
90 $this->isConnected = false;
91 sqlsrv_close($this->resource);
92 }
93
94 /*********************************************************
95 * Query
96 *********************************************************/
97
101 protected function queryInternal($sql, array $binds = null, \Bitrix\Main\Diag\SqlTrackerQuery $trackerQuery = null)
102 {
103 $this->connectInternal();
104
105 $trackerQuery?->startQuery($sql, $binds);
106
107 $result = sqlsrv_query($this->resource, $sql, [], ["Scrollable" => 'forward']);
108
109 $trackerQuery?->finishQuery();
110
111 $this->lastQueryResult = $result;
112
113 if (!$result)
114 {
115 throw new SqlQueryException('MS Sql query error', $this->getErrorMessage(), $sql);
116 }
117
118 return $result;
119 }
120
124 protected function createResult($result, \Bitrix\Main\Diag\SqlTrackerQuery $trackerQuery = null)
125 {
126 return new MssqlResult($result, $this, $trackerQuery);
127 }
128
132 public function getInsertedId()
133 {
134 return $this->queryScalar("SELECT @@IDENTITY as ID");
135 }
136
140 public function getAffectedRowsCount()
141 {
142 return sqlsrv_rows_affected($this->lastQueryResult);
143 }
144
148 public function isTableExists($tableName)
149 {
150 $tableName = preg_replace("/[^A-Za-z0-9%_]+/i", "", $tableName);
151 $tableName = trim($tableName);
152
153 if ($tableName == '')
154 {
155 return false;
156 }
157
158 $result = $this->queryScalar(
159 "SELECT COUNT(TABLE_NAME) " .
160 "FROM INFORMATION_SCHEMA.TABLES " .
161 "WHERE TABLE_NAME LIKE '" . $this->getSqlHelper()->forSql($tableName) . "'"
162 );
163 return ($result > 0);
164 }
165
169 public function isIndexExists($tableName, array $columns)
170 {
171 return $this->getIndexName($tableName, $columns) !== null;
172 }
173
177 public function getIndexName($tableName, array $columns, $strict = false)
178 {
179 if (empty($columns))
180 {
181 return null;
182 }
183
184 //2005
185 //$rs = $this->query("SELECT index_id, COL_NAME(object_id, column_id) AS column_name, key_ordinal FROM SYS.INDEX_COLUMNS WHERE object_id=OBJECT_ID('".$this->forSql($tableName)."')", true);
186
187 //2000
188 $rs = $this->query(
189 "SELECT s.indid as index_id, s.keyno as key_ordinal, c.name column_name, si.name index_name " .
190 "FROM sysindexkeys s " .
191 " INNER JOIN syscolumns c ON s.id = c.id AND s.colid = c.colid " .
192 " INNER JOIN sysobjects o ON s.id = o.Id AND o.xtype = 'U' " .
193 " LEFT JOIN sysindexes si ON si.indid = s.indid AND si.id = s.id " .
194 "WHERE o.name = UPPER('" . $this->getSqlHelper()->forSql($tableName) . "')");
195
196 $indexes = [];
197 while ($ar = $rs->fetch())
198 {
199 $indexes[$ar["index_name"]][$ar["key_ordinal"] - 1] = $ar["column_name"];
200 }
201
202 return static::findIndex($indexes, $columns, $strict);
203 }
204
208 public function getTableFields($tableName)
209 {
210 if (!isset($this->tableColumnsCache[$tableName]))
211 {
212 $this->connectInternal();
213
214 $query = $this->queryInternal("SELECT TOP 0 * FROM " . $this->getSqlHelper()->quote($tableName));
215
216 $result = $this->createResult($query);
217
218 $this->tableColumnsCache[$tableName] = $result->getFields();
219 }
220 return $this->tableColumnsCache[$tableName];
221 }
222
226 public function createTable($tableName, $fields, $primary = [], $autoincrement = [])
227 {
228 $sql = 'CREATE TABLE ' . $this->getSqlHelper()->quote($tableName) . ' (';
229 $sqlFields = [];
230
231 foreach ($fields as $columnName => $field)
232 {
233 if (!($field instanceof ScalarField))
234 {
235 throw new ArgumentException(sprintf(
236 'Field `%s` should be an Entity\ScalarField instance', $columnName
237 ));
238 }
239
240 $realColumnName = $field->getColumnName();
241
242 $sqlFields[] = $this->getSqlHelper()->quote($realColumnName)
243 . ' ' . $this->getSqlHelper()->getColumnTypeByField($field)
244 . ' NOT NULL'
245 . (in_array($columnName, $autoincrement, true) ? ' IDENTITY (1, 1)' : '');
246 }
247
248 $sql .= join(', ', $sqlFields);
249
250 if (!empty($primary))
251 {
252 foreach ($primary as &$primaryColumn)
253 {
254 $realColumnName = $fields[$primaryColumn]->getColumnName();
255 $primaryColumn = $this->getSqlHelper()->quote($realColumnName);
256 }
257
258 $sql .= ', PRIMARY KEY(' . join(', ', $primary) . ')';
259 }
260
261 $sql .= ')';
262
263 $this->query($sql);
264 }
265
269 public function renameTable($currentName, $newName)
270 {
271 $this->query('EXEC sp_rename ' . $this->getSqlHelper()->quote($currentName) . ', ' . $this->getSqlHelper()->quote($newName));
272 }
273
277 public function dropTable($tableName)
278 {
279 $this->query('DROP TABLE ' . $this->getSqlHelper()->quote($tableName));
280 }
281
282 /*********************************************************
283 * Transaction
284 *********************************************************/
285
289 public function startTransaction()
290 {
291 $this->connectInternal();
292 sqlsrv_begin_transaction($this->resource);
293 }
294
298 public function commitTransaction()
299 {
300 $this->connectInternal();
301 sqlsrv_commit($this->resource);
302 }
303
307 public function rollbackTransaction()
308 {
309 $this->connectInternal();
310 sqlsrv_rollback($this->resource);
311 }
312
313 /*********************************************************
314 * Type, version, cache, etc.
315 *********************************************************/
316
320 public function getType()
321 {
322 return "mssql";
323 }
324
328 public function getVersion()
329 {
330 if ($this->version == null)
331 {
332 $version = $this->queryScalar("SELECT @@VERSION");
333 if ($version != null)
334 {
335 $version = trim($version);
336 $this->versionExpress = (mb_strpos($version, "Express Edition") > 0);
337 preg_match("#[0-9]+\\.[0-9]+\\.[0-9]+#", $version, $arr);
338 $this->version = $arr[0];
339 }
340 }
341
342 return [$this->version, $this->versionExpress];
343 }
344
348 public function getErrorMessage()
349 {
350 $errors = "";
351 foreach (sqlsrv_errors(SQLSRV_ERR_ERRORS) as $error)
352 {
353 $errors .= "SQLSTATE: " . $error['SQLSTATE'] . ";" . " code: " . $error['code'] . "; message: " . $error['message'] . "\n";
354 }
355 return $errors;
356 }
357}
$connection
Определения actionsdefinitions.php:38
xml version
Определения yandex.php:67
$login
Определения change_password.php:8
isTableExists($tableName)
Определения mssqlconnection.php:148
dropTable($tableName)
Определения mssqlconnection.php:277
getIndexName($tableName, array $columns, $strict=false)
Определения mssqlconnection.php:177
getTableFields($tableName)
Определения mssqlconnection.php:208
renameTable($currentName, $newName)
Определения mssqlconnection.php:269
isIndexExists($tableName, array $columns)
Определения mssqlconnection.php:169
createResult($result, \Bitrix\Main\Diag\SqlTrackerQuery $trackerQuery=null)
Определения mssqlconnection.php:124
queryInternal($sql, array $binds=null, \Bitrix\Main\Diag\SqlTrackerQuery $trackerQuery=null)
Определения mssqlconnection.php:101
createTable($tableName, $fields, $primary=[], $autoincrement=[])
Определения mssqlconnection.php:226
$arr
Определения file_new.php:624
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$query
Определения get_search.php:11
$errors
Определения iblock_catalog_edit.php:74
Определения cachetracker.php:2
$password
Определения mysql_to_pgsql.php:34
$ar
Определения options.php:199
$error
Определения subscription_card_product.php:20
$rs
Определения action.php:82
$fields
Определения yandex_run.php:501