Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
mysqlconnection.php
1<?php
2namespace Bitrix\Main\DB;
3
5
7{
8 /**********************************************************
9 * SqlHelper
10 **********************************************************/
11
15 protected function createSqlHelper()
16 {
17 return new MysqlSqlHelper($this);
18 }
19
20 /***********************************************************
21 * Connection and disconnection
22 ***********************************************************/
23
32 protected function connectInternal()
33 {
34 if($this->isConnected)
35 {
36 return;
37 }
38
39 if(($this->options & self::PERSISTENT) != 0)
40 {
41 $connection = mysql_pconnect($this->host, $this->login, $this->password);
42 }
43 else
44 {
45 $connection = mysql_connect($this->host, $this->login, $this->password, true);
46 }
47
48 if(!$connection)
49 {
50 throw new ConnectionException('Mysql connect error ['.$this->host.', '.gethostbyname($this->host).']', mysql_error());
51 }
52
53 if($this->database !== null)
54 {
55 if(!mysql_select_db($this->database, $connection))
56 {
57 throw new ConnectionException('Mysql select db error ['.$this->database.']', mysql_error($connection));
58 }
59 }
60
61 $this->resource = $connection;
62 $this->isConnected = true;
63
64 $this->afterConnected();
65 }
66
73 public function disconnectInternal()
74 {
75 if (!$this->isConnected)
76 return;
77
78 mysql_close($this->resource);
79
80 $this->isConnected = false;
81 }
82
83 /*********************************************************
84 * Query
85 *********************************************************/
86
90 protected function queryInternal($sql, array $binds = null, \Bitrix\Main\Diag\SqlTrackerQuery $trackerQuery = null)
91 {
92 $this->connectInternal();
93
94 if ($trackerQuery != null)
95 $trackerQuery->startQuery($sql, $binds);
96
97 $result = mysql_query($sql, $this->resource);
98
99 if ($trackerQuery != null)
100 $trackerQuery->finishQuery();
101
102 $this->lastQueryResult = $result;
103
104 if (!$result)
105 throw new SqlQueryException('Mysql query error', mysql_error($this->resource), $sql);
106
107 return $result;
108 }
109
113 protected function createResult($result, \Bitrix\Main\Diag\SqlTrackerQuery $trackerQuery = null)
114 {
115 return new MysqlResult($result, $this, $trackerQuery);
116 }
117
121 public function getInsertedId()
122 {
123 $this->connectInternal();
124 return mysql_insert_id($this->resource);
125 }
126
130 public function getAffectedRowsCount()
131 {
132 return mysql_affected_rows($this->getResource());
133 }
134
135 /*********************************************************
136 * Type, version, cache, etc.
137 *********************************************************/
138
142 public function getVersion()
143 {
144 if ($this->version == null)
145 {
146 $version = $this->queryScalar("SELECT VERSION()");
147 if ($version != null)
148 {
149 $version = trim($version);
150 preg_match("#[0-9]+\\.[0-9]+\\.[0-9]+#", $version, $ar);
151 $this->version = $ar[0];
152 }
153 }
154
155 return array($this->version, null);
156 }
157
161 protected function getErrorMessage()
162 {
163 return sprintf("[%s] %s", mysql_errno($this->resource), mysql_error($this->resource));
164 }
165
172 public function selectDatabase($database)
173 {
174 return mysql_select_db($database, $this->resource);
175 }
176}
createResult($result, \Bitrix\Main\Diag\SqlTrackerQuery $trackerQuery=null)
queryInternal($sql, array $binds=null, \Bitrix\Main\Diag\SqlTrackerQuery $trackerQuery=null)