PageRenderTime 61ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/db/DatabaseMysqli.php

https://gitlab.com/link233/bootmw
PHP | 332 lines | 161 code | 46 blank | 125 comment | 22 complexity | ec78f71da9320fa7495d44d938f96181 MD5 | raw file
  1. <?php
  2. /**
  3. * This is the MySQLi database abstraction layer.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Database
  22. */
  23. /**
  24. * Database abstraction object for PHP extension mysqli.
  25. *
  26. * @ingroup Database
  27. * @since 1.22
  28. * @see Database
  29. */
  30. class DatabaseMysqli extends DatabaseMysqlBase {
  31. /** @var mysqli */
  32. protected $mConn;
  33. /**
  34. * @param string $sql
  35. * @return resource
  36. */
  37. protected function doQuery( $sql ) {
  38. $conn = $this->getBindingHandle();
  39. if ( $this->bufferResults() ) {
  40. $ret = $conn->query( $sql );
  41. } else {
  42. $ret = $conn->query( $sql, MYSQLI_USE_RESULT );
  43. }
  44. return $ret;
  45. }
  46. /**
  47. * @param string $realServer
  48. * @return bool|mysqli
  49. * @throws DBConnectionError
  50. */
  51. protected function mysqlConnect( $realServer ) {
  52. global $wgDBmysql5;
  53. # Avoid suppressed fatal error, which is very hard to track down
  54. if ( !function_exists( 'mysqli_init' ) ) {
  55. throw new DBConnectionError( $this, "MySQLi functions missing,"
  56. . " have you compiled PHP with the --with-mysqli option?\n" );
  57. }
  58. // Other than mysql_connect, mysqli_real_connect expects an explicit port
  59. // and socket parameters. So we need to parse the port and socket out of
  60. // $realServer
  61. $port = null;
  62. $socket = null;
  63. $hostAndPort = IP::splitHostAndPort( $realServer );
  64. if ( $hostAndPort ) {
  65. $realServer = $hostAndPort[0];
  66. if ( $hostAndPort[1] ) {
  67. $port = $hostAndPort[1];
  68. }
  69. } elseif ( substr_count( $realServer, ':' ) == 1 ) {
  70. // If we have a colon and something that's not a port number
  71. // inside the hostname, assume it's the socket location
  72. $hostAndSocket = explode( ':', $realServer );
  73. $realServer = $hostAndSocket[0];
  74. $socket = $hostAndSocket[1];
  75. }
  76. $connFlags = 0;
  77. if ( $this->mFlags & DBO_SSL ) {
  78. $connFlags |= MYSQLI_CLIENT_SSL;
  79. }
  80. if ( $this->mFlags & DBO_COMPRESS ) {
  81. $connFlags |= MYSQLI_CLIENT_COMPRESS;
  82. }
  83. if ( $this->mFlags & DBO_PERSISTENT ) {
  84. $realServer = 'p:' . $realServer;
  85. }
  86. $mysqli = mysqli_init();
  87. if ( $wgDBmysql5 ) {
  88. // Tell the server we're communicating with it in UTF-8.
  89. // This may engage various charset conversions.
  90. $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' );
  91. } else {
  92. $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' );
  93. }
  94. $mysqli->options( MYSQLI_OPT_CONNECT_TIMEOUT, 3 );
  95. if ( $mysqli->real_connect( $realServer, $this->mUser,
  96. $this->mPassword, $this->mDBname, $port, $socket, $connFlags )
  97. ) {
  98. return $mysqli;
  99. }
  100. return false;
  101. }
  102. protected function connectInitCharset() {
  103. // already done in mysqlConnect()
  104. return true;
  105. }
  106. /**
  107. * @param string $charset
  108. * @return bool
  109. */
  110. protected function mysqlSetCharset( $charset ) {
  111. $conn = $this->getBindingHandle();
  112. if ( method_exists( $conn, 'set_charset' ) ) {
  113. return $conn->set_charset( $charset );
  114. } else {
  115. return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
  116. }
  117. }
  118. /**
  119. * @return bool
  120. */
  121. protected function closeConnection() {
  122. $conn = $this->getBindingHandle();
  123. return $conn->close();
  124. }
  125. /**
  126. * @return int
  127. */
  128. function insertId() {
  129. $conn = $this->getBindingHandle();
  130. return (int)$conn->insert_id;
  131. }
  132. /**
  133. * @return int
  134. */
  135. function lastErrno() {
  136. if ( $this->mConn ) {
  137. return $this->mConn->errno;
  138. } else {
  139. return mysqli_connect_errno();
  140. }
  141. }
  142. /**
  143. * @return int
  144. */
  145. function affectedRows() {
  146. $conn = $this->getBindingHandle();
  147. return $conn->affected_rows;
  148. }
  149. /**
  150. * @param string $db
  151. * @return bool
  152. */
  153. function selectDB( $db ) {
  154. $conn = $this->getBindingHandle();
  155. $this->mDBname = $db;
  156. return $conn->select_db( $db );
  157. }
  158. /**
  159. * @param mysqli $res
  160. * @return bool
  161. */
  162. protected function mysqlFreeResult( $res ) {
  163. $res->free_result();
  164. return true;
  165. }
  166. /**
  167. * @param mysqli $res
  168. * @return bool
  169. */
  170. protected function mysqlFetchObject( $res ) {
  171. $object = $res->fetch_object();
  172. if ( $object === null ) {
  173. return false;
  174. }
  175. return $object;
  176. }
  177. /**
  178. * @param mysqli $res
  179. * @return bool
  180. */
  181. protected function mysqlFetchArray( $res ) {
  182. $array = $res->fetch_array();
  183. if ( $array === null ) {
  184. return false;
  185. }
  186. return $array;
  187. }
  188. /**
  189. * @param mysqli $res
  190. * @return mixed
  191. */
  192. protected function mysqlNumRows( $res ) {
  193. return $res->num_rows;
  194. }
  195. /**
  196. * @param mysqli $res
  197. * @return mixed
  198. */
  199. protected function mysqlNumFields( $res ) {
  200. return $res->field_count;
  201. }
  202. /**
  203. * @param mysqli $res
  204. * @param int $n
  205. * @return mixed
  206. */
  207. protected function mysqlFetchField( $res, $n ) {
  208. $field = $res->fetch_field_direct( $n );
  209. // Add missing properties to result (using flags property)
  210. // which will be part of function mysql-fetch-field for backward compatibility
  211. $field->not_null = $field->flags & MYSQLI_NOT_NULL_FLAG;
  212. $field->primary_key = $field->flags & MYSQLI_PRI_KEY_FLAG;
  213. $field->unique_key = $field->flags & MYSQLI_UNIQUE_KEY_FLAG;
  214. $field->multiple_key = $field->flags & MYSQLI_MULTIPLE_KEY_FLAG;
  215. $field->binary = $field->flags & MYSQLI_BINARY_FLAG;
  216. $field->numeric = $field->flags & MYSQLI_NUM_FLAG;
  217. $field->blob = $field->flags & MYSQLI_BLOB_FLAG;
  218. $field->unsigned = $field->flags & MYSQLI_UNSIGNED_FLAG;
  219. $field->zerofill = $field->flags & MYSQLI_ZEROFILL_FLAG;
  220. return $field;
  221. }
  222. /**
  223. * @param resource|ResultWrapper $res
  224. * @param int $n
  225. * @return mixed
  226. */
  227. protected function mysqlFieldName( $res, $n ) {
  228. $field = $res->fetch_field_direct( $n );
  229. return $field->name;
  230. }
  231. /**
  232. * @param resource|ResultWrapper $res
  233. * @param int $n
  234. * @return mixed
  235. */
  236. protected function mysqlFieldType( $res, $n ) {
  237. $field = $res->fetch_field_direct( $n );
  238. return $field->type;
  239. }
  240. /**
  241. * @param resource|ResultWrapper $res
  242. * @param int $row
  243. * @return mixed
  244. */
  245. protected function mysqlDataSeek( $res, $row ) {
  246. return $res->data_seek( $row );
  247. }
  248. /**
  249. * @param mysqli $conn Optional connection object
  250. * @return string
  251. */
  252. protected function mysqlError( $conn = null ) {
  253. if ( $conn === null ) {
  254. return mysqli_connect_error();
  255. } else {
  256. return $conn->error;
  257. }
  258. }
  259. /**
  260. * Escapes special characters in a string for use in an SQL statement
  261. * @param string $s
  262. * @return string
  263. */
  264. protected function mysqlRealEscapeString( $s ) {
  265. $conn = $this->getBindingHandle();
  266. return $conn->real_escape_string( $s );
  267. }
  268. protected function mysqlPing() {
  269. $conn = $this->getBindingHandle();
  270. return $conn->ping();
  271. }
  272. /**
  273. * Give an id for the connection
  274. *
  275. * mysql driver used resource id, but mysqli objects cannot be cast to string.
  276. * @return string
  277. */
  278. public function __toString() {
  279. if ( $this->mConn instanceof mysqli ) {
  280. return (string)$this->mConn->thread_id;
  281. } else {
  282. // mConn might be false or something.
  283. return (string)$this->mConn;
  284. }
  285. }
  286. }