PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/db/DbMySQLi.php

https://gitlab.com/mtellezgalindo/PrestaShop
PHP | 285 lines | 168 code | 38 blank | 79 comment | 35 complexity | 492ca3e809516e9638459e0473be5d18 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-3.0
  1. <?php
  2. /*
  3. * 2007-2014 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2014 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. /**
  27. * @since 1.5.0
  28. */
  29. class DbMySQLiCore extends Db
  30. {
  31. /**
  32. * @see DbCore::connect()
  33. */
  34. public function connect()
  35. {
  36. if (strpos($this->server, ':') !== false)
  37. {
  38. list($server, $port) = explode(':', $this->server);
  39. $this->link = @new mysqli($server, $this->user, $this->password, $this->database, $port);
  40. }
  41. else
  42. $this->link = @new mysqli($this->server, $this->user, $this->password, $this->database);
  43. // Do not use object way for error because this work bad before PHP 5.2.9
  44. if (mysqli_connect_error())
  45. throw new PrestaShopDatabaseException(sprintf(Tools::displayError('Link to database cannot be established: %s'), mysqli_connect_error()));
  46. // UTF-8 support
  47. if (!$this->link->query('SET NAMES \'utf8\''))
  48. throw new PrestaShopDatabaseException(Tools::displayError('PrestaShop Fatal error: no utf-8 support. Please check your server configuration.'));
  49. return $this->link;
  50. }
  51. public static function createDatabase($host, $user = null, $password = null, $database = null, $dropit = false)
  52. {
  53. if (is_null($user))
  54. $user = $this->user;
  55. if (is_null($password))
  56. $password = $this->password;
  57. if (is_null($database))
  58. $database = $this->database;
  59. if (strpos($host, ':') !== false)
  60. {
  61. list($host, $port) = explode(':', $host);
  62. $link = @new mysqli($host, $user, $password, null, $port);
  63. }
  64. else
  65. $link = @new mysqli($host, $user, $password);
  66. $success = $link->query('CREATE DATABASE `'.str_replace('`', '\\`', $database).'`');
  67. if ($dropit && ($link->query('DROP DATABASE `'.str_replace('`', '\\`', $database).'`') !== false))
  68. return true;
  69. return $success;
  70. }
  71. /**
  72. * @see DbCore::disconnect()
  73. */
  74. public function disconnect()
  75. {
  76. @$this->link->close();
  77. }
  78. /**
  79. * @see DbCore::_query()
  80. */
  81. protected function _query($sql)
  82. {
  83. return $this->link->query($sql);
  84. }
  85. /**
  86. * @see DbCore::nextRow()
  87. */
  88. public function nextRow($result = false)
  89. {
  90. if (!$result)
  91. $result = $this->result;
  92. if (!is_object($result))
  93. return false;
  94. return $result->fetch_assoc();
  95. }
  96. /**
  97. * @see DbCore::getAll()
  98. */
  99. protected function getAll($result = false)
  100. {
  101. if (!$result)
  102. $result = $this->result;
  103. if (!is_object($result))
  104. return false;
  105. return $result->fetch_all(MYSQLI_ASSOC);
  106. }
  107. /**
  108. * @see DbCore::_numRows()
  109. */
  110. protected function _numRows($result)
  111. {
  112. return $result->num_rows;
  113. }
  114. /**
  115. * @see DbCore::Insert_ID()
  116. */
  117. public function Insert_ID()
  118. {
  119. return $this->link->insert_id;
  120. }
  121. /**
  122. * @see DbCore::Affected_Rows()
  123. */
  124. public function Affected_Rows()
  125. {
  126. return $this->link->affected_rows;
  127. }
  128. /**
  129. * @see DbCore::getMsgError()
  130. */
  131. public function getMsgError($query = false)
  132. {
  133. return $this->link->error;
  134. }
  135. /**
  136. * @see DbCore::getNumberError()
  137. */
  138. public function getNumberError()
  139. {
  140. return $this->link->errno;
  141. }
  142. /**
  143. * @see DbCore::getVersion()
  144. */
  145. public function getVersion()
  146. {
  147. return $this->getValue('SELECT VERSION()');
  148. }
  149. /**
  150. * @see DbCore::_escape()
  151. */
  152. public function _escape($str)
  153. {
  154. return $this->link->real_escape_string($str);
  155. }
  156. /**
  157. * @see DbCore::set_db()
  158. */
  159. public function set_db($db_name)
  160. {
  161. return $this->link->query('USE `'.bqSQL($db_name).'`');
  162. }
  163. /**
  164. * @see Db::hasTableWithSamePrefix()
  165. */
  166. public static function hasTableWithSamePrefix($server, $user, $pwd, $db, $prefix)
  167. {
  168. $link = @new mysqli($server, $user, $pwd, $db);
  169. if (mysqli_connect_error())
  170. return false;
  171. $sql = 'SHOW TABLES LIKE \''.$prefix.'%\'';
  172. $result = $link->query($sql);
  173. return (bool)$result->fetch_assoc();
  174. }
  175. /**
  176. * @see Db::checkConnection()
  177. */
  178. public static function tryToConnect($server, $user, $pwd, $db, $newDbLink = true, $engine = null, $timeout = 5)
  179. {
  180. $link = mysqli_init();
  181. if (!$link)
  182. return -1;
  183. if (!$link->options(MYSQLI_OPT_CONNECT_TIMEOUT, $timeout))
  184. return 1;
  185. // There is an @ because mysqli throw a warning when the database does not exists
  186. if (!@$link->real_connect($server, $user, $pwd, $db))
  187. return (mysqli_connect_errno() == 1049) ? 2 : 1;
  188. $link->close();
  189. return 0;
  190. }
  191. public function getBestEngine()
  192. {
  193. $value = 'InnoDB';
  194. $sql = 'SHOW VARIABLES WHERE Variable_name = \'have_innodb\'';
  195. $result = $this->link->query($sql);
  196. if (!$result)
  197. $value = 'MyISAM';
  198. $row = $result->fetch_assoc();
  199. if (!$row || strtolower($row['Value']) != 'yes')
  200. $value = 'MyISAM';
  201. /* MySQL >= 5.6 */
  202. $sql = 'SHOW ENGINES';
  203. $result = $this->link->query($sql);
  204. while ($row = $result->fetch_assoc())
  205. if ($row['Engine'] == 'InnoDB')
  206. {
  207. if (in_array($row['Support'], array('DEFAULT', 'YES')))
  208. $value = 'InnoDB';
  209. break;
  210. }
  211. return $value;
  212. }
  213. public static function checkCreatePrivilege($server, $user, $pwd, $db, $prefix, $engine = null)
  214. {
  215. $link = @new mysqli($server, $user, $pwd, $db);
  216. if (mysqli_connect_error())
  217. return false;
  218. if ($engine === null)
  219. $engine = 'MyISAM';
  220. $result = $link->query('
  221. CREATE TABLE `'.$prefix.'test` (
  222. `test` tinyint(1) unsigned NOT NULL
  223. ) ENGINE='.$engine);
  224. if (!$result)
  225. return $link->error;
  226. $link->query('DROP TABLE `'.$prefix.'test`');
  227. return true;
  228. }
  229. /**
  230. * @see Db::checkEncoding()
  231. */
  232. static public function tryUTF8($server, $user, $pwd)
  233. {
  234. $link = @new mysqli($server, $user, $pwd);
  235. $ret = $link->query("SET NAMES 'UTF8'");
  236. $link->close();
  237. return $ret;
  238. }
  239. public static function checkAutoIncrement($server, $user, $pwd)
  240. {
  241. $link = @new mysqli($server, $user, $pwd);
  242. $ret = (bool)(($result = $link->query('SELECT @@auto_increment_increment as aii')) && ($row = $result->fetch_assoc()) && $row['aii'] == 1);
  243. $ret &= (bool)(($result = $link->query('SELECT @@auto_increment_offset as aio')) && ($row = $result->fetch_assoc()) && $row['aio'] == 1);
  244. $link->close();
  245. return $ret;
  246. }
  247. }